public static void main(String[] args) throws Exception { // Create the Server object and a corresponding ServerConnector and then set the port for the // connector. In // this example the server will listen on port 8090. If you set this to port 0 then when the // server has been // started you can called connector.getLocalPort() to programmatically get the port the server // started on. Server server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(8090); server.setConnectors(new Connector[] {connector}); // Create a Context Handler and ResourceHandler. The ContextHandler is getting set to "/" path // but this could // be anything you like for builing out your url. Note how we are setting the ResourceBase using // our jetty // maven testing utilities to get the proper resource directory, you needn't use these, // you simply need to supply the paths you are looking to serve content from. ContextHandler context0 = new ContextHandler(); context0.setContextPath("/"); ResourceHandler rh0 = new ResourceHandler(); rh0.setBaseResource(Resource.newResource(MavenTestingUtils.getTestResourceDir("dir0"))); context0.setHandler(rh0); // Rinse and repeat the previous item, only specifying a different resource base. ContextHandler context1 = new ContextHandler(); context1.setContextPath("/"); ResourceHandler rh1 = new ResourceHandler(); rh1.setBaseResource(Resource.newResource(MavenTestingUtils.getTestResourceDir("dir1"))); context1.setHandler(rh1); // Create a ContextHandlerCollection and set the context handlers to it. This will let jetty // process urls // against the declared contexts in order to match up content. ContextHandlerCollection contexts = new ContextHandlerCollection(); contexts.setHandlers(new Handler[] {context0, context1}); server.setHandler(contexts); // Start things up! By using the server.join() the server thread will join with the current // thread. // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more // details. server.start(); System.err.println(server.dump()); server.join(); }
@Test public void testLoadAllModules() throws IOException { File homeDir = MavenTestingUtils.getTestResourceDir("usecases/home"); BaseHome basehome = new BaseHome(homeDir, homeDir); Modules modules = new Modules(); modules.registerAll(basehome); Assert.assertThat("Module count", modules.count(), is(28)); }
@Test public void testResolve_ServerHttp() throws IOException { File homeDir = MavenTestingUtils.getTestResourceDir("usecases/home"); BaseHome basehome = new BaseHome(homeDir, homeDir); // Register modules Modules modules = new Modules(); modules.registerAll(basehome); modules.buildGraph(); // Enable 2 modules modules.enable("server", TEST_SOURCE); modules.enable("http", TEST_SOURCE); // Collect active module list List<Module> active = modules.resolveEnabled(); // Assert names are correct, and in the right order List<String> expectedNames = new ArrayList<>(); expectedNames.add("base"); expectedNames.add("xml"); expectedNames.add("server"); expectedNames.add("http"); List<String> actualNames = new ArrayList<>(); for (Module actual : active) { actualNames.add(actual.getName()); } Assert.assertThat( "Resolved Names: " + actualNames, actualNames, contains(expectedNames.toArray())); // Assert Library List List<String> expectedLibs = new ArrayList<>(); expectedLibs.add("lib/jetty-util-${jetty.version}.jar"); expectedLibs.add("lib/jetty-io-${jetty.version}.jar"); expectedLibs.add("lib/jetty-xml-${jetty.version}.jar"); expectedLibs.add("lib/servlet-api-3.1.jar"); expectedLibs.add("lib/jetty-schemas-3.1.jar"); expectedLibs.add("lib/jetty-http-${jetty.version}.jar"); expectedLibs.add("lib/jetty-continuation-${jetty.version}.jar"); expectedLibs.add("lib/jetty-server-${jetty.version}.jar"); List<String> actualLibs = modules.normalizeLibs(active); Assert.assertThat("Resolved Libs: " + actualLibs, actualLibs, contains(expectedLibs.toArray())); // Assert XML List List<String> expectedXmls = new ArrayList<>(); expectedXmls.add("etc/jetty.xml"); expectedXmls.add("etc/jetty-http.xml"); List<String> actualXmls = modules.normalizeXmls(active); Assert.assertThat("Resolved XMLs: " + actualXmls, actualXmls, contains(expectedXmls.toArray())); }
/** @throws java.lang.Exception */ @Before public void setUp() throws Exception { File testJettyHome = MavenTestingUtils.getTestResourceDir("jetty.home"); System.setProperty("jetty.home", testJettyHome.getAbsolutePath()); }