Search in sources :

Example 16 with MBeanContainer

use of org.eclipse.jetty.jmx.MBeanContainer in project jetty.project by eclipse.

the class OneWebApp method main.

public static void main(String[] args) throws Exception {
    // Create a basic jetty server object that will listen on port 8080.
    // Note that if you set this to port 0 then a randomly available port
    // will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);
    // The WebAppContext is the entity that controls the environment in
    // which a web application lives and breathes. In this example the
    // context path is being set to "/" so it is suitable for serving root
    // context requests and then we see it setting the location of the war.
    // A whole host of other configurations are available, ranging from
    // configuring to support annotation scanning in the webapp (through
    // PlusConfiguration) to choosing where the webapp will unpack itself.
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    File warFile = new File("../../tests/test-jmx/jmx-webapp/target/jmx-webapp");
    webapp.setWar(warFile.getAbsolutePath());
    // A WebAppContext is a ContextHandler as well so it needs to be set to
    // the server so it is aware of where to send the appropriate requests.
    server.setHandler(webapp);
    // Start things up! 
    server.start();
    // The use of server.join() the will make the current thread join and
    // wait until the server is done executing.
    // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
    server.join();
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) File(java.io.File)

Example 17 with MBeanContainer

use of org.eclipse.jetty.jmx.MBeanContainer in project jetty.project by eclipse.

the class TestServer method main.

public static void main(String[] args) throws Exception {
    ((StdErrLog) Log.getLog()).setSource(false);
    String jetty_root = "../../..";
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMaxThreads(100);
    // Setup server
    Server server = new Server(threadPool);
    server.manage(threadPool);
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);
    server.addBean(Log.getLog());
    // Common HTTP configuration
    HttpConfiguration config = new HttpConfiguration();
    config.setSecurePort(8443);
    config.addCustomizer(new ForwardedRequestCustomizer());
    config.addCustomizer(new SecureRequestCustomizer());
    config.setSendDateHeader(true);
    config.setSendServerVersion(true);
    // Http Connector
    HttpConnectionFactory http = new HttpConnectionFactory(config);
    ServerConnector httpConnector = new ServerConnector(server, http);
    httpConnector.setPort(8080);
    httpConnector.setIdleTimeout(30000);
    server.addConnector(httpConnector);
    // Handlers
    HandlerCollection handlers = new HandlerCollection();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    handlers.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
    // Add restart handler to test the ability to save sessions and restart
    RestartHandler restart = new RestartHandler();
    restart.setHandler(handlers);
    server.setHandler(restart);
    // Setup context
    HashLoginService login = new HashLoginService();
    login.setName("Test Realm");
    login.setConfig(jetty_root + "/tests/test-webapps/test-jetty-webapp/src/main/config/demo-base/etc/realm.properties");
    server.addBean(login);
    File log = File.createTempFile("jetty-yyyy_mm_dd", "log");
    NCSARequestLog requestLog = new NCSARequestLog(log.toString());
    requestLog.setExtended(false);
    requestLogHandler.setRequestLog(requestLog);
    server.setStopAtShutdown(true);
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/test");
    webapp.setParentLoaderPriority(true);
    webapp.setResourceBase("./src/main/webapp");
    webapp.setAttribute("testAttribute", "testValue");
    File sessiondir = File.createTempFile("sessions", null);
    if (sessiondir.exists())
        sessiondir.delete();
    sessiondir.mkdir();
    sessiondir.deleteOnExit();
    DefaultSessionCache ss = new DefaultSessionCache(webapp.getSessionHandler());
    FileSessionDataStore sds = new FileSessionDataStore();
    ss.setSessionDataStore(sds);
    sds.setStoreDir(sessiondir);
    webapp.getSessionHandler().setSessionCache(ss);
    contexts.addHandler(webapp);
    ContextHandler srcroot = new ContextHandler();
    srcroot.setResourceBase(".");
    srcroot.setHandler(new ResourceHandler());
    srcroot.setContextPath("/src");
    contexts.addHandler(srcroot);
    server.start();
    server.join();
}
Also used : DefaultSessionCache(org.eclipse.jetty.server.session.DefaultSessionCache) StdErrLog(org.eclipse.jetty.util.log.StdErrLog) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) HashLoginService(org.eclipse.jetty.security.HashLoginService) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) NCSARequestLog(org.eclipse.jetty.server.NCSARequestLog) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) FileSessionDataStore(org.eclipse.jetty.server.session.FileSessionDataStore) File(java.io.File)

Example 18 with MBeanContainer

use of org.eclipse.jetty.jmx.MBeanContainer in project jetty.project by eclipse.

the class TestTransparentProxyServer method main.

public static void main(String[] args) throws Exception {
    ((StdErrLog) Log.getLog()).setSource(false);
    String jetty_root = "../../..";
    // Setup Threadpool
    QueuedThreadPool threadPool = new QueuedThreadPool();
    threadPool.setMaxThreads(100);
    // Setup server
    Server server = new Server(threadPool);
    server.manage(threadPool);
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);
    server.addBean(Log.getLog());
    // Common HTTP configuration
    HttpConfiguration config = new HttpConfiguration();
    config.setSecurePort(8443);
    config.addCustomizer(new ForwardedRequestCustomizer());
    config.setSendDateHeader(true);
    config.setSendServerVersion(true);
    // Http Connector
    HttpConnectionFactory http = new HttpConnectionFactory(config);
    ServerConnector httpConnector = new ServerConnector(server, http);
    httpConnector.setPort(8080);
    httpConnector.setIdleTimeout(30000);
    server.addConnector(httpConnector);
    // SSL configurations
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath(jetty_root + "/jetty-server/src/main/config/etc/keystore");
    sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    sslContextFactory.setTrustStorePath(jetty_root + "/jetty-server/src/main/config/etc/keystore");
    sslContextFactory.setTrustStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    sslContextFactory.setExcludeCipherSuites("SSL_RSA_WITH_DES_CBC_SHA", "SSL_DHE_RSA_WITH_DES_CBC_SHA", "SSL_DHE_DSS_WITH_DES_CBC_SHA", "SSL_RSA_EXPORT_WITH_RC4_40_MD5", "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA", "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA");
    sslContextFactory.setCipherComparator(new HTTP2Cipher.CipherComparator());
    // HTTPS Configuration
    HttpConfiguration https_config = new HttpConfiguration(config);
    https_config.addCustomizer(new SecureRequestCustomizer());
    // HTTP2 factory
    HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(https_config);
    ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
    alpn.setDefaultProtocol(h2.getProtocol());
    // SSL Factory
    SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());
    // HTTP2 Connector
    ServerConnector http2Connector = new ServerConnector(server, ssl, alpn, h2, new HttpConnectionFactory(https_config));
    http2Connector.setPort(8443);
    http2Connector.setIdleTimeout(15000);
    server.addConnector(http2Connector);
    // Handlers
    HandlerCollection handlers = new HandlerCollection();
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    handlers.setHandlers(new Handler[] { contexts, new DefaultHandler() });
    server.setHandler(handlers);
    // Setup proxy webapp
    WebAppContext webapp = new WebAppContext();
    webapp.setResourceBase("src/main/webapp");
    contexts.addHandler(webapp);
    // start server
    server.setStopAtShutdown(true);
    server.start();
    server.join();
}
Also used : StdErrLog(org.eclipse.jetty.util.log.StdErrLog) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ALPNServerConnectionFactory(org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) ForwardedRequestCustomizer(org.eclipse.jetty.server.ForwardedRequestCustomizer) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HTTP2Cipher(org.eclipse.jetty.http2.HTTP2Cipher) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection)

Example 19 with MBeanContainer

use of org.eclipse.jetty.jmx.MBeanContainer in project jetty.project by eclipse.

the class DoSFilterJMXTest method testDoSFilterJMX.

@Test
public void testDoSFilterJMX() throws Exception {
    Server server = new Server();
    Connector connector = new ServerConnector(server);
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    DoSFilter filter = new DoSFilter();
    FilterHolder holder = new FilterHolder(filter);
    String name = "dos";
    holder.setName(name);
    holder.setInitParameter(DoSFilter.MANAGED_ATTR_INIT_PARAM, "true");
    context.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
    context.setInitParameter(ServletContextHandler.MANAGED_ATTRIBUTES, name);
    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    MBeanContainer mbeanContainer = new MBeanContainer(mbeanServer);
    server.addBean(mbeanContainer);
    server.start();
    String domain = DoSFilter.class.getPackage().getName();
    Set<ObjectName> mbeanNames = mbeanServer.queryNames(ObjectName.getInstance(domain + ":*"), null);
    Assert.assertEquals(1, mbeanNames.size());
    ObjectName objectName = mbeanNames.iterator().next();
    boolean value = (Boolean) mbeanServer.getAttribute(objectName, "enabled");
    mbeanServer.setAttribute(objectName, new Attribute("enabled", !value));
    Assert.assertEquals(!value, filter.isEnabled());
    String whitelist = (String) mbeanServer.getAttribute(objectName, "whitelist");
    String address = "127.0.0.1";
    Assert.assertFalse(whitelist.contains(address));
    boolean result = (Boolean) mbeanServer.invoke(objectName, "addWhitelistAddress", new Object[] { address }, new String[] { String.class.getName() });
    Assert.assertTrue(result);
    whitelist = (String) mbeanServer.getAttribute(objectName, "whitelist");
    Assert.assertTrue(whitelist.contains(address));
    result = (Boolean) mbeanServer.invoke(objectName, "removeWhitelistAddress", new Object[] { address }, new String[] { String.class.getName() });
    Assert.assertTrue(result);
    whitelist = (String) mbeanServer.getAttribute(objectName, "whitelist");
    Assert.assertFalse(whitelist.contains(address));
    server.stop();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MBeanServer(javax.management.MBeanServer) Server(org.eclipse.jetty.server.Server) Attribute(javax.management.Attribute) ObjectName(javax.management.ObjectName) ServerConnector(org.eclipse.jetty.server.ServerConnector) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) MBeanServer(javax.management.MBeanServer) Test(org.junit.Test)

Example 20 with MBeanContainer

use of org.eclipse.jetty.jmx.MBeanContainer in project jetty.project by eclipse.

the class DeploymentManagerLifeCyclePathTest method testStateTransition_DeployedToUndeployed.

@Test
public void testStateTransition_DeployedToUndeployed() throws Exception {
    DeploymentManager depman = new DeploymentManager();
    // no default
    depman.setDefaultLifeCycleGoal(null);
    AppLifeCyclePathCollector pathtracker = new AppLifeCyclePathCollector();
    MockAppProvider mockProvider = new MockAppProvider();
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    depman.addBean(mbContainer);
    depman.addLifeCycleBinding(pathtracker);
    depman.addAppProvider(mockProvider);
    depman.setContexts(new ContextHandlerCollection());
    // Start DepMan
    depman.start();
    // Trigger new App
    mockProvider.findWebapp("foo-webapp-1.war");
    App app = depman.getAppByOriginId("mock-foo-webapp-1.war");
    // Request Deploy of App
    depman.requestAppGoal(app, "deployed");
    JmxServiceConnection jmxConnection = new JmxServiceConnection();
    jmxConnection.connect();
    MBeanServerConnection mbsConnection = jmxConnection.getConnection();
    ObjectName dmObjName = new ObjectName("org.eclipse.jetty.deploy:type=deploymentmanager,id=0");
    String[] params = new String[] { "mock-foo-webapp-1.war", "undeployed" };
    String[] signature = new String[] { "java.lang.String", "java.lang.String" };
    mbsConnection.invoke(dmObjName, "requestAppGoal", params, signature);
    // Setup Expectations.
    List<String> expected = new ArrayList<String>();
    // SHOULD NOT SEE THIS NODE VISITED - expected.add("undeployed");
    expected.add("deploying");
    expected.add("deployed");
    expected.add("undeploying");
    expected.add("undeployed");
    pathtracker.assertExpected("Test JMX StateTransition / Deployed -> Undeployed", expected);
}
Also used : ArrayList(java.util.ArrayList) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ObjectName(javax.management.ObjectName) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Aggregations

MBeanContainer (org.eclipse.jetty.jmx.MBeanContainer)24 Server (org.eclipse.jetty.server.Server)15 ServerConnector (org.eclipse.jetty.server.ServerConnector)8 File (java.io.File)7 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)7 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)6 MBeanServer (javax.management.MBeanServer)5 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)5 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)5 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)5 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)5 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)4 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)4 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)4 JMXServiceURL (javax.management.remote.JMXServiceURL)3 HashLoginService (org.eclipse.jetty.security.HashLoginService)3 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)3 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)3 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)3