Esempio n. 1
0
  public synchronized ServerImpl getServer(String addr) {
    if (server == null) {
      checkProperties();

      ClassLoaderHolder loader = null;
      try {
        if (bus != null) {
          ClassLoader newLoader = bus.getExtension(ClassLoader.class);
          if (newLoader != null) {
            loader = ClassLoaderUtils.setThreadContextClassloader(newLoader);
          }
        }

        // Initialize the endpointName so we can do configureObject
        QName origEpn = endpointName;
        if (endpointName == null) {
          JaxWsImplementorInfo implInfo = new JaxWsImplementorInfo(getImplementorClass());
          endpointName = implInfo.getEndpointName();
        }

        if (serviceFactory != null) {
          serverFactory.setServiceFactory(serviceFactory);
        }

        /*if (serviceName != null) {
            serverFactory.getServiceFactory().setServiceName(serviceName);
        }*/

        configureObject(this);
        endpointName = origEpn;

        // Set up the server factory
        serverFactory.setAddress(addr);
        serverFactory.setStart(false);
        serverFactory.setEndpointName(endpointName);
        serverFactory.setServiceBean(implementor);
        serverFactory.setBus(bus);
        serverFactory.setFeatures(getFeatures());
        serverFactory.setInvoker(invoker);
        serverFactory.setSchemaLocations(schemaLocations);
        if (serverFactory.getProperties() != null) {
          serverFactory.getProperties().putAll(properties);
        } else {
          serverFactory.setProperties(properties);
        }

        // Be careful not to override any serverfactory settings as a user might
        // have supplied their own.
        if (getWsdlLocation() != null) {
          serverFactory.setWsdlURL(getWsdlLocation());
        }

        if (bindingUri != null) {
          serverFactory.setBindingId(bindingUri);
        }

        if (serviceName != null) {
          serverFactory.getServiceFactory().setServiceName(serviceName);
        }

        if (implementorClass != null) {
          serverFactory.setServiceClass(implementorClass);
        }

        if (executor != null) {
          serverFactory.getServiceFactory().setExecutor(executor);
        }
        if (handlers.size() > 0) {
          serverFactory.addHandlers(handlers);
        }

        configureObject(serverFactory);

        server = serverFactory.create();

        org.apache.cxf.endpoint.Endpoint endpoint = getEndpoint();
        if (in != null) {
          endpoint.getInInterceptors().addAll(in);
        }
        if (out != null) {
          endpoint.getOutInterceptors().addAll(out);
        }
        if (inFault != null) {
          endpoint.getInFaultInterceptors().addAll(inFault);
        }
        if (outFault != null) {
          endpoint.getOutFaultInterceptors().addAll(outFault);
        }

        if (properties != null) {
          endpoint.putAll(properties);
        }

        configureObject(endpoint.getService());
        configureObject(endpoint);
        this.service = endpoint.getService();

        if (getWsdlLocation() == null) {
          // hold onto the wsdl location so cache won't clear till we go away
          setWsdlLocation(serverFactory.getWsdlURL());
        }

        if (serviceName == null) {
          setServiceName(serverFactory.getServiceFactory().getServiceQName());
        }
        if (endpointName == null) {
          endpointName = endpoint.getEndpointInfo().getName();
        }
      } finally {
        if (loader != null) {
          loader.reset();
        }
      }
    }
    return (ServerImpl) server;
  }
Esempio n. 2
0
  public static void doHacks() {
    if (skipHack("org.apache.cxf.JDKBugHacks.all")) {
      return;
    }
    try {
      // Use the system classloader as the victim for all this
      // ClassLoader pinning we're about to do.
      ClassLoaderHolder orig =
          ClassLoaderUtils.setThreadContextClassloader(ClassLoader.getSystemClassLoader());
      try {
        try {
          // Trigger a call to sun.awt.AppContext.getAppContext()
          if (!skipHack("org.apache.cxf.JDKBugHacks.imageIO")) {
            ImageIO.getCacheDirectory();
          }
        } catch (Throwable t) {
          // ignore
        }
        try {
          // DocumentBuilderFactory seems to SOMETIMES pin the classloader
          if (!skipHack("org.apache.cxf.JDKBugHacks.documentBuilderFactory")) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.newDocumentBuilder();
          }
        } catch (Throwable e) {
          // ignore
        }
        // Several components end up calling:
        // sun.misc.GC.requestLatency(long)
        //
        // Those libraries / components known to trigger memory leaks due to
        // eventual calls to requestLatency(long) are:
        // - javax.management.remote.rmi.RMIConnectorServer.start()
        try {
          if (!skipHack("org.apache.cxf.JDKBugHacks.gcRequestLatency")) {
            Class<?> clazz = Class.forName("sun.misc.GC");
            Method method = clazz.getDeclaredMethod("currentLatencyTarget");
            Long l = (Long) method.invoke(null);
            if (l != null && l.longValue() == 0) {
              // something already set it, move on
              method = clazz.getDeclaredMethod("requestLatency", new Class[] {Long.TYPE});
              method.invoke(null, Long.valueOf(36000000));
            }
          }
        } catch (Throwable e) {
          // ignore
        }

        // Calling getPolicy retains a static reference to the context
        // class loader.
        try {
          // Policy.getPolicy();
          if (!skipHack("org.apache.cxf.JDKBugHacks.policy")) {
            Class<?> policyClass = Class.forName("javax.security.auth.Policy");
            Method method = policyClass.getMethod("getPolicy");
            method.invoke(null);
          }
        } catch (Throwable e) {
          // ignore
        }
        try {
          // Initializing javax.security.auth.login.Configuration retains a static reference
          // to the context class loader.
          if (!skipHack("org.apache.cxf.JDKBugHacks.authConfiguration")) {
            Class.forName(
                "javax.security.auth.login.Configuration",
                true,
                ClassLoader.getSystemClassLoader());
          }
        } catch (Throwable e) {
          // Ignore
        }
        // Creating a MessageDigest during web application startup
        // initializes the Java Cryptography Architecture. Under certain
        // conditions this starts a Token poller thread with TCCL equal
        // to the web application class loader.
        if (!skipHack("org.apache.cxf.JDKBugHacks.securityProviders")) {
          java.security.Security.getProviders();
        }

        try {
          // Several components end up opening JarURLConnections without first
          // disabling caching. This effectively locks the file.
          // JAXB does this and thus affects us pretty badly.
          // Doesn't matter that this JAR doesn't exist - just as long as
          // the URL is well-formed
          if (!skipHack("org.apache.cxf.JDKBugHacks.defaultUsesCaches")) {
            URL url = new URL("jar:file://dummy.jar!/");
            URLConnection uConn =
                new URLConnection(url) {
                  @Override
                  public void connect() throws IOException {
                    // NOOP
                  }
                };
            uConn.setDefaultUseCaches(false);
          }
        } catch (Throwable e) {
          // ignore
        }
      } finally {
        if (orig != null) {
          orig.reset();
        }
      }
    } catch (Throwable t) {
      // ignore
    }
  }
Esempio n. 3
0
  /**
   * Performs the publication action by setting up a {@link Server} instance based on this
   * endpoint's configuration.
   *
   * @param addr the optional endpoint address.
   * @throws IllegalStateException if the endpoint cannot be published/republished
   * @throws SecurityException if permission checking is enabled and policy forbids publishing
   * @throws WebServiceException if there is an error publishing the endpoint
   * @see #checkPublishPermission()
   * @see #checkPublishable()
   * @see #getServer(String)
   */
  protected void doPublish(String addr) {
    checkPublishPermission();
    checkPublishable();

    ServerImpl serv = null;

    ClassLoaderHolder loader = null;
    try {
      if (bus != null) {
        ClassLoader newLoader = bus.getExtension(ClassLoader.class);
        if (newLoader != null) {
          loader = ClassLoaderUtils.setThreadContextClassloader(newLoader);
        }
      }
      serv = getServer(addr);
      if (addr != null) {
        EndpointInfo endpointInfo = serv.getEndpoint().getEndpointInfo();
        if (!endpointInfo.getAddress().contains(addr)) {
          endpointInfo.setAddress(addr);
        }
        if (publishedEndpointUrl != null) {
          endpointInfo.setProperty(WSDLGetUtils.PUBLISHED_ENDPOINT_URL, publishedEndpointUrl);
        }
        if (publishedEndpointUrl != null && wsdlLocation != null) {
          // early update the publishedEndpointUrl so that endpoints in the same app sharing the
          // same wsdl
          // do not require all of them to be queried for wsdl before the wsdl is finally fully
          // updated
          Definition def =
              endpointInfo
                  .getService()
                  .getProperty(WSDLServiceBuilder.WSDL_DEFINITION, Definition.class);
          if (def == null) {
            def = bus.getExtension(WSDLManager.class).getDefinition(wsdlLocation);
          }
          new WSDLGetUtils().updateWSDLPublishedEndpointAddress(def, endpointInfo);
        }

        if (null != properties) {
          for (Entry<String, Object> entry : properties.entrySet()) {
            endpointInfo.setProperty(entry.getKey(), entry.getValue());
          }
        }

        this.address = endpointInfo.getAddress();
      }
      serv.start();
      publishable = false;
    } catch (Exception ex) {
      try {
        stop();
      } catch (Exception e) {
        // Nothing we can do.
      }

      throw new WebServiceException(ex);
    } finally {
      if (loader != null) {
        loader.reset();
      }
    }
  }
Esempio n. 4
0
  private Object doChainedInvocation(
      URI uri,
      MultivaluedMap<String, String> headers,
      OperationResourceInfo ori,
      Object body,
      int bodyIndex,
      Exchange exchange,
      Map<String, Object> invocationContext)
      throws Throwable {
    Bus configuredBus = getConfiguration().getBus();
    Bus origBus = BusFactory.getAndSetThreadDefaultBus(configuredBus);
    ClassLoaderHolder origLoader = null;
    try {
      ClassLoader loader = configuredBus.getExtension(ClassLoader.class);
      if (loader != null) {
        origLoader = ClassLoaderUtils.setThreadContextClassloader(loader);
      }
      Message outMessage =
          createMessage(body, ori.getHttpMethod(), headers, uri, exchange, invocationContext, true);
      if (bodyIndex != -1) {
        outMessage.put(Type.class, ori.getMethodToInvoke().getGenericParameterTypes()[bodyIndex]);
      }
      outMessage.getExchange().setOneWay(ori.isOneway());
      setSupportOnewayResponseProperty(outMessage);
      outMessage.setContent(OperationResourceInfo.class, ori);
      setPlainOperationNameProperty(outMessage, ori.getMethodToInvoke().getName());
      outMessage.getExchange().put(Method.class, ori.getMethodToInvoke());

      outMessage.put(
          Annotation.class.getName(), getMethodAnnotations(ori.getAnnotatedMethod(), bodyIndex));

      if (body != null) {
        outMessage.put("BODY_INDEX", bodyIndex);
      }
      outMessage.getInterceptorChain().add(bodyWriter);

      Map<String, Object> reqContext = getRequestContext(outMessage);
      reqContext.put(OperationResourceInfo.class.getName(), ori);
      reqContext.put("BODY_INDEX", bodyIndex);

      // execute chain
      doRunInterceptorChain(outMessage);

      Object[] results = preProcessResult(outMessage);
      if (results != null && results.length == 1) {
        return results[0];
      }

      try {
        return handleResponse(outMessage, ori.getClassResourceInfo().getServiceClass());
      } finally {
        completeExchange(outMessage.getExchange(), true);
      }
    } finally {
      if (origLoader != null) {
        origLoader.reset();
      }
      if (origBus != configuredBus) {
        BusFactory.setThreadDefaultBus(origBus);
      }
    }
  }