KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > MBeanManufacturer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.admin.jmx;
24
25 //JMX imports
26
import javax.management.ObjectName JavaDoc;
27
28 import com.sun.enterprise.admin.meta.MBeanDescriptor;
29 import com.sun.enterprise.admin.meta.MBeanRegistry;
30
31 //Admin imports
32
/*import com.iplanet.ias.admin.common.ObjectNames;
33 import com.iplanet.ias.admin.common.ObjectNameHelper;
34 import com.iplanet.ias.admin.common.exception.MBeanConfigException;
35 import com.iplanet.ias.admin.util.HostAndPort;
36 //import com.iplanet.ias.admin.server.core.mbean.config.*;
37 import com.iplanet.ias.admin.common.constant.AdminConstants;
38 import com.iplanet.ias.admin.server.core.mbean.config.GenericConfigurator;
39 import com.iplanet.ias.admin.server.core.mbean.config.ServerController;
40 import com.iplanet.ias.admin.server.core.mbean.config.ManagedAdminServerInstance;
41 import com.iplanet.ias.admin.server.core.mbean.config.ManagedServerInstance;
42 import com.iplanet.ias.admin.server.core.mbean.config.naming.ConfigMBeanNamingInfo;
43
44  */

45
46 //Other imports
47
//import com.iplanet.ias.config.serverbeans.*;
48
//import com.iplanet.ias.instance.ServerManager;
49

50 import java.util.logging.Level JavaDoc;
51 import java.util.logging.Logger JavaDoc;
52 import java.util.Set JavaDoc;
53 import java.util.List JavaDoc;
54
55 import javax.management.modelmbean.ModelMBean JavaDoc;
56 import javax.management.MBeanServer JavaDoc;
57 import javax.management.MBeanServerFactory JavaDoc;
58
59 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
60 import javax.management.Descriptor JavaDoc;
61
62 //import org.apache.commons.modeler.ManagedBean;
63
//import org.apache.commons.modeler.Registry;
64
//import org.apache.commons.modeler.FieldInfo;
65

66 import com.sun.enterprise.admin.meta.MBeanRegistry;
67
68 import java.io.InputStream JavaDoc;
69 import java.net.URL JavaDoc;
70
71 //i18n import
72
//import com.iplanet.ias.util.i18n.StringManager;
73

74
75 /**
76     A class to construct proper type of MBean from its object name. This
77     class gives the instances of mbeans that would be registered as given
78     object names.
79 */

80
81 public class MBeanManufacturer
82 {
83     private static MBeanRegistry registry = null;
84     /*
85     private ObjectName mObjectName = null;
86     private Object mConfigBean = null;
87
88   // private static final Logger _logger = Logger.getLogger(AdminConstants.kLoggerName);
89     
90     // i18n StringManager
91     private static StringManager localStrings =
92         StringManager.getManager( MBeanManufacturer.class );
93     
94     public MBeanManufacturer (ObjectName oName, Object configBean)
95     {
96         if (oName == null || configBean == null)
97         {
98             String msg = localStrings.getString( "admin.server.core.jmx.storage.null_object_name_or_config_bean" );
99             throw new IllegalArgumentException( msg );
100         }
101         mObjectName = oName;
102         mConfigBean = configBean;
103     }
104
105     /**
106         Returns an instance of proper MBean. This is required, when somebody
107         wants to create the MBean for the first time in MBean Repository.
108         The necessary parameters for MBean construction are derived from the
109         configBean passed during consruction of this class.\
110         @return instance of proper MBean corresponding to ObjectName. ObjectName
111         is passed during the construction of this class. May not be null.
112     * /
113     public Object createMBeanInstance()
114     {
115         Object mbeanInstance = null;
116
117         String type = ObjectNameHelper.getType(mObjectName);
118         String instanceName = ObjectNameHelper.getServerInstanceName(mObjectName);
119         Level logLevel = Level.SEVERE;
120
121         try
122         {
123             if (type.equals(ObjectNames.kController))
124             {
125                 mbeanInstance = new ServerController();
126             }
127             else if (type.equals(ObjectNames.kGenericConfigurator))
128             {
129                 mbeanInstance = new GenericConfigurator();
130             }
131             else if (type.equals(ObjectNames.kServerInstance))
132             {
133                 if (instanceName.equals(ServerManager.ADMINSERVER_ID))
134                 {
135                     mbeanInstance = createAdminServerInstance();
136                 }
137                 else
138                 {
139                     mbeanInstance = createServerInstanceMBean(instanceName);
140                 }
141             }
142             else
143             {
144                 logLevel = Level.FINE;
145                 mbeanInstance = createGenericConfigMBean(mObjectName);
146             }
147         }
148         catch(Exception e)
149         {
150             _logger.log(logLevel, "mbean.config.admin.create_mbean_instance_failed", e );
151         }
152         
153         return ( mbeanInstance );
154     }
155     
156     private ManagedServerInstance createServerInstanceMBean(String instanceName) throws Exception
157     {
158         Server server = (Server)mConfigBean;
159         HttpService https = server.getHttpService();
160         
161         HttpListener[] hlArray = https.getHttpListener();
162         //check not needed since there should always be atleast 1 httplistener
163         //if you don't find one, use first one.
164         HttpListener ls = hlArray[0];
165         //default is the first one that is enabled.
166         for(int i = 0;i<hlArray.length;i++) {
167             if(hlArray[i].isEnabled()) {
168                 ls = hlArray[i];
169                 break;
170             }
171         }
172         String port = ls.getPort();
173         int intPort = Integer.parseInt (port);
174         HostAndPort hp = new HostAndPort("localhost", intPort);
175         return new ManagedServerInstance(instanceName, hp, false);
176     }
177
178     private ManagedAdminServerInstance createAdminServerInstance()
179         throws Exception
180     {
181         return new ManagedAdminServerInstance();
182     }
183
184     private Object createGenericConfigMBean(ObjectName objectName) throws MBeanConfigException
185     {
186         ConfigMBeanNamingInfo mbeanInfo = new ConfigMBeanNamingInfo(objectName);
187         return mbeanInfo.constructConfigMBean();
188     }
189
190      */

191     
192     static void findPersistent(ObjectName JavaDoc name) {
193         System.out.println("Find persistant for "+name);
194         org.apache.commons.modeler.Registry rr;
195         //FIXME: may be we can have more than 1 registries.
196
if(registry == null) createRegistry();
197         try {
198         
199         MBeanServer JavaDoc server = (MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(null).get(0);
200         
201         //already exists.
202
Set JavaDoc result = server.queryMBeans(name, null);
203         if(result != null && result.size() !=0) return;
204         
205         //krav - find naming descriptor by object name
206
ModelMBean JavaDoc mm = MBeanDescriptor.instantiateMBean(name, null);
207
208         //TEST PRINT
209
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> info from Model mbean <<<<<<<<<<<<<<<<<<<<<<<<<");
210         ModelMBeanInfo JavaDoc inf = (ModelMBeanInfo JavaDoc)mm.getMBeanInfo();
211         System.out.println(">>info="+ inf);
212         Descriptor md = inf.getMBeanDescriptor();
213         System.out.println(">>mbdescr="+ md);
214         System.out.println(">>location="+ md.getFieldValue("persistLocation"));
215         System.out.println(">>>>>>>description="+ inf.getDescription());
216
217            //test code
218
// com.sun.enterprise.admin.config.EjbContainerTest demo =
219
// new com.sun.enterprise.admin.config.EjbContainerTest();
220

221             //mm.addAttributeChangeNotificationListener(demo, "shutdown", null);
222

223 // server.registerMBean(mm, name);
224

225         } catch(Exception JavaDoc e) {
226             e.printStackTrace();
227         }
228     }
229     
230     /**
231      * Create and configure the registry of managed objects.
232      */

233     private static void createRegistry() {
234
235         System.out.println("Create configuration registry ...");
236         try {
237             URL JavaDoc url = MBeanManufacturer.class.getResource
238                 ("/mbeans-descriptors.xml");
239             InputStream JavaDoc stream = url.openStream();
240             registry = new MBeanRegistry();
241             registry.loadMBeanRegistry(stream);
242             stream.close();
243         } catch (Throwable JavaDoc t) {
244             t.printStackTrace(System.out);
245             System.exit(1);
246         }
247
248     }
249     
250 }
251
Popular Tags