/** * Gets my listen port in Tomcat 7. Returns zero if not possible */ public static int getTomcat7Port() { try { MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0); ObjectName name = new ObjectName("Catalina", "type", "Server"); Server server = (Server) mBeanServer.getAttribute(name, "managedResource"); Service[] services = server.findServices(); for (Service service : services) { for (Connector connector : service.findConnectors()) { ProtocolHandler protocolHandler = connector.getProtocolHandler(); if (protocolHandler instanceof Http11Protocol) { return connector.getPort(); } } } return 0; } catch(Exception ex) { ex.printStackTrace(System.err); return 0; } }
/** * Process a "destroy" event for this Context. */ protected synchronized void destroy() { // Called from StandardContext.destroy() if (log.isDebugEnabled()) log.debug(sm.getString("contextConfig.destroy")); // Skip clearing the work directory if Tomcat is being shutdown Server s = getServer(); if (s != null && !s.getState().isAvailable()) { return; } // Changed to getWorkPath per Bugzilla 35819. if (context instanceof StandardContext) { String workDir = ((StandardContext) context).getWorkPath(); if (workDir != null) { ExpandWar.delete(new File(workDir)); } } }
private Server getServer() { Container c = context; while (c != null && !(c instanceof Engine)) { c = c.getParent(); } if (c == null) { return null; } Service s = ((Engine)c).getService(); if (s == null) { return null; } return s.getServer(); }
/** * Creates a new StandardService and StandardEngine. * * @param domain Domain name for the container instance * @param defaultHost Name of the default host to be used in the Engine * @param baseDir Base directory value for Engine * * @exception Exception if an MBean cannot be created or registered */ public String createStandardServiceEngine(String domain, String defaultHost, String baseDir) throws Exception{ if (!(container instanceof Server)) { throw new Exception("Container not Server"); } StandardEngine engine = new StandardEngine(); engine.setDomain(domain); engine.setName(domain); engine.setDefaultHost(defaultHost); engine.setBaseDir(baseDir); Service service = new StandardService(); service.setContainer(engine); service.setName(domain); ((Server) container).addService(service); return engine.getObjectName().toString(); }
/** * Return the Server object that is the ultimate parent for the container * with which this Realm is associated. If the server cannot be found (eg * because the container hierarchy is not complete), <code>null</code> is * returned. */ protected Server getServer() { Container c = container; if (c instanceof Context) { c = c.getParent(); } if (c instanceof Host) { c = c.getParent(); } if (c instanceof Engine) { Service s = ((Engine)c).getService(); if (s != null) { return s.getServer(); } } return null; }
/** * Create a new StandardService. * * @param parent MBean Name of the associated parent component * @param name Unique name of this StandardService * * @exception Exception if an MBean cannot be created or registered */ public String createStandardService(String parent, String name, String domain) throws Exception { // Create a new StandardService instance StandardService service = new StandardService(); service.setName(name); // Add the new instance to its parent component Server server = ServerFactory.getServer(); server.addService(service); // Return the corresponding MBean name return (service.getObjectName().toString()); }
/** * Create, register, and return an MBean for this * <code>Server</code> object. * * @param server The Server to be managed * * @exception Exception if an MBean cannot be created or registered */ static DynamicMBean createMBean(Server server) throws Exception { String mname = createManagedName(server); ManagedBean managed = registry.findManagedBean(mname); if (managed == null) { Exception e = new Exception("ManagedBean is not found with "+mname); throw new MBeanException(e); } String domain = managed.getDomain(); if (domain == null) domain = mserver.getDefaultDomain(); DynamicMBean mbean = managed.createMBean(server); ObjectName oname = createObjectName(domain, server); if( mserver.isRegistered( oname )) { mserver.unregisterMBean(oname); } mserver.registerMBean(mbean, oname); return (mbean); }
/** * Deregister the MBean for this * <code>Server</code> object. * * @param server The Server to be managed * * @exception Exception if an MBean cannot be deregistered */ static void destroyMBean(Server server) throws Exception { String mname = createManagedName(server); ManagedBean managed = registry.findManagedBean(mname); if (managed == null) { return; } String domain = managed.getDomain(); if (domain == null) domain = mserver.getDefaultDomain(); ObjectName oname = createObjectName(domain, server); if( mserver.isRegistered(oname) ) mserver.unregisterMBean(oname); }
@Override public Server getServer() { if (server != null) { return server; } ExtendedCatalina extendedCatalina = new ExtendedCatalina(); Digester digester = extendedCatalina.createStartDigester(); digester.push(extendedCatalina); try { server = ((ExtendedCatalina) digester .parse(new File(System.getProperty("catalina.base") + RELATIVE_SERVERXML_PATH))).getServer(); this.initBaseDir(); } catch (Exception e) { log.error("Error while parsing server.xml", e); server = null; } finally { return server; } }
/** * Create a new StandardEngine. * * @param parent MBean Name of the associated parent component * @param name Unique name of this Engine * @param defaultHost Default hostname of this Engine * * @exception Exception if an MBean cannot be created or registered */ public String createStandardEngine(String parent, String name, String defaultHost) throws Exception { // Create a new StandardEngine instance StandardEngine engine = new StandardEngine(); engine.setName(name); engine.setDefaultHost(defaultHost); // Add the new instance to its parent component ObjectName pname = new ObjectName(parent); Server server = ServerFactory.getServer(); Service service = server.findService(pname.getKeyProperty("name")); service.setContainer(engine); // Return the corresponding MBean name ManagedBean managed = registry.findManagedBean("StandardEngine"); ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), engine); return (oname.toString()); }
/** * Create a new StandardService. * * @param parent MBean Name of the associated parent component * @param name Unique name of this StandardService * * @exception Exception if an MBean cannot be created or registered */ public String createStandardService(String parent, String name) throws Exception { // Create a new StandardService instance StandardService service = new StandardService(); service.setName(name); // Add the new instance to its parent component Server server = ServerFactory.getServer(); server.addService(service); // Return the corresponding MBean name ManagedBean managed = registry.findManagedBean("StandardService"); ObjectName oname = MBeanUtils.createObjectName(managed.getDomain(), service); return (oname.toString()); }
/** * 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); }
/** * Remove an existing Host. * * @param name MBean Name of the comonent 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 serviceName = oname.getKeyProperty("service"); String hostName = oname.getKeyProperty("host"); Server server = ServerFactory.getServer(); Service service = server.findService(serviceName); Engine engine = (Engine) service.getContainer(); Host host = (Host) engine.findChild(hostName); // Remove this component from its parent component engine.removeChild(host); }
/** * Create, register, and return an MBean for this * <code>Server</code> object. * * @param server The Server to be managed * * @exception Exception if an MBean cannot be created or registered */ public static ModelMBean createMBean(Server server) throws Exception { String mname = createManagedName(server); ManagedBean managed = registry.findManagedBean(mname); if (managed == null) { Exception e = new Exception("ManagedBean is not found with "+mname); throw new MBeanException(e); } String domain = managed.getDomain(); if (domain == null) domain = mserver.getDefaultDomain(); ModelMBean mbean = managed.createMBean(server); ObjectName oname = createObjectName(domain, server); mserver.registerMBean(mbean, oname); return (mbean); }
/** * Deregister the MBean for this * <code>Server</code> object. * * @param server The Server to be managed * * @exception Exception if an MBean cannot be deregistered */ public static void destroyMBean(Server server) throws Exception { String mname = createManagedName(server); ManagedBean managed = registry.findManagedBean(mname); if (managed == null) { return; } String domain = managed.getDomain(); if (domain == null) domain = mserver.getDefaultDomain(); ObjectName oname = createObjectName(domain, server); mserver.unregisterMBean(oname); }
private Server getServer() { Container c = context; while (c != null && !(c instanceof Engine)) { c = c.getParent(); } if (c == null) { return null; } Service s = ((Engine) c).getService(); if (s == null) { return null; } return s.getServer(); }
/** * Creates a new StandardService and StandardEngine. * * @param domain * Domain name for the container instance * @param defaultHost * Name of the default host to be used in the Engine * @param baseDir * Base directory value for Engine * * @exception Exception * if an MBean cannot be created or registered */ public String createStandardServiceEngine(String domain, String defaultHost, String baseDir) throws Exception { if (!(container instanceof Server)) { throw new Exception("Container not Server"); } StandardEngine engine = new StandardEngine(); engine.setDomain(domain); engine.setName(domain); engine.setDefaultHost(defaultHost); engine.setBaseDir(baseDir); Service service = new StandardService(); service.setContainer(engine); service.setName(domain); ((Server) container).addService(service); // engine.setService(service); return engine.getObjectName().toString(); }
/** * Return the Server object that is the ultimate parent for the container * with which this Realm is associated. If the server cannot be found (eg * because the container hierarchy is not complete), <code>null</code> is * returned. */ protected Server getServer() { Container c = container; if (c instanceof Context) { c = c.getParent(); } if (c instanceof Host) { c = c.getParent(); } if (c instanceof Engine) { Service s = ((Engine) c).getService(); if (s != null) { return s.getServer(); } } return null; }
public void lifecycleEvent(LifecycleEvent event) { if (!(event.getLifecycle() instanceof Server)) return; if (!startupCompleted && Lifecycle.AFTER_START_EVENT.equals(event.getType())) handleEvent((Server) event.getLifecycle(), "startup"); else if (startupCompleted) { if(Lifecycle.BEFORE_STOP_EVENT.equals(event.getType())) handleEvent((Server) event.getLifecycle(), "prestop"); else if(Lifecycle.STOP_EVENT.equals(event.getType())) handleEvent((Server) event.getLifecycle(), "stop"); else if(Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) handleEvent((Server) event.getLifecycle(), "poststop"); } return; }
/** * Create an <code>ObjectName</code> for this * <code>Service</code> object. * * @param domain Domain in which this name is to be created * @param environment The ContextEnvironment to be named * * @exception MalformedObjectNameException if a name cannot be created */ public static ObjectName createObjectName(String domain, ContextEnvironment environment) throws MalformedObjectNameException { ObjectName name = null; Object container = environment.getNamingResources().getContainer(); if (container instanceof Server) { name = new ObjectName(domain + ":type=Environment" + ",resourcetype=Global,name=" + environment.getName()); } else if (container instanceof Context) { Context context = ((Context)container); ContextName cn = new ContextName(context.getName(), false); Container host = context.getParent(); name = new ObjectName(domain + ":type=Environment" + ",resourcetype=Context,context=" + cn.getDisplayName() + ",host=" + host.getName() + ",name=" + environment.getName()); } return (name); }
/** * Create an <code>ObjectName</code> for this * <code>Server</code> object. * * @param domain Domain in which this name is to be created * @param resources The NamingResources to be named * * @exception MalformedObjectNameException if a name cannot be created * @deprecated Unused. Will be removed in Tomcat 8.0.x */ @Deprecated static ObjectName createObjectName(String domain, NamingResources resources) throws MalformedObjectNameException { ObjectName name = null; Object container = resources.getContainer(); if (container instanceof Server) { name = new ObjectName(domain + ":type=NamingResources" + ",resourcetype=Global"); } else if (container instanceof Context) { Context context = ((Context)container); ContextName cn = new ContextName(context.getName(), false); Container host = context.getParent(); name = new ObjectName(domain + ":type=NamingResources" + ",resourcetype=Context,context=" + cn.getDisplayName() + ",host=" + host.getName()); } return (name); }