1 22 package org.jboss.ejb3; 23 24 import java.lang.reflect.Method ; 25 26 import javax.management.InstanceNotFoundException ; 27 import javax.management.MBeanException ; 28 import javax.management.MBeanInfo ; 29 import javax.management.MBeanOperationInfo ; 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 import javax.management.ReflectionException ; 33 34 import org.jboss.beans.metadata.plugins.AbstractBeanMetaData; 35 import org.jboss.beans.metadata.plugins.AbstractConstructorMetaData; 36 import org.jboss.beans.metadata.plugins.AbstractValueMetaData; 37 import org.jboss.beans.metadata.plugins.AbstractDemandMetaData; 38 import org.jboss.kernel.Kernel; 39 import org.jboss.kernel.spi.registry.KernelRegistryEntry; 40 41 import org.jboss.ejb3.embedded.resource.RARDeployment; 42 43 import org.jboss.logging.Logger; 44 45 51 public class MCKernelAbstraction 52 extends MCClientKernelAbstraction 53 implements KernelAbstraction 54 { 55 private static final Logger log = Logger.getLogger(MCKernelAbstraction.class); 56 57 public static class AlreadyInstantiated extends AbstractConstructorMetaData 58 { 59 private Object bean; 60 61 public class Factory 62 { 63 64 public Object create() 65 { 66 return bean; 67 } 68 } 69 70 public AlreadyInstantiated(Object bean) 71 { 72 this.bean = bean; 73 this.setFactory(new AbstractValueMetaData(new Factory())); 74 this.setFactoryClass(Factory.class.getName()); 75 this.setFactoryMethod("create"); 76 } 77 } 78 79 private MBeanServer server; 80 81 public MCKernelAbstraction(Kernel kernel, MBeanServer server) 82 { 83 super(kernel); 84 this.server = server; 85 } 86 87 public void setMbeanServer(MBeanServer server) 88 { 89 this.server = server; 90 } 91 92 private boolean hasOperation(MBeanInfo info, String operationName) 93 { 94 for(MBeanOperationInfo operationInfo : info.getOperations()) 95 { 96 if(operationInfo.getName().equals(operationName) == false) 97 continue; 98 99 if(operationInfo.getReturnType().equals("void") == false) 101 continue; 102 103 if(operationInfo.getSignature().length != 0) 105 continue; 106 107 return true; 108 } 109 110 return false; 111 } 112 113 public void install(String name, DependencyPolicy dependencies, Object service) 114 { 115 AbstractBeanMetaData bean = new AbstractBeanMetaData(name, service.getClass().getName()); 116 bean.setConstructor(new AlreadyInstantiated(service)); 117 MCDependencyPolicy policy = (MCDependencyPolicy) dependencies; 118 bean.setDemands(policy.getDependencies()); 119 log.info("installing bean: " + name + " with dependencies:"); 120 for (Object obj : policy.getDependencies()) 121 { 122 String msg; 123 if (obj instanceof AbstractDemandMetaData) 124 { 125 msg = ((AbstractDemandMetaData)obj).getDemand().toString(); 126 } 127 else 128 { 129 msg = obj.toString(); 130 } 131 log.info("\t" + msg); 132 } 133 try 134 { 135 try 136 { 137 kernel.getController().uninstall(name); 138 } 139 catch (IllegalStateException e){} 140 141 kernel.getController().install(bean); 142 } 143 catch (Throwable throwable) 144 { 145 throw new RuntimeException (throwable); 146 } 147 } 148 149 public void installMBean(ObjectName on, DependencyPolicy dependencies, Object service) 150 { 151 try 152 { 153 server.registerMBean(service, on); 154 install(on.toString(), dependencies, service); 155 156 MBeanInfo info = server.getMBeanInfo(on); invokeOptionalMethod(on, info, "create"); 159 invokeOptionalMethod(on, info, "start"); 160 } 161 catch (Exception e) 162 { 163 e.printStackTrace(); 164 throw new RuntimeException (e); 165 } 166 } 167 168 private void invokeOptionalMethod(ObjectName on, MBeanInfo info, String operationName) throws InstanceNotFoundException , MBeanException , ReflectionException 169 { 170 Object params[] = { }; 171 String signature[] = { }; 172 if(hasOperation(info, operationName)) 173 server.invoke(on, operationName, params, signature); 174 } 175 176 public void uninstallMBean(ObjectName on) 177 { 178 try 179 { 180 MBeanInfo info = server.getMBeanInfo(on); invokeOptionalMethod(on, info, "stop"); 183 invokeOptionalMethod(on, info, "destroy"); 184 185 server.unregisterMBean(on); 186 kernel.getController().uninstall(on.toString()); 187 } 188 catch (Exception e) 189 { 190 throw new RuntimeException (e); 191 } 192 } 193 194 public void uninstall(String name) 195 { 196 try 197 { 198 kernel.getController().uninstall(name); 199 } 200 catch (Exception e) 201 { 202 throw new RuntimeException (e); 203 } 204 } 205 206 public Object invoke(ObjectName objectName, String operationName, Object [] params, String [] signature) throws Exception 207 { 208 String name = objectName.getCanonicalName(); 209 KernelRegistryEntry entry = kernel.getRegistry().getEntry(name); 210 if (entry != null) 211 { 212 Object target = entry.getTarget(); 213 if (target instanceof RARDeployment) 214 { 215 RARDeployment deployment = (RARDeployment)target; 216 return deployment.invoke(operationName, params, signature); 217 } 218 else 219 { 220 Class [] types = new Class [signature.length]; 221 for (int i = 0; i < signature.length; ++i) 222 { 223 types[i] = Thread.currentThread().getContextClassLoader().loadClass(signature[i]); 224 } 225 Method method = target.getClass().getMethod(operationName, types); 226 return method.invoke(target, params); 227 } 228 } 229 return null; 230 } 231 } 232 | Popular Tags |