/** * Log a message on the Logger associated with our Container (if any). * * @param message Message to be logged */ protected void log(String message) { Logger logger = null; if (container != null) logger = container.getLogger(); if (logger != null) logger.log("StandardPipeline[" + container.getName() + "]: " + message); else System.out.println("StandardPipeline[" + container.getName() + "]: " + message); }
@Override public String getMBeanKeyProperties() { Container c = this; 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 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(); }
/** * Create an <code>ObjectName</code> for this <code>Manager</code> object. * * @param domain Domain in which this name is to be created * @param manager The Manager to be named * @exception MalformedObjectNameException if a name cannot be created */ static ObjectName createObjectName(String domain, Manager manager) throws MalformedObjectNameException { ObjectName name = null; Container container = manager.getContainer(); if (container instanceof Engine) { name = new ObjectName(domain + ":type=Manager"); } else if (container instanceof Host) { name = new ObjectName(domain + ":type=Manager,host=" + container.getName()); } else if (container instanceof Context) { String path = ((Context) container).getPath(); if (path.length() < 1) { path = "/"; } Host host = (Host) container.getParent(); name = new ObjectName(domain + ":type=Manager,path=" + path + ",host=" + host.getName()); } else if (container == null) { DefaultContext defaultContext = manager.getDefaultContext(); if (defaultContext != null) { Container parent = defaultContext.getParent(); if (parent instanceof Engine) { name = new ObjectName(domain + ":type=DefaultManager"); } else if (parent instanceof Host) { name = new ObjectName(domain + ":type=DefaultManager,host=" + parent.getName()); } } } return (name); }
/** * Log a message on the Logger associated with our Container (if any). * * @param message Message to be logged * @param throwable Associated exception */ protected void log(String message, Throwable throwable) { Logger logger = null; if (container != null) logger = container.getLogger(); if (logger != null) logger.log("StandardPipeline[" + container.getName() + "]: " + message, throwable); else { System.out.println("StandardPipeline[" + container.getName() + "]: " + message); throwable.printStackTrace(System.out); } }
/** Un-initialization. */ @Override public void destroy() throws Exception { log.debug("TomcatVHostLoader un-init"); Container[] children = host.findChildren(); for (Container c : children) { if (c instanceof StandardContext) { try { ((StandardContext) c).stop(); host.removeChild(c); } catch (Exception e) { log.error("Could not stop context: {}", c.getName(), e); } } } // remove system prop String propertyPrefix = name; if (domain != null) { propertyPrefix += '_' + domain.replace('.', '_'); } System.clearProperty(propertyPrefix + ".webapp.root"); // stop the host try { ((StandardHost) host).stop(); } catch (LifecycleException e) { log.error("Could not stop host: {}", host.getName(), e); } // remove host engine.removeChild(host); // unregister jmx unregisterJMX(); }
/** * @param name * @param manager * @return TODO */ @Override public String getManagerName(String name, Manager manager) { String clusterName = name; if (clusterName == null) clusterName = manager.getContext().getName(); if (getContainer() instanceof Engine) { Context context = manager.getContext(); if (context != null) { Container host = context.getParent(); if (host != null && host instanceof Host && clusterName != null && !(clusterName.startsWith(host.getName() + "#"))) { clusterName = host.getName() + "#" + clusterName; } } } return clusterName; }
/** Return the name for this instance (built from container name) */ public String getName() { if (name == null) { Container container = manager.getContainer(); String contextName = container.getName(); String hostName = ""; String engineName = ""; if (container.getParent() != null) { Container host = container.getParent(); hostName = host.getName(); if (host.getParent() != null) { engineName = host.getParent().getName(); } } name = "/" + engineName + "/" + hostName + contextName; } return name; }
/** Return a String rendering of this object. */ @Override public String toString() { StringBuilder sb = new StringBuilder(this.getClass().getName()); sb.append('['); if (container == null) { sb.append("Container is null"); } else { sb.append(container.getName()); } sb.append(']'); return sb.toString(); }
/** * Create an <code>ObjectName</code> for this <code>Valve</code> object. * * @param domain Domain in which this name is to be created * @param valve The Valve to be named * @exception MalformedObjectNameException if a name cannot be created */ static ObjectName createObjectName(String domain, GlassFishValve valve) throws MalformedObjectNameException { if (valve instanceof ValveBase) { ObjectName name = ((ValveBase) valve).getObjectName(); if (name != null) return name; } ObjectName name = null; Container container = null; String className = valve.getClass().getName(); int period = className.lastIndexOf('.'); if (period >= 0) className = className.substring(period + 1); if (valve instanceof Contained) { container = ((Contained) valve).getContainer(); } if (container == null) { throw new MalformedObjectNameException( "Cannot create mbean for non-contained valve " + valve); } if (container instanceof Engine) { String local = ""; int seq = getSeq(local); String ext = ""; if (seq > 0) { ext = ",seq=" + seq; } name = new ObjectName(domain + ":type=Valve,name=" + className + ext + local); } else if (container instanceof Host) { String local = ",host=" + container.getName(); int seq = getSeq(local); String ext = ""; if (seq > 0) { ext = ",seq=" + seq; } name = new ObjectName(domain + ":type=Valve,name=" + className + ext + local); } else if (container instanceof Context) { String path = ((Context) container).getPath(); if (path.length() < 1) { path = "/"; } Host host = (Host) container.getParent(); String local = ",path=" + path + ",host=" + host.getName(); int seq = getSeq(local); String ext = ""; if (seq > 0) { ext = ",seq=" + seq; } name = new ObjectName(domain + ":type=Valve,name=" + className + ext + local); } return (name); }
private File getConfigBase() { File configBase = new File(System.getProperty("catalina.base"), "conf"); 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) { configBase = new File(configBase, engine.getName()); } if (host != null) { configBase = new File(configBase, host.getName()); } return configBase; }
/** * Log a message on the Logger associated with our Container (if any) * * @param message Message to be logged */ protected void log(String message) { Logger logger = null; String name = null; if (container != null) { logger = container.getLogger(); name = container.getName(); } if (logger != null) { logger.log(getName() + "[" + name + "]: " + message); } else { System.out.println(getName() + "[" + name + "]: " + message); } }
// -------------------- JMX and Registration -------------------- @Override public String getObjectNameKeyProperties() { StringBuilder name = new StringBuilder("type=Manager"); if (container instanceof Context) { name.append(",context="); String contextName = container.getName(); if (!contextName.startsWith("/")) { name.append('/'); } name.append(contextName); Context context = (Context) container; name.append(",host="); name.append(context.getParent().getName()); } else { // Unlikely / impossible? Handle it to be safe name.append(",container="); name.append(container.getName()); } return name.toString(); }
/** * Logs the given message to the Logger associated with the Container (if any) of this * StandardPipeline. * * @param message the message * @param t the Throwable */ protected void log(String message, Throwable t) { org.apache.catalina.Logger logger = null; if (container != null) { logger = container.getLogger(); String msg = MessageFormat.format( rb.getString(STANDARD_PIPELINE_INFO), new Object[] {container.getName(), message}); if (logger != null) { logger.log(msg, t, org.apache.catalina.Logger.WARNING); } else { log.log(Level.WARNING, msg, t); } } else { String msg = MessageFormat.format(rb.getString(STANDARD_PIPELINE_NULL_INFO), message); log.log(Level.WARNING, msg, t); // INFO set to WARNING } }
/** * Return a File object representing the "configuration root" directory for our associated Host. */ protected File configBase() { if (configBase != null) { return configBase; } File file = new File(System.getProperty("catalina.base"), "conf"); Container parent = host.getParent(); if ((parent != null) && (parent instanceof Engine)) { file = new File(file, parent.getName()); } file = new File(file, host.getName()); try { configBase = file.getCanonicalFile(); } catch (IOException e) { configBase = file; } return (configBase); }
/** * Create an <code>ObjectName</code> for this <code>Realm</code> object. * * @param domain Domain in which this name is to be created * @param realm The Realm to be named * @exception MalformedObjectNameException if a name cannot be created */ static ObjectName createObjectName(String domain, Realm realm) throws MalformedObjectNameException { ObjectName name = null; Container container = realm.getContainer(); if (container instanceof Engine) { name = new ObjectName(domain + ":type=Realm"); } else if (container instanceof Host) { name = new ObjectName(domain + ":type=Realm,host=" + container.getName()); } else if (container instanceof Context) { String path = ((Context) container).getPath(); if (path.length() < 1) { path = "/"; } Host host = (Host) container.getParent(); name = new ObjectName(domain + ":type=Realm,path=" + path + ",host=" + host.getName()); } return (name); }
/** * Log a message on the Logger associated with our Container (if any). * * @param message Message to be logged */ protected void log(String message) { org.apache.catalina.Logger logger = null; if (container != null) { logger = container.getLogger(); String msg = MessageFormat.format( rb.getString(STANDARD_PIPELINE_INFO), new Object[] {container.getName(), message}); if (logger != null) { logger.log(msg); } else { if (log.isLoggable(Level.INFO)) { log.log(Level.INFO, msg); } } } else { if (log.isLoggable(Level.INFO)) { String msg = MessageFormat.format(rb.getString(STANDARD_PIPELINE_NULL_INFO), message); log.log(Level.INFO, msg); } } }
/** * Initialization. * * @throws ServletException */ @SuppressWarnings("cast") @Override public void start() throws ServletException { log.info("Loading tomcat virtual host"); if (webappFolder != null) { // check for match with base webapp root if (webappFolder.equals(webappRoot)) { log.error("Web application root cannot be the same as base"); return; } } ClassLoader classloader = Thread.currentThread().getContextClassLoader(); // ensure we have a host if (host == null) { host = createHost(); } host.setParentClassLoader(classloader); String propertyPrefix = name; if (domain != null) { propertyPrefix += '_' + domain.replace('.', '_'); } log.debug("Generating name (for props) {}", propertyPrefix); System.setProperty(propertyPrefix + ".webapp.root", webappRoot); log.info("Virtual host root: {}", webappRoot); log.info("Virtual host context id: {}", defaultApplicationContextId); // Root applications directory File appDirBase = new File(webappRoot); // Subdirs of root apps dir File[] dirs = appDirBase.listFiles(new TomcatLoader.DirectoryFilter()); // Search for additional context files for (File dir : dirs) { String dirName = '/' + dir.getName(); // check to see if the directory is already mapped if (null == host.findChild(dirName)) { String webappContextDir = FileUtil.formatPath(appDirBase.getAbsolutePath(), dirName); Context ctx = null; if ("/root".equals(dirName) || "/root".equalsIgnoreCase(dirName)) { log.debug("Adding ROOT context"); ctx = addContext("/", webappContextDir); } else { log.debug("Adding context from directory scan: {}", dirName); ctx = addContext(dirName, webappContextDir); } log.debug("Context: {}", ctx); webappContextDir = null; } } appDirBase = null; dirs = null; // Dump context list if (log.isDebugEnabled()) { for (Container cont : host.findChildren()) { log.debug("Context child name: {}", cont.getName()); } } engine.addChild(host); // Start server try { log.info("Starting Tomcat virtual host"); // may not have to do this step for every host LoaderBase.setApplicationLoader( new TomcatApplicationLoader(embedded, host, applicationContext)); for (Container cont : host.findChildren()) { if (cont instanceof StandardContext) { StandardContext ctx = (StandardContext) cont; ServletContext servletContext = ctx.getServletContext(); log.debug("Context initialized: {}", servletContext.getContextPath()); // set the hosts id servletContext.setAttribute("red5.host.id", getHostId()); String prefix = servletContext.getRealPath("/"); log.debug("Path: {}", prefix); try { Loader cldr = ctx.getLoader(); log.debug("Loader type: {}", cldr.getClass().getName()); ClassLoader webClassLoader = cldr.getClassLoader(); log.debug("Webapp classloader: {}", webClassLoader); // create a spring web application context XmlWebApplicationContext appctx = new XmlWebApplicationContext(); appctx.setClassLoader(webClassLoader); appctx.setConfigLocations(new String[] {"/WEB-INF/red5-*.xml"}); // check for red5 context bean if (applicationContext.containsBean(defaultApplicationContextId)) { appctx.setParent( (ApplicationContext) applicationContext.getBean(defaultApplicationContextId)); } else { log.warn( "{} bean was not found in context: {}", defaultApplicationContextId, applicationContext.getDisplayName()); // lookup context loader and attempt to get what we need from it if (applicationContext.containsBean("context.loader")) { ContextLoader contextLoader = (ContextLoader) applicationContext.getBean("context.loader"); appctx.setParent(contextLoader.getContext(defaultApplicationContextId)); } else { log.debug("Context loader was not found, trying JMX"); MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); // get the ContextLoader from jmx ContextLoaderMXBean proxy = null; ObjectName oName = null; try { oName = new ObjectName("org.red5.server:name=contextLoader,type=ContextLoader"); if (mbs.isRegistered(oName)) { proxy = JMX.newMXBeanProxy(mbs, oName, ContextLoaderMXBean.class, true); log.debug("Context loader was found"); proxy.setParentContext(defaultApplicationContextId, appctx.getId()); } else { log.warn("Context loader was not found"); } } catch (Exception e) { log.warn("Exception looking up ContextLoader", e); } } } if (log.isDebugEnabled()) { if (appctx.getParent() != null) { log.debug("Parent application context: {}", appctx.getParent().getDisplayName()); } } // appctx.setServletContext(servletContext); // set the root webapp ctx attr on the each servlet context so spring can find it later servletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appctx); appctx.refresh(); } catch (Throwable t) { log.error("Error setting up context: {}", servletContext.getContextPath(), t); if (log.isDebugEnabled()) { t.printStackTrace(); } } } } } catch (Exception e) { log.error("Error loading Tomcat virtual host", e); } }
/** * Return the name of the cluster that this Server is currently configured to operate within. * * @return The name of the cluster associated with this server */ @Override public String getClusterName() { if (clusterName == null && container != null) return container.getName(); return clusterName; }