Exemplo n.º 1
0
  @SuppressWarnings("unchecked")
  public synchronized void initialize(Service service) {

    inInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);
    inFaultInterceptors.addIfAbsent(JAXBAttachmentSchemaValidationHack.INSTANCE);

    // context is already set, don't redo it
    if (context != null) {
      return;
    }

    contextClasses = new LinkedHashSet<Class<?>>();
    Map<String, Object> unmarshallerProps = new HashMap<String, Object>();
    this.setUnmarshallerProperties(unmarshallerProps);
    for (ServiceInfo serviceInfo : service.getServiceInfos()) {

      JAXBContextInitializer initializer =
          new JAXBContextInitializer(
              serviceInfo, contextClasses, typeRefs, this.getUnmarshallerProperties());
      initializer.walk();
      if (serviceInfo.getProperty("extra.class") != null) {
        Set<Class<?>> exClasses = serviceInfo.getProperty("extra.class", Set.class);
        contextClasses.addAll(exClasses);
      }
    }

    String tns = getNamespaceToUse(service);
    CachedContextAndSchemas cachedContextAndSchemas = null;
    JAXBContext ctx = null;
    try {
      cachedContextAndSchemas = createJAXBContextAndSchemas(contextClasses, tns);
    } catch (JAXBException e1) {
      throw new ServiceConstructionException(e1);
    }
    ctx = cachedContextAndSchemas.getContext();
    if (LOG.isLoggable(Level.FINE)) {
      LOG.log(Level.FINE, "CREATED_JAXB_CONTEXT", new Object[] {ctx, contextClasses});
    }
    setContext(ctx);

    for (ServiceInfo serviceInfo : service.getServiceInfos()) {
      SchemaCollection col = serviceInfo.getXmlSchemaCollection();

      if (col.getXmlSchemas().length > 1) {
        // someone has already filled in the types
        justCheckForJAXBAnnotations(serviceInfo);
        continue;
      }

      boolean schemasFromCache = false;
      Collection<DOMSource> schemas = getSchemas();
      if (schemas == null || schemas.size() == 0) {
        schemas = cachedContextAndSchemas.getSchemas();
        if (schemas != null) {
          schemasFromCache = true;
        }
      } else {
        schemasFromCache = true;
      }
      Set<DOMSource> bi = new LinkedHashSet<DOMSource>();
      if (schemas == null) {
        schemas = new LinkedHashSet<DOMSource>();
        try {
          for (DOMResult r : generateJaxbSchemas()) {
            DOMSource src = new DOMSource(r.getNode(), r.getSystemId());
            if (BUILT_IN_SCHEMAS.containsValue(r)) {
              bi.add(src);
            } else {
              schemas.add(src);
            }
          }
          // put any builtins at the end.   Anything that DOES import them
          // will cause it to load automatically and we'll skip them later
          schemas.addAll(bi);
        } catch (IOException e) {
          throw new ServiceConstructionException("SCHEMA_GEN_EXC", LOG, e);
        }
      }
      Set<String> ids = new HashSet<String>();
      for (DOMSource r : schemas) {
        ids.add(r.getSystemId());
      }
      for (DOMSource r : schemas) {
        if (bi.contains(r)) {
          String ns = ((Document) r.getNode()).getDocumentElement().getAttribute("targetNamespace");
          if (serviceInfo.getSchema(ns) != null) {
            continue;
          }
        }
        // StaxUtils.print(r.getNode());
        // System.out.println();
        addSchemaDocument(serviceInfo, col, (Document) r.getNode(), r.getSystemId());
      }

      JAXBSchemaInitializer schemaInit =
          new JAXBSchemaInitializer(serviceInfo, col, context, this.qualifiedSchemas, tns);
      schemaInit.walk();
      if (cachedContextAndSchemas != null && !schemasFromCache) {
        cachedContextAndSchemas.setSchemas(schemas);
      }
    }
  }