user3256152
user3256152

Reputation: 1

jetty get webapp list

I need to get some server properties from server to my servlet in init method, before any request execute(in init method). Actually I neet get list on all working on this server connectors, all working webapps and, most important, - port numbers or connectors. Desired jetty version - up to 8 version including.

So I need something like org.eclipse.jetty.server.Server, but not for embdeded, but from existing server, on wich my servlet is running. This information shuld be on jetty as far as webapp deployer operating with this info. But I can't find where.

Upvotes: 0

Views: 1633

Answers (1)

laz
laz

Reputation: 28648

Enabling JMX in Jetty's start.ini would allow you to use code similar to the following:

final MBeanServer mBeanServerConnection = ManagementFactory.getPlatformMBeanServer();
final String[] portAttribute = new String[] {"port"};

// Jetty 9 MBeans
final ObjectName webappcontext9 = new ObjectName("org.eclipse.jetty.webapp:context=*,type=webappcontext,id=*");
final Set<ObjectName> webappcontexts9 = mBeanServerConnection.queryNames(webappcontext9, null);
for (final ObjectName objectName : webappcontexts9) {
    System.out.println(objectName.getKeyProperty("context"));
}

final ObjectName serverconnector9 = new ObjectName("org.eclipse.jetty.server:context=*,type=serverconnector,id=*");
final Set<ObjectName> serverconnectors9 = mBeanServerConnection.queryNames(serverconnector9, null);
for (final ObjectName objectName : serverconnectors9) {
    System.out.println("listening port for " + objectName.getCanonicalName() + " is " + mBeanServerConnection.getAttributes(objectName, portAttribute).asList().get(0).getValue());
}

// Jetty 8 and 7 MBeans
final ObjectName webappcontext8 = new ObjectName("org.eclipse.jetty.webapp:type=webappcontext,id=*,name=*");
final Set<ObjectName> webappcontexts8 = mBeanServerConnection.queryNames(webappcontext8, null);
for (final ObjectName objectName : webappcontexts8) {
    System.out.println(objectName.getKeyProperty("name"));
}

final ObjectName serverconnector8 = new ObjectName("org.eclipse.jetty.server.nio:type=selectchannelconnector,id=*");
final Set<ObjectName> serverconnectors8 = mBeanServerConnection.queryNames(serverconnector8, null);
for (final ObjectName objectName : serverconnectors8) {
    System.out.println("listening port for " + objectName.getCanonicalName() + " is " + mBeanServerConnection.getAttributes(objectName, portAttribute).asList().get(0).getValue());
}

Of course you'll need to configure Jetty to load the web application that contains this code last or it won't get the complete list of other web applications loaded.

Upvotes: 5

Related Questions