1 22 package org.jboss.system.microcontainer; 23 24 import java.lang.reflect.InvocationHandler ; 25 import java.lang.reflect.Method ; 26 import java.lang.reflect.Proxy ; 27 import java.util.HashMap ; 28 29 import javax.management.MBeanInfo ; 30 import javax.management.MBeanOperationInfo ; 31 import javax.management.MBeanServer ; 32 import javax.management.ObjectName ; 33 34 import org.jboss.mx.util.JMXExceptionDecoder; 35 import org.jboss.system.Service; 36 import org.jboss.system.ServiceController; 37 38 56 public class ServiceProxy implements InvocationHandler 57 { 58 62 private static HashMap <String , Integer > serviceOpMap = new HashMap <String , Integer >(); 63 64 67 static 68 { 69 serviceOpMap.put("create", new Integer (0)); 70 serviceOpMap.put("start", new Integer (1)); 71 serviceOpMap.put("destroy", new Integer (2)); 72 serviceOpMap.put("stop", new Integer (3)); 73 } 74 75 private boolean[] hasOp = {false, false, false, false}; 76 private ObjectName objectName; 77 private MBeanServer server; 78 79 80 private boolean hasJBossInternalLifecycle; 81 82 90 public static Service getServiceProxy(ObjectName objectName, MBeanServer server) throws Exception 91 { 92 Service service = null; 93 MBeanInfo info = server.getMBeanInfo(objectName); 94 MBeanOperationInfo [] opInfo = info.getOperations(); 95 Class [] interfaces = { Service.class }; 96 InvocationHandler handler = new ServiceProxy(objectName, server, opInfo); 97 service = (Service) Proxy.newProxyInstance(Service.class.getClassLoader(), interfaces, handler); 98 99 return service; 100 } 101 102 111 public ServiceProxy(ObjectName objectName, MBeanServer server, MBeanOperationInfo [] opInfo) 112 { 113 this.server = server; 114 this.objectName = objectName; 115 116 for (int op = 0; op < opInfo.length; op++) 117 { 118 MBeanOperationInfo info = opInfo[op]; 119 String name = info.getName(); 120 121 if (name.equals(ServiceController.JBOSS_INTERNAL_LIFECYCLE)) 122 { 123 hasJBossInternalLifecycle = true; 124 continue; 125 } 126 127 Integer opID = serviceOpMap.get(name); 128 if (opID == null) 129 { 130 continue; 131 } 132 133 if (info.getReturnType().equals("void") == false) 135 { 136 continue; 137 } 138 if (info.getSignature().length != 0) 139 { 140 continue; 141 } 142 143 hasOp[opID.intValue()] = true; 144 } 145 } 146 147 158 public Object invoke(Object proxy, Method method, Object [] args) 159 throws Throwable 160 { 161 String name = method.getName(); 162 163 if (hasJBossInternalLifecycle) 164 { 165 try 166 { 167 server.invoke(objectName, ServiceController.JBOSS_INTERNAL_LIFECYCLE, new Object [] { name }, ServiceController.JBOSS_INTERNAL_LIFECYCLE_SIG); 168 return null; 169 } 170 catch (Exception e) 171 { 172 throw JMXExceptionDecoder.decode(e); 173 } 174 } 175 176 Integer opID = serviceOpMap.get(name); 177 178 if (opID != null && hasOp[opID.intValue()] == true) 179 { 180 try 182 { 183 String [] sig = {}; 184 server.invoke(objectName, name, args, sig); 185 } 186 catch (Exception e) 187 { 188 throw JMXExceptionDecoder.decode(e); 189 } 190 } 191 192 return null; 193 } 194 } 195 | Popular Tags |