1 23 24 29 30 31 package com.sun.enterprise.admin.jmx.remote.server; 32 33 import java.lang.reflect.Method ; 34 import java.util.Set ; 35 import java.util.Iterator ; 36 import java.util.logging.Logger ; 37 import javax.management.remote.message.MBeanServerRequestMessage; 38 import javax.management.remote.message.MBeanServerResponseMessage; 39 40 import javax.management.MBeanServerConnection ; 41 import javax.management.MBeanServer ; 42 import javax.management.MBeanServerFactory ; 43 import javax.security.auth.Subject ; 44 45 import javax.servlet.ServletConfig ; 46 47 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration; 48 49 import com.sun.enterprise.admin.jmx.remote.protocol.Version; 50 51 import com.sun.enterprise.admin.jmx.remote.server.notification.ServerNotificationManager; 52 53 import com.sun.enterprise.admin.jmx.remote.server.callers.MethodCallers; 54 import com.sun.enterprise.admin.jmx.remote.server.callers.MBeanServerConnectionMethodCaller; 55 56 import com.sun.enterprise.admin.jmx.remote.internal.Shifter; 57 58 59 60 61 69 70 public class MBeanServerRequestHandler { 71 72 73 74 final private Set callers; 75 76 private static final Logger logger = Logger.getLogger( 77 DefaultConfiguration.JMXCONNECTOR_LOGGER); 79 80 private static Version sv; 81 static { 82 try { 83 sv = (Version)Class.forName(Version.CLASS_NAME).newInstance(); 84 } 85 catch(Exception e) { 86 throw new RuntimeException (e); 87 } 88 } 89 90 final private static ServerVersionMatcher matcher = ServerVersionMatcher.getMatcher(); 91 92 private ServerNotificationManager notifyMgr = null; 93 94 public MBeanServerRequestHandler(ServletConfig cfg) { 96 MBeanServerConnection mbsc = getMBeanServerConnection(cfg); 97 notifyMgr = new ServerNotificationManager(mbsc); 98 notifyMgr.setBufSiz(cfg); 99 callers = MethodCallers.callers(mbsc, notifyMgr); 101 102 logger.finer("Server Jmx Connector Version: " + sv.toString()); 103 } 104 105 106 public ServerNotificationManager getNotificationManager() { 107 return notifyMgr; 108 } 109 110 private MBeanServerConnection getMBeanServerConnection(ServletConfig cfg) { 112 String factoryClass = cfg.getInitParameter(DefaultConfiguration.MBEANSERVER_FACTORY_PROPERTY_NAME); 114 final MBeanServerConnection mbsc = introspectMBS(factoryClass); 116 117 if (mbsc != null) { 118 return mbsc; 119 } 120 final java.util.ArrayList servers = MBeanServerFactory.findMBeanServer(null); 121 final MBeanServer systemMBS = (MBeanServer )servers.get(0); 122 return ((MBeanServerConnection )systemMBS); 123 } 124 125 public MBeanServerResponseMessage handle(MBeanServerRequestMessage request0) { 126 assert (request0.getParams().length >= 1) : "Invalid Object Array"; MBeanServerResponseMessage response = null; 128 final Version cv = (Version)request0.getParams()[0]; 129 logger.finer("Client Version = " + cv.toString()); 130 if (! isCompatible(cv)) { 131 response = incompatibleVersionMessage(request0); 132 return ( response ); 133 } 134 final MBeanServerRequestMessage request = removeVersion(request0); 136 boolean handled = false; 137 final Iterator iter = callers.iterator(); 138 while (iter.hasNext()) { 139 final MBeanServerConnectionMethodCaller caller = 140 (MBeanServerConnectionMethodCaller)iter.next(); 141 if (caller.canCall(request)) { 142 response = caller.call(request); 143 handled = true; 144 break; 145 } 146 } 147 assert handled : "The request is not handled -- catastrophe"; 148 return ( response ); 149 } 150 151 156 157 private MBeanServerConnection introspectMBS(String factoryClass) { 159 160 MBeanServerConnection mbsc = null; 161 162 String FACTORY_CLASS = factoryClass; 164 if (factoryClass == null || factoryClass.trim().length() == 0) 165 FACTORY_CLASS = System.getProperty(DefaultConfiguration.MBEANSERVER_FACTORY_PROPERTY_NAME); 166 167 final String FACTORY_METHOD = "getMBeanServer"; 168 169 if (FACTORY_CLASS == null || FACTORY_CLASS.trim().length() == 0) 170 return null; 171 172 try { 173 logger.finer("Introspecting the MBeanServerConnection"); 174 final Class c = Class.forName(FACTORY_CLASS); final Method m = c.getMethod(FACTORY_METHOD, null); 176 final Object r = m.invoke(c, null); 177 assert (r instanceof MBeanServer ) : "Reflection does not return the correct type"; 178 mbsc = (MBeanServerConnection )r; 179 logger.finer("Introspected the MBeanServerConnection successfully!!"); 180 } 181 catch (Throwable t) { 182 logger.throwing(this.getClass().getName(), "introspectMBS", t); 183 } 184 return ( mbsc ); 185 } 186 187 private MBeanServerRequestMessage removeVersion(MBeanServerRequestMessage from) { 188 final int id = from.getMethodId(); 189 final Subject s = from.getDelegationSubject(); 190 final Shifter sh = new Shifter(from.getParams()); 191 sh.shiftLeft(); 192 final Object [] np = sh.state(); 193 return ( new MBeanServerRequestMessage(id, np, s) ); 194 } 195 private boolean isCompatible(Version cv) { 196 return ( matcher.match(cv, sv) ); 197 } 198 199 private MBeanServerResponseMessage incompatibleVersionMessage(MBeanServerRequestMessage r) { 200 final int id = r.getMethodId(); 202 final boolean isException = true; 203 final Version cv = (Version)r.getParams()[0]; 204 Exception e = null; 205 assert (! isCompatible(cv)) : "No message for compatible versions"; 206 207 if (! matcher.majorCompatible(cv, sv)) { 208 e = majorVersionIncompatible(cv, sv); 209 } 210 else if (! matcher.minorCompatible(cv, sv)) { 211 e = minorVersionIncompatible(cv, sv); 212 } 213 else if (! matcher.upgradeCompatible(cv, sv)) { 214 e = upgradeIncompatible(cv, sv); 215 } 216 assert (e != null) : "Either minor/major version or upgrade data have to fail the match"; 217 218 return ( new MBeanServerResponseMessage(id, e, isException) ); 219 } 220 221 private Exception majorVersionIncompatible(Version cv, Version sv) { 222 final StringBuffer sb = new StringBuffer (); 224 sb.append("The major versions don't match: "). 225 append("Client Major Version = " + cv.getMajorVersion()). 226 append("Server Major Version = " + sv.getMajorVersion()). 227 append(" Upgrade the software accordingly"); 228 229 return ( new RuntimeException (sb.toString()) ); 230 } 231 private Exception minorVersionIncompatible(Version cv, Version sv) { 232 final StringBuffer sb = new StringBuffer (); 234 sb.append("The minor versions don't match: "). 235 append("Client Minor Version = " + cv.getMinorVersion()). 236 append("Server Minor Version = " + sv.getMinorVersion()). 237 append(" Upgrade the software accordingly"); 238 239 return ( new RuntimeException (sb.toString()) ); 240 } 241 private Exception upgradeIncompatible(Version cv, Version sv) { 242 final StringBuffer sb = new StringBuffer (); 244 sb.append("The upgrade data in versions does not match: "). 245 append("Client Upgrade Data = " + cv.toString()). 246 append("Server Upgrade Data = " + sv.toString()). 247 append(" Upgrade the software accordingly"); 248 249 return ( new RuntimeException (sb.toString()) ); 250 } 251 } 252 | Popular Tags |