KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > custom > loading > CustomMBeanRegistrationImpl


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
24
25 package com.sun.enterprise.admin.mbeans.custom.loading;
26 import java.util.logging.Logger JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.InstanceAlreadyExistsException JavaDoc;
29 import javax.management.MBeanRegistrationException JavaDoc;
30 import javax.management.NotCompliantMBeanException JavaDoc;
31 import javax.management.ObjectInstance JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import com.sun.enterprise.admin.mbeans.custom.CMBStrings;
35 import com.sun.enterprise.admin.mbeans.custom.ObjectNameSelectionAlgorithm;
36 import com.sun.enterprise.config.serverbeans.Mbean;
37 import com.sun.enterprise.config.serverbeans.ElementProperty;
38 import com.sun.enterprise.admin.common.constant.AdminConstants;
39 import com.sun.enterprise.admin.mbeans.custom.CustomMBeanConstants;
40 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration;
41 import com.sun.enterprise.util.SystemPropertyConstants;
42 import java.util.Hashtable JavaDoc;
43 import java.util.List JavaDoc;
44 import javax.management.AttributeNotFoundException JavaDoc;
45 import javax.management.InstanceNotFoundException JavaDoc;
46 import javax.management.InvalidAttributeValueException JavaDoc;
47 import javax.management.MBeanException JavaDoc;
48 import javax.management.ReflectionException JavaDoc;
49 import javax.management.RuntimeOperationsException JavaDoc;
50
51 /** Class to register all the MBeans defined in the server's configuration. Registering the MBeans is done
52  * as described in the design document. The self-management rules are to be taken into account for this.
53  * As it stands now, (AS 9.0) this class is not designed to be thread-safe.
54  * The calling code must ensure serial access if needed.
55  * @since SJSAS 9.0
56  */

57 public final class CustomMBeanRegistrationImpl implements CustomMBeanRegistration {
58     
59     /** Creates a new instance of CustomMBeanRegistrar */
60     private final MBeanServer JavaDoc mbs;
61     private ClassLoader JavaDoc cl;
62     private static final Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
63
64     public CustomMBeanRegistrationImpl(final MBeanServer JavaDoc mbs) throws IllegalArgumentException JavaDoc {
65         this.mbs = mbs;
66         this.cl = new MBeanClassLoader();
67     }
68     
69     
70     public void registerMBeans(final List JavaDoc<Mbean> mbeans, final boolean continueReg) throws RuntimeException JavaDoc {
71         if (mbeans == null)
72             throw new IllegalArgumentException JavaDoc(
73                 CMBStrings.get("InternalError", "registerMBeans() received a null mbeans argument"));
74         for (Mbean mbean : mbeans) {
75             try {
76                 registerMBean(mbean);
77             } catch(final Throwable JavaDoc t) {
78                 if (continueReg) {
79                     logger.info(CMBStrings.get("cmb.registerError", mbean.getName()));
80                 }
81                 else {
82                     throw new RuntimeException JavaDoc(t);
83                 }
84             }
85         }
86     }
87     
88     public void registerMBeans(final List JavaDoc<Mbean> mbeans) throws RuntimeException JavaDoc {
89         this.registerMBeans(mbeans, true);
90     }
91     public void setClassLoader(final ClassLoader JavaDoc cl) throws IllegalArgumentException JavaDoc {
92         if (cl == null)
93             throw new IllegalArgumentException JavaDoc(CMBStrings.get("InternalError", "setClassLoader() received a null argument"));
94         this.cl = cl;
95     }
96     public ObjectName JavaDoc registerMBean(final Mbean mbean) throws RuntimeException JavaDoc {
97         /* This is the only place where the "registration of the MBean happens.
98          */

99         if (mbean == null)
100             throw new IllegalArgumentException JavaDoc(CMBStrings.get("InternalError", "registerMBean() received a null argument"));
101         
102         ObjectName JavaDoc ron = null;
103         try {
104             logger.fine(CMBStrings.get("cmb.loadingMBean1", mbean.getName()));
105             final ObjectName JavaDoc mon = getCascadingAwareObjectName(mbean);
106             logger.fine(CMBStrings.get("cmb.loadingMBean2", mon.toString()));
107             final Class JavaDoc mc = loadIt(mbean.getImplClassName());
108             final Object JavaDoc mo = newIt(mc);
109             ron = registerIt(mo, mon);
110
111             
112             // if the MBean implements MBeanRegistration -- it can set the ON to
113
// anything at all...
114
if(!ObjectNameSelectionAlgorithm.implementsMBeanRegistrationInterface(mbean.getImplClassName()))
115             {
116                 if(!mon.equals(ron))
117                     throw new RuntimeException JavaDoc(CMBStrings.get("objNameMismatch", mon, ron));
118             }
119             initIt(mbean, ron);
120    
121             // WBN 12-15-2005
122
// this is just defensive programming -- the Listener should not be
123
// calling us on a disabled mbean. In the case we are being called
124
// as part of pre-reg in order to get an objectname, this will make
125
// one extra unneeded call to unregisterIt()
126
// I say, safety first, performance second!
127
if(!mbean.isEnabled())
128                 unregisterIt(mbean, ron);
129             return ( ron );
130         } catch (final ClassNotFoundException JavaDoc cnfe) {
131             logger.info(CMBStrings.get("cmb.loadingMBean3", mbean.getImplClassName(), cl.getClass().getName()));
132             throw new RuntimeException JavaDoc(cnfe);
133         } catch (final NoClassDefFoundError JavaDoc ncdfe) {
134             logger.info(CMBStrings.get("cmb.loadingMBean4", mbean.getImplClassName(), cl.getClass().getName()));
135             throw new RuntimeException JavaDoc(ncdfe);
136         } catch (final InstantiationException JavaDoc ie) {
137             logger.info(CMBStrings.get("cmb.loadingMBean5", mbean.getImplClassName()));
138             throw new RuntimeException JavaDoc(ie);
139         } catch (final IllegalAccessException JavaDoc iae) {
140             logger.info(CMBStrings.get("cmb.loadingMBean6", mbean.getImplClassName()));
141             throw new RuntimeException JavaDoc(iae);
142         } catch (final ExceptionInInitializerError JavaDoc eie) {
143             logger.info(CMBStrings.get("cmb.loadingMBean7", mbean.getImplClassName()));
144             throw new RuntimeException JavaDoc(eie);
145         }
146         catch (final Throwable JavaDoc e) {
147             //Roll back if registration fails. Failure to initialize is failure to register.
148
//e.printStackTrace();
149
if (ron != null)
150                 unregisterIt(mbean, ron);
151             throw new RuntimeException JavaDoc(e);
152         }
153     }
154     
155     public static ObjectName JavaDoc getCascadingAwareObjectName(final Mbean mbean) throws RuntimeException JavaDoc {
156         try {
157             final ObjectName JavaDoc configON = new ObjectName JavaDoc(mbean.getObjectName());
158             return (getCascadingAwareObjectName(configON) );
159         } catch(final Exception JavaDoc e) {
160             throw new RuntimeException JavaDoc(e);
161         }
162     }
163     public static ObjectName JavaDoc getCascadingAwareObjectName(final ObjectName JavaDoc configON) throws RuntimeException JavaDoc {
164         try {
165             final String JavaDoc serverNameKey = CustomMBeanConstants.SERVER_KEY;
166             final String JavaDoc serverNameVal = System.getProperty(SystemPropertyConstants.SERVER_NAME);
167             final Hashtable JavaDoc properties = configON.getKeyPropertyList();
168             properties.put(serverNameKey, serverNameVal);
169             final ObjectName JavaDoc ron = new ObjectName JavaDoc(configON.getDomain(), properties);
170             return ( ron );
171         } catch(final Exception JavaDoc e) {
172             throw new RuntimeException JavaDoc(e);
173         }
174     }
175     public static ObjectName JavaDoc getCascadingUnawareObjectName(final ObjectName JavaDoc cascadeON) throws RuntimeException JavaDoc {
176         try {
177             if (cascadeON == null) {
178                 throw new IllegalArgumentException JavaDoc(CMBStrings.get("InternalError", "getCascadingUnawareObjectName() received a null argument"));
179             }
180             ObjectName JavaDoc ron = cascadeON;
181             final String JavaDoc serverNameKey = CustomMBeanConstants.SERVER_KEY;
182             final Hashtable JavaDoc properties = cascadeON.getKeyPropertyList(); // this may be unmodifiable
183
if (properties.containsKey(serverNameKey)) {
184                 final Hashtable JavaDoc np = new Hashtable JavaDoc(properties);
185                 np.remove(serverNameKey);
186                 ron = new ObjectName JavaDoc(cascadeON.getDomain(), np);
187             }
188             return ( ron );
189         } catch(final Exception JavaDoc e) {
190             throw new RuntimeException JavaDoc(e);
191         }
192     }
193     ////////// Private Methods ///////
194
private Class JavaDoc loadIt(final String JavaDoc classname) throws ClassNotFoundException JavaDoc, NoClassDefFoundError JavaDoc {
195         final Class JavaDoc c = cl.loadClass(classname);
196         logger.fine(CMBStrings.get("cmb.loadingMBean8", c.getName(), cl.getClass().getName(), c.getClassLoader().getClass().getName()));
197         return ( c );
198     }
199     private Object JavaDoc newIt(final Class JavaDoc c) throws IllegalAccessException JavaDoc, InstantiationException JavaDoc,
200         ExceptionInInitializerError JavaDoc {
201         return ( c.newInstance() );
202     }
203     private ObjectName JavaDoc registerIt(final Object JavaDoc mo, final ObjectName JavaDoc on) throws InstanceAlreadyExistsException JavaDoc,
204         MBeanRegistrationException JavaDoc, NotCompliantMBeanException JavaDoc, RuntimeOperationsException JavaDoc {
205         if(mo == null)
206             throw new RuntimeException JavaDoc(CMBStrings.get("objNameNull"));
207         final ObjectInstance JavaDoc oi = mbs.registerMBean(mo, on);
208         return ( oi.getObjectName() );
209     }
210     private void initIt(final Mbean mbc, final ObjectName JavaDoc on) throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
211         InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
212         final MBeanAttributeSetter mas = new MBeanAttributeSetter(mbs, on);
213         final ElementProperty[] ats = mbc.getElementProperty();
214         for (ElementProperty p : ats) {
215             mas.setIt(p.getName(), p.getValue());
216             logger.fine(CMBStrings.get("cmb.initMBean", p.getName(), mbc.getName()));
217         }
218     }
219     private void unregisterIt(final Mbean m, final ObjectName JavaDoc ron) {
220         //attempt to unregister the mbean, not being able to do so is not fatal
221
try {
222             if (mbs.isRegistered(ron))
223                 mbs.unregisterMBean(ron);
224         } catch (final Throwable JavaDoc e) {
225             logger.warning(CMBStrings.get("cmb.unloadMBeanError", m.getName()));
226         }
227     }
228     ////////// Private Methods ///////
229
}
Popular Tags