1 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 ; 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.management.MBeanServerConnection ; 18 import javax.management.MBeanServerInvocationHandler ; 19 import javax.management.MalformedObjectNameException ; 20 import javax.management.ObjectName ; 21 import javax.management.Query ; 22 import javax.management.QueryExp ; 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 typesByName = Collections.synchronizedMap(new HashMap ()); 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 type; 37 38 private Type(final String type) { 39 this.type = type; 40 typesByName.put(type, this); 41 } 42 43 public String toString() { 44 return type; 45 } 46 47 static Type getType(String name) { 48 return (Type) typesByName.get(name); 49 } 50 } 51 52 public static class Subsystem { 53 54 private static final Map subsystemByName = Collections.synchronizedMap(new HashMap ()); 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 subsystem; 61 62 private Subsystem(final String subsystem) { 63 this.subsystem = subsystem; 64 subsystemByName.put(subsystem, this); 65 } 66 67 public String toString() { 68 return subsystem; 69 } 70 71 static Subsystem getSubsystem(String name) { 72 return (Subsystem) subsystemByName.get(name); 73 } 74 } 75 76 public static interface MBeanKeys { 77 public static final String TYPE = "type"; 78 public static final String NODE = "node"; 79 public static final String SUBSYSTEM = "subsystem"; 80 public static final String NAME = "name"; 81 } 82 83 public static final String PUBLIC_DOMAIN = MANAGEMENT_RESOURCES.getPublicMBeanDomain(); 84 public static final String INTERNAL_DOMAIN = MANAGEMENT_RESOURCES.getInternalMBeanDomain(); 85 86 private static final String COMMA = ","; 87 private static final String COLON = ":"; 88 private static final String EQUALS = "="; 89 private static final String SLASH = "/"; 90 91 private static final String NODE_PREFIX = "clients" + EQUALS + "Clients"; 92 93 private static final String NODE = System.getProperty(MANAGEMENT_RESOURCES.getNodeNameSystemProperty()); 94 95 public static ObjectName createObjectName(final Type type, final Subsystem subsystem, 96 final TCSocketAddress remoteBeanHome, final String uiFriendlyName, final boolean isPublic) 97 throws MalformedObjectNameException { 98 final StringBuffer objName = new StringBuffer (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 (objName.toString()); 113 } 114 115 public static ObjectName addNodeInfo(ObjectName objName, TCSocketAddress addr) throws MalformedObjectNameException { 116 if (objName.getKeyProperty(MBeanKeys.NODE) != null) { return objName; } 117 118 String 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 findMBean(final ObjectName objectName, final Class mBeanInterface) throws Exception ; 125 126 public static final Object findMBean(final ObjectName objectName, final Class mBeanInterface, 127 MBeanServerConnection mBeanServer) throws IOException { 128 final Set matchingBeans = mBeanServer.queryMBeans(objectName, null); 129 final Iterator 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 matchAllTerracottaMBeans() { 136 try { 137 return Query.or(new ObjectName (PUBLIC_DOMAIN + ":*"), new ObjectName (INTERNAL_DOMAIN + ":*")); 138 } catch (MalformedObjectNameException e) { 139 throw new RuntimeException (e); 140 } 141 } 142 143 public static final String quoteIfNecessary(String objectNamePart) { 144 if (objectNamePart.matches("[,=:*?\"']")) { return ObjectName.quote(objectNamePart); } 145 return objectNamePart; 146 } 147 148 public static final Set getAllSessionMonitorMBeans(MBeanServerConnection mbs) throws MalformedObjectNameException , 149 NullPointerException , IOException { 150 return mbs.queryNames(new ObjectName (MBeanNames.SESSION_INTERNAL.getCanonicalName() + ",*"), null); 151 } 152 153 public static final SessionMonitorMBean getClientSessionMonitorMBean(MBeanServerConnection mbs, 154 Set sessionMonitorMBeans, String nodeName) throws IOException { 155 for (Iterator iter = sessionMonitorMBeans.iterator(); iter.hasNext();) { 156 ObjectName smObjectName = (ObjectName ) iter.next(); 157 if (nodeName.equals(smObjectName.getKeyProperty(MBeanKeys.NODE))) { return (SessionMonitorMBean) TerracottaManagement 158 .findMBean(smObjectName, SessionMonitorMBean.class, mbs); } 159 } 160 throw new AssertionError ("No SessionMonitorMBean found for " + nodeName); 161 } 162 } 163 | Popular Tags |