KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > container > ExoContainer


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5 package org.exoplatform.container;
6
7 import java.lang.reflect.Constructor JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import javax.management.InstanceAlreadyExistsException JavaDoc;
12 import javax.management.MBeanAttributeInfo JavaDoc;
13 import javax.management.MBeanInfo JavaDoc;
14 import javax.management.MBeanOperationInfo JavaDoc;
15 import javax.management.MBeanParameterInfo JavaDoc;
16 import javax.management.MBeanServer JavaDoc;
17 import javax.management.MalformedObjectNameException JavaDoc;
18 import javax.management.ObjectName JavaDoc;
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 /**
28  * Created by The eXo Platform SARL
29  * Author : Tuan Nguyen
30  * tuan08@users.sourceforge.net
31  * Date: Jul 18, 2004
32  * Time: 12:15:28 AM
33  */

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 JavaDoc getMBeanServer() {
44     throw new UnsupportedOperationException JavaDoc("This container do not support jmx management") ;
45   }
46   
47   public GroovyManager getGroovyManager() {
48     throw new UnsupportedOperationException JavaDoc("This container do not support jmx management") ;
49   }
50   
51   /*
52   public void registerManageableComponentInstance(Object key , Object instance) {
53     manageMBean(key, instance) ;
54     registerComponentInstance(key, instance) ;
55   }
56   
57   public void registerManageableComponentImplementation(Class clazz) {
58     registerComponent(new MX4JComponentAdapter(clazz, clazz)) ;
59   }
60   
61   public void registerManageableComponentImplementation(Object key, Class clazz) {
62     registerComponent(new MX4JComponentAdapter(key, clazz)) ;
63   }
64   */

65   
66   public Object JavaDoc createComponent(Class JavaDoc clazz) throws Exception JavaDoc {
67     Constructor JavaDoc[] constructors = getSortedConstructors(clazz) ;
68     for(int k = 0; k < constructors.length; k++) {
69       Constructor JavaDoc constructor = constructors[k];
70       Class JavaDoc[] parameters = constructor.getParameterTypes() ;
71       Object JavaDoc[] args = new Object JavaDoc[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 JavaDoc("Cannot find a stisfid constructor for " + clazz) ;
85   }
86   
87   private Constructor JavaDoc[] getSortedConstructors(Class JavaDoc clazz) {
88     Constructor JavaDoc[] 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 JavaDoc 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 JavaDoc key, Object JavaDoc bean) {
102     ObjectName JavaDoc name = null;
103     MBeanServer JavaDoc mbeanServer = getMBeanServer() ;
104     Object JavaDoc mbean = null;
105     try {
106       name = asObjectName(mbeanServer, key);
107       mbean = new ExoContainerMBean(bean);
108       mbeanServer.registerMBean(mbean, name);
109     } catch (InstanceAlreadyExistsException JavaDoc e) {
110       try {
111         mbeanServer.unregisterMBean(name);
112         mbeanServer.registerMBean(mbean, name);
113       } catch (Exception JavaDoc e1) {
114         throw new MX4JRegistrationException("Failed to register MBean '" + name + "' for component '" + key + "', due to " + e.getMessage(), e);
115       }
116     } catch (Exception JavaDoc e) {
117       throw new MX4JRegistrationException("Failed to register MBean '" + name + "' for component '" + key + "', due to " + e.getMessage(), e);
118     }
119     
120   }
121   
122   /**
123    * Ensures that the given componentKey is converted to a JMX ObjectName
124    * @param componentKey
125    * @return an ObjectName based on the given componentKey
126    */

127   private static ObjectName JavaDoc asObjectName(MBeanServer JavaDoc mserver, Object JavaDoc componentKey) throws MalformedObjectNameException JavaDoc {
128     if (componentKey == null) {
129       throw new NullPointerException JavaDoc("componentKey cannot be null");
130     }
131     if (componentKey instanceof ObjectName JavaDoc) {
132       return (ObjectName JavaDoc) componentKey;
133     }
134     if (componentKey instanceof Class JavaDoc) {
135       Class JavaDoc clazz = (Class JavaDoc) componentKey;
136       String JavaDoc name = clazz.getName() ;
137       int idx = name.lastIndexOf(".");
138       if(idx > 0) {
139        name = name.substring(0, idx) ;
140       }
141       return new ObjectName JavaDoc(name + ":type=" + clazz.getName());
142     }
143     String JavaDoc text = componentKey.toString();
144     // Fix, so it works under WebSphere ver. 5
145
if (text.indexOf(':') == -1) {
146       text = mserver.getDefaultDomain() + ":type=" + text;
147     }
148     return new ObjectName JavaDoc(text);
149   }
150   
151   public void printMBeanServer() {
152     MBeanServer JavaDoc server = getMBeanServer() ;
153     final Set JavaDoc names = server.queryNames(null, null);
154     for (final Iterator JavaDoc i = names.iterator(); i.hasNext();) {
155       ObjectName JavaDoc name = (ObjectName JavaDoc) i.next();
156       try {
157         MBeanInfo JavaDoc info = server.getMBeanInfo(name);
158         MBeanAttributeInfo JavaDoc[] 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 JavaDoc o = server.getAttribute(name, attrs[j].getName());
164             } catch (Exception JavaDoc x) {
165               x.printStackTrace();
166             }
167           }
168         }
169         MBeanOperationInfo JavaDoc[] methods = info.getOperations();
170         for (int j = 0; j < methods.length; j++) {
171           MBeanParameterInfo JavaDoc[] params = methods[j].getSignature() ;
172           for(int k = 0 ; k < params.length; k++) {
173           }
174         }
175       } catch (Exception JavaDoc x) {
176         //x.printStackTrace(System.err);
177
}
178     }
179   }
180 }
Popular Tags