1 5 package com.tc.management; 6 7 import com.tc.config.schema.setup.L2TVSConfigurationSetupManager; 8 import com.tc.exception.TCRuntimeException; 9 import com.tc.logging.CustomerLogging; 10 import com.tc.logging.TCLogging; 11 import com.tc.management.beans.L2MBeanNames; 12 import com.tc.management.beans.TCServerInfoMBean; 13 import com.tc.management.beans.object.ObjectManagementMonitor; 14 import com.tc.management.beans.object.ObjectManagementMonitorMBean; 15 import com.tc.util.PortChooser; 16 17 import java.io.File ; 18 import java.io.IOException ; 19 import java.rmi.RemoteException ; 20 import java.rmi.registry.LocateRegistry ; 21 import java.rmi.registry.Registry ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 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.MBeanServerFactory ; 31 import javax.management.NotCompliantMBeanException ; 32 import javax.management.ObjectName ; 33 import javax.management.remote.JMXConnectorServer ; 34 import javax.management.remote.JMXServiceURL ; 35 import javax.management.remote.rmi.RMIConnectorServer ; 36 import javax.management.remote.rmi.RMIJRMPServerImpl ; 37 38 public class L2Management extends TerracottaManagement { 39 40 private MBeanServer mBeanServer; 41 private JMXConnectorServer jmxConnectorServer; 42 private final L2TVSConfigurationSetupManager configurationSetupManager; 43 private final TCServerInfoMBean tcServerInfo; 44 private final ObjectManagementMonitor objectManagementBean; 45 private static final Map rmiRegistryMap = new HashMap (); 46 47 public L2Management(TCServerInfoMBean tcServerInfo, L2TVSConfigurationSetupManager configurationSetupManager) 48 throws MBeanRegistrationException , NotCompliantMBeanException , InstanceAlreadyExistsException { 49 this.tcServerInfo = tcServerInfo; 50 this.configurationSetupManager = configurationSetupManager; 51 52 try { 53 objectManagementBean = new ObjectManagementMonitor(); 54 } catch (NotCompliantMBeanException ncmbe) { 55 throw new TCRuntimeException( 56 "Unable to construct one of the L2 MBeans: this is a programming error in one of those beans", 57 ncmbe); 58 } 59 60 java.util.logging.Logger jmxLogger = java.util.logging.Logger.getLogger("javax.management.remote.generic"); 62 jmxLogger.setLevel(java.util.logging.Level.OFF); 63 64 final List jmxServers = MBeanServerFactory.findMBeanServer(null); 65 if (jmxServers != null && !jmxServers.isEmpty()) { 66 mBeanServer = (MBeanServer ) jmxServers.get(0); 67 } else { 68 mBeanServer = MBeanServerFactory.createMBeanServer(); 69 } 70 registerMBeans(); 71 } 72 73 77 private static Registry getRMIRegistry(int jmxPort) throws RemoteException { 78 Integer key = new Integer (jmxPort); 79 Registry registry = (Registry ) rmiRegistryMap.get(key); 80 if (registry == null) { 81 rmiRegistryMap.put(key, registry = LocateRegistry.createRegistry(jmxPort)); 82 } 83 return registry; 84 } 85 86 public synchronized void start() throws Exception { 87 int jmxPort = configurationSetupManager.commonl2Config().jmxPort().getInt(); 88 if (jmxPort == 0) { 89 jmxPort = new PortChooser().chooseRandomPort(); 90 } 91 JMXServiceURL url; 92 Map env = new HashMap (); 93 String authMsg = "Authentication OFF"; 94 String credentialsMsg = ""; 95 if (configurationSetupManager.commonl2Config().authentication()) { 96 String pwd = configurationSetupManager.commonl2Config().authenticationPasswordFile(); 97 String access = configurationSetupManager.commonl2Config().authenticationAccessFile(); 98 if (!new File (pwd).exists()) CustomerLogging.getConsoleLogger().error("Password file does not exist: " + pwd); 99 if (!new File (access).exists()) CustomerLogging.getConsoleLogger().error("Access file does not exist: " + access); 100 env.put("jmx.remote.x.password.file", pwd); 101 env.put("jmx.remote.x.access.file", access); 102 authMsg = "Authentication ON"; 103 credentialsMsg = "Credentials: " + configurationSetupManager.commonl2Config().authenticationPasswordFile() + " " 104 + configurationSetupManager.commonl2Config().authenticationAccessFile(); 105 } 106 env.put("jmx.remote.x.server.connection.timeout", new Long (Long.MAX_VALUE)); 107 url = new JMXServiceURL ("service:jmx:rmi://"); 108 RMIJRMPServerImpl server = new RMIJRMPServerImpl (jmxPort, null, null, env); 109 jmxConnectorServer = new RMIConnectorServer (url, env, server, mBeanServer); 110 jmxConnectorServer.start(); 111 getRMIRegistry(jmxPort).bind("jmxrmi", server); 112 CustomerLogging.getConsoleLogger().info( 113 "JMX Server started. " + authMsg + " - Available at URL[" 114 + "service:jmx:rmi:///jndi/rmi://localhost:" + jmxPort + "/jmxrmi" 115 + "]"); 116 if (!credentialsMsg.equals("")) CustomerLogging.getConsoleLogger().info(credentialsMsg); 117 } 118 119 public synchronized void stop() throws IOException , InstanceNotFoundException , MBeanRegistrationException { 120 unregisterMBeans(); 121 if (jmxConnectorServer != null) { 122 jmxConnectorServer.stop(); 123 } 124 } 125 126 public Object findMBean(ObjectName objectName, Class mBeanInterface) throws IOException { 127 return findMBean(objectName, mBeanInterface, mBeanServer); 128 } 129 130 public MBeanServer getMBeanServer() { 131 return mBeanServer; 132 } 133 134 public ObjectManagementMonitorMBean findObjectManagementMonitorMBean() { 135 return objectManagementBean; 136 } 137 138 private void registerMBeans() throws MBeanRegistrationException , NotCompliantMBeanException , 139 InstanceAlreadyExistsException { 140 mBeanServer.registerMBean(tcServerInfo, L2MBeanNames.TC_SERVER_INFO); 141 mBeanServer.registerMBean(TCLogging.getJMXAppender().getMBean(), L2MBeanNames.LOGGER); 142 mBeanServer.registerMBean(objectManagementBean, L2MBeanNames.OBJECT_MANAGEMENT); 143 } 144 145 private void unregisterMBeans() throws InstanceNotFoundException , MBeanRegistrationException { 146 mBeanServer.unregisterMBean(L2MBeanNames.TC_SERVER_INFO); 147 mBeanServer.unregisterMBean(L2MBeanNames.LOGGER); 148 mBeanServer.unregisterMBean(L2MBeanNames.OBJECT_MANAGEMENT); 149 } 150 } 151 | Popular Tags |