Skip to content

Commit

Permalink
Implementing JMX for grid node
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Nov 17, 2017
1 parent 96b735c commit 047c0b0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Logger;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

/**
Expand Down Expand Up @@ -65,7 +66,11 @@ public boolean add(TestSession testSession) {

public boolean remove(TestSession o, SessionTerminationReason reason) {
updateReason(o, reason);
new JMXHelper().unregister(o.getObjectName());
try {
new JMXHelper().unregister(o.getObjectName());
} catch (MalformedObjectNameException e) {
e.printStackTrace();
}
return activeTestSessions.remove(o);
}

Expand Down
15 changes: 5 additions & 10 deletions java/server/src/org/openqa/grid/internal/TestSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,11 @@ public boolean isForwardingRequest() {
return forwardingRequest;
}

public ObjectName getObjectName() {
try {
return new ObjectName(
String.format("org.seleniumhq.qrid:type=TestSession,node=\"%s\",browser=\"%s\",id=%s",
getSlot().getRemoteURL(), getRequestedCapabilities().get("browserName"),
getInternalKey()));
} catch (MalformedObjectNameException e) {
e.printStackTrace();
return null;
}
public ObjectName getObjectName() throws MalformedObjectNameException {
return new ObjectName(
String.format("org.seleniumhq.qrid:type=TestSession,node=\"%s\",browser=\"%s\",id=%s",
getSlot().getRemoteURL(), getRequestedCapabilities().get("browserName"),
getInternalKey()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.SessionId;
import org.openqa.selenium.remote.server.handler.DeleteSession;
import org.openqa.selenium.remote.server.jmx.JMXHelper;
import org.openqa.selenium.remote.server.jmx.ManagedService;
import org.seleniumhq.jetty9.security.ConstraintMapping;
import org.seleniumhq.jetty9.security.ConstraintSecurityHandler;
import org.seleniumhq.jetty9.server.Connector;
Expand All @@ -51,6 +53,7 @@
/**
* Provides a server that can launch and manage selenium sessions.
*/
@ManagedService(objectName = "org.seleniumhq.server:type=SeleniumServer")
public class SeleniumServer implements GridNodeServer {

private final static Logger LOG = Logger.getLogger(SeleniumServer.class.getName());
Expand All @@ -73,6 +76,8 @@ public class SeleniumServer implements GridNodeServer {

public SeleniumServer(StandaloneConfiguration configuration) {
this.configuration = configuration;

new JMXHelper().register(this);
}

public int getRealPort() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
import org.openqa.selenium.remote.http.W3CHttpCommandCodec;
import org.openqa.selenium.remote.http.W3CHttpResponseCodec;
import org.openqa.selenium.remote.internal.ApacheHttpClient;
import org.openqa.selenium.remote.server.jmx.JMXHelper;
import org.openqa.selenium.remote.server.jmx.ManagedService;
import org.openqa.selenium.remote.server.jmx.ManagedAttribute;
import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
Expand All @@ -61,6 +64,10 @@
import java.util.Set;
import java.util.function.Function;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

@ManagedService
public class ServicedSession implements ActiveSession {

private final DriverService service;
Expand Down Expand Up @@ -94,6 +101,8 @@ public ServicedSession(
this.driver = new Augmenter().augment(new RemoteWebDriver(
executor,
new ImmutableCapabilities(getCapabilities())));

new JMXHelper().register(this);
}


Expand All @@ -103,26 +112,31 @@ public void execute(HttpRequest req, HttpResponse resp) throws IOException {
}

@Override
@ManagedAttribute
public SessionId getId() {
return id;
}

@Override
@ManagedAttribute
public Dialect getUpstreamDialect() {
return upstream;
}

@Override
@ManagedAttribute
public Dialect getDownstreamDialect() {
return downstream;
}

@Override
@ManagedAttribute
public Map<String, Object> getCapabilities() {
return capabilities;
}

@Override
@ManagedAttribute
public WebDriver getWrappedDriver() {
return driver;
}
Expand Down Expand Up @@ -279,4 +293,9 @@ private ResponseCodec<HttpResponse> getResponseCodec(Dialect dialect) {
}
}
}

public ObjectName getObjectName() throws MalformedObjectNameException {
return new ObjectName(String.format("org.seleniumhq.server:type=Session,browser=\"%s\",id=%s",
getCapabilities().get("browserName"), getId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ private ObjectName generateObjectName(Object bean) {
try {
return (ObjectName) bean.getClass().getMethod("getObjectName").invoke(bean);
} catch (IllegalAccessException|InvocationTargetException|NoSuchMethodException e) {
return new ObjectName(bean.getClass().getName());
return new ObjectName(String.format("%s:%s",
bean.getClass().getPackage().getName(),
bean.getClass().getSimpleName()));
}
} else {
return new ObjectName(mBean.objectName());
Expand All @@ -219,7 +221,7 @@ public Object getAttribute(String attribute)
return ((Map<?,?>) res).entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey, e -> e.getValue().toString()));
} else {
return res;
return res.toString();
}
} catch (IllegalAccessException|InvocationTargetException e) {
e.printStackTrace();
Expand Down

0 comments on commit 047c0b0

Please sign in to comment.