Example #1
0
 private void refresh(ClassLoader classLoader, int timeout) {
   if (!registeredDispatchers.isEmpty()) {
     try {
       Class<?> cmdClass =
           Class.forName(RefreshDispatchersCommand.class.getName(), true, appClassLoader);
       Command cmd = (Command) cmdClass.newInstance();
       ReflectionHelper.invoke(
           cmd,
           cmdClass,
           "setupCmd",
           new Class[] {java.lang.ClassLoader.class, java.util.Set.class},
           classLoader,
           registeredDispatchers);
       scheduler.scheduleCommand(cmd, timeout);
     } catch (Exception e) {
       LOGGER.error("refresh() exception {}.", e.getMessage());
     }
   }
 }
Example #2
0
  /**
   * Actual plugin initialization write plugin info and handle webappDir property.
   *
   * <p>If the webappDir property is set, call:
   *
   * <pre>
   *      contextHandler.setBaseResource(new ResourceCollection(
   *          new FileResource(webappDir),
   *          contextHandler.getBaseResource()
   *      ));
   * </pre>
   *
   * @param contextHandler instance of ContextHandler - main jetty class for webapp.
   */
  public void init(Object contextHandler) {

    // resolve jetty classes
    ClassLoader loader = contextHandler.getClass().getClassLoader();
    Class contextHandlerClass;
    Class resourceClass;
    Class fileResourceClass;
    Class resourceCollectionClass;

    try {
      contextHandlerClass = loader.loadClass("org.eclipse.jetty.server.handler.ContextHandler");
      resourceClass = loader.loadClass("org.eclipse.jetty.util.resource.Resource");
      fileResourceClass = loader.loadClass("org.eclipse.jetty.util.resource.FileResource");
      resourceCollectionClass =
          loader.loadClass("org.eclipse.jetty.util.resource.ResourceCollection");
    } catch (ClassNotFoundException e) {
      try {
        contextHandlerClass = loader.loadClass("org.mortbay.jetty.handler.ContextHandler");
        resourceClass = loader.loadClass("org.mortbay.resource.Resource");
        fileResourceClass = loader.loadClass("org.mortbay.resource.FileResource");
        resourceCollectionClass = loader.loadClass("org.mortbay.resource.ResourceCollection");
      } catch (ClassNotFoundException e1) {
        LOGGER.error(
            "Unable to load ContextHandler class from contextHandler {} classloader",
            contextHandler);
        return;
      }
    }

    String version;
    // find version in Servlet package
    // (http://docs.codehaus.org/display/JETTY/How+to+find+out+the+version+of+Jetty)
    try {
      Object server =
          ReflectionHelper.invoke(contextHandler, contextHandlerClass, "getServer", new Class[] {});
      version = server.getClass().getPackage().getImplementationVersion();
    } catch (Exception e) {
      version = "unknown [" + e.getMessage() + "]";
    }

    // set different resource
    URL webappDir = pluginConfiguration.getWebappDir();
    if (webappDir != null) {
      LOGGER.debug(
          "Setting webappDir to directory '{}' for Jetty webapp {}.", webappDir, contextHandler);
      try {
        Object originalBaseResource =
            ReflectionHelper.invoke(
                contextHandler, contextHandlerClass, "getBaseResource", new Class[] {});
        Object fileResource =
            fileResourceClass.getDeclaredConstructor(URL.class).newInstance(webappDir);
        Object resourceArray = Array.newInstance(resourceClass, 2);
        Array.set(resourceArray, 0, fileResource);
        Array.set(resourceArray, 1, originalBaseResource);

        Object resourceCollection =
            resourceCollectionClass
                .getDeclaredConstructor(resourceArray.getClass())
                .newInstance(resourceArray);

        ReflectionHelper.invoke(
            contextHandler,
            contextHandlerClass,
            "setBaseResource",
            new Class[] {resourceClass},
            resourceCollection);

      } catch (Exception e) {
        LOGGER.error(
            "Unable to set webappDir to directory '{}' for Jetty webapp {}. This configuration will not work.",
            e,
            webappDir,
            contextHandler);
      }
    }

    LOGGER.info(
        "Jetty plugin initialized - Jetty version '{}', context {}", version, contextHandler);
  }