@BeforeClass(alwaysRun = true) public void setUpGlobal() throws Exception { port1 = findFreePort(); embedded = new Embedded(); String path = new File(".").getAbsolutePath(); embedded.setCatalinaHome(path); Engine engine = embedded.createEngine(); engine.setDefaultHost("localhost"); Host host = embedded.createHost("localhost", path); engine.addChild(host); Context c = embedded.createContext("/", path); c.setReloadable(false); Wrapper w = c.createWrapper(); w.addMapping("/*"); w.setServletClass(org.apache.catalina.servlets.WebdavServlet.class.getName()); w.addInitParameter("readonly", "false"); w.addInitParameter("listings", "true"); w.setLoadOnStartup(0); c.addChild(w); host.addChild(c); Connector connector = embedded.createConnector("localhost", port1, Http11NioProtocol.class.getName()); connector.setContainer(host); embedded.addEngine(engine); embedded.addConnector(connector); embedded.start(); }
@Override public Embedded configure() throws Exception { final URL root = new URL(Tomcat6Configurator.class.getResource("/"), "../../../tomcat-core/target/test-classes"); final String cleanedRoot = URLDecoder.decode(root.getFile(), "UTF-8"); final String docBase = cleanedRoot + File.separator + appName; MemoryRealm memoryRealm = new MemoryRealm(); memoryRealm.setPathname(docBase + File.separator + "tomcat-users.xml"); final Embedded catalina = new Embedded(memoryRealm); if (!clientOnly) { P2PLifecycleListener listener = new P2PLifecycleListener(); listener.setConfigLocation(configLocation); catalina.addLifecycleListener(listener); } else { catalina.addLifecycleListener(new ClientServerLifecycleListener()); } final StandardServer server = new StandardServer(); server.addService(catalina); final Engine engine = catalina.createEngine(); engine.setName("engine-" + port); engine.setDefaultHost(DEFAULT_HOST); engine.setJvmRoute("tomcat-" + port); catalina.addEngine(engine); engine.setService(catalina); final Host host = catalina.createHost(DEFAULT_HOST, docBase); engine.addChild(host); final Context context = createContext(catalina, "/", docBase); host.addChild(context); this.manager = new HazelcastSessionManager(); context.setManager((HazelcastSessionManager) manager); updateManager((HazelcastSessionManager) manager); context.setBackgroundProcessorDelay(1); context.setCookies(true); final Connector connector = catalina.createConnector("localhost", port, false); connector.setProperty("bindOnInit", "false"); catalina.addConnector(connector); return catalina; }
private Context createContext(final Embedded catalina, final String contextPath, final String docBase) { return catalina.createContext(contextPath, docBase); }
public static void main(String[] args) throws Exception { // Using Boostrap class to boot the common.loader and other catalina specific // class loaders. Bootstrap bootstrap = new Bootstrap(); bootstrap.init(); // Now we need to add the sqoop webapp classes into the class loader. Sadly // we have to do a lot of things ourselves. The procedure is: // 1) Unpack Sqoop war file // 2) Build the ClassLoader using Tomcat's ClassLoaderFactory // Various paths to war file and locations inside the war file String webappPath = System.getProperty(PROPERTY_APPBASE_PATH, DEFAULT_APPBASE_PATH); String catalinaBase = Bootstrap.getCatalinaBase(); String fullWebappPath = catalinaBase + File.separator + webappPath; String fullSqoopWarPath = fullWebappPath + File.separator + "sqoop.war"; String fullSqoopClassesPath = fullWebappPath + File.separator + "sqoop" + File.separator + "WEB-INF" + File.separator + "classes"; String fullSqoopLibPath = fullWebappPath + File.separator + "sqoop" + File.separator + "WEB-INF" + File.separator + "lib"; // Expand the war into the usual location, this operation is idempotent // (nothing bad happens if it's already expanded) Embedded embedded = new Embedded(); Host host = embedded.createHost("Sqoop Tool Virtual Host", fullWebappPath); ExpandWar .expand(host, new URL("jar:file://" + fullSqoopWarPath + "!/")); // We have expanded war file, so we build the classloader from File[] unpacked = new File[1]; unpacked[0] = new File(fullSqoopClassesPath); File[] packed = new File[1]; packed[0] = new File(fullSqoopLibPath); ClassLoader loader = ClassLoaderFactory.createClassLoader(unpacked, packed, Thread.currentThread().getContextClassLoader()); Thread.currentThread().setContextClassLoader(loader); // Finally we can call the usual ToolRunner. We have to use reflection // as // as the time of loading this class, Sqoop dependencies are not on // classpath. Class klass = Class.forName("org.apache.sqoop.tools.ToolRunner", true, loader); Method method = klass.getMethod("main", String[].class); method.invoke(null, (Object) args); }
public TomcatServer(String contextPath, int port, String appBase, boolean shutdownHook) { if(contextPath == null || appBase == null || appBase.length() == 0) { throw new IllegalArgumentException("Context path or appbase should not be null"); } if(!contextPath.startsWith("/")) { contextPath = "/" + contextPath; } this.port = port; tomcat = new Embedded(); tomcat.setName("TomcatEmbeddedtomcat"); Host localHost = tomcat.createHost("localhost", appBase); localHost.setAutoDeploy(false); StandardContext rootContext = (StandardContext) tomcat.createContext(contextPath, "webapp"); rootContext.setDefaultWebXml("web.xml"); localHost.addChild(rootContext); Engine engine = tomcat.createEngine(); engine.setDefaultHost(localHost.getName()); engine.setName("TomcatEngine"); engine.addChild(localHost); tomcat.addEngine(engine); Connector connector = tomcat.createConnector(localHost.getName(), port, false); tomcat.addConnector(connector); // register shutdown hook if(shutdownHook) { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { if(isRunning) { if(isInfo) LOG.info("Stopping the Tomcat tomcat, through shutdown hook"); try { if (tomcat != null) { tomcat.stop(); } } catch (LifecycleException e) { LOG.error("Error while stopping the Tomcat tomcat, through shutdown hook", e); } } } }); } }