KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > config > ConfigMBeanHelper


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  * $Id: ConfigMBeanHelper.java,v 1.6 2006/03/12 01:26:56 jluehe Exp $
26  * @author: alexkrav
27  *
28  * $Log: ConfigMBeanHelper.java,v $
29  * Revision 1.6 2006/03/12 01:26:56 jluehe
30  * Renamed AS's org.apache.commons.* to com.sun.org.apache.commons.*, to avoid collisions with org.apache.commons.* packages bundled by webapps.
31  *
32  * Tests run: QL, Servlet TCK
33  *
34  * Revision 1.5 2006/02/25 00:05:34 kravtch
35  * Bug #6378808(Unable to set/add a blank password property on JDBC Pool through Web UI)
36  * admin-core/admin/.../config:
37  * - new ConfigMBeanHelper.PROPERTY_SPECIAL_EMPTY_VALUE introduced;
38  * - ManagedConfigBean now checks setting property value; if it equals to SPECIAL_EMPTY_VALUE it will be set to "";
39  * Submitted by: kravtch
40  * Reviewed by: Kedar
41  * Affected modules admin-core/admin
42  * Tests passed: QLT/EE
43  *
44  * Revision 1.4 2005/12/25 03:47:30 tcfujii
45  * Updated copyright text and year.
46  *
47  * Revision 1.3 2005/08/16 22:19:30 kravtch
48  * M3: 1. ConfigMBeans: Support for generic getXXXNamesList() operation (request from management-rules).
49  * 2. MBeanRegistry: support for getElementPrintName() to provide readable element's description for validator's messages
50  * Submitted by: kravtch
51  * Reviewed by: Shreedhar
52  * Affected modules admin-core/admin
53  * Tests passed: QLT/EE + devtests
54  *
55  * Revision 1.2 2005/06/27 21:19:40 tcfujii
56  * Issue number: CDDL header updates.
57  *
58  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
59  * GlassFish first drop
60  *
61  * Revision 1.9 2004/11/14 07:04:17 tcfujii
62  * Updated copyright text and/or year.
63  *
64  * Revision 1.8 2004/06/04 19:17:03 kravtch
65  * Reviewer: Sridatta
66  * getConfigBeansObjectNames - helper methods are added to support convertion config beans to ObjcectNames
67  * Tests passed: QLT PE/EE
68  *
69  * Revision 1.7 2004/02/20 03:56:07 qouyang
70  *
71  *
72  * First pass at code merge.
73  *
74  * Details for the merge will be published at:
75  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
76  *
77  * Revision 1.6.4.2 2004/02/02 07:25:14 tcfujii
78  * Copyright updates notices; reviewer: Tony Ng
79  *
80  * Revision 1.6.4.1 2003/12/23 01:51:43 kravtch
81  * Bug #4959186
82  * Reviewer: Sridatta
83  * Checked in PE8FCS_BRANCH
84  * (1) admin/admin-core/admin-cli/admin-gui/appserv-core/assembly-tool: switch to new domain name "ias:" -> "com.sun.appserv"
85  * (2) admin-core and admin-cli: switch to "dashed" attribute names
86  * (3) admin-core: support both "dashed"/"underscored" names for get/setAttributes
87  * (4) admin-gui: hook for reverse converting attribute names (temporary hack);
88  *
89  * Revision 1.6 2003/09/10 00:24:22 kravtch
90  * Reviewer: Sridatta
91  * New operation in DomainMBean is returning list of default attribute values according to mbean type. This operation will return DTD defined default values if custom mbean does not implement its own static operation overriding(extending) standard ones.
92  *
93  * Revision 1.5 2003/08/07 00:41:04 kravtch
94  * - new DTD related changes;
95  * - properties support added;
96  * - getDefaultAttributeValue() implemented for config MBeans;
97  * - merge Jsr77 and config activity in runtime mbeans;
98  *
99  * Revision 1.4 2003/07/18 20:14:43 kravtch
100  * 1. ALL config mbeans are now covered by descriptors.xml
101  * 2. new infrastructure for runtime mbeans is added
102  * 3. generic constructors added to jsr77Mdl beans (String[])
103  * 4. new test cases are added to admintest
104  * 5. MBeanRegistryFactory has now different methods to obtain admin/runtime registries
105  * 6. runtime-descriptors xml-file is added to build
106  *
107  * Revision 1.3 2003/06/25 20:03:37 kravtch
108  * 1. java file headers modified
109  * 2. properties handling api is added
110  * 3. fixed bug for xpathes containing special symbols;
111  * 4. new testcases added for jdbc-resource
112  * 5. introspector modified by not including base classes operations;
113  *
114  *
115 */

116
117 package com.sun.enterprise.admin.config;
118
119 import java.util.Enumeration JavaDoc;
120 import java.util.Hashtable JavaDoc;
121 import java.text.CharacterIterator JavaDoc;
122 import java.text.StringCharacterIterator JavaDoc;
123
124
125 import java.lang.reflect.Method JavaDoc;
126 import java.lang.reflect.Modifier JavaDoc;
127
128 //JMX imports
129
import javax.management.*;
130 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
131 import javax.management.modelmbean.ModelMBeanNotificationInfo JavaDoc;
132 import javax.management.modelmbean.ModelMBeanConstructorInfo JavaDoc;
133 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
134 import javax.management.modelmbean.ModelMBeanInfoSupport JavaDoc;
135 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
136
137 import com.sun.org.apache.commons.modeler.AttributeInfo;
138
139 //admin
140
import com.sun.enterprise.admin.MBeanHelper;
141
142 import com.sun.enterprise.admin.meta.MBeanRegistry;
143 import com.sun.enterprise.admin.meta.MBeanRegistryEntry;
144 //naming
145
import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo;
146 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor;
147 import com.sun.enterprise.admin.meta.MBeanMetaHelper;
148
149 //config
150
import com.sun.enterprise.config.ConfigException;
151 import com.sun.enterprise.config.ConfigBean;
152 import com.sun.enterprise.config.ConfigBeansFactory;
153 import com.sun.enterprise.config.ConfigContext;
154 import com.sun.enterprise.config.serverbeans.ElementProperty;
155
156 /****************************************************************************************************************
157  */

158 public class ConfigMBeanHelper extends MBeanHelper
159 {
160    private static final String JavaDoc XPATH_SEPARATOR = "/";
161    public static final String JavaDoc PROPERTY_NAME_PREFIX = "property.";
162    public static final String JavaDoc PROPERTY_SPECIAL_EMPTY_VALUE = "()";
163     
164     private ConfigBean m_baseConfigBean;
165     private BaseConfigMBean m_mbean;
166     public ConfigMBeanHelper(BaseConfigMBean mbean, ConfigBean cb)
167     {
168         m_baseConfigBean = cb;
169         m_mbean = mbean;
170 // if(!descriptor.isConfigBeanRetrospected())
171
// {
172
// }
173
}
174
175 /* public static ConfigBeanHelper getConfigBeanHelper(BaseConfigMBean mbean)
176     {
177         return new ConfigBeanHelper((MBeanDescriptor)mbean.getMBeanInfo(), mbean.getManagedConfigBean());
178     }
179 */

180     
181
182         
183 //********************************************************************************************************************
184
public static Object JavaDoc converConfigBeansToObjectNames(MBeanRegistry registry, ModelMBeanInfo JavaDoc parentInfo, Object JavaDoc ret) throws Exception JavaDoc
185     {
186         if(ret!=null)
187         {
188             if(ret instanceof ConfigBean)
189                 return (Object JavaDoc)getChildObjectName(registry, parentInfo, (ConfigBean)ret);
190             if(ret instanceof ConfigBean[])
191                 return (Object JavaDoc)getChildObjectNames(registry, parentInfo, (ConfigBean[])ret);
192         }
193         return ret;
194     }
195     
196     //****************************************************************************************************
197
public static Class JavaDoc getConfigBeanClass(String JavaDoc xPath)
198     {
199         // get ConfigBean classname from XPath
200
String JavaDoc beanName = ConfigBeansFactory.getConfigBeanNameByXPath(xPath);
201         //getting the class object
202
try
203         {
204             Class JavaDoc cl = Class.forName("com.sun.enterprise.config.serverbeans."+beanName);
205             return cl;
206         }
207         catch(Exception JavaDoc e)
208         {
209             return null;
210         }
211     }
212
213     //********************************************************************************************************************
214
public static AttributeList getDefaultAttributeValues(MBeanNamingDescriptor descr, String JavaDoc attrNames[]) throws Exception JavaDoc
215     {
216         if(attrNames==null || attrNames.length<1)
217             return null;
218         AttributeList attrs = new AttributeList();
219         Class JavaDoc cl = getConfigBeanClass(descr.getXPathPattern());
220         if(cl==null)
221             return null;
222         Method JavaDoc method = cl.getDeclaredMethod("getDefaultAttributeValue", new Class JavaDoc[]{Class.forName("java.lang.String")});
223         for(int i=0; i<attrNames.length; i++)
224         {
225             try {
226                 Object JavaDoc value = (String JavaDoc)method.invoke(null, new Object JavaDoc[]{MBeanMetaHelper.mapToConfigBeanAttributeName(attrNames[i])});
227                 if(value!=null)
228                     attrs.add(new Attribute(attrNames[i], value));
229             } catch(Exception JavaDoc e) {};
230         }
231         return attrs;
232     }
233     
234     //********************************************************************************************************************
235
public static ObjectName getOwnObjectName(MBeanRegistry registry, ModelMBeanInfo JavaDoc parentInfo) throws Exception JavaDoc
236     {
237         Descriptor descr = parentInfo.getMBeanDescriptor();
238         String JavaDoc type = (String JavaDoc)descr.getFieldValue(NMTYPE_FIELD_NAME);
239         String JavaDoc[] location = (String JavaDoc[])descr.getFieldValue(NMLOCATION_FIELD_NAME);
240         MBeanRegistryEntry entry = registry.findMBeanRegistryEntryByType(type);
241         MBeanNamingDescriptor namingDescr = entry.getNamingDescriptor();
242         return namingDescr.createObjectName(location);
243     }
244     //********************************************************************************************************************
245
public static ObjectName[] getChildObjectNames(MBeanRegistry registry, ModelMBeanInfo JavaDoc parentInfo, ConfigBean[] children) throws Exception JavaDoc
246     {
247         Descriptor descr = parentInfo.getMBeanDescriptor();
248         return getConfigBeansObjectNames(registry, (String JavaDoc)descr.getFieldValue(DOMAIN_FIELD_NAME), children);
249     }
250     //********************************************************************************************************************
251
public static ObjectName getChildObjectName(MBeanRegistry registry, ModelMBeanInfo JavaDoc parentInfo, ConfigBean childBean) throws Exception JavaDoc
252     {
253         Descriptor descr = parentInfo.getMBeanDescriptor();
254         return getConfigBeanObjectName(registry, (String JavaDoc)descr.getFieldValue(DOMAIN_FIELD_NAME), childBean);
255     }
256     //********************************************************************************************************************
257
public static ObjectName[] getConfigBeansObjectNames(MBeanRegistry registry, String JavaDoc domainName, ConfigBean[] beans) throws Exception JavaDoc
258     {
259         ObjectName[] objNames = new ObjectName[beans.length];
260         for(int i=0; i<beans.length; i++)
261             objNames[i] = getConfigBeanObjectName(registry, domainName, beans[i]);
262         return objNames;
263     }
264     //********************************************************************************************************************
265
public static ObjectName getConfigBeanObjectName(MBeanRegistry registry, String JavaDoc domainName, ConfigBean childBean) throws Exception JavaDoc
266     {
267         String JavaDoc xpath = childBean.getAbsoluteXPath("");
268         MBeanRegistryEntry entry = registry.findMBeanRegistryEntryByXPath(xpath);
269         MBeanNamingDescriptor namingDescr = entry.getNamingDescriptor();
270         String JavaDoc[] location = namingDescr.extractParmListFromXPath(xpath);
271         location[0] = domainName;
272         return namingDescr.createObjectName((Object JavaDoc[])location);
273     }
274 //********************************************************************************************************************
275
public static String JavaDoc[] getChildNamesList(ConfigBean[] beans) throws Exception JavaDoc
276     {
277         String JavaDoc[] names = new String JavaDoc[beans.length];
278         for(int i=0; i<beans.length; i++)
279         {
280             String JavaDoc xpath = beans[i].getAbsoluteXPath("");
281             names[i] = MBeanMetaHelper.getMultipleElementKeyValue(xpath);
282         }
283         return names;
284     }
285 //********************************************************************************************************************
286

287     protected static void debug(String JavaDoc s) {
288         //TODO: change this to app server logging
289
System.out.println(s);
290     }
291     protected static void info(String JavaDoc s) {
292         //TODO: change this to app server logging
293
System.out.println(s);
294     }
295     protected static void error(String JavaDoc s) {
296         //TODO: change this to app server logging
297
System.out.println(s);
298     }
299
300     /****************************************************************************************************************
301      * Gets MBean's child element.
302      * @param childName the MBean's child element name.
303      * @return The value of the property retrieved.
304      * @throws MBeanException exception
305      * @throws AttributeNotFoundException exception
306      */

307     public ConfigBean getChildElementByName(String JavaDoc methodName, String JavaDoc name) throws MBeanException,AttributeNotFoundException
308     {
309
310         Class JavaDoc cl = m_baseConfigBean.getClass();
311         ConfigBean bean;
312         try
313         {
314            Method JavaDoc method = cl.getDeclaredMethod(methodName, new Class JavaDoc[]{Class.forName("java.lang.String")});
315            return (ConfigBean)method.invoke(m_baseConfigBean, new Object JavaDoc[]{name});
316         }
317         catch (Exception JavaDoc e)
318         {
319             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.getattribute.undefined_childelement_in_base_element"+ name );
320             throw new MBeanException(new MBeanConfigException( msg ));
321         }
322     }
323
324     /****************************************************************************************************************
325      * Gets MBean's property value.
326      * @param externalName the MBean's property name.
327      * @return The value of the property retrieved.
328      * @throws MBeanException exception
329      * @throws AttributeNotFoundException exception
330      */

331     public Object JavaDoc getPropertyElementValue(String JavaDoc propertyName) throws MBeanException,AttributeNotFoundException
332     {
333
334         Class JavaDoc cl = m_baseConfigBean.getClass();
335         ElementProperty prop;
336         try
337         {
338            Method JavaDoc method = cl.getDeclaredMethod("getElementPropertyByName", new Class JavaDoc[]{Class.forName("java.lang.String")});
339            prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object JavaDoc[]{propertyName});
340         }
341         catch (Exception JavaDoc e)
342         {
343             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.getattribute.undefined_properties_in_base_element"+ propertyName );
344             throw new MBeanException(new MBeanConfigException( msg ));
345         }
346         if(prop==null) {
347             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.getattribute_properties_not_found_in_base_element"+ propertyName );
348             throw new MBeanException(new MBeanConfigException( msg ));
349         }
350         return prop.getValue();
351     }
352     /****************************************************************************************************************
353      * Sets MBean's property value.
354      * @param attr The identification of the property to be set and the value it is to be set to.
355      * @throws MBeanException exception
356      * @throws AttributeNotFoundException exception
357      */

358     public void setPropertyElementValue(Attribute attr, boolean bAllowsEmptyValue) throws MBeanException,AttributeNotFoundException
359     {
360         String JavaDoc propertyName = attr.getName();
361         String JavaDoc value = (String JavaDoc)attr.getValue();
362         
363         Class JavaDoc cl = m_baseConfigBean.getClass();
364         ElementProperty prop;
365         try
366         {
367            Method JavaDoc method = cl.getDeclaredMethod("getElementPropertyByName", new Class JavaDoc[]{Class.forName("java.lang.String")});
368            prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object JavaDoc[]{propertyName});
369         }
370         catch (Exception JavaDoc e)
371         {
372             String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.setattribute_undefined_properties_in_base_element"+ propertyName );
373             throw new MBeanException(new MBeanConfigException( msg ));
374         }
375         if(prop==null && value!=null && (bAllowsEmptyValue || !value.equals("")))
376         {
377             prop = new ElementProperty();
378             prop.setName(propertyName);
379             prop.setValue(value);
380             try
381             {
382                 Method JavaDoc method = cl.getDeclaredMethod("addElementProperty", new Class JavaDoc[]{prop.getClass()});
383                 method.invoke(m_baseConfigBean, new Object JavaDoc[]{prop});
384             }
385             catch (Exception JavaDoc e)
386             {
387                 String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.setproperty_invoke_error"+propertyName );
388                 throw new MBeanException(new MBeanConfigException( msg ));
389             }
390         }
391         else
392         {
393             if(value==null || (!bAllowsEmptyValue && value.equals("")))
394             {
395                 try
396                 {
397                     Method JavaDoc method = cl.getDeclaredMethod("removeElementProperty", new Class JavaDoc[]{prop.getClass()});
398                     method.invoke(m_baseConfigBean, new Object JavaDoc[]{prop});
399                 }
400                 catch (Exception JavaDoc e)
401                 {
402                     String JavaDoc msg = /*localStrings.getString*/( "admin.server.core.mbean.config.setproperty_could_not_remove_propery"+ propertyName );
403                     throw new MBeanException(new MBeanConfigException( msg ));
404                 }
405             }
406             else
407                 prop.setValue(value);
408         }
409         
410 /* try
411         {
412             m_configContext.flush();
413         }
414         catch (ConfigException e)
415         {
416             throw new MBeanException(new MBeanConfigException(e.getMessage()));
417         }
418 */

419     }
420     // THE FOLLOWING METHOD IS LUDO's CODE
421
/**
422      * Convert a DTD name into a bean name:
423      *
424      * Any - or _ character is removed. The letter following - and _
425      * is changed to be upper-case.
426      * If the user mixes upper-case and lower-case, the case is not
427      * changed.
428      * If the Word is entirely in upper-case, the word is changed to
429      * lower-case (except the characters following - and _)
430      * The first letter is always upper-case.
431      */

432     public static String JavaDoc convertTagName(String JavaDoc name)
433     {
434         CharacterIterator JavaDoc ci;
435         StringBuffer JavaDoc n = new StringBuffer JavaDoc();
436         boolean up = true;
437         boolean keepCase = false;
438         char c;
439         
440         ci = new StringCharacterIterator JavaDoc(name);
441         c = ci.first();
442         
443         // If everything is uppercase, we'll lowercase the name.
444
while (c != CharacterIterator.DONE)
445         {
446             if (Character.isLowerCase(c))
447             {
448                 keepCase = true;
449                 break;
450             }
451             c = ci.next();
452         }
453         
454         c = ci.first();
455         while (c != CharacterIterator.DONE)
456         {
457             if (c == '-' || c == '_')
458                 up = true;
459             else
460             {
461                 if (up)
462                     c = Character.toUpperCase(c);
463                 else
464                     if (!keepCase)
465                         c = Character.toLowerCase(c);
466                 n.append(c);
467                 up = false;
468             }
469             c = ci.next();
470         }
471         return n.toString();
472     } // end of convertTagName()
473

474 }
475
Popular Tags