/** * 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(); }
/** * Create the MBeans for the specified Service and its nested components. * * @param service Service for which to create MBeans * * @exception Exception if an exception is thrown during MBean creation */ protected void createMBeans(Service service) throws Exception { // Create the MBean for the Service itself if (log.isDebugEnabled()) log.debug("Creating MBean for Service " + service); //MBeanUtils.createMBean(service); if (service instanceof StandardService) { ((StandardService) service).addPropertyChangeListener(this); } // Create the MBeans for the corresponding Connectors Connector connectors[] = service.findConnectors(); for (int j = 0; j < connectors.length; j++) { createMBeans(connectors[j]); } // Create the MBean for the associated Engine and friends Engine engine = (Engine) service.getContainer(); if (engine != null) { createMBeans(engine); } }
/** * Deregister the MBeans for the specified Service and its nested * components. * * @param service Service for which to destroy MBeans * * @exception Exception if an exception is thrown during MBean destruction */ protected void destroyMBeans(Service service) throws Exception { // Deregister the MBeans for the associated Engine Engine engine = (Engine) service.getContainer(); if (engine != null) { //destroyMBeans(engine); } // // Deregister the MBeans for the corresponding Connectors // Connector connectors[] = service.findConnectors(); // for (int j = 0; j < connectors.length; j++) { // destroyMBeans(connectors[j], service); // } // Deregister the MBean for the Service itself if (log.isDebugEnabled()) { log.debug("Destroying MBean for Service " + service); } //MBeanUtils.destroyMBean(service); if (service instanceof StandardService) { ((StandardService) service).removePropertyChangeListener(this); } }
/** * 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 the MBeans for the specified Service and its nested components. * * @param service Service for which to create MBeans * * @exception Exception if an exception is thrown during MBean creation */ protected void createMBeans(Service service) throws Exception { // Create the MBean for the Service itself if (debug >= 2) log("Creating MBean for Service " + service); MBeanUtils.createMBean(service); if (service instanceof StandardService) { ((StandardService) service).addPropertyChangeListener(this); } // Create the MBeans for the corresponding Connectors Connector connectors[] = service.findConnectors(); for (int j = 0; j < connectors.length; j++) { createMBeans(connectors[j]); } // Create the MBean for the associated Engine and friends Engine engine = (Engine) service.getContainer(); if (engine != null) { createMBeans(engine); } }
/** * Deregister the MBeans for the specified Service and its nested * components. * * @param service Service for which to destroy MBeans * * @exception Exception if an exception is thrown during MBean destruction */ protected void destroyMBeans(Service service) throws Exception { // Deregister the MBeans for the associated Engine Engine engine = (Engine) service.getContainer(); if (engine != null) { destroyMBeans(engine); } // Deregister the MBeans for the corresponding Connectors Connector connectors[] = service.findConnectors(); for (int j = 0; j < connectors.length; j++) { destroyMBeans(connectors[j], service); } // Deregister the MBean for the Service itself if (debug >= 2) { log("Destroying MBean for Service " + service); } MBeanUtils.destroyMBean(service); if (service instanceof StandardService) { ((StandardService) service).removePropertyChangeListener(this); } }
/** * 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()); }
/** * 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(); }
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; }
@Test(description = "Tests the construction of Application Server URL for a sample request") public void testConstructionOfApplicationServerURL() { Request request = new Request(); Connector connector = new Connector(); connector.setProtocol(TestConstants.SSL_PROTOCOL); connector.setPort(TestConstants.SSL_PORT); connector.setScheme(TestConstants.SSL_PROTOCOL); Engine engine = new StandardEngine(); Service service = new StandardService(); engine.setService(service); engine.getService().addConnector(connector); Host host = new StandardHost(); host.setName(TestConstants.DEFAULT_TOMCAT_HOST); request.getMappingData().host = host; host.setParent(engine); Optional<String> actual = SSOUtils.constructApplicationServerURL(request); if (actual.isPresent()) { Assert.assertEquals(actual.get(), TestConstants.DEFAULT_APPLICATION_SERVER_URL); } else { Assert.fail(); } }
@Test(description = "Tests the construction of Application Server URL for no SSL/TLS Connector") public void testConstructionOfApplicationServerURLWithNoConnector() { Request request = new Request(); Engine engine = new StandardEngine(); Service service = new StandardService(); engine.setService(service); Host host = new StandardHost(); host.setName(TestConstants.DEFAULT_TOMCAT_HOST); request.getMappingData().host = host; host.setParent(engine); Optional<String> actual = SSOUtils.constructApplicationServerURL(request); Assert.assertTrue(!actual.isPresent()); }
private void prepareCatalinaComponents() { engine = new StandardEngine(); host = new StandardHost(); fooContext = new StandardContext(); barContext = new StandardContext(); Connector connector = new Connector(); connector.setProtocol(TestConstants.SSL_PROTOCOL); connector.setPort(TestConstants.SSL_PORT); connector.setScheme(TestConstants.SSL_PROTOCOL); Service service = new StandardService(); engine.setService(service); engine.getService().addConnector(connector); host.setAppBase(TestConstants.WEB_APP_BASE); host.setName(TestConstants.DEFAULT_TOMCAT_HOST); host.setParent(engine); fooContext.setParent(host); fooContext.setDocBase(TestConstants.FOO_CONTEXT); barContext.setParent(host); barContext.setDocBase(TestConstants.BAR_CONTEXT); }
private StandardService createCatalinaServer(Bundle bundle) throws Exception { // first try to use the XML file URL xmlConfiguration = bundle.getResource(XML_CONF_LOCATION); if (xmlConfiguration != null) { log.info("Using custom XML configuration " + xmlConfiguration); } else { xmlConfiguration = bundle.getResource(DEFAULT_XML_CONF_LOCATION); if (xmlConfiguration == null) log.error("No XML configuration found; bailing out..."); else log.info("Using default XML configuration " + xmlConfiguration); } return createServerFromXML(xmlConfiguration); }
private StandardService createServerFromXML(URL xmlConfiguration) throws IOException { OsgiCatalina catalina = new OsgiCatalina(); catalina.setAwait(false); catalina.setUseShutdownHook(false); catalina.setName("Catalina"); catalina.setParentClassLoader(Thread.currentThread().getContextClassLoader()); // copy the URL file to a local temporary file (since Catalina doesn't use URL unfortunately) File configTempFile = File.createTempFile("dm.catalina", ".cfg.xml"); configTempFile.deleteOnExit(); // copy URL to temporary file copyURLToFile(xmlConfiguration.openStream(), new FileOutputStream(configTempFile)); log.debug("Copied configuration " + xmlConfiguration + " to temporary file " + configTempFile); catalina.setConfigFile(configTempFile.getAbsolutePath()); catalina.load(); Server server = catalina.getServer(); return (StandardService) server.findServices()[0]; }
private ServiceRegistration publishServerAsAService(StandardService server) { Properties props = new Properties(); // put some extra properties to easily identify the service props.put(Constants.SERVICE_VENDOR, "Spring Dynamic Modules"); props.put(Constants.SERVICE_DESCRIPTION, ServerInfo.getServerInfo()); props.put(Constants.BUNDLE_VERSION, ServerInfo.getServerNumber()); props.put(Constants.BUNDLE_NAME, bundleContext.getBundle().getSymbolicName()); // spring-dm specific property props.put("org.springframework.osgi.bean.name", "tomcat-server"); // publish just the interfaces and the major classes (server/handlerWrapper) String[] classes = new String[] { StandardService.class.getName(), Service.class.getName(), MBeanRegistration.class.getName(), Lifecycle.class.getName() }; return bundleContext.registerService(classes, server, props); }
/** * Create a new StandardEngine. * * @param parent MBean Name of the associated parent component * @param engineName Unique name of this Engine * @param defaultHost Default hostname of this Engine * @param serviceName Unique name of this Service * * @exception Exception if an MBean cannot be created or registered */ public Vector createStandardEngineService(String parent, String engineName, String defaultHost, String serviceName) throws Exception { // Create a new StandardService instance StandardService service = new StandardService(); service.setName(serviceName); // Create a new StandardEngine instance StandardEngine engine = new StandardEngine(); engine.setName(engineName); engine.setDefaultHost(defaultHost); // Need to set engine before adding it to server in order to set domain service.setContainer(engine); // Add the new instance to its parent component Server server = ServerFactory.getServer(); server.addService(service); Vector onames = new Vector(); // FIXME service & engine.getObjectName //ObjectName oname = engine.getObjectName(); ObjectName oname = MBeanUtils.createObjectName(engineName, engine); onames.add(0, oname); //oname = service.getObjectName(); oname = MBeanUtils.createObjectName(engineName, service); onames.add(1, oname); return (onames); }
@Test public void test() throws Exception { StandardContext context = new StandardContext(); context.setName("foo"); WebappLoader loader = new WebappLoader() { @Override public ClassLoader getClassLoader() { return WebappLoader.class.getClassLoader(); } }; context.setLoader(loader); StandardHost host = new StandardHost(); StandardEngine engine = new StandardEngine(); engine.setService(new StandardService()); host.setParent(engine); context.setParent(host); loader.setContext(context); RedisSessionManager manager = new RedisSessionManager(); manager.setSessionIdGenerator(new StandardSessionIdGenerator()); manager.setContext(context); manager.initializeSerializer(); manager.initializeDatabaseConnection(); manager.clear(); StandardSession session = manager.createSession(null); session.setAttribute("foo", "test"); manager.afterRequest(); StandardSession loaded = manager.findSession(session.getId()); Assert.assertEquals(session.getAttribute("foo"), loaded.getAttribute("foo")); Assert.assertEquals(1, manager.getSize()); Assert.assertArrayEquals(new String[] { session.getId() }, manager.keys()); manager.processExpires(); }
/** * Remove an existing Context. * * @param contextName * MBean Name of the component to remove * * @exception Exception * if a component cannot be removed */ public void removeContext(String contextName) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(contextName); String domain = oname.getDomain(); StandardService service = (StandardService) getService(oname); Engine engine = (Engine) service.getContainer(); String name = oname.getKeyProperty("name"); name = name.substring(2); int i = name.indexOf('/'); String hostName = name.substring(0, i); String path = name.substring(i); ObjectName deployer = new ObjectName(domain + ":type=Deployer,host=" + hostName); String pathStr = getPathStr(path); if (mserver.isRegistered(deployer)) { mserver.invoke(deployer, "addServiced", new Object[] { pathStr }, new String[] { "java.lang.String" }); mserver.invoke(deployer, "unmanageApp", new Object[] { pathStr }, new String[] { "java.lang.String" }); mserver.invoke(deployer, "removeServiced", new Object[] { pathStr }, new String[] { "java.lang.String" }); } else { log.warn("Deployer not found for " + hostName); Host host = (Host) engine.findChild(hostName); Context context = (Context) host.findChild(pathStr); // Remove this component from its parent component host.removeChild(context); if (context instanceof StandardContext) try { ((StandardContext) context).destroy(); } catch (Exception e) { log.warn("Error during context [" + context.getName() + "] destroy ", e); } } }
Service build() { Service service = new StandardService(); service.setName(name); for (ConnectorConfiguration connectorConfiguration : connectors) { Connector connector = connectorConfiguration.build(); service.addConnector(connector); } return service; }
public Service getJBoss7TomcatService() { StandardService result = null; WebServer webServer = getWebServer(); if (webServer == null) { LOGGER.log(Level.SEVERE, "The web server is null!"); } else { result = webServer.getService(); } return result; }
/** * Remove an existing Context. * * @param contextName MBean Name of the component to remove * * @exception Exception if a component cannot be removed */ public void removeContext(String contextName) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(contextName); String domain = oname.getDomain(); StandardService service = (StandardService) getService(oname); Engine engine = (Engine) service.getContainer(); String name = oname.getKeyProperty("name"); name = name.substring(2); int i = name.indexOf('/'); String hostName = name.substring(0,i); String path = name.substring(i); ObjectName deployer = new ObjectName(domain+":type=Deployer,host="+ hostName); String pathStr = getPathStr(path); if(mserver.isRegistered(deployer)) { mserver.invoke(deployer,"addServiced", new Object[]{pathStr}, new String[] {"java.lang.String"}); mserver.invoke(deployer,"unmanageApp", new Object[] {pathStr}, new String[] {"java.lang.String"}); mserver.invoke(deployer,"removeServiced", new Object[] {pathStr}, new String[] {"java.lang.String"}); } else { log.warn("Deployer not found for "+hostName); Host host = (Host) engine.findChild(hostName); Context context = (Context) host.findChild(pathStr); // Remove this component from its parent component host.removeChild(context); if(context instanceof StandardContext) try { ((StandardContext)context).destroy(); } catch (Exception e) { log.warn("Error during context [" + context.getName() + "] destroy ", e); } } }
/** * Get the service object. Can be used to add more * connectors and few other global settings. */ public StandardService getService() { getServer(); return service; }
/** * Remove an existing Context. * * @param contextName MBean Name of the comonent to remove * * @exception Exception if a component cannot be removed */ public void removeContext(String contextName) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(contextName); String domain = oname.getDomain(); StandardService service = (StandardService) getService(oname); Engine engine = (Engine) service.getContainer(); String name = oname.getKeyProperty("name"); name = name.substring(2); int i = name.indexOf("/"); String hostName = name.substring(0,i); String path = name.substring(i); ObjectName deployer = new ObjectName(domain+":type=Deployer,host="+ hostName); String pathStr = getPathStr(path); if(mserver.isRegistered(deployer)) { mserver.invoke(deployer,"addServiced", new Object[]{pathStr}, new String[] {"java.lang.String"}); mserver.invoke(deployer,"unmanageApp", new Object[] {pathStr}, new String[] {"java.lang.String"}); mserver.invoke(deployer,"removeServiced", new Object[] {pathStr}, new String[] {"java.lang.String"}); } else { log.warn("Deployer not found for "+hostName); Host host = (Host) engine.findChild(hostName); Context context = (Context) host.findChild(pathStr); // Remove this component from its parent component host.removeChild(context); if(context instanceof StandardContext) try { ((StandardContext)context).destroy(); } catch (Exception e) { log.warn("Error during context [" + context.getName() + "] destroy ", e); } } }
@Test public void test() throws Exception { StandardContext context = new StandardContext(); context.setName("foo"); WebappLoader loader = new WebappLoader() { @Override public ClassLoader getClassLoader() { return WebappLoader.class.getClassLoader(); } }; context.setLoader(loader); StandardHost host = new StandardHost(); StandardEngine engine = new StandardEngine(); engine.setService(new StandardService()); host.setParent(engine); context.setParent(host); loader.setContext(context); MongoPersistentManager manager = new MongoPersistentManager(); manager.setContext(context); MongoStore store = new MongoStore(); store.setManager(manager); store.setHosts("localhost:27017"); store.setDbName("mongo_session_test"); store.setCollectionName("mongo_session_test"); manager.setStore(store); store.start(); StandardSession session = new StandardSession(manager); session.setId("4711"); session.setNew(true); session.setValid(true); session.setCreationTime(System.currentTimeMillis()); session.setAttribute("foo", "test"); store.save(session); StandardSession loaded = store.load(session.getId()); Assert.assertEquals(session.getAttribute("foo"), loaded.getAttribute("foo")); Assert.assertEquals(1, store.getSize()); Assert.assertArrayEquals(new String[] { "4711" }, store.keys()); }
@Bean public TomcatEmbeddedServletContainerFactory tomcatFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer( Tomcat tomcat) { Server server = tomcat.getServer(); Service service = new StandardService(); service.setName("ssl-redirect-service"); Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setPort(sslRedirectPort); service.addConnector(connector); server.addService(service); Engine engine = new StandardEngine(); service.setContainer(engine); Host host = new StandardHost(); host.setName("ssl-redirect-host"); engine.addChild(host); engine.setDefaultHost(host.getName()); Context context = new StandardContext(); context.addLifecycleListener(new Tomcat.FixContextListener()); context.setName("ssl-redirect-context"); context.setPath(""); host.addChild(context); Wrapper wrapper = context.createWrapper(); wrapper.setServlet(new HttpServlet() { @Override public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletServerHttpRequest r = new ServletServerHttpRequest(req); UriComponentsBuilder b = UriComponentsBuilder.fromHttpRequest(r); b.scheme("https"); b.port(null); res.sendRedirect(b.toUriString()); } }); wrapper.setName("ssl-redirect-servlet"); context.addChild(wrapper); context.addServletMapping("/", wrapper.getName()); return super.getTomcatEmbeddedServletContainer(tomcat); } }; }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); this.service = new StandardService(); this.service.setContainer(this.engine); }
/** * Remove an existing Context. * * @param contextName MBean Name of the component to remove * * @exception Exception if a component cannot be removed */ public void removeContext(String contextName) throws Exception { // Acquire a reference to the component to be removed ObjectName oname = new ObjectName(contextName); String domain = oname.getDomain(); StandardService service = (StandardService) getService(oname); Engine engine = (Engine) service.getContainer(); String name = oname.getKeyProperty("name"); name = name.substring(2); int i = name.indexOf("/"); String hostName = name.substring(0,i); String path = name.substring(i); ObjectName deployer = new ObjectName(domain+":type=Deployer,host="+ hostName); String pathStr = getPathStr(path); if(mserver.isRegistered(deployer)) { mserver.invoke(deployer,"addServiced", new Object[]{pathStr}, new String[] {"java.lang.String"}); mserver.invoke(deployer,"unmanageApp", new Object[] {pathStr}, new String[] {"java.lang.String"}); mserver.invoke(deployer,"removeServiced", new Object[] {pathStr}, new String[] {"java.lang.String"}); } else { log.warn("Deployer not found for "+hostName); Host host = (Host) engine.findChild(hostName); Context context = (Context) host.findChild(pathStr); // Remove this component from its parent component host.removeChild(context); if(context instanceof StandardContext) try { ((StandardContext)context).destroy(); } catch (Exception e) { log.warn("Error during context [" + context.getName() + "] destroy ", e); } } }