KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > L2Management


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.rmi.RemoteException JavaDoc;
20 import java.rmi.registry.LocateRegistry JavaDoc;
21 import java.rmi.registry.Registry JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.management.InstanceAlreadyExistsException JavaDoc;
27 import javax.management.InstanceNotFoundException JavaDoc;
28 import javax.management.MBeanRegistrationException JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.MBeanServerFactory JavaDoc;
31 import javax.management.NotCompliantMBeanException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.remote.JMXConnectorServer JavaDoc;
34 import javax.management.remote.JMXServiceURL JavaDoc;
35 import javax.management.remote.rmi.RMIConnectorServer JavaDoc;
36 import javax.management.remote.rmi.RMIJRMPServerImpl JavaDoc;
37
38 public class L2Management extends TerracottaManagement {
39
40   private MBeanServer JavaDoc mBeanServer;
41   private JMXConnectorServer JavaDoc jmxConnectorServer;
42   private final L2TVSConfigurationSetupManager configurationSetupManager;
43   private final TCServerInfoMBean tcServerInfo;
44   private final ObjectManagementMonitor objectManagementBean;
45   private static final Map JavaDoc rmiRegistryMap = new HashMap JavaDoc();
46
47   public L2Management(TCServerInfoMBean tcServerInfo, L2TVSConfigurationSetupManager configurationSetupManager)
48       throws MBeanRegistrationException JavaDoc, NotCompliantMBeanException JavaDoc, InstanceAlreadyExistsException JavaDoc {
49     this.tcServerInfo = tcServerInfo;
50     this.configurationSetupManager = configurationSetupManager;
51
52     try {
53       objectManagementBean = new ObjectManagementMonitor();
54     } catch (NotCompliantMBeanException JavaDoc 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     // LKC-2990 and LKC-3171: Remove the JMX generic optional logging
61
java.util.logging.Logger JavaDoc jmxLogger = java.util.logging.Logger.getLogger("javax.management.remote.generic");
62     jmxLogger.setLevel(java.util.logging.Level.OFF);
63
64     final List JavaDoc jmxServers = MBeanServerFactory.findMBeanServer(null);
65     if (jmxServers != null && !jmxServers.isEmpty()) {
66       mBeanServer = (MBeanServer JavaDoc) jmxServers.get(0);
67     } else {
68       mBeanServer = MBeanServerFactory.createMBeanServer();
69     }
70     registerMBeans();
71   }
72
73   /**
74    * Keep track of RMI Registries by jmxPort. In 1.5 and forward you can create multiple RMI
75    * Registries in a single VM.
76    */

77   private static Registry JavaDoc getRMIRegistry(int jmxPort) throws RemoteException JavaDoc {
78     Integer JavaDoc key = new Integer JavaDoc(jmxPort);
79     Registry JavaDoc registry = (Registry JavaDoc) 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 JavaDoc {
87     int jmxPort = configurationSetupManager.commonl2Config().jmxPort().getInt();
88     if (jmxPort == 0) {
89       jmxPort = new PortChooser().chooseRandomPort();
90     }
91     JMXServiceURL JavaDoc url;
92     Map JavaDoc env = new HashMap JavaDoc();
93     String JavaDoc authMsg = "Authentication OFF";
94     String JavaDoc credentialsMsg = "";
95     if (configurationSetupManager.commonl2Config().authentication()) {
96       String JavaDoc pwd = configurationSetupManager.commonl2Config().authenticationPasswordFile();
97       String JavaDoc access = configurationSetupManager.commonl2Config().authenticationAccessFile();
98       if (!new File JavaDoc(pwd).exists()) CustomerLogging.getConsoleLogger().error("Password file does not exist: " + pwd);
99       if (!new File JavaDoc(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 JavaDoc(Long.MAX_VALUE));
107     url = new JMXServiceURL JavaDoc("service:jmx:rmi://");
108     RMIJRMPServerImpl JavaDoc server = new RMIJRMPServerImpl JavaDoc(jmxPort, null, null, env);
109     jmxConnectorServer = new RMIConnectorServer JavaDoc(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 JavaDoc, InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
120     unregisterMBeans();
121     if (jmxConnectorServer != null) {
122       jmxConnectorServer.stop();
123     }
124   }
125
126   public Object JavaDoc findMBean(ObjectName JavaDoc objectName, Class JavaDoc mBeanInterface) throws IOException JavaDoc {
127     return findMBean(objectName, mBeanInterface, mBeanServer);
128   }
129
130   public MBeanServer JavaDoc getMBeanServer() {
131     return mBeanServer;
132   }
133
134   public ObjectManagementMonitorMBean findObjectManagementMonitorMBean() {
135     return objectManagementBean;
136   }
137
138   private void registerMBeans() throws MBeanRegistrationException JavaDoc, NotCompliantMBeanException JavaDoc,
139       InstanceAlreadyExistsException JavaDoc {
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 JavaDoc, MBeanRegistrationException JavaDoc {
146     mBeanServer.unregisterMBean(L2MBeanNames.TC_SERVER_INFO);
147     mBeanServer.unregisterMBean(L2MBeanNames.LOGGER);
148     mBeanServer.unregisterMBean(L2MBeanNames.OBJECT_MANAGEMENT);
149   }
150 }
151
Popular Tags