1 5 package org.exoplatform.container; 6 7 import java.lang.reflect.Constructor ; 8 import java.util.Iterator ; 9 import java.util.Set ; 10 11 import javax.management.InstanceAlreadyExistsException ; 12 import javax.management.MBeanAttributeInfo ; 13 import javax.management.MBeanInfo ; 14 import javax.management.MBeanOperationInfo ; 15 import javax.management.MBeanParameterInfo ; 16 import javax.management.MBeanServer ; 17 import javax.management.MalformedObjectNameException ; 18 import javax.management.ObjectName ; 19 20 import org.exoplatform.container.groovy.GroovyManager; 21 import org.exoplatform.container.jmx.ExoContainerMBean; 22 import org.exoplatform.container.jmx.MX4JRegistrationException; 23 import org.picocontainer.PicoContainer; 24 import org.picocontainer.defaults.ComponentAdapterFactory; 25 import org.picocontainer.defaults.DefaultPicoContainer; 26 27 34 public class ExoContainer extends DefaultPicoContainer { 35 public ExoContainer(PicoContainer parent) { 36 super(parent) ; 37 } 38 39 public ExoContainer(ComponentAdapterFactory factory, PicoContainer parent) { 40 super(factory, parent) ; 41 } 42 43 public MBeanServer getMBeanServer() { 44 throw new UnsupportedOperationException ("This container do not support jmx management") ; 45 } 46 47 public GroovyManager getGroovyManager() { 48 throw new UnsupportedOperationException ("This container do not support jmx management") ; 49 } 50 51 65 66 public Object createComponent(Class clazz) throws Exception { 67 Constructor [] constructors = getSortedConstructors(clazz) ; 68 for(int k = 0; k < constructors.length; k++) { 69 Constructor constructor = constructors[k]; 70 Class [] parameters = constructor.getParameterTypes() ; 71 Object [] args = new Object [parameters.length] ; 72 boolean satisfied = true ; 73 for (int i = 0; i < args.length; i++) { 74 args[i] = getComponentInstanceOfType(parameters[i]) ; 75 if (args[i] == null) { 76 satisfied = false ; 77 break ; 78 } 79 } 80 if(satisfied) { 81 return constructor.newInstance(args) ; 82 } 83 } 84 throw new Exception ("Cannot find a stisfid constructor for " + clazz) ; 85 } 86 87 private Constructor [] getSortedConstructors(Class clazz) { 88 Constructor [] constructors = clazz.getConstructors() ; 89 for(int i = 0 ; i < constructors.length; i++) { 90 for(int j = i + 1 ; j < constructors.length; j++) { 91 if(constructors[i].getParameterTypes().length < constructors[j].getParameterTypes().length) { 92 Constructor tmp = constructors[i] ; 93 constructors[i] = constructors[j] ; 94 constructors[j] = tmp ; 95 } 96 } 97 } 98 return constructors ; 99 } 100 101 public void manageMBean(Object key, Object bean) { 102 ObjectName name = null; 103 MBeanServer mbeanServer = getMBeanServer() ; 104 Object mbean = null; 105 try { 106 name = asObjectName(mbeanServer, key); 107 mbean = new ExoContainerMBean(bean); 108 mbeanServer.registerMBean(mbean, name); 109 } catch (InstanceAlreadyExistsException e) { 110 try { 111 mbeanServer.unregisterMBean(name); 112 mbeanServer.registerMBean(mbean, name); 113 } catch (Exception e1) { 114 throw new MX4JRegistrationException("Failed to register MBean '" + name + "' for component '" + key + "', due to " + e.getMessage(), e); 115 } 116 } catch (Exception e) { 117 throw new MX4JRegistrationException("Failed to register MBean '" + name + "' for component '" + key + "', due to " + e.getMessage(), e); 118 } 119 120 } 121 122 127 private static ObjectName asObjectName(MBeanServer mserver, Object componentKey) throws MalformedObjectNameException { 128 if (componentKey == null) { 129 throw new NullPointerException ("componentKey cannot be null"); 130 } 131 if (componentKey instanceof ObjectName ) { 132 return (ObjectName ) componentKey; 133 } 134 if (componentKey instanceof Class ) { 135 Class clazz = (Class ) componentKey; 136 String name = clazz.getName() ; 137 int idx = name.lastIndexOf("."); 138 if(idx > 0) { 139 name = name.substring(0, idx) ; 140 } 141 return new ObjectName (name + ":type=" + clazz.getName()); 142 } 143 String text = componentKey.toString(); 144 if (text.indexOf(':') == -1) { 146 text = mserver.getDefaultDomain() + ":type=" + text; 147 } 148 return new ObjectName (text); 149 } 150 151 public void printMBeanServer() { 152 MBeanServer server = getMBeanServer() ; 153 final Set names = server.queryNames(null, null); 154 for (final Iterator i = names.iterator(); i.hasNext();) { 155 ObjectName name = (ObjectName ) i.next(); 156 try { 157 MBeanInfo info = server.getMBeanInfo(name); 158 MBeanAttributeInfo [] attrs = info.getAttributes(); 159 if (attrs == null) continue; 160 for (int j = 0; j < attrs.length; j++) { 161 if (attrs[j].isReadable()) { 162 try { 163 Object o = server.getAttribute(name, attrs[j].getName()); 164 } catch (Exception x) { 165 x.printStackTrace(); 166 } 167 } 168 } 169 MBeanOperationInfo [] methods = info.getOperations(); 170 for (int j = 0; j < methods.length; j++) { 171 MBeanParameterInfo [] params = methods[j].getSignature() ; 172 for(int k = 0 ; k < params.length; k++) { 173 } 174 } 175 } catch (Exception x) { 176 } 178 } 179 } 180 } | Popular Tags |