| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package uk.co.gidley.jmxmonitor.services; |
| 18 |
|
|
| 19 |
|
import org.apache.commons.configuration.Configuration; |
| 20 |
|
import org.apache.tapestry5.ioc.annotations.EagerLoad; |
| 21 |
|
import org.apache.tapestry5.ioc.services.RegistryShutdownHub; |
| 22 |
|
import org.apache.tapestry5.ioc.services.RegistryShutdownListener; |
| 23 |
|
import org.slf4j.Logger; |
| 24 |
|
import org.slf4j.LoggerFactory; |
| 25 |
|
|
| 26 |
|
import javax.management.InstanceAlreadyExistsException; |
| 27 |
|
import javax.management.InstanceNotFoundException; |
| 28 |
|
import javax.management.MBeanRegistrationException; |
| 29 |
|
import javax.management.MBeanServer; |
| 30 |
|
import javax.management.MalformedObjectNameException; |
| 31 |
|
import javax.management.NotCompliantMBeanException; |
| 32 |
|
import javax.management.ObjectName; |
| 33 |
|
import javax.management.remote.JMXConnectorServer; |
| 34 |
|
import javax.management.remote.JMXConnectorServerFactory; |
| 35 |
|
import javax.management.remote.JMXServiceURL; |
| 36 |
|
import java.io.IOException; |
| 37 |
|
import java.lang.management.ManagementFactory; |
| 38 |
|
import java.rmi.NoSuchObjectException; |
| 39 |
|
import java.rmi.RemoteException; |
| 40 |
|
import java.rmi.registry.LocateRegistry; |
| 41 |
|
import java.rmi.registry.Registry; |
| 42 |
|
import java.rmi.server.UnicastRemoteObject; |
| 43 |
|
import java.util.Iterator; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
@EagerLoad |
|
|
|
| 78.7% |
Uncovered Elements: 13 (61) |
Complexity: 19 |
Complexity Density: 0.4 |
|
| 49 |
|
public class InternalJmx implements RegistryShutdownListener { |
| 50 |
|
private static final Logger logger = LoggerFactory.getLogger(InternalJmx.class); |
| 51 |
|
private final String PROPERTY_PREFIX; |
| 52 |
|
private JMXConnectorServer jmxConnectorServer; |
| 53 |
|
private Registry registry; |
| 54 |
|
|
| 55 |
|
public final ObjectName connectorServerName; |
| 56 |
|
|
| 57 |
|
private final MBeanServer MBEAN_SERVER; |
| 58 |
|
|
| 59 |
|
private static final String LOCAL_RMI_PORT = "localRmiPort"; |
| 60 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 61 |
6
|
public InternalJmx(MainConfiguration configuration,... |
| 62 |
|
RegistryShutdownHub registryShutdownHub) throws InitialisationException, MalformedObjectNameException { |
| 63 |
|
|
| 64 |
6
|
this.PROPERTY_PREFIX = ThreadManager.PROPERTY_PREFIX; |
| 65 |
6
|
registryShutdownHub.addRegistryShutdownListener(this); |
| 66 |
6
|
connectorServerName = ObjectName |
| 67 |
|
.getInstance("connectors:protocol=rmi"); |
| 68 |
6
|
MBEAN_SERVER = ManagementFactory.getPlatformMBeanServer(); |
| 69 |
|
|
| 70 |
6
|
start(configuration.getConfiguration()); |
| 71 |
|
} |
| 72 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 73 |
6
|
public void start(Configuration configuration) throws InitialisationException {... |
| 74 |
|
|
| 75 |
6
|
Iterator<String> keys = configuration.getKeys(); |
| 76 |
44
|
while (keys.hasNext()) { |
| 77 |
38
|
String key = keys.next(); |
| 78 |
38
|
if (key.equals(PROPERTY_PREFIX + LOCAL_RMI_PORT)) { |
| 79 |
4
|
startRmiRegistry(configuration); |
| 80 |
4
|
startJmxConnector(configuration); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
} |
| 85 |
|
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 86 |
4
|
private void startRmiRegistry(Configuration configuration) throws InitialisationException {... |
| 87 |
4
|
try { |
| 88 |
4
|
int port = configuration.getInt(PROPERTY_PREFIX + LOCAL_RMI_PORT); |
| 89 |
4
|
logger.debug("Creating RMI Registry on {}", port); |
| 90 |
4
|
registry = LocateRegistry.createRegistry(port); |
| 91 |
|
} catch (RemoteException e) { |
| 92 |
0
|
logger.error("{}", e); |
| 93 |
0
|
throw new InitialisationException(e); |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
|
|
|
|
|
| 46.7% |
Uncovered Elements: 8 (15) |
Complexity: 5 |
Complexity Density: 0.33 |
|
| 97 |
4
|
private void startJmxConnector(Configuration configuration) throws InitialisationException {... |
| 98 |
4
|
try { |
| 99 |
4
|
String url = configuration.getString(PROPERTY_PREFIX + "localJmx"); |
| 100 |
4
|
logger.debug("Initialising local JMX server on URL {}", url); |
| 101 |
4
|
JMXServiceURL address = new JMXServiceURL(url); |
| 102 |
4
|
jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer( |
| 103 |
|
address, null, |
| 104 |
|
MBEAN_SERVER); |
| 105 |
|
|
| 106 |
4
|
MBEAN_SERVER.registerMBean(jmxConnectorServer, connectorServerName); |
| 107 |
4
|
jmxConnectorServer.start(); |
| 108 |
|
} catch (IOException e) { |
| 109 |
0
|
logger.error("{}", e); |
| 110 |
0
|
throw new InitialisationException(e); |
| 111 |
|
} catch (InstanceAlreadyExistsException e) { |
| 112 |
0
|
logger.error("{}", e); |
| 113 |
0
|
throw new InitialisationException(e); |
| 114 |
|
} catch (MBeanRegistrationException e) { |
| 115 |
0
|
logger.error("{}", e); |
| 116 |
0
|
throw new InitialisationException(e); |
| 117 |
|
} catch (NotCompliantMBeanException e) { |
| 118 |
0
|
logger.error("{}", e); |
| 119 |
0
|
throw new InitialisationException(e); |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
|
|
|
|
| 83.3% |
Uncovered Elements: 3 (18) |
Complexity: 7 |
Complexity Density: 0.5 |
|
| 123 |
6
|
@Override... |
| 124 |
|
public void registryDidShutdown() { |
| 125 |
6
|
if (jmxConnectorServer != null) { |
| 126 |
4
|
logger.debug("Shutting down JMX"); |
| 127 |
4
|
try { |
| 128 |
4
|
jmxConnectorServer.stop(); |
| 129 |
|
} catch (IOException e) { |
| 130 |
0
|
logger.error("{}", e); |
| 131 |
|
} |
| 132 |
|
} |
| 133 |
6
|
if (registry != null) { |
| 134 |
4
|
logger.debug("Shutting down RMI"); |
| 135 |
4
|
try { |
| 136 |
4
|
UnicastRemoteObject.unexportObject(this.registry, true); |
| 137 |
|
} catch (NoSuchObjectException e) { |
| 138 |
0
|
logger.error("{}", e); |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
|
| 142 |
6
|
try { |
| 143 |
6
|
MBEAN_SERVER.unregisterMBean(connectorServerName); |
| 144 |
|
} catch (InstanceNotFoundException e) { |
| 145 |
2
|
logger.warn("{}", e); |
| 146 |
|
} catch (MBeanRegistrationException e) { |
| 147 |
0
|
logger.warn("{}", e); |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
} |
| 151 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 152 |
3
|
public ObjectName getConnectorServerName() {... |
| 153 |
3
|
return connectorServerName; |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
} |