コード例 #1
0
  public Context addContext(
      Map<String, String> contextParams,
      Map<String, Object> contextAttributes,
      String contextName,
      HttpContext httpContext,
      AccessControlContext accessControllerContext,
      Map<ServletContainerInitializer, Set<Class<?>>> containerInitializers,
      URL jettyWebXmlURL,
      List<String> virtualHosts,
      List<String> connectors,
      String basedir) {
    silence(host, "/" + contextName);
    Context ctx = new HttpServiceContext(getHost(), accessControllerContext);
    ctx.setName(contextName);
    ctx.setPath("/" + contextName);
    ctx.setDocBase(basedir);
    ctx.addLifecycleListener(new FixContextListener());

    // Add Session config
    ctx.setSessionCookieName(configurationSessionCookie);
    // configurationSessionCookieHttpOnly
    ctx.setUseHttpOnly(configurationSessionCookieHttpOnly);
    // configurationSessionTimeout
    ctx.setSessionTimeout(configurationSessionTimeout);
    // configurationWorkerName //TODO: missing

    // new OSGi methods
    ((HttpServiceContext) ctx).setHttpContext(httpContext);
    // TODO: what about the AccessControlContext?
    // TODO: the virtual host section below
    // TODO: what about the VirtualHosts?
    // TODO: what about the tomcat-web.xml config?
    // TODO: connectors are needed for virtual host?
    if (containerInitializers != null) {
      for (Entry<ServletContainerInitializer, Set<Class<?>>> entry :
          containerInitializers.entrySet()) {
        ctx.addServletContainerInitializer(entry.getKey(), entry.getValue());
      }
    }

    // Add default JSP ContainerInitializer
    if (isJspAvailable()) { // use JasperClassloader
      try {
        @SuppressWarnings("unchecked")
        Class<ServletContainerInitializer> loadClass =
            (Class<ServletContainerInitializer>)
                getClass().getClassLoader().loadClass("org.ops4j.pax.web.jsp.JasperInitializer");
        ctx.addServletContainerInitializer(loadClass.newInstance(), null);
      } catch (ClassNotFoundException e) {
        LOG.error("Unable to load JasperInitializer", e);
      } catch (InstantiationException e) {
        LOG.error("Unable to instantiate JasperInitializer", e);
      } catch (IllegalAccessException e) {
        LOG.error("Unable to instantiate JasperInitializer", e);
      }
    }

    // ctx.addLifecycleListener(new LifecycleListener() {
    //
    // @Override
    // public void lifecycleEvent(LifecycleEvent event) {
    // if (Lifecycle.CONFIGURE_START_EVENT.equals(event.getType())) {
    // // Assert.isInstanceOf(StandardContext.class, event.getSource());
    // StandardContext standardContext = (StandardContext)
    // event.getSource();
    // for (ServletContextInitializer initializer : this.initializers) {
    // try {
    // initializer.onStartup(standardContext.getServletContext());
    // }
    // catch (Exception ex) {
    // this.startUpException = ex;
    // // Prevent Tomcat from logging and re-throwing when we know we can
    // // deal with it in the main thread, but log for information here.
    // logger.error("Error starting Tomcat context: "
    // + ex.getClass().getName());
    // break;
    // }
    // }
    // }
    // }
    // });

    if (host == null) {
      ((ContainerBase) getHost()).setStartChildren(false);
      getHost().addChild(ctx);
    } else {
      ((ContainerBase) host).setStartChildren(false);
      host.addChild(ctx);
    }

    // Custom Service Valve for checking authentication stuff ...
    ctx.getPipeline().addValve(new ServiceValve(httpContext));
    // Custom OSGi Security
    ctx.getPipeline().addValve(new OSGiAuthenticatorValve(httpContext));

    // try {
    // ctx.stop();
    // } catch (LifecycleException e) {
    // LOG.error("context couldn't be started", e);
    // // e.printStackTrace();
    // }
    return ctx;
  }
コード例 #2
0
ファイル: HostConfig.java プロジェクト: shahidminhas/abc
  /** Check resources for redeployment and reloading. */
  protected synchronized void checkResources(DeployedApplication app) {
    String[] resources = (String[]) app.redeployResources.keySet().toArray(new String[0]);
    for (int i = 0; i < resources.length; i++) {
      File resource = new File(resources[i]);
      if (log.isDebugEnabled())
        log.debug("Checking context[" + app.name + "] redeploy resource " + resource);
      if (resource.exists()) {
        long lastModified = ((Long) app.redeployResources.get(resources[i])).longValue();
        if ((!resource.isDirectory()) && resource.lastModified() > lastModified) {
          // Undeploy application
          if (log.isInfoEnabled()) log.info(sm.getString("hostConfig.undeploy", app.name));
          ContainerBase context = (ContainerBase) host.findChild(app.name);
          try {
            host.removeChild(context);
          } catch (Throwable t) {
            log.warn(sm.getString("hostConfig.context.remove", app.name), t);
          }
          try {
            context.destroy();
          } catch (Throwable t) {
            log.warn(sm.getString("hostConfig.context.destroy", app.name), t);
          }
          // Delete other redeploy resources
          for (int j = i + 1; j < resources.length; j++) {
            try {
              File current = new File(resources[j]);
              current = current.getCanonicalFile();
              if ((current
                      .getAbsolutePath()
                      .startsWith(appBase().getAbsolutePath() + File.separator))
                  || (current.getAbsolutePath().startsWith(configBase().getAbsolutePath()))) {
                if (log.isDebugEnabled()) log.debug("Delete " + current);
                ExpandWar.delete(current);
              }
            } catch (IOException e) {
              log.warn(sm.getString("hostConfig.canonicalizing", app.name), e);
            }
          }
          deployed.remove(app.name);
          return;
        }
      } else {
        // There is a chance the the resource was only missing
        // temporarily eg renamed during a text editor save
        try {
          Thread.sleep(500);
        } catch (InterruptedException e1) {
          // Ignore
        }
        // Recheck the resource to see if it was really deleted
        if (resource.exists()) {
          continue;
        }
        long lastModified = ((Long) app.redeployResources.get(resources[i])).longValue();

        if (lastModified == 0L) {
          continue;
        }
        // Undeploy application
        if (log.isInfoEnabled()) log.info(sm.getString("hostConfig.undeploy", app.name));
        ContainerBase context = (ContainerBase) host.findChild(app.name);
        try {
          host.removeChild(context);
        } catch (Throwable t) {
          log.warn(sm.getString("hostConfig.context.remove", app.name), t);
        }
        try {
          context.destroy();
        } catch (Throwable t) {
          log.warn(sm.getString("hostConfig.context.destroy", app.name), t);
        }
        // Delete all redeploy resources
        for (int j = i + 1; j < resources.length; j++) {
          try {
            File current = new File(resources[j]);
            current = current.getCanonicalFile();
            if ((current.getAbsolutePath().startsWith(appBase().getAbsolutePath() + File.separator))
                || (current.getAbsolutePath().startsWith(configBase().getAbsolutePath()))) {
              if (log.isDebugEnabled()) log.debug("Delete " + current);
              ExpandWar.delete(current);
            }
          } catch (IOException e) {
            log.warn(sm.getString("hostConfig.canonicalizing", app.name), e);
          }
        }
        // Delete reload resources as well (to remove any remaining .xml descriptor)
        String[] resources2 = (String[]) app.reloadResources.keySet().toArray(new String[0]);
        for (int j = 0; j < resources2.length; j++) {
          try {
            File current = new File(resources2[j]);
            current = current.getCanonicalFile();
            if ((current.getAbsolutePath().startsWith(appBase().getAbsolutePath() + File.separator))
                || ((current.getAbsolutePath().startsWith(configBase().getAbsolutePath())
                    && (current.getAbsolutePath().endsWith(".xml"))))) {
              if (log.isDebugEnabled()) log.debug("Delete " + current);
              ExpandWar.delete(current);
            }
          } catch (IOException e) {
            log.warn(sm.getString("hostConfig.canonicalizing", app.name), e);
          }
        }
        deployed.remove(app.name);
        return;
      }
    }
    resources = (String[]) app.reloadResources.keySet().toArray(new String[0]);
    for (int i = 0; i < resources.length; i++) {
      File resource = new File(resources[i]);
      if (log.isDebugEnabled())
        log.debug("Checking context[" + app.name + "] reload resource " + resource);
      long lastModified = ((Long) app.reloadResources.get(resources[i])).longValue();
      if ((!resource.exists() && lastModified != 0L) || (resource.lastModified() != lastModified)) {
        // Reload application
        if (log.isInfoEnabled()) log.info(sm.getString("hostConfig.reload", app.name));
        Container context = host.findChild(app.name);
        try {
          ((Lifecycle) context).stop();
        } catch (Exception e) {
          log.warn(sm.getString("hostConfig.context.restart", app.name), e);
        }
        // If the context was not started (for example an error
        // in web.xml) we'll still get to try to start
        try {
          ((Lifecycle) context).start();
        } catch (Exception e) {
          log.warn(sm.getString("hostConfig.context.restart", app.name), e);
        }
        // Update times
        app.reloadResources.put(resources[i], new Long(resource.lastModified()));
        app.timestamp = System.currentTimeMillis();
        return;
      }
    }
  }