Clover Coverage Report - jmxmonitor 1.0.2
Coverage timestamp: Wed Feb 10 2010 07:36:51 GMT
../../../../../img/srcFileCovDistChart8.png 50% of files have more coverage
47   156   19   7.83
8   117   0.4   6
6     3.17  
1    
 
  InternalJmx       Line # 49 47 0% 19 13 78.7% 0.78688526
 
  (4)
 
1    /*
2    * Copyright 2009 Ben Gidley
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
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    * Holds the internal JMX configuration Created by IntelliJ IDEA. User: ben Date: Jan 5, 2010 Time: 10:08:37 AM
47    */
48    @EagerLoad
 
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   
 
61  6 toggle public InternalJmx(MainConfiguration configuration,
62    RegistryShutdownHub registryShutdownHub) throws InitialisationException, MalformedObjectNameException {
63    //TODO make this a symbol
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   
 
73  6 toggle 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   
 
86  4 toggle 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   
 
97  4 toggle 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   
 
123  6 toggle @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   
 
152  3 toggle public ObjectName getConnectorServerName() {
153  3 return connectorServerName;
154    }
155   
156    }