001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.management;
006:
007: import com.tc.config.schema.setup.L2TVSConfigurationSetupManager;
008: import com.tc.exception.TCRuntimeException;
009: import com.tc.logging.CustomerLogging;
010: import com.tc.logging.TCLogging;
011: import com.tc.management.beans.L2Dumper;
012: import com.tc.management.beans.L2MBeanNames;
013: import com.tc.management.beans.LockStatisticsMonitorMBean;
014: import com.tc.management.beans.TCDumper;
015: import com.tc.management.beans.TCServerInfoMBean;
016: import com.tc.management.beans.object.ObjectManagementMonitor;
017: import com.tc.properties.TCPropertiesImpl;
018: import com.tc.util.PortChooser;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.rmi.RemoteException;
023: import java.rmi.registry.LocateRegistry;
024: import java.rmi.registry.Registry;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.management.InstanceAlreadyExistsException;
030: import javax.management.InstanceNotFoundException;
031: import javax.management.MBeanRegistrationException;
032: import javax.management.MBeanServer;
033: import javax.management.MBeanServerFactory;
034: import javax.management.NotCompliantMBeanException;
035: import javax.management.ObjectName;
036: import javax.management.remote.JMXConnectorServer;
037: import javax.management.remote.JMXConnectorServerFactory;
038: import javax.management.remote.JMXServiceURL;
039: import javax.management.remote.rmi.RMIConnectorServer;
040: import javax.management.remote.rmi.RMIJRMPServerImpl;
041:
042: public class L2Management extends TerracottaManagement {
043:
044: private MBeanServer mBeanServer;
045: private JMXConnectorServer jmxConnectorServer;
046: private final L2TVSConfigurationSetupManager configurationSetupManager;
047: private final TCServerInfoMBean tcServerInfo;
048: private final TCDumper tcDumper;
049: private final ObjectManagementMonitor objectManagementBean;
050: private final LockStatisticsMonitorMBean lockStatistics;
051: private static final Map rmiRegistryMap = new HashMap();
052:
053: public L2Management(TCServerInfoMBean tcServerInfo,
054: LockStatisticsMonitorMBean lockStatistics,
055: L2TVSConfigurationSetupManager configurationSetupManager,
056: TCDumper tcDumper) throws MBeanRegistrationException,
057: NotCompliantMBeanException, InstanceAlreadyExistsException {
058: this .tcServerInfo = tcServerInfo;
059: this .lockStatistics = lockStatistics;
060: this .configurationSetupManager = configurationSetupManager;
061: this .tcDumper = tcDumper;
062:
063: try {
064: objectManagementBean = new ObjectManagementMonitor();
065: } catch (NotCompliantMBeanException ncmbe) {
066: throw new TCRuntimeException(
067: "Unable to construct one of the L2 MBeans: this is a programming error in one of those beans",
068: ncmbe);
069: }
070:
071: // LKC-2990 and LKC-3171: Remove the JMX generic optional logging
072: java.util.logging.Logger jmxLogger = java.util.logging.Logger
073: .getLogger("javax.management.remote.generic");
074: jmxLogger.setLevel(java.util.logging.Level.OFF);
075:
076: final List jmxServers = MBeanServerFactory
077: .findMBeanServer(null);
078: if (jmxServers != null && !jmxServers.isEmpty()) {
079: mBeanServer = (MBeanServer) jmxServers.get(0);
080: } else {
081: mBeanServer = MBeanServerFactory.createMBeanServer();
082: }
083: registerMBeans();
084: }
085:
086: /**
087: * Keep track of RMI Registries by jmxPort. In 1.5 and forward you can create multiple RMI Registries in a single VM.
088: */
089: private static Registry getRMIRegistry(int jmxPort)
090: throws RemoteException {
091: Integer key = new Integer(jmxPort);
092: Registry registry = (Registry) rmiRegistryMap.get(key);
093: if (registry == null) {
094: rmiRegistryMap.put(key, registry = LocateRegistry
095: .createRegistry(jmxPort));
096: }
097: return registry;
098: }
099:
100: public synchronized void start() throws Exception {
101: int jmxPort = configurationSetupManager.commonl2Config()
102: .jmxPort().getInt();
103: if (jmxPort == 0) {
104: jmxPort = new PortChooser().chooseRandomPort();
105: }
106: JMXServiceURL url;
107: Map env = new HashMap();
108: String authMsg = "Authentication OFF";
109: String credentialsMsg = "";
110: env.put("jmx.remote.x.server.connection.timeout", new Long(
111: Long.MAX_VALUE));
112: if (configurationSetupManager.commonl2Config().authentication()) {
113: String pwd = configurationSetupManager.commonl2Config()
114: .authenticationPasswordFile();
115: String access = configurationSetupManager.commonl2Config()
116: .authenticationAccessFile();
117: if (!new File(pwd).exists())
118: CustomerLogging.getConsoleLogger().error(
119: "Password file does not exist: " + pwd);
120: if (!new File(access).exists())
121: CustomerLogging.getConsoleLogger().error(
122: "Access file does not exist: " + access);
123: env.put("jmx.remote.x.password.file", pwd);
124: env.put("jmx.remote.x.access.file", access);
125: authMsg = "Authentication ON";
126: credentialsMsg = "Credentials: "
127: + configurationSetupManager.commonl2Config()
128: .authenticationPasswordFile()
129: + " "
130: + configurationSetupManager.commonl2Config()
131: .authenticationAccessFile();
132: url = new JMXServiceURL("service:jmx:rmi://");
133: RMIJRMPServerImpl server = new RMIJRMPServerImpl(jmxPort,
134: null, null, env);
135: jmxConnectorServer = new RMIConnectorServer(url, env,
136: server, mBeanServer);
137: jmxConnectorServer.start();
138: getRMIRegistry(jmxPort).bind("jmxrmi", server);
139: CustomerLogging
140: .getConsoleLogger()
141: .info(
142: "JMX Server started. "
143: + authMsg
144: + " - Available at URL["
145: + "service:jmx:rmi:///jndi/rmi://localhost:"
146: + jmxPort + "/jmxrmi" + "]");
147: if (!credentialsMsg.equals(""))
148: CustomerLogging.getConsoleLogger().info(credentialsMsg);
149: } else {
150: url = new JMXServiceURL("jmxmp", "localhost", jmxPort);
151: jmxConnectorServer = JMXConnectorServerFactory
152: .newJMXConnectorServer(url, env, mBeanServer);
153: jmxConnectorServer.start();
154: CustomerLogging.getConsoleLogger()
155: .info(
156: "JMX Server started. Available at URL["
157: + url + "]");
158: }
159: }
160:
161: public synchronized void stop() throws IOException,
162: InstanceNotFoundException, MBeanRegistrationException {
163: unregisterMBeans();
164: if (jmxConnectorServer != null) {
165: jmxConnectorServer.stop();
166: }
167: }
168:
169: public Object findMBean(ObjectName objectName, Class mBeanInterface)
170: throws IOException {
171: return findMBean(objectName, mBeanInterface, mBeanServer);
172: }
173:
174: public MBeanServer getMBeanServer() {
175: return mBeanServer;
176: }
177:
178: public ObjectManagementMonitor findObjectManagementMonitorMBean() {
179: return objectManagementBean;
180: }
181:
182: private void registerMBeans() throws MBeanRegistrationException,
183: NotCompliantMBeanException, InstanceAlreadyExistsException {
184: mBeanServer.registerMBean(tcServerInfo,
185: L2MBeanNames.TC_SERVER_INFO);
186: mBeanServer.registerMBean(
187: TCLogging.getJMXAppender().getMBean(),
188: L2MBeanNames.LOGGER);
189: mBeanServer.registerMBean(objectManagementBean,
190: L2MBeanNames.OBJECT_MANAGEMENT);
191: mBeanServer.registerMBean(lockStatistics,
192: L2MBeanNames.LOCK_STATISTICS);
193:
194: if (TCPropertiesImpl.getProperties().getBoolean(
195: "tc.management.test.mbeans.enabled")) {
196: mBeanServer.registerMBean(new L2Dumper(tcDumper),
197: L2MBeanNames.DUMPER);
198: }
199: }
200:
201: private void unregisterMBeans() throws InstanceNotFoundException,
202: MBeanRegistrationException {
203: mBeanServer.unregisterMBean(L2MBeanNames.TC_SERVER_INFO);
204: mBeanServer.unregisterMBean(L2MBeanNames.LOGGER);
205: mBeanServer.unregisterMBean(L2MBeanNames.OBJECT_MANAGEMENT);
206: mBeanServer.unregisterMBean(L2MBeanNames.LOCK_STATISTICS);
207:
208: if (TCPropertiesImpl.getProperties().getBoolean(
209: "tc.management.test.mbeans.enabled")) {
210: mBeanServer.unregisterMBean(L2MBeanNames.DUMPER);
211: }
212: }
213: }
|