Пример #1
0
  /* (non-Javadoc)
   * @see org.glassfish.hk2.xml.api.XmlService#unmarshall(java.net.URI, java.lang.Class)
   */
  @Override
  public <T> XmlRootHandle<T> unmarshall(
      URI uri,
      Class<T> jaxbAnnotatedInterface,
      boolean advertiseInRegistry,
      boolean advertiseInHub) {
    if (uri == null || jaxbAnnotatedInterface == null) throw new IllegalArgumentException();
    if (!jaxbAnnotatedInterface.isInterface()) {
      throw new IllegalArgumentException(
          "Only an interface can be given to unmarshall: " + jaxbAnnotatedInterface.getName());
    }

    XmlServiceParser localParser = parser.get();
    if (localParser == null) {
      throw new IllegalStateException("There is no XmlServiceParser implementation");
    }

    try {
      boolean generateAll =
          PreGenerationRequirement.MUST_PREGENERATE.equals(
              localParser.getPreGenerationRequirement());
      jaUtilities.convertRootAndLeaves(jaxbAnnotatedInterface, generateAll);

      ModelImpl model = jaUtilities.getModel(jaxbAnnotatedInterface);

      return unmarshallClass(uri, model, localParser, null, advertiseInRegistry, advertiseInHub);
    } catch (RuntimeException re) {
      throw re;
    } catch (Throwable e) {
      throw new MultiException(e);
    }
  }
Пример #2
0
  private <T> XmlRootHandle<T> unmarshallClass(
      URI uri,
      ModelImpl model,
      XmlServiceParser localParser,
      XMLStreamReader reader,
      boolean advertise,
      boolean advertiseInHub)
      throws Exception {
    long elapsedUpToJAXB = 0;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedUpToJAXB = System.currentTimeMillis();
    }

    Hk2JAXBUnmarshallerListener listener =
        new Hk2JAXBUnmarshallerListener(jaUtilities, classReflectionHelper);

    long jaxbUnmarshallElapsedTime = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      jaxbUnmarshallElapsedTime = System.currentTimeMillis();
      elapsedUpToJAXB = jaxbUnmarshallElapsedTime - elapsedUpToJAXB;
      Logger.getLogger()
          .debug("Time up to parsing " + uri + " is " + elapsedUpToJAXB + " milliseconds");
    }

    T root;
    if (localParser != null) {
      root = localParser.parseRoot(model, uri, listener);
    } else {
      root = XmlStreamImpl.parseRoot(this, model, reader, listener);
    }

    long elapsedJAXBToAdvertisement = 0;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedJAXBToAdvertisement = System.currentTimeMillis();
      jaxbUnmarshallElapsedTime = elapsedJAXBToAdvertisement - jaxbUnmarshallElapsedTime;
      Logger.getLogger()
          .debug(
              "Time parsing "
                  + uri
                  + " is "
                  + jaxbUnmarshallElapsedTime
                  + " milliseconds "
                  + ", now with "
                  + jaUtilities.getNumGenerated()
                  + " proxies generated and "
                  + jaUtilities.getNumPreGenerated()
                  + " pre generated proxies loaded");
    }

    DynamicChangeInfo changeControl =
        new DynamicChangeInfo(
            jaUtilities,
            ((advertiseInHub) ? hub : null),
            this,
            ((advertise) ? dynamicConfigurationService : null),
            serviceLocator);

    for (BaseHK2JAXBBean base : listener.getAllBeans()) {
      String instanceName = Utilities.createInstanceName(base);
      base._setInstanceName(instanceName);

      base._setDynamicChangeInfo(changeControl);

      if (DEBUG_PARSING) {
        Logger.getLogger().debug("XmlServiceDebug found bean " + base);
      }
    }
    if (DEBUG_PARSING) {
      Logger.getLogger().debug("XmlServiceDebug after parsing all beans in " + uri);
    }

    long elapsedPreAdvertisement = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedPreAdvertisement = System.currentTimeMillis();
      elapsedJAXBToAdvertisement = elapsedPreAdvertisement - elapsedJAXBToAdvertisement;
      Logger.getLogger()
          .debug(
              "Time from parsing to PreAdvertisement "
                  + uri
                  + " is "
                  + elapsedJAXBToAdvertisement
                  + " milliseconds");
    }

    DynamicConfiguration config =
        (advertise) ? dynamicConfigurationService.createDynamicConfiguration() : null;
    WriteableBeanDatabase wdb = (advertiseInHub) ? hub.getWriteableDatabaseCopy() : null;

    LinkedList<BaseHK2JAXBBean> allBeans = listener.getAllBeans();
    List<ActiveDescriptor<?>> addedDescriptors =
        new ArrayList<ActiveDescriptor<?>>(allBeans.size());
    for (BaseHK2JAXBBean bean : allBeans) {
      ActiveDescriptor<?> added = Utilities.advertise(wdb, config, bean);
      if (added != null) {
        addedDescriptors.add(added);
      }
    }

    long elapsedHK2Advertisement = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedHK2Advertisement = System.currentTimeMillis();
      elapsedPreAdvertisement = elapsedHK2Advertisement - elapsedPreAdvertisement;
      Logger.getLogger()
          .debug(
              "Time from JAXB to PreAdvertisement "
                  + uri
                  + " is "
                  + elapsedPreAdvertisement
                  + " milliseconds");
    }

    if (config != null) {
      config.commit();
    }

    long elapsedHubAdvertisement = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedHubAdvertisement = System.currentTimeMillis();
      elapsedHK2Advertisement = elapsedHubAdvertisement - elapsedHK2Advertisement;
      Logger.getLogger()
          .debug(
              "Time to advertise "
                  + uri
                  + " in HK2 is "
                  + elapsedHK2Advertisement
                  + " milliseconds");
    }

    if (wdb != null) {
      wdb.commit(new XmlHubCommitMessage() {});
    }

    // The following is put here so that InstanceLifecycleListeners will get invoked at this time
    // rather than later so that defaulting can be done now.  It doesn't hurt because these are
    // all constant descriptors
    for (ActiveDescriptor<?> ad : addedDescriptors) {
      serviceLocator.getServiceHandle(ad).getService();
    }

    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedHubAdvertisement = System.currentTimeMillis() - elapsedHubAdvertisement;
      Logger.getLogger()
          .debug(
              "Time to advertise "
                  + uri
                  + " in Hub is "
                  + elapsedHubAdvertisement
                  + " milliseconds");
    }

    return new XmlRootHandleImpl<T>(
        this, hub, root, model, uri, advertise, advertiseInHub, changeControl);
  }