KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.tc.management;
5
6 import com.tc.management.beans.MBeanNames;
7 import com.tc.management.beans.sessions.SessionMonitorMBean;
8 import com.tc.net.TCSocketAddress;
9
10 import java.io.IOException JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.management.MBeanServerConnection JavaDoc;
18 import javax.management.MBeanServerInvocationHandler JavaDoc;
19 import javax.management.MalformedObjectNameException JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21 import javax.management.Query JavaDoc;
22 import javax.management.QueryExp JavaDoc;
23
24 public abstract class TerracottaManagement {
25
26   private static final ManagementResources MANAGEMENT_RESOURCES = new ManagementResources();
27
28   public static class Type {
29
30     private static final Map JavaDoc typesByName = Collections.synchronizedMap(new HashMap JavaDoc());
31     public static final Type DsoClient = new Type(MANAGEMENT_RESOURCES.getDsoClientType());
32     public static final Type Sessions = new Type(MANAGEMENT_RESOURCES.getSessionsType());
33     public static final Type Server = new Type(MANAGEMENT_RESOURCES.getTerracottaServerType());
34     public static final Type Cluster = new Type(MANAGEMENT_RESOURCES.getTerracottaClusterType());
35
36     private final String JavaDoc type;
37
38     private Type(final String JavaDoc type) {
39       this.type = type;
40       typesByName.put(type, this);
41     }
42
43     public String JavaDoc toString() {
44       return type;
45     }
46
47     static Type getType(String JavaDoc name) {
48       return (Type) typesByName.get(name);
49     }
50   }
51
52   public static class Subsystem {
53
54     private static final Map JavaDoc subsystemByName = Collections.synchronizedMap(new HashMap JavaDoc());
55     public static final Subsystem Tx = new Subsystem(MANAGEMENT_RESOURCES.getTransactionSubsystem());
56     public static final Subsystem Locking = new Subsystem(MANAGEMENT_RESOURCES.getLockingSubsystem());
57     public static final Subsystem ObjectManagement = new Subsystem(MANAGEMENT_RESOURCES.getObjectManagementSubsystem());
58     public static final Subsystem None = new Subsystem(MANAGEMENT_RESOURCES.getNoneSubsystem());
59
60     private final String JavaDoc subsystem;
61
62     private Subsystem(final String JavaDoc subsystem) {
63       this.subsystem = subsystem;
64       subsystemByName.put(subsystem, this);
65     }
66
67     public String JavaDoc toString() {
68       return subsystem;
69     }
70
71     static Subsystem getSubsystem(String JavaDoc name) {
72       return (Subsystem) subsystemByName.get(name);
73     }
74   }
75
76   public static interface MBeanKeys {
77     public static final String JavaDoc TYPE = "type";
78     public static final String JavaDoc NODE = "node";
79     public static final String JavaDoc SUBSYSTEM = "subsystem";
80     public static final String JavaDoc NAME = "name";
81   }
82
83   public static final String JavaDoc PUBLIC_DOMAIN = MANAGEMENT_RESOURCES.getPublicMBeanDomain();
84   public static final String JavaDoc INTERNAL_DOMAIN = MANAGEMENT_RESOURCES.getInternalMBeanDomain();
85
86   private static final String JavaDoc COMMA = ",";
87   private static final String JavaDoc COLON = ":";
88   private static final String JavaDoc EQUALS = "=";
89   private static final String JavaDoc SLASH = "/";
90
91   private static final String JavaDoc NODE_PREFIX = "clients" + EQUALS + "Clients";
92
93   private static final String JavaDoc NODE = System.getProperty(MANAGEMENT_RESOURCES.getNodeNameSystemProperty());
94
95   public static ObjectName JavaDoc createObjectName(final Type type, final Subsystem subsystem,
96       final TCSocketAddress remoteBeanHome, final String JavaDoc uiFriendlyName, final boolean isPublic)
97       throws MalformedObjectNameException JavaDoc {
98     final StringBuffer JavaDoc objName = new StringBuffer JavaDoc(isPublic ? PUBLIC_DOMAIN : INTERNAL_DOMAIN);
99     objName.append(COLON);
100     if (NODE != null) {
101       objName.append(NODE_PREFIX).append(COMMA).append(MBeanKeys.NODE).append(EQUALS).append(NODE).append(COMMA);
102     } else if (remoteBeanHome != null) {
103       objName.append(NODE_PREFIX).append(COMMA).append(MBeanKeys.NODE).append(EQUALS).append(
104           remoteBeanHome.getAddress().getCanonicalHostName()).append(SLASH).append(remoteBeanHome.getPort()).append(
105           COMMA);
106     }
107     objName.append(MBeanKeys.TYPE).append(EQUALS).append(type);
108     if (subsystem != Subsystem.None) {
109       objName.append(COMMA).append(MBeanKeys.SUBSYSTEM).append(EQUALS).append(subsystem);
110     }
111     objName.append(COMMA).append(MBeanKeys.NAME).append(EQUALS).append(uiFriendlyName);
112     return new ObjectName JavaDoc(objName.toString());
113   }
114
115   public static ObjectName JavaDoc addNodeInfo(ObjectName JavaDoc objName, TCSocketAddress addr) throws MalformedObjectNameException JavaDoc {
116     if (objName.getKeyProperty(MBeanKeys.NODE) != null) { return objName; }
117
118     String JavaDoc keyProperty = objName.getKeyProperty(MBeanKeys.SUBSYSTEM);
119     Subsystem subsystem = keyProperty != null ? Subsystem.getSubsystem(keyProperty) : Subsystem.None;
120     return createObjectName(Type.getType(objName.getKeyProperty(MBeanKeys.TYPE)), (subsystem != null ? subsystem
121         : Subsystem.None), addr, objName.getKeyProperty(MBeanKeys.NAME), objName.getDomain().equals(PUBLIC_DOMAIN));
122   }
123
124   public abstract Object JavaDoc findMBean(final ObjectName JavaDoc objectName, final Class JavaDoc mBeanInterface) throws Exception JavaDoc;
125
126   public static final Object JavaDoc findMBean(final ObjectName JavaDoc objectName, final Class JavaDoc mBeanInterface,
127       MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc {
128     final Set JavaDoc matchingBeans = mBeanServer.queryMBeans(objectName, null);
129     final Iterator JavaDoc beanPos = matchingBeans.iterator();
130     if (beanPos.hasNext()) { return MBeanServerInvocationHandler.newProxyInstance(mBeanServer, objectName,
131         mBeanInterface, false); }
132     return null;
133   }
134
135   public static final QueryExp JavaDoc matchAllTerracottaMBeans() {
136     try {
137       return Query.or(new ObjectName JavaDoc(PUBLIC_DOMAIN + ":*"), new ObjectName JavaDoc(INTERNAL_DOMAIN + ":*"));
138     } catch (MalformedObjectNameException JavaDoc e) {
139       throw new RuntimeException JavaDoc(e);
140     }
141   }
142
143   public static final String JavaDoc quoteIfNecessary(String JavaDoc objectNamePart) {
144     if (objectNamePart.matches("[,=:*?\"']")) { return ObjectName.quote(objectNamePart); }
145     return objectNamePart;
146   }
147
148   public static final Set JavaDoc getAllSessionMonitorMBeans(MBeanServerConnection JavaDoc mbs) throws MalformedObjectNameException JavaDoc,
149       NullPointerException JavaDoc, IOException JavaDoc {
150     return mbs.queryNames(new ObjectName JavaDoc(MBeanNames.SESSION_INTERNAL.getCanonicalName() + ",*"), null);
151   }
152
153   public static final SessionMonitorMBean getClientSessionMonitorMBean(MBeanServerConnection JavaDoc mbs,
154       Set JavaDoc sessionMonitorMBeans, String JavaDoc nodeName) throws IOException JavaDoc {
155     for (Iterator JavaDoc iter = sessionMonitorMBeans.iterator(); iter.hasNext();) {
156       ObjectName JavaDoc smObjectName = (ObjectName JavaDoc) iter.next();
157       if (nodeName.equals(smObjectName.getKeyProperty(MBeanKeys.NODE))) { return (SessionMonitorMBean) TerracottaManagement
158           .findMBean(smObjectName, SessionMonitorMBean.class, mbs); }
159     }
160     throw new AssertionError JavaDoc("No SessionMonitorMBean found for " + nodeName);
161   }
162 }
163
Popular Tags