Пример #1
0
 static EmbeddedTomcat newEmbeddedTomcat(Configuration configuration) {
   EmbeddedTomcat result = new EmbeddedTomcat();
   result.getServer().setCatalina(new FakeCatalina());
   result.getService().setName("Catalina");
   result.getEngine().setName("Catalina");
   result.configure(configuration);
   return result;
 }
 @Override
 public void start() {
   LOG.debug("start server");
   try {
     final long t1 = System.nanoTime();
     m_server.getHost();
     m_server.start();
     final long t2 = System.nanoTime();
     if (LOG.isInfoEnabled()) {
       LOG.info("TomCat server startup in " + ((t2 - t1) / 1000000) + " ms");
     }
   } catch (final LifecycleException e) {
     throw new ServerStartException(m_server.getServer().getInfo(), e);
   }
 }
 @Override
 public void stop() {
   LOG.debug("stop server");
   final LifecycleState state = m_server.getServer().getState();
   if (LifecycleState.STOPPING_PREP.compareTo(state) <= 0
       && LifecycleState.DESTROYED.compareTo(state) >= 0) {
     throw new IllegalStateException("stop already called!");
   } else {
     try {
       m_server.stop();
       m_server.destroy();
     } catch (final Throwable e) {
       // throw new ServerStopException(
       // m_server.getServer().getInfo(), e );
       LOG.error("LifecycleException caught {}", e);
     }
   }
 }
 private TomcatServerWrapper(final EmbeddedTomcat server) {
   NullArgumentException.validateNotNull(server, "server");
   this.m_server = server;
   ((ContainerBase) m_server.getHost()).setStartChildren(false);
 }
 private Context findContext(final ContextModel contextModel) {
   final String contextName = contextModel.getContextName();
   return m_server.findContext(contextName);
 }
  private Context createContext(final ContextModel contextModel) {
    final Bundle bundle = contextModel.getBundle();
    final BundleContext bundleContext = BundleUtils.getBundleContext(bundle);

    final Context context =
        m_server.addContext(
            contextModel.getContextParams(),
            getContextAttributes(bundleContext),
            contextModel.getContextName(),
            contextModel.getHttpContext(),
            contextModel.getAccessControllerContext(),
            contextModel.getContainerInitializers(),
            contextModel.getJettyWebXmlURL(),
            contextModel.getVirtualHosts(),
            contextModel.getConnectors(),
            m_server.getBasedir());

    context.setParentClassLoader(contextModel.getClassLoader());
    // TODO: is the context already configured?
    // TODO: how about security, classloader?
    // TODO: compare with JettyServerWrapper.addContext
    // TODO: what about the init parameters?
    /*
     * Do not start context here, but register it to be started lazily. This
     * ensures that all servlets, listeners, initializers etc. are
     * registered before the context is started.
     */
    ServletContextManager.addContext(
        context.getPath(), new TomcatServletContextWrapper(context, m_server.getHost()));

    final LifecycleState state = context.getState();
    if (state != LifecycleState.STARTED
        && state != LifecycleState.STARTING
        && state != LifecycleState.STARTING_PREP) {

      // if( !context.isStarted() && !context.isStarting() )
      // {
      LOG.debug("Registering ServletContext as service. ");
      final Dictionary<String, String> properties = new Hashtable<String, String>();
      properties.put("osgi.web.symbolicname", bundle.getSymbolicName());

      final Dictionary<String, String> headers = bundle.getHeaders();
      final String version = (String) headers.get(Constants.BUNDLE_VERSION);
      if (version != null && version.length() > 0) {
        properties.put("osgi.web.version", version);
      }

      String webContextPath = (String) headers.get(WEB_CONTEXT_PATH);
      final String webappContext = (String) headers.get("Webapp-Context");

      final ServletContext servletContext = context.getServletContext();

      // This is the default context, but shouldn't it be called default?
      // See PAXWEB-209
      if ("/".equalsIgnoreCase(context.getPath())
          && (webContextPath == null || webappContext == null)) {
        webContextPath = context.getPath();
      }

      // makes sure the servlet context contains a leading slash
      webContextPath = webContextPath != null ? webContextPath : webappContext;
      if (webContextPath != null && !webContextPath.startsWith("/")) {
        webContextPath = "/" + webContextPath;
      }

      if (webContextPath == null) {
        LOG.warn("osgi.web.contextpath couldn't be set, it's not configured");
      }

      properties.put("osgi.web.contextpath", webContextPath);

      servletContextService =
          bundleContext.registerService(ServletContext.class, servletContext, properties);
      LOG.debug("ServletContext registered as service. ");
    }
    m_contexts.put(contextModel.getHttpContext(), context);

    return context;
  }
 @Override
 public ServerWrapper newServer(Configuration configuration) {
   return TomcatServerWrapper.getInstance(EmbeddedTomcat.newEmbeddedTomcat(configuration));
 }