Пример #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;
  }