KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > custom > MBeanValidator


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 package com.sun.enterprise.admin.mbeans.custom;
25
26 import com.sun.enterprise.admin.mbeans.custom.loading.CustomMBeanRegistrationImpl;
27 import com.sun.enterprise.admin.server.core.CustomMBeanRegistration;
28 import com.sun.enterprise.config.serverbeans.ElementProperty;
29 import com.sun.enterprise.config.serverbeans.Mbean;
30 import com.sun.enterprise.util.SystemPropertyConstants;
31 import java.util.HashMap JavaDoc;
32 import java.util.Hashtable JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35 import javax.management.MBeanServerFactory JavaDoc;
36 import javax.management.MalformedObjectNameException JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38
39 /** A class to validate if nothing changes, it would be possible to register given custom MBean
40  * in a JMX MBeanServer. It is used to decide, with a reasonable confidence, whether
41  * the MBean would be <i> registration-capable </i> when time comes to actually register it.
42  * This is to ensure (statically) that at the runtime, there are no unforeseen problems with
43  * the registration. If this class indicates that an MBean could be registered, it should
44  * imply that the MBean that user is trying to create is <i> good </i>. This approach is
45  * selected to avoid any duplication of work in <code> validating </code> an MBean. A JMX
46  * MBeanServer is the best judge to decide if an MBean implementation class and its management
47  * interface is something that can be an MBean. In other words, it is simulating the actual
48  * MBean registration by doing a dummy registration with all the bells and whistles. It is important
49  * the the callers of this class carefully destroy the instance of this class. For a given MBean it
50  * always makes sense to create an instance of this class, try registration, unregistration and then
51  * cleanup serially.
52  * @since SJSAS9.0
53 */

54 public final class MBeanValidator {
55     
56     private final MBeanServer JavaDoc mbs;
57     /** Creates a new instance of MBeanValidator */
58     public MBeanValidator() {
59         mbs = MBeanServerFactory.newMBeanServer();
60     }
61     public ObjectName JavaDoc registerTestMBean(final Map JavaDoc<String JavaDoc, String JavaDoc> params, final Map JavaDoc<String JavaDoc, String JavaDoc> attributes) throws RuntimeException JavaDoc {
62         try {
63             CustomMBeanRegistration cmr = new CustomMBeanRegistrationImpl(mbs);
64             final Mbean m = toMbean(params, attributes, true);
65             final ObjectName JavaDoc ron = cmr.registerMBean(m);
66             return ( ron );
67         } catch (final Exception JavaDoc e) {
68             throw new RuntimeException JavaDoc(e);
69         }
70         
71     }
72     public void unregisterTestMBean(final ObjectName JavaDoc ron) throws RuntimeException JavaDoc {
73         try {
74             if (mbs.isRegistered(ron))
75                 mbs.unregisterMBean(ron);
76         } catch (final Exception JavaDoc e) {
77             throw new RuntimeException JavaDoc(e);
78         }
79     }
80     /** A convenience method to create a config bean @{link Mbean} corresponding a the given
81      * map of input parameters. It is not guaranteed that this Mbean passes validation.
82      * @param params a Map consisting of name, class-name, object-name etc. of the Mbean
83      * @param attributes a Map consisting of name and value of the MBean attribute which will be added as <property> sub-elements in <mbean>.
84      * @param enabledNotUsed a vestigial parameter. <b>This parameter has no effect</b> It is still here because this is a public method that may have callers anywhere at all.
85      */

86     public static final Mbean toMbean(final Map JavaDoc<String JavaDoc, String JavaDoc> params, final Map JavaDoc<String JavaDoc, String JavaDoc> attributes, final boolean enabledNotUsed) {
87         final Mbean cmb = new Mbean();
88         final String JavaDoc name = params.get(CustomMBeanConstants.NAME_KEY);
89         final String JavaDoc cName = params.get(CustomMBeanConstants.IMPL_CLASS_NAME_KEY);
90         final String JavaDoc on = params.get(CustomMBeanConstants.OBJECT_NAME_KEY);
91         final String JavaDoc ot = params.get(CustomMBeanConstants.OBJECT_TYPE_KEY);
92         final String JavaDoc enabledString = params.get(CustomMBeanConstants.ENABLED_KEY);
93         
94         boolean enabled = true;
95         
96         if(enabledString != null)
97             enabled = new Boolean JavaDoc(enabledString);
98
99         cmb.setName(name);
100         cmb.setImplClassName(cName);
101         cmb.setObjectName(on);
102         cmb.setObjectType(ot);
103         cmb.setEnabled(enabled);
104         cmb.setElementProperty(map2Properties(attributes));
105         return ( cmb );
106     }
107     
108     public static ObjectName JavaDoc formDefaultObjectName(final Map JavaDoc<String JavaDoc, String JavaDoc> params) throws MalformedObjectNameException JavaDoc {
109         final String JavaDoc domain = CustomMBeanConstants.CUSTOM_MBEAN_DOMAIN;
110         //form the mandatory class-name property, can be safely assumed to be available here.
111
final Map JavaDoc<String JavaDoc, String JavaDoc> onProperties = new HashMap JavaDoc<String JavaDoc, String JavaDoc> ();
112         if (params.containsKey(CustomMBeanConstants.IMPL_CLASS_NAME_KEY))
113             onProperties.put(CustomMBeanConstants.IMPL_CLASS_NAME_KEY, params.get(CustomMBeanConstants.IMPL_CLASS_NAME_KEY));
114         if (params.containsKey(CustomMBeanConstants.NAME_KEY))
115             onProperties.put(CustomMBeanConstants.NAME_KEY, params.get(CustomMBeanConstants.NAME_KEY));
116         final ObjectName JavaDoc on = new ObjectName JavaDoc(domain, new Hashtable JavaDoc<String JavaDoc, String JavaDoc>(onProperties));
117         return ( on );
118     }
119     
120     private static ElementProperty[] map2Properties(final Map JavaDoc<String JavaDoc, String JavaDoc> attributes) {
121         final ElementProperty[] props = new ElementProperty[attributes.size()];
122         int i = 0;
123         for (String JavaDoc n : attributes.keySet()) {
124             final ElementProperty prop = new ElementProperty();
125             prop.setName(n);
126             prop.setValue(attributes.get(n));
127             props[i] = prop;
128             i++;
129         }
130         return ( props ) ;
131     }
132     
133 }
134
Popular Tags