/** * Prepare for active use of the public methods of this Component. * * @exception LifecycleException if this component detects a fatal error * that prevents it from being started */ public synchronized void start() throws LifecycleException { try { StandardServer server = (StandardServer) ServerFactory.getServer(); Context context = server.getGlobalNamingContext(); database = (UserDatabase) context.lookup(resourceName); } catch (Throwable e) { e.printStackTrace(); log(sm.getString("userDatabaseRealm.lookup", resourceName), e); database = null; } if (database == null) { throw new LifecycleException (sm.getString("userDatabaseRealm.noDatabase", resourceName)); } // Perform normal superclass initialization super.start(); }
private static Tomcat createTomcat(int port, String contextPath, String docBase) throws ServletException { String tmpDir = System.getProperty("java.io.tmpdir"); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(tmpDir); tomcat.getHost().setAppBase(tmpDir); tomcat.getHost().setAutoDeploy(false); tomcat.getEngine().setBackgroundProcessorDelay(-1); tomcat.setConnector(newNioConnector()); tomcat.getConnector().setPort(port); tomcat.getService().addConnector(tomcat.getConnector()); Context context = tomcat.addWebapp(contextPath, docBase); StandardServer server = (StandardServer) tomcat.getServer(); server.addLifecycleListener(new AprLifecycleListener()); server.addLifecycleListener(new JreMemoryLeakPreventionListener()); return tomcat; }
@BeforeClass public void init() { System.setProperty(Globals.CATALINA_BASE_PROP, Paths.get(TestConstants.TEST_RESOURCES_LOCATION).toString()); prepareCatalinaComponents(); // load the server level configurations ServerConfigurationLoader loader = new ServerConfigurationLoader(); List<Lifecycle> components = new ArrayList<>(); components.add(new StandardServer()); components.add(engine); components.stream() .forEach(component -> loader.lifecycleEvent( new LifecycleEvent(component, Lifecycle.BEFORE_START_EVENT, null))); // load the context configurations ContextConfigurationLoader contextLoader = new ContextConfigurationLoader(); contextLoader.lifecycleEvent(new LifecycleEvent(barContext, Lifecycle.BEFORE_START_EVENT, null)); contextLoader.lifecycleEvent(new LifecycleEvent(fooContext, Lifecycle.BEFORE_START_EVENT, null)); }
private void startTomcat() throws Exception { tomcat = new Tomcat(); tomcat.setPort(8080); File tomcatBaseDir = new File("target/tomcat"); tomcatBaseDir.mkdirs(); tomcat.setBaseDir(tomcatBaseDir.getAbsolutePath()); StandardServer server = (StandardServer) tomcat.getServer(); server.addLifecycleListener(new AprLifecycleListener()); String contextPath = "/logging-webapp"; File warFile = new File("target/wars/logging-webapp.war"); tomcat.addWebapp(contextPath, warFile.getAbsolutePath()); tomcat.start(); }
public Server build() { Server server = new StandardServer(); server.setPort(shutdownPort); server.setShutdown(shutdownString); if (enableJSP) { server.addLifecycleListener(new JasperListener()); } for (ServiceConfiguration serviceConfiguration : getServices()) { Service service = serviceConfiguration.build(); server.addService(service); } return server; }
public static void importOpenEJBResourcesInTomcat(final Collection<ResourceInfo> resources, final StandardServer server) { final NamingResourcesImpl naming = server.getGlobalNamingResources(); for (final ResourceInfo info : resources) { final String name = info.id; // if invalid or existing or lazy just skip it cause doesnt work during startup if (name == null || naming.findResource(name) != null || info.properties.containsKey("UseAppClassLoader")) { continue; } final ContextResource resource = new ContextResource(); resource.setName(name); resource.setProperty(Constants.FACTORY, ResourceFactory.class.getName()); resource.setProperty(NamingUtil.NAME, name); resource.setType(info.className); resource.setAuth("Container"); naming.addResource(resource); } }
public void start(final StandardServer server) { if (SystemInstance.get().isDefaultProfile()) { // add user tomee is no user are specified try { final NamingResourcesImpl resources = server.getGlobalNamingResources(); final ContextResource userDataBaseResource = resources.findResource("UserDatabase"); final UserDatabase db = (UserDatabase) server.getGlobalNamingContext().lookup(userDataBaseResource.getName()); if (!db.getUsers().hasNext() && db instanceof MemoryUserDatabase) { final MemoryUserDatabase mudb = (MemoryUserDatabase) db; final boolean oldRo = mudb.getReadonly(); try { ((MemoryUserDatabase) db).setReadonly(false); db.createRole("tomee-admin", "tomee admin role"); db.createUser("tomee", "tomee", "TomEE"); db.findUser("tomee").addRole(db.findRole("tomee-admin")); } finally { mudb.setReadonly(oldRo); } } } catch (final Throwable t) { // no-op } } }
private void startTomcat() throws Exception { tomcat = new Tomcat(); tomcat.setPort(8080); File tomcatBaseDir = new File("target/tomcat"); tomcatBaseDir.mkdirs(); tomcat.setBaseDir(tomcatBaseDir.getAbsolutePath()); StandardServer server = (StandardServer) tomcat.getServer(); server.addLifecycleListener(new AprLifecycleListener()); String contextPath = "/logging-webapp"; File[] warFiles = new File("target").listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.getName().endsWith(".war"); } }); assertEquals("Not exactly one war file found", 1, warFiles.length); tomcat.addWebapp(contextPath, warFiles[0].getAbsolutePath()); tomcat.start(); }
private void startTomcat() throws Exception { String appBase = "target/wars/logging-webapp.war"; tomcat = new Tomcat(); tomcat.setPort(8080); tomcat.setBaseDir("."); tomcat.getHost().setAppBase("."); String contextPath = "/logging-webapp"; StandardServer server = (StandardServer) tomcat.getServer(); server.addLifecycleListener(new AprLifecycleListener()); tomcat.addWebapp(contextPath, appBase); tomcat.start(); }
protected void deployBpmPlatform(LifecycleEvent event) { final StandardServer server = (StandardServer) event.getSource(); containerDelegate.getServiceContainer().createDeploymentOperation("deploy BPM platform") .addAttachment(TomcatAttachments.SERVER, server) .addStep(new TomcatParseBpmPlatformXmlStep()) .addStep(new DiscoverBpmPlatformPluginsStep()) .addStep(new StartManagedThreadPoolStep()) .addStep(new StartJobExecutorStep()) .addStep(new PlatformXmlStartProcessEnginesStep()) .execute(); LOG.camundaBpmPlatformSuccessfullyStarted(server.getServerInfo()); }
/** * Write the configuration information for this entire <code>Server</code> * out to the server.xml configuration file. * * @exception InstanceNotFoundException if the managed resource object * cannot be found * @exception MBeanException if the initializer of the object throws * an exception, or persistence is not supported * @exception RuntimeOperationsException if an exception is reported * by the persistence mechanism */ public synchronized void store() throws InstanceNotFoundException, MBeanException, RuntimeOperationsException { Server server = ServerFactory.getServer(); if (server instanceof StandardServer) { try { ((StandardServer) server).storeConfig(); } catch (Exception e) { throw new MBeanException(e, "Error updating conf/server.xml"); } } }
/** * Create the MBeans for the specified Server and its nested components. * * @param server Server for which to create MBeans * * @exception Exception if an exception is thrown during MBean creation */ protected void createMBeans(Server server) throws Exception { // Create the MBean for the Server itself if (log.isDebugEnabled()) log.debug("Creating MBean for Server " + server); //MBeanUtils.createMBean(server); if (server instanceof StandardServer) { ((StandardServer) server).addPropertyChangeListener(this); } // Create the MBeans for the global NamingResources (if any) NamingResources resources = server.getGlobalNamingResources(); if (resources != null) { createMBeans(resources); } // Create the MBeans for each child Service Service services[] = server.findServices(); for (int i = 0; i < services.length; i++) { // FIXME - Warp object hierarchy not currently supported if (services[i].getContainer().getClass().getName().equals ("org.apache.catalina.connector.warp.WarpEngine")) { if (log.isDebugEnabled()) { log.debug("Skipping MBean for Service " + services[i]); } continue; } createMBeans(services[i]); } }
/** * Deregister the MBeans for the specified Server and its related * components. * * @param server Server for which to destroy MBeans * * @exception Exception if an exception is thrown during MBean destruction */ protected void destroyMBeans(Server server) throws Exception { // Destroy the MBeans for the global NamingResources (if any) NamingResources resources = server.getGlobalNamingResources(); if (resources != null) { destroyMBeans(resources); } // Destroy the MBeans for each child Service Service services[] = server.findServices(); for (int i = 0; i < services.length; i++) { // FIXME - Warp object hierarchy not currently supported if (services[i].getContainer().getClass().getName().equals ("org.apache.catalina.connector.warp.WarpEngine")) { if (log.isDebugEnabled()) { log.debug("Skipping MBean for Service " + services[i]); } continue; } destroyMBeans(services[i]); } // Destroy the MBean for the Server itself if (log.isDebugEnabled()) { log.debug("Destroying MBean for Server " + server); } //MBeanUtils.destroyMBean(server); if (server instanceof StandardServer) { ((StandardServer) server).removePropertyChangeListener(this); } }
/** * Process a PUT request for the specified resource. * * @param request The servlet request we are processing * @param response The servlet response we are creating * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet-specified error occurs */ public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Verify that we were not accessed using the invoker servlet if (request.getAttribute(Globals.INVOKED_ATTR) != null) throw new UnavailableException (sm.getString("managerServlet.cannotInvoke")); // Identify the request parameters that we need String command = request.getPathInfo(); if (command == null) command = request.getServletPath(); String path = request.getParameter("path"); // Prepare our output writer to generate the response message response.setContentType("text/plain"); Locale locale = Locale.getDefault(); response.setLocale(locale); PrintWriter writer = response.getWriter(); // Process the requested command if (command == null) { writer.println(sm.getString("managerServlet.noCommand")); } else if (command.equals("/deploy")) { deploy(writer, path, request); } else { writer.println(sm.getString("managerServlet.unknownCommand", command)); } // Saving configuration Server server = ServerFactory.getServer(); if ((server != null) && (server instanceof StandardServer)) { try { ((StandardServer) server).store(); } catch (Exception e) { writer.println(sm.getString("managerServlet.saveFail", e.getMessage())); } } // Finish up the response writer.flush(); writer.close(); }
/** * Write the configuration information for this entire <code>Server</code> * out to the server.xml configuration file. * * @exception InstanceNotFoundException if the managed resource object * cannot be found * @exception MBeanException if the initializer of the object throws * an exception, or persistence is not supported * @exception RuntimeOperationsException if an exception is reported * by the persistence mechanism */ public synchronized void store() throws InstanceNotFoundException, MBeanException, RuntimeOperationsException { Server server = ServerFactory.getServer(); if (server instanceof StandardServer) { try { ((StandardServer) server).store(); } catch (Exception e) { throw new MBeanException(e, "Error updating conf/server.xml"); } } }
/** * Create the MBeans for the specified Server and its nested components. * * @param server Server for which to create MBeans * * @exception Exception if an exception is thrown during MBean creation */ protected void createMBeans(Server server) throws Exception { // Create the MBean for the Server itself if (debug >= 2) log("Creating MBean for Server " + server); MBeanUtils.createMBean(server); if (server instanceof StandardServer) { ((StandardServer) server).addPropertyChangeListener(this); } // Create the MBeans for the global NamingResources (if any) NamingResources resources = server.getGlobalNamingResources(); if (resources != null) { createMBeans(resources); } // Create the MBeans for each child Service Service services[] = server.findServices(); for (int i = 0; i < services.length; i++) { // FIXME - Warp object hierarchy not currently supported if (services[i].getContainer().getClass().getName().equals ("org.apache.catalina.connector.warp.WarpEngine")) { if (debug >= 1) { log("Skipping MBean for Service " + services[i]); } continue; } createMBeans(services[i]); } }
/** * Deregister the MBeans for the specified Server and its related * components. * * @param server Server for which to destroy MBeans * * @exception Exception if an exception is thrown during MBean destruction */ protected void destroyMBeans(Server server) throws Exception { // Destroy the MBeans for the global NamingResources (if any) NamingResources resources = server.getGlobalNamingResources(); if (resources != null) { destroyMBeans(resources); } // Destroy the MBeans for each child Service Service services[] = server.findServices(); for (int i = 0; i < services.length; i++) { // FIXME - Warp object hierarchy not currently supported if (services[i].getContainer().getClass().getName().equals ("org.apache.catalina.connector.warp.WarpEngine")) { if (debug >= 1) { log("Skipping MBean for Service " + services[i]); } continue; } destroyMBeans(services[i]); } // Destroy the MBean for the Server itself if (debug >= 2) { log("Destroying MBean for Server " + server); } MBeanUtils.destroyMBean(server); if (server instanceof StandardServer) { ((StandardServer) server).removePropertyChangeListener(this); } }
/** * Process a PUT request for the specified resource. * * @param request The servlet request we are processing * @param response The servlet response we are creating * @throws IOException if an input/output error occurs * @throws ServletException if a servlet-specified error occurs */ public void doPut(final HttpServletRequest request, final HttpServletResponse response) throws IOException, ServletException { // Verify that we were not accessed using the invoker servlet if (request.getAttribute(Globals.INVOKED_ATTR) != null) { throw new UnavailableException (sm.getString("managerServlet.cannotInvoke")); } // Identify the request parameters that we need String command = request.getPathInfo(); if (command == null) { command = request.getServletPath(); } final String path = request.getParameter("path"); // Prepare our output writer to generate the response message response.setContentType("text/plain"); final Locale locale = Locale.getDefault(); response.setLocale(locale); final PrintWriter writer = response.getWriter(); // Process the requested command if (command == null) { writer.println(sm.getString("managerServlet.noCommand")); } else if (command.equals("/deploy")) { deploy(writer, path, request); } else { writer.println(sm.getString("managerServlet.unknownCommand", command)); } // Saving configuration final Server server = ServerFactory.getServer(); if (server != null && server instanceof StandardServer) { try { ((StandardServer) server).store(); } catch (Exception e) { writer.println(sm.getString("managerServlet.saveFail", e.getMessage())); } } // Finish up the response writer.flush(); writer.close(); }
public RealmAuthentication() { // TODO: Fix this! // For now, log in to the Realm realms = new HashMap<String, VistaAccessVerifyRealm>(); StandardServer server = (StandardServer)ServerFactory.getServer(); for (Service service : server.findServices()) { addVistaRealmsToList(service); } }
private synchronized static List<Realm> getRealms() { if(realms == null) { realms = new ArrayList<Realm>(); StandardServer server = (StandardServer)ServerFactory.getServer(); for (Service service : server.findServices()) { addVistaRealmsToList(service); } } return realms; }
public javax.naming.Context getGlobalContext() { Server server = ServerFactory.getServer(); javax.naming.Context globalContext = null; if( (server != null) && (server instanceof StandardServer) ) globalContext = ((StandardServer) server).getGlobalNamingContext(); return globalContext; }
/** * * @return */ private SortedSet<Realm> initializeRealms() { SortedSet<Realm> realms = new TreeSet<Realm>( new RealmHierarchyComparator() ); StandardServer server = (StandardServer)ServerFactory.getServer(); for (Service service : server.findServices()) recurseContainers(service.getContainer(), realms); return realms; }
TomcatServiceConfig(String serviceName, String engineName, Path baseDir, Realm realm, String hostname, Path docBase, String jarRoot, List<Consumer<? super StandardServer>> configurators) { this.engineName = engineName; this.serviceName = serviceName; this.baseDir = baseDir; this.realm = realm; this.hostname = hostname; this.docBase = docBase; this.jarRoot = jarRoot; this.configurators = configurators; }
public static void main(String[] args) throws Exception { setupDebugEnv(); int port = 7070; if (args.length >= 1) { port = Integer.parseInt(args[0]); } File webBase = new File("../webapp/app"); File webInfDir = new File(webBase, "WEB-INF"); FileUtils.deleteDirectory(webInfDir); FileUtils.copyDirectoryToDirectory(new File("../server/src/main/webapp/WEB-INF"), webBase); Tomcat tomcat = new Tomcat(); tomcat.setPort(port); tomcat.setBaseDir("."); // Add AprLifecycleListener StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); server.addLifecycleListener(listener); Context webContext = tomcat.addWebapp("/kylin", webBase.getAbsolutePath()); ErrorPage notFound = new ErrorPage(); notFound.setErrorCode(404); notFound.setLocation("/index.html"); webContext.addErrorPage(notFound); webContext.addWelcomeFile("index.html"); // tomcat start tomcat.start(); tomcat.getServer().await(); }
private static List<Lifecycle> generateSampleTomcatComponents() { List<Lifecycle> components = new ArrayList<>(); components.add(new StandardHost()); components.add(new StandardServer()); return components; }
@BeforeTest public void prepareConfigurations() { ServerConfigurationLoader serverConfigurationLoader = new ServerConfigurationLoader(); StandardServer server = new StandardServer(); serverConfigurationLoader.lifecycleEvent(new LifecycleEvent(server, Lifecycle.BEFORE_START_EVENT, null)); ContextConfigurationLoader contextConfigurationLoader = new ContextConfigurationLoader(); StandardContext context = new StandardContext(); context.setParent(new StandardHost()); context.setDocBase("mock-doc"); contextConfigurationLoader.lifecycleEvent(new LifecycleEvent(context, Lifecycle.BEFORE_START_EVENT, null)); classLoaderContext = new WebappClassLoaderContext(context); }
@Test public void run() { try (final Container container = new Container(new Configuration().conf("ConfTest"))) { final StandardServer standardServer = TomcatHelper.getServer(); final Realm engineRealm = standardServer.findServices()[0].getContainer().getRealm(); assertTrue(String.valueOf(engineRealm), TomEERealm.class.isInstance(engineRealm)); assertTrue(String.valueOf(engineRealm), JAASRealm.class.isInstance(TomEERealm.class.cast(engineRealm).getNestedRealms()[0])); final JAASRealm jaas = JAASRealm.class.cast(TomEERealm.class.cast(engineRealm).getNestedRealms()[0]); assertEquals("PropertiesLoginModule", jaas.getAppName()); assertEquals("org.apache.openejb.core.security.jaas.UserPrincipal", jaas.getUserClassNames()); assertEquals("org.apache.openejb.core.security.jaas.GroupPrincipal", jaas.getRoleClassNames()); assertEquals("test", SystemInstance.get().getProperty("ConfTest.value")); } }
public TomcatWsRegistry() { final StandardServer standardServer = TomcatHelper.getServer(); for (final Service service : standardServer.findServices()) { if (service.getContainer() instanceof Engine) { connectors = Arrays.asList(service.findConnectors()); engine = (Engine) service.getContainer(); break; } } }
/** * Process running web applications for ejb deployments. * * @param tomcatWebAppBuilder tomcat web app builder instance * @param standardServer tomcat server instance */ private void processRunningApplications(final TomcatWebAppBuilder tomcatWebAppBuilder, final StandardServer standardServer) { for (final org.apache.catalina.Service service : standardServer.findServices()) { if (service.getContainer() instanceof Engine) { final Engine engine = (Engine) service.getContainer(); for (final Container engineChild : engine.findChildren()) { if (engineChild instanceof Host) { final Host host = (Host) engineChild; for (final Container hostChild : host.findChildren()) { if (hostChild instanceof StandardContext) { final StandardContext standardContext = (StandardContext) hostChild; final int state = TomcatHelper.getContextState(standardContext); if (state == 0) { // context only initialized tomcatWebAppBuilder.init(standardContext); } else if (state == 1) { // context already started standardContext.addParameter("openejb.start.late", "true"); final ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(standardContext.getLoader().getClassLoader()); try { tomcatWebAppBuilder.init(standardContext); tomcatWebAppBuilder.beforeStart(standardContext); tomcatWebAppBuilder.start(standardContext); tomcatWebAppBuilder.afterStart(standardContext); } finally { Thread.currentThread().setContextClassLoader(oldCL); } standardContext.removeParameter("openejb.start.late"); } } } } } } } }
/** * Creates a new instance. * * @param standardServer tomcat server instance * @param contextListener context listener instance */ public GlobalListenerSupport(final StandardServer standardServer, final ContextListener contextListener) { if (standardServer == null) { throw new NullPointerException("standardServer is null"); } if (contextListener == null) { throw new NullPointerException("contextListener is null"); } this.standardServer = standardServer; this.contextListener = contextListener; // this.contextListener is now an instance of TomcatWebAppBuilder }
@Override public void lifecycleEvent(final LifecycleEvent event) { // only install once if (listenerInstalled || !Lifecycle.AFTER_INIT_EVENT.equals(event.getType())) { return; } try { File webappDir = findOpenEjbWar(); if (webappDir == null && event.getSource() instanceof StandardServer) { final StandardServer server = (StandardServer) event.getSource(); webappDir = tryToFindAndExtractWar(server); if (webappDir != null) { // we are using webapp startup final File exploded = extractDirectory(webappDir); if (exploded != null) { extract(webappDir, exploded); } webappDir = exploded; TomcatHelper.setServer(server); } } if (webappDir != null) { LOGGER.info("found the tomee webapp on " + webappDir.getPath()); final Properties properties = new Properties(); properties.setProperty("tomee.war", webappDir.getAbsolutePath()); properties.setProperty("openejb.embedder.source", OpenEJBListener.class.getSimpleName()); TomcatEmbedder.embed(properties, StandardServer.class.getClassLoader()); listenerInstalled = true; } else if (logWebappNotFound) { LOGGER.info("tomee webapp not found from the listener, will try from the webapp if exists"); logWebappNotFound = false; } } catch (final Exception e) { LOGGER.log(Level.SEVERE, "TomEE Listener can't start OpenEJB", e); // e.printStackTrace(System.err); } }
private static File tryToFindAndExtractWar(final StandardServer source) { if (System.getProperties().containsKey("openejb.war")) { return new File(System.getProperty("openejb.war")); } for (final Service service : source.findServices()) { final Container container = service.getContainer(); if (container instanceof StandardEngine) { final StandardEngine engine = (StandardEngine) container; for (final Container child : engine.findChildren()) { if (child instanceof StandardHost) { final StandardHost host = (StandardHost) child; final File base = hostDir(System.getProperty("catalina.base"), host.getAppBase()); final File[] files = base.listFiles(); if (files != null) { for (final File file : files) { if (isTomEEWar(file)) { return file; } } } } } } } return null; }
private void check() { final StandardServer server = TomcatHelper.getServer(); for (final Service service : server.findServices()) { if (service.getContainer() instanceof Engine) { final Engine engine = (Engine) service.getContainer(); for (final Container engineChild : engine.findChildren()) { if (engineChild instanceof StandardHost) { final StandardHost host = (StandardHost) engineChild; webappBuilder.checkHost(host); } } } } }
public TomcatHessianRegistry() { final StandardServer standardServer = TomcatHelper.getServer(); for (final Service service : standardServer.findServices()) { if (Engine.class.isInstance(service.getContainer())) { connectors = Arrays.asList(service.findConnectors()); engine = Engine.class.cast(service.getContainer()); break; } } }
public Server getJBoss7TomcatServer() { StandardServer result = null; WebServer webServer = getWebServer(); if (webServer == null) { LOGGER.log(Level.SEVERE, "The web server is null!"); } else { result = webServer.getServer(); } return result; }