/** * Select the appropriate child Host to process this request, * based on the requested server name. If no matching Host can * be found, return an appropriate HTTP error. * * @param request Request to be processed * @param response Response to be produced * * @exception IOException if an input/output error occurred * @exception ServletException if a servlet error occurred */ @Override public final void invoke(Request request, Response response) throws IOException, ServletException { // Select the Host to be used for this Request Host host = request.getHost(); if (host == null) { response.sendError (HttpServletResponse.SC_BAD_REQUEST, sm.getString("standardEngine.noHost", request.getServerName())); return; } if (request.isAsyncSupported()) { request.setAsyncSupported(host.getPipeline().isAsyncSupported()); } // Ask this Host to process this request host.getPipeline().getFirst().invoke(request, response); }
/** * Select the appropriate child Host to process this request, * based on the requested server name. If no matching Host can * be found, return an appropriate HTTP error. * * @param request Request to be processed * @param response Response to be produced * @param valveContext Valve context used to forward to the next Valve * * @exception IOException if an input/output error occurred * @exception ServletException if a servlet error occurred */ public final void invoke(Request request, Response response) throws IOException, ServletException { // Select the Host to be used for this Request Host host = request.getHost(); if (host == null) { response.sendError (HttpServletResponse.SC_BAD_REQUEST, sm.getString("standardEngine.noHost", request.getServerName())); return; } // Ask this Host to process this request host.getPipeline().getFirst().invoke(request, response); }
/** * Render a list of the currently active Contexts in our virtual host. * * @param writer Writer to render to */ protected void list(PrintWriter writer, StringManager smClient) { if (debug >= 1) { log(sm.getString("hostManagerServlet.list", engine.getName())); } writer.println(smClient.getString("hostManagerServlet.listed", engine.getName())); Container[] hosts = engine.findChildren(); for (int i = 0; i < hosts.length; i++) { Host host = (Host) hosts[i]; String name = host.getName(); String[] aliases = host.findAliases(); StringBuilder buf = new StringBuilder(); if (aliases.length > 0) { buf.append(aliases[0]); for (int j = 1; j < aliases.length; j++) { buf.append(',').append(aliases[j]); } } writer.println(smClient.getString("hostManagerServlet.listitem", name, buf.toString())); } }
public Context addContext(Host host, String contextPath, String contextName, String dir) { silence(host, contextName); Context ctx = createContext(host, contextPath); ctx.setName(contextName); ctx.setPath(contextPath); ctx.setDocBase(dir); ctx.addLifecycleListener(new FixContextListener()); if (host == null) { getHost().addChild(ctx); } else { host.addChild(ctx); } return ctx; }
private String getLoggerName(Host host, String contextName) { if (host == null) { host = getHost(); } StringBuilder loggerName = new StringBuilder(); loggerName.append(ContainerBase.class.getName()); loggerName.append(".["); // Engine name loggerName.append(host.getParent().getName()); loggerName.append("].["); // Host name loggerName.append(host.getName()); loggerName.append("].["); // Context name if (contextName == null || contextName.equals("")) { loggerName.append("/"); } else if (contextName.startsWith("##")) { loggerName.append("/"); loggerName.append(contextName); } loggerName.append(']'); return loggerName.toString(); }
/** * Process the START event for an associated Host. * * @param event The lifecycle event that has occurred */ @Override public void lifecycleEvent(LifecycleEvent event) { // Identify the host we are associated with try { host = (Host) event.getLifecycle(); } catch (ClassCastException e) { log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e); return; } // Process the event that has occurred if (event.getType().equals(Lifecycle.START_EVENT)) start(); else if (event.getType().equals(Lifecycle.STOP_EVENT)) stop(); }
@Override public void startInternal() throws LifecycleException { setState(LifecycleState.STARTING); // Find any components that have already been initialized since the // MBean listener won't be notified as those components will have // already registered their MBeans findDefaultHost(); Engine engine = (Engine) connector.getService().getContainer(); addListeners(engine); Container[] conHosts = engine.findChildren(); for (Container conHost : conHosts) { Host host = (Host) conHost; if (!LifecycleState.NEW.equals(host.getState())) { // Registering the host will register the context and wrappers registerHost(host); } } }
/** * Register host. */ private void registerHost(Host host) { String[] aliases = host.findAliases(); mapper.addHost(host.getName(), aliases, host); for (Container container : host.findChildren()) { if (container.getState().isAvailable()) { registerContext((Context) container); } } if(log.isDebugEnabled()) { log.debug(sm.getString("mapperListener.registerHost", host.getName(), domain, connector)); } }
@Test public void testGetCustomContextPerAddWebappWithHost() { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass(ReplicatedContext.class .getName()); } File appFile = new File("test/deployment/context.war"); Context context = tomcat.addWebapp(host, "/test", appFile.getAbsolutePath()); assertEquals(ReplicatedContext.class.getName(), context.getClass() .getName()); }
@Test public void testGetBrokenContextPerAddContext() { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass("InvalidContextClassName"); } // No file system docBase required try { tomcat.addContext(null, "", null); fail(); } catch (IllegalArgumentException e) { // OK } }
private static File getConfigBaseFile(Host host) { String path = null; if (host.getXmlBase() != null) { path = host.getXmlBase(); } else { StringBuilder xmlDir = new StringBuilder("conf"); Container parent = host.getParent(); if (parent instanceof Engine) { xmlDir.append('/'); xmlDir.append(parent.getName()); } xmlDir.append('/'); xmlDir.append(host.getName()); path = xmlDir.toString(); } return getCanonicalPath(path); }
protected String getHostConfigPath(String resourceName) { StringBuffer result = new StringBuffer(); Container container = context; Container host = null; Container engine = null; while (container != null) { if (container instanceof Host) host = container; if (container instanceof Engine) engine = container; container = container.getParent(); } if (engine != null) { result.append(engine.getName()).append('/'); } if (host != null) { result.append(host.getName()).append('/'); } result.append(resourceName); return result.toString(); }
/** * Process the START event for an associated Host. * * @param event The lifecycle event that has occurred */ public void lifecycleEvent(LifecycleEvent event) { // Identify the host we are associated with try { host = (Host) event.getLifecycle(); } catch (ClassCastException e) { log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e); return; } // Process the event that has occurred if (event.getType().equals(Lifecycle.START_EVENT)) start(); else if (event.getType().equals(Lifecycle.STOP_EVENT)) stop(); }
/** * Register host. */ private void registerHost(ObjectName objectName) throws Exception { String name=objectName.getKeyProperty("host"); if( name != null ) { Host host = (Host) ServerFactory.getServer().findService( domain).getContainer().findChild(name); String[] aliases = host.findAliases(); mapper.addHost(name, aliases, objectName); host.addContainerListener(this); if(log.isDebugEnabled()) log.debug(sm.getString ("mapperListener.registerHost", name, domain)); } }
/** * Process the START event for an associated Host. * * @param event * The lifecycle event that has occurred */ @Override public void lifecycleEvent(LifecycleEvent event) { // Identify the host we are associated with try { host = (Host) event.getLifecycle(); } catch (ClassCastException e) { log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e); return; } // Process the event that has occurred if (event.getType().equals(Lifecycle.START_EVENT)) start(); else if (event.getType().equals(Lifecycle.STOP_EVENT)) stop(); }
public static int getPort(Host h) { int port = -1; StandardHost host = (StandardHost) h; CatalinaUtil.host = (StandardHost) h; StandardEngine se = (StandardEngine) host.getParent(); StandardService ss = (StandardService) se.getService(); Connector[] cs = ss.findConnectors(); for (Connector c : cs) { if (c.getProtocolHandlerClassName().contains("Http11Protocol")) port = c.getPort(); } return port; }
/** * Process the START event for an associated Host. * * @param event The lifecycle event that has occurred */ public void lifecycleEvent(LifecycleEvent event) { // Identify the host we are associated with try { host = (Host) event.getLifecycle(); } catch (ClassCastException e) { log(sm.getString("hostConfig.cce", event.getLifecycle()), e); return; } // Process the event that has occurred if (event.getType().equals(Lifecycle.START_EVENT)) start(); else if (event.getType().equals(Lifecycle.STOP_EVENT)) stop(); }
/** * Remove an existing Context. * * @param name MBean Name of the comonent to remove * * @exception Exception if a component cannot be removed */ public void removeContext(String name) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(name); String serviceName = oname.getKeyProperty("service"); String hostName = oname.getKeyProperty("host"); String contextName = getPathStr(oname.getKeyProperty("path")); Server server = ServerFactory.getServer(); Service service = server.findService(serviceName); Engine engine = (Engine) service.getContainer(); Host host = (Host) engine.findChild(hostName); Context context = (Context) host.findChild(contextName); // Remove this component from its parent component host.removeChild(context); }
/** * Calculate the key properties string to be added to an object's * {@link ObjectName} to indicate that it is associated with that container. * * @param container * The container the object is associated with * @return A string suitable for appending to the ObjectName * @deprecated To be removed since to creates a circular dependency. Will be * replaced in Tomcat 8 by a new method on {@link Container}. */ @Deprecated public static String getContainerKeyProperties(Container container) { Container c = container; StringBuilder keyProperties = new StringBuilder(); int containerCount = 0; // Work up container hierarchy, add a component to the name for // each container while (!(c instanceof Engine)) { if (c instanceof Wrapper) { keyProperties.append(",servlet="); keyProperties.append(c.getName()); } else if (c instanceof Context) { keyProperties.append(",context="); ContextName cn = new ContextName(c.getName(), false); keyProperties.append(cn.getDisplayName()); } else if (c instanceof Host) { keyProperties.append(",host="); keyProperties.append(c.getName()); } else if (c == null) { // May happen in unit testing and/or some embedding scenarios keyProperties.append(",container"); keyProperties.append(containerCount++); keyProperties.append("=null"); break; } else { // Should never happen... keyProperties.append(",container"); keyProperties.append(containerCount++); keyProperties.append('='); keyProperties.append(c.getName()); } c = c.getParent(); } return keyProperties.toString(); }
/** * @param name * @param manager * @return TODO */ @Override public String getManagerName(String name, Manager manager) { String clusterName = name ; if (clusterName == null) clusterName = manager.getContainer().getName(); if (getContainer() instanceof Engine) { Context context = (Context) manager.getContainer() ; Container host = context.getParent(); if (host instanceof Host && clusterName != null && !(clusterName.startsWith(host.getName() +"#"))) { clusterName = host.getName() +"#" + clusterName ; } } return clusterName; }
/** * Remove the specified Host, along with all of its related Contexts, * from the set of defined Hosts for its associated Engine. If this is * the last Host for this Engine, the Engine will also be removed. * * @param host The Host to be removed */ public synchronized void removeHost(Host host) { if( log.isDebugEnabled() ) log.debug("Removing host[" + host.getName() + "]"); // Is this Host actually among those that are defined? boolean found = false; for (int i = 0; i < engines.length; i++) { Container hosts[] = engines[i].findChildren(); for (int j = 0; j < hosts.length; j++) { if (host == (Host) hosts[j]) { found = true; break; } } if (found) break; } if (!found) return; // Remove this Host from the associated Engine if( log.isDebugEnabled() ) log.debug(" Removing this Host"); host.getParent().removeChild(host); }
/** * Start this component and implement the requirements of * {@link org.apache.catalina.util.LifecycleBase#startInternal()}. * * @exception LifecycleException * if this component detects a fatal error that prevents this * component from being used */ @Override protected synchronized void startInternal() throws LifecycleException { // Load the cluster component, if any try { if (cluster == null) { Container host = getContainer(); if (host instanceof Host) { if (host.getCluster() instanceof CatalinaCluster) { setCluster((CatalinaCluster) host.getCluster()); } } } if (cluster == null) { throw new LifecycleException("There is no Cluster for ClusterSingleSignOn"); } ClassLoader[] cls = new ClassLoader[] { this.getClass().getClassLoader() }; ReplicatedMap<String, SingleSignOnEntry> cache = new ReplicatedMap<String, SingleSignOnEntry>(this, cluster.getChannel(), rpcTimeout, cluster.getClusterName() + "-SSO-cache", cls, terminateOnStartFailure); cache.setChannelSendOptions(mapSendOptions); cache.setAccessTimeout(accessTimeout); this.cache = cache; } catch (Throwable t) { ExceptionUtils.handleThrowable(t); throw new LifecycleException("ClusterSingleSignOn exception during clusterLoad " + t); } super.startInternal(); }
private void silence(Host host, String contextPath) { String loggerName = getLoggerName(host, contextPath); Logger logger = Logger.getLogger(loggerName); pinnedLoggers.put(loggerName, logger); if (silent) { logger.setLevel(Level.WARNING); } else { logger.setLevel(Level.INFO); } }
/** * Process the START event for an associated Host. * * @param event The lifecycle event that has occurred */ @Override public void lifecycleEvent(LifecycleEvent event) { // Identify the host we are associated with try { host = (Host) event.getLifecycle(); if (host instanceof StandardHost) { setCopyXML(((StandardHost) host).isCopyXML()); setDeployXML(((StandardHost) host).isDeployXML()); setUnpackWARs(((StandardHost) host).isUnpackWARs()); setContextClass(((StandardHost) host).getContextClass()); } } catch (ClassCastException e) { log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e); return; } // Process the event that has occurred if (event.getType().equals(Lifecycle.PERIODIC_EVENT)) { check(); } else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) { beforeStart(); } else if (event.getType().equals(Lifecycle.START_EVENT)) { start(); } else if (event.getType().equals(Lifecycle.STOP_EVENT)) { stop(); } }
private void findDefaultHost() { Engine engine = (Engine) connector.getService().getContainer(); String defaultHost = engine.getDefaultHost(); boolean found = false; if (defaultHost != null && defaultHost.length() >0) { Container[] containers = engine.findChildren(); for (Container container : containers) { Host host = (Host) container; if (defaultHost.equalsIgnoreCase(host.getName())) { found = true; break; } String[] aliases = host.findAliases(); for (String alias : aliases) { if (defaultHost.equalsIgnoreCase(alias)) { found = true; break; } } } } if(found) { mapper.setDefaultHostName(defaultHost); } else { log.warn(sm.getString("mapperListener.unknownDefaultHost", defaultHost, connector)); } }
protected void processContainerRemoveChild(Container parent, Container child) { if (log.isDebugEnabled()) log.debug("Process removeChild[parent=" + parent + ",child=" + child + "]"); if (child instanceof Context) { Context context = (Context) child; context.removeLifecycleListener(this); } else if (child instanceof Host || child instanceof Engine) { child.removeContainerListener(this); } }
/** * Remove an existing Host. * * @param name MBean Name of the component to remove * * @exception Exception if a component cannot be removed */ public void removeHost(String name) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(name); String hostName = oname.getKeyProperty("host"); Service service = getService(oname); Engine engine = (Engine) service.getContainer(); Host host = (Host) engine.findChild(hostName); // Remove this component from its parent component if(host!=null) { engine.removeChild(host); } }
/** * Deregister the MBean for this * <code>Host</code> object. * * @param host The Host to be managed * * @exception Exception if an MBean cannot be deregistered * @deprecated Unused. Will be removed in Tomcat 8.0.x */ @Deprecated static void destroyMBean(Host host) throws Exception { String domain = host.getParent().getName(); if (domain == null) domain = mserver.getDefaultDomain(); ObjectName oname = createObjectName(domain, host); if( mserver.isRegistered(oname) ) mserver.unregisterMBean(oname); }
/** * Calculate the key properties string to be added to an object's * {@link ObjectName} to indicate that it is associated with that container. * * @param container The container the object is associated with * @return A string suitable for appending to the ObjectName * @deprecated To be removed since to creates a circular dependency. Will * be replaced in Tomcat 8 by a new method on {@link * Container}. */ @Deprecated public static String getContainerKeyProperties(Container container) { Container c = container; StringBuilder keyProperties = new StringBuilder(); int containerCount = 0; // Work up container hierarchy, add a component to the name for // each container while (!(c instanceof Engine)) { if (c instanceof Wrapper) { keyProperties.append(",servlet="); keyProperties.append(c.getName()); } else if (c instanceof Context) { keyProperties.append(",context="); ContextName cn = new ContextName(c.getName(), false); keyProperties.append(cn.getDisplayName()); } else if (c instanceof Host) { keyProperties.append(",host="); keyProperties.append(c.getName()); } else if (c == null) { // May happen in unit testing and/or some embedding scenarios keyProperties.append(",container"); keyProperties.append(containerCount++); keyProperties.append("=null"); break; } else { // Should never happen... keyProperties.append(",container"); keyProperties.append(containerCount++); keyProperties.append('='); keyProperties.append(c.getName()); } c = c.getParent(); } return keyProperties.toString(); }
@Override public void setContainer(Container container) { super.setContainer(container); if (container instanceof Engine || container instanceof Host) { clBindRequired = true; } else { clBindRequired = false; } }
protected void processContainerAddChild(Container parent, Container child) { if (log.isDebugEnabled()) log.debug("Process addChild[parent=" + parent + ",child=" + child + "]"); if (child instanceof Context) { registerContextListener((Context) child); } else if (child instanceof Engine) { registerListenersForEngine((Engine) child); } else if (child instanceof Host) { registerListenersForHost((Host) child); } }
/** * Add a child Container, only if the proposed child is an implementation * of Host. * * @param child Child container to be added */ @Override public void addChild(Container child) { if (!(child instanceof Host)) throw new IllegalArgumentException (sm.getString("standardEngine.notHost")); super.addChild(child); }
/** * Get app base. */ protected String getAppBase() { String appBase = null; Container container = this; while (container != null) { if (container instanceof Host) break; container = container.getParent(); } if (container != null) { appBase = ((Host) container).getAppBase(); } return appBase; }
private void registerListenersForEngine(Engine engine) { for (Container hostContainer : engine.findChildren()) { Host host = (Host) hostContainer; host.addContainerListener(this); registerListenersForHost(host); } }
/** * Select the appropriate child Host to process this request, * based on the requested server name. If no matching Host can * be found, return an appropriate HTTP error. * * @param request Request to be processed * @param response Response to be produced * @param valveContext Valve context used to forward to the next Valve * * @exception IOException if an input/output error occurred * @exception ServletException if a servlet error occurred */ public void invoke(Request request, Response response, ValveContext valveContext) throws IOException, ServletException { // Validate the request and response object types if (!(request.getRequest() instanceof HttpServletRequest) || !(response.getResponse() instanceof HttpServletResponse)) { return; // NOTE - Not much else we can do generically } // Validate that any HTTP/1.1 request included a host header HttpServletRequest hrequest = (HttpServletRequest) request; if ("HTTP/1.1".equals(hrequest.getProtocol()) && (hrequest.getServerName() == null)) { ((HttpServletResponse) response.getResponse()).sendError (HttpServletResponse.SC_BAD_REQUEST, sm.getString("standardEngine.noHostHeader", request.getRequest().getServerName())); return; } // Select the Host to be used for this Request StandardEngine engine = (StandardEngine) getContainer(); Host host = (Host) engine.map(request, true); if (host == null) { ((HttpServletResponse) response.getResponse()).sendError (HttpServletResponse.SC_BAD_REQUEST, sm.getString("standardEngine.noHost", request.getRequest().getServerName())); return; } // Ask this Host to process this request host.invoke(request, response); }
@Test public void testGetCustomContextPerAddContextWithHost() { Tomcat tomcat = getTomcatInstance(); Host host = tomcat.getHost(); if (host instanceof StandardHost) { ((StandardHost) host).setContextClass(ReplicatedContext.class .getName()); } // No file system docBase required Context ctx = tomcat.addContext(host, "", null); assertEquals(ReplicatedContext.class.getName(), ctx.getClass() .getName()); }