KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > config > ConfigMBeanBase


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.server.core.mbean.config;
25
26 import java.text.CharacterIterator JavaDoc;
27 import java.text.StringCharacterIterator JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 //JMX imports
37
import javax.management.*;
38
39 //Config imports
40
import com.sun.enterprise.instance.InstanceEnvironment;
41 import com.sun.enterprise.config.ConfigException;
42 //import com.sun.enterprise.config.ConfigNode;
43
import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
44 import com.sun.enterprise.config.serverbeans.ServerTags;
45 import com.sun.enterprise.config.ConfigContext;
46 import com.sun.enterprise.config.ConfigFactory;
47 import com.sun.enterprise.config.ConfigBean;
48 import com.sun.enterprise.config.ConfigBeansFactory;
49 import com.sun.enterprise.config.serverbeans.ElementProperty;
50
51 //Admin imports
52
import com.sun.enterprise.admin.AdminContext;
53 import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeanNamingInfo;
54
55 import com.sun.enterprise.admin.server.core.mbean.meta.MBeanEasyConfig;
56 import com.sun.enterprise.admin.common.constant.AdminConstants;
57 import com.sun.enterprise.admin.common.constant.ConfigAttributeName;
58 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
59 //import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeanNamingInfo;
60

61 //i18n import
62
import com.sun.enterprise.util.i18n.StringManager;
63
64
65 /****************************************************************************************************************
66  * This base admin Dynamic MBean class provides access to ConfigNode's elements attributes according to attribute
67  * descriptions provided by child class.
68  */

69 public class ConfigMBeanBase extends AdminBase //implements DynamicMBean
70
{
71     public static final Logger JavaDoc sLogger = Logger.getLogger(AdminConstants.kLoggerName);
72     private final static String JavaDoc MSG_BASE_GET_ATTRIBUTE = "mbean.config.base_get_attribute";
73     private final static String JavaDoc MSG_BASE_SET_ATTRIBUTE = "mbean.config.base_set_attribute";
74     private final static String JavaDoc MSG_BASE_GOT_ATTRIBUTE = "mbean.config.base_got_attribute";
75     private final static String JavaDoc MSG_BASE_GET_PROPERTY = "mbean.config.base_get_property";
76     private final static String JavaDoc MSG_BASE_SET_PROPERTY = "mbean.config.base_set_property";
77     private final static String JavaDoc MSG_BASE_GET_DEF_ATTR_VALUE = "mbean.config.get_def_attr_value";
78     private final static String JavaDoc MSG_GET_CONFBEANBYXPATH = "mbean.config.get_confbeanbyxpath";
79     private final static String JavaDoc MSG_LOG_CONF_CTX = "mbean.config.log_config_id";
80     public static final char ATTRIBUTE_CHAR = '@'; //attribute name prefix
81
public static final char ALLOWS_EMPTY_CHAR = '@'; //"allows empty value" prefix symbol (can be only after ATTRIBUTE_CHAR)
82
public static final String JavaDoc ATTRIBUTE = ""+ATTRIBUTE_CHAR; //attribute name prefix
83
public static final String JavaDoc E_ATTRIBUTE = "" + ATTRIBUTE_CHAR + ALLOWS_EMPTY_CHAR; //"allows empty value" attribute name prefix
84
public static final String JavaDoc PROPERTY = ServerTags.ELEMENT_PROPERTY+ ServerXPathHelper.XPATH_SEPARATOR+ATTRIBUTE; //property name prefix
85
public static final String JavaDoc E_PROPERTY = ServerTags.ELEMENT_PROPERTY+ ServerXPathHelper.XPATH_SEPARATOR+E_ATTRIBUTE; //property name prefix
86
public static final String JavaDoc ELEMENT_CONTENT = ""; //element content prefix
87
public static final String JavaDoc PSEUDO_ATTR_DESCRIPTION = ServerTags.DESCRIPTION;
88
89     private AdminContext m_AdminContext;
90     private ConfigContext m_configContext;
91     private String JavaDoc m_BasePath; //target root-node
92
private Hashtable JavaDoc m_Attrs;
93     private MBeanInfo m_MBeanInfo;
94     private String JavaDoc m_ServerInstanceName;
95     private ConfigMBeanNamingInfo m_MBeanNamingInfo;
96     
97     // i18n StringManager
98
private static StringManager localStrings =
99         StringManager.getManager( ConfigMBeanBase.class );
100
101   
102     /****************************************************************************************************************
103      * Binds MBean with Config Bean
104      * @param instanceName instance name.
105      * @throws MbeanConfigException exception if the param parsing is not successful
106      */

107     public void initialize(ConfigMBeanNamingInfo namingInfo) throws MBeanConfigException
108     {
109         String JavaDoc instanceName = namingInfo.getServerInstanceName();
110         ConfigContext configContext;
111         if (m_AdminContext != null) {
112             configContext = m_AdminContext.getAdminConfigContext();
113         } else {
114             try
115             {
116                 InstanceEnvironment instanceEnvironment = new InstanceEnvironment(instanceName);
117                 //String fileUrl = instanceEnvironment.getConfigFilePath();
118
/*Everything should be set in the backup file*/
119                 String JavaDoc fileUrl = instanceEnvironment.getConfigFilePath();
120                 configContext = ConfigFactory.createConfigContext(fileUrl);
121             }
122             catch(ConfigException e)
123             {
124                 //e.printStackTrace();
125
String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.error_locating_server_node", e.getMessage() );
126                 throw new MBeanConfigException( msg );
127             }
128         }
129         
130         sLogger.log(Level.FINEST, MSG_LOG_CONF_CTX, new Object JavaDoc[] {
131                 this.getClass().getName(), new Long JavaDoc(configContext.hashCode())});
132         m_ServerInstanceName = instanceName;
133         m_configContext = configContext;
134         m_MBeanNamingInfo = namingInfo;
135         m_BasePath = namingInfo.getXPath();
136     }
137     /*
138      Binds MBean with Config Bean
139      **/

140     public void initialize(String JavaDoc mbeanType, String JavaDoc[] locations) throws MBeanConfigException
141     {
142         ConfigMBeanNamingInfo namingInfo;
143         try
144         {
145             namingInfo = new ConfigMBeanNamingInfo(mbeanType, locations);
146         }
147         catch (MBeanConfigException mce)
148         {
149             throw mce;
150         }
151         catch (Exception JavaDoc e)
152         {
153             throw new MBeanConfigException(e.getMessage());
154         }
155         
156         initialize(namingInfo);
157     }
158
159     /*
160      Binds MBean with Config Bean
161      **/

162     public void initialize(String JavaDoc dottedName) throws MBeanConfigException
163     {
164         ConfigMBeanNamingInfo namingInfo;
165         try
166         {
167             namingInfo = new ConfigMBeanNamingInfo(dottedName);
168         }
169         catch (MBeanConfigException mce)
170         {
171             throw mce;
172         }
173         catch (Exception JavaDoc e)
174         {
175             throw new MBeanConfigException(e.getMessage());
176         }
177         
178         initialize(namingInfo);
179     }
180
181     /*
182      Binds MBean with Config Bean
183      **/

184     public void initialize(ObjectName objectName) throws MBeanConfigException
185     {
186         ConfigMBeanNamingInfo namingInfo;
187         try
188         {
189             namingInfo = new ConfigMBeanNamingInfo(objectName);
190         }
191         catch (MBeanConfigException mce)
192         {
193             throw mce;
194         }
195         catch (Exception JavaDoc e)
196         {
197             throw new MBeanConfigException(e.getMessage());
198         }
199         
200         initialize(namingInfo);
201     }
202
203     
204     
205     /****************************************************************************************************************
206      * Sets Config MBean description data (this method usually is calling from the child class constructor)
207      * @param attrsMapList array of attribute mapping descriptions, each subarray represents one attribute by string pairs:
208      * <br> <b>{ &ltexternal name>, &ltpath to value> }</b> for each attribute, where
209      * <ul> <b>&ltexternal name></b> defines name identifying value in <code>getAttribute()/SetAttribute()</code> methods;
210      * <br> <b>&ltpath to value></b> is relative path to value from the base node in XPath notation;
211      * </ul> Example (attribute value):
212      * <br> ORBListener port attribute in orblistener node can be described as
213      * <ul> <b>{ "port", "@port" }</b>
214      * </ul>
215      * If entire description for attribute is missing, then <code>getAttribute()/SetAttribute()</code> treats this situation the same way as
216      * it was described as <b>{"@&ltattrName>]","&ltattrName>"}</b>.
217      * </ul> Examples (element value):
218      * <ul> <b>{ "about", "description[@type='thisInstance')]" }</b> //this example for value of element with tag 'description' ans attribute 'type' equales to 'myInstance'
219      * <br> <b>{ "about", "description" }</b> //this example for value of <b>unique</b> element with tag 'description'
220      * </ul>
221      * @param attrDescriptions String array of MBean attribute descriptions in MBeanEasyConfig format.
222      * @param operDescriptions String array of MBean operations descriptions in MBeanEasyConfig format.
223      * @throws MBeanConfigException exception if the param parsing is not successful
224      */

225     public void setDescriptions(String JavaDoc attrsMapList[][], String JavaDoc[] attrDescriptions, String JavaDoc[] operDescriptions) throws MBeanConfigException
226     {
227         m_Attrs = createAttrsDescriptors(attrsMapList);
228         MBeanEasyConfig easyConfig = new MBeanEasyConfig(getClass(), attrDescriptions, operDescriptions, null);
229         m_MBeanInfo = easyConfig.getMBeanInfo();
230     }
231     
232     public void setAdminContext(AdminContext adminContext) {
233         m_AdminContext = adminContext;
234     }
235
236     public AdminContext getAdminContext() {
237         return m_AdminContext;
238     }
239
240     /****************************************************************************************************************
241      */

242     public ConfigContext getConfigContext()
243     {
244         return m_configContext;
245     }
246     
247     /****************************************************************************************************************
248      */

249     public String JavaDoc getServerInstanceName()
250     {
251         return m_ServerInstanceName;
252     }
253     
254     /****************************************************************************************************************
255      */

256     public ConfigMBeanNamingInfo getConfigMBeanNamingInfo()
257     {
258         return m_MBeanNamingInfo;
259     }
260     
261     /****************************************************************************************************************
262    Get path to base node; all attributes pathes are defined relatively this node.
263    @return XPath style path to the ConfigNode object of node which is used as base for locationg of the attributes.
264      */

265     public String JavaDoc getBasePath()
266     {
267         return m_BasePath;
268     }
269     
270     /****************************************************************************************************************
271    Get base node Config Bean;
272    @return ConfigBean related to the ConfigNode object of node which is used as base for locationg of the attributes.
273      */

274     public ConfigBean getBaseConfigBean()
275     {
276         try
277         {
278            return getConfigBeanByXPath(m_BasePath);
279         }
280         catch (Exception JavaDoc e)
281         {
282            return null;
283         }
284     }
285     
286     /****************************************************************************************************************
287       Get <code>MBeanInfo</code> MBean description object (required dynamic MBean method).
288       @return <code>MBeanInfo</code> description object for configurable MBean;
289      */

290     public MBeanInfo getMBeanInfo()
291     {
292         return m_MBeanInfo;
293     }
294     /****************************************************************************************************************
295       Get <code>ConfigBean</code> object which contains correspondent attribute.
296       @param externalName external name of the attribute
297       @return <code>ConfigBean</code> object which contains correspondent attribute
298      */

299     public ConfigBean getConfigBean(String JavaDoc externalName)
300     {
301         //this method can be overriten in child class (temporary until Config static method getBeanByXPath will be ready)
302
AttrDescriptor descr = getDescriptor(externalName);
303         try
304         {
305             return descr.getConfigBean();
306         }
307         catch (Exception JavaDoc e)
308         {
309             return null;
310         }
311     }
312     
313     /****************************************************************************************************************
314       Get <code>ConfigBean</code> object which binded to ConfigNode with given XPath.
315       @param xPath XPath to ConfigNode
316       @return <code>ConfigBean</code> object which linked to ConfigNode with given XPath.
317      */

318     public ConfigBean getConfigBeanByXPath(String JavaDoc xPath) throws ConfigException
319     {
320         sLogger.log(Level.FINEST, MSG_GET_CONFBEANBYXPATH, xPath);
321         return ConfigBeansFactory.getConfigBeanByXPath(m_configContext, xPath);
322     }
323     
324     /****************************************************************************************************************
325      * Gets MBean's attribute value.
326      * @param externalName the MBean's attribute name.
327      * @return The value of the attribute retrieved.
328      * @throws MBeanException exception
329      * @throws AttributeNotFoundException exception
330      */

331     public Object JavaDoc getAttribute(String JavaDoc externalName) throws MBeanException,AttributeNotFoundException
332     {
333         sLogger.log(Level.FINEST, MSG_BASE_GET_ATTRIBUTE, externalName);
334         MBeanAttributeInfo ai = getAttrInfo(externalName);
335         boolean isProperty = externalName.startsWith(ConfigAttributeName.PROPERTY_NAME_PREFIX);
336
337
338         if(ai==null && !isProperty)
339         {
340             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.attribute_not_defined", externalName );
341             throw new AttributeNotFoundException( msg );
342         }
343         if(ai!=null && !ai.isReadable())
344         {
345             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.attribute_not_readable", externalName );
346             throw new AttributeNotFoundException( msg );
347         }
348
349         
350         
351         try
352         {
353         if(isProperty)
354         {
355             // get property value
356
return getPropertyElementValue(externalName.substring(ConfigAttributeName.PROPERTY_NAME_PREFIX.length()));
357         }
358         else
359         {
360             AttrDescriptor descr = getDescriptor(externalName);
361             ConfigBean bean = getConfigBean(externalName);
362             String JavaDoc value = null;
363             if(descr.isElement())
364             {
365                 //it looks that now we have no Elements with values
366
//value = descr.getNode().getContent();
367
String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.getattribute_not_implemented_for_xml" );
368                 throw new MBeanException(new MBeanConfigException( msg ));
369             }
370             else
371             {
372                 value = bean.getRawAttributeValue(descr.getAttributeName());
373
374                 //value = descr.getNode().getRawAttributeValue(descr.getAttributeName());
375
}
376             sLogger.log(Level.FINEST, MSG_BASE_GOT_ATTRIBUTE, new Object JavaDoc[]{externalName,value, MBeanEasyConfig.convertStringValueToProperType(value, ai.getType())});
377             return MBeanEasyConfig.convertStringValueToProperType(value, ai.getType());
378         }
379         }
380         catch (MBeanConfigException e)
381         {
382             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.getattribute_attribute_exception", externalName, e.getMessage() );
383             throw new MBeanException(new MBeanConfigException( msg ));
384         }
385
386  }
387     
388     /****************************************************************************************************************
389      * Gets MBean's property value.
390      * @param externalName the MBean's property name.
391      * @return The value of the property retrieved.
392      * @throws MBeanException exception
393      * @throws AttributeNotFoundException exception
394      */

395     public Object JavaDoc getPropertyElementValue(String JavaDoc propertyName) throws MBeanException,AttributeNotFoundException
396     {
397         sLogger.log(Level.FINEST, MSG_BASE_GET_PROPERTY, propertyName);
398
399         ConfigBean baseBean = getBaseConfigBean();
400         Class JavaDoc cl = baseBean.getClass();
401         ElementProperty prop;
402         try
403         {
404            Method JavaDoc method = cl.getDeclaredMethod("getElementPropertyByName", new Class JavaDoc[]{Class.forName("java.lang.String")});
405            prop = (ElementProperty)method.invoke(baseBean, new Object JavaDoc[]{propertyName});
406         }
407         catch (Exception JavaDoc e)
408         {
409             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.getattribute.undefined_properties_in_base_element", propertyName );
410             throw new MBeanException(new MBeanConfigException( msg ));
411         }
412         if(prop==null) {
413             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.getattribute_properties_not_found_in_base_element", propertyName );
414             throw new MBeanException(new MBeanConfigException( msg ));
415         }
416         return prop.getValue();
417     }
418     /****************************************************************************************************************
419      * Sets MBean's property value.
420      * @param attr The identification of the property to be set and the value it is to be set to.
421      * @throws MBeanException exception
422      * @throws AttributeNotFoundException exception
423      */

424     public void setPropertyElementValue(Attribute attr, boolean bAllowsEmptyValue) throws MBeanException,AttributeNotFoundException
425     {
426         String JavaDoc propertyName = attr.getName();
427         String JavaDoc value = (String JavaDoc)attr.getValue();
428         sLogger.log(Level.FINEST, MSG_BASE_SET_PROPERTY, new Object JavaDoc[]{propertyName, value});
429         
430         ConfigBean baseBean = getBaseConfigBean();
431
432         Class JavaDoc cl = baseBean.getClass();
433         ElementProperty prop;
434         try
435         {
436            Method JavaDoc method = cl.getDeclaredMethod("getElementPropertyByName", new Class JavaDoc[]{Class.forName("java.lang.String")});
437            prop = (ElementProperty)method.invoke(baseBean, new Object JavaDoc[]{propertyName});
438         }
439         catch (Exception JavaDoc e)
440         {
441             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.setattribute_undefined_properties_in_base_element", propertyName );
442             throw new MBeanException(new MBeanConfigException( msg ));
443         }
444         if(prop==null && value!=null && (bAllowsEmptyValue || !value.equals("")))
445         {
446             prop = new ElementProperty();
447             prop.setName(propertyName);
448             prop.setValue(value);
449             try
450             {
451                 Method JavaDoc method = cl.getDeclaredMethod("addElementProperty", new Class JavaDoc[]{prop.getClass()});
452                 method.invoke(baseBean, new Object JavaDoc[]{prop});
453             }
454             catch (Exception JavaDoc e)
455             {
456                 String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.setproperty_invoke_error", propertyName );
457                 throw new MBeanException(new MBeanConfigException( msg ));
458             }
459         }
460         else
461         {
462             if(value==null || (!bAllowsEmptyValue && value.equals("")))
463             {
464                 try
465                 {
466                     Method JavaDoc method = cl.getDeclaredMethod("removeElementProperty", new Class JavaDoc[]{prop.getClass()});
467                     method.invoke(baseBean, new Object JavaDoc[]{prop});
468                 }
469                 catch (Exception JavaDoc e)
470                 {
471                     String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.setproperty_could_not_remove_propery", propertyName );
472                     throw new MBeanException(new MBeanConfigException( msg ));
473                 }
474             }
475             else
476                 prop.setValue(value);
477         }
478         
479         try
480         {
481             m_configContext.flush();
482         }
483         catch (ConfigException e)
484         {
485             throw new MBeanException(new MBeanConfigException(e.getMessage()));
486         }
487     }
488     
489
490     /****************************************************************************************************************
491      * Gets MBean's attribute default value or null (if def value is not defined).
492      * @param externalName the MBean's attribute name.
493      * @return The default value of the attribute retrieved null, if def value is not defined.
494      * @throws MBeanException exception
495      * @throws AttributeNotFoundException exception
496      */

497     public Object JavaDoc getDefaultAttributeValue(String JavaDoc externalName) throws AttributeNotFoundException,MBeanConfigException
498     {
499         sLogger.log(Level.FINEST, MSG_BASE_GET_DEF_ATTR_VALUE, externalName);
500         MBeanAttributeInfo ai = getAttrInfo(externalName);
501         if(ai==null) {
502             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.getdefaultattribute_undefined_attribute", externalName );
503             throw new AttributeNotFoundException( msg );
504         }
505 // try
506
{
507             AttrDescriptor descr = getDescriptor(externalName);
508             String JavaDoc value = descr.getDefaultValue();
509             return MBeanEasyConfig.convertStringValueToProperType(value, ai.getType());
510         }
511 /* catch (ConfigException e)
512         {
513             String msg = localStrings.getString( "admin.server.core.mbean.config.getdefaultattributevalue_exception_for_externalname_basexpath", externalName, m_BasePath, e.getMessage() );
514             throw new MBeanException(new ConfigException( msg ));
515         }
516 */

517     }
518
519     /****************************************************************************************************************
520      * Sets MBean's attribute value.
521      * @param attr The identification of the attribute to be set and the value it is to be set to.
522      * @throws MBeanException exception
523      * @throws AttributeNotFoundException exception
524      */

525     public void setAttribute(Attribute attr) throws MBeanException,AttributeNotFoundException
526     {
527         String JavaDoc externalName = attr.getName();
528         Object JavaDoc value = attr.getValue();
529         sLogger.log(Level.FINEST, MSG_BASE_SET_ATTRIBUTE, new Object JavaDoc[]{externalName, value});
530         MBeanAttributeInfo ai = getAttrInfo(externalName);
531         boolean isProperty = externalName.startsWith(ConfigAttributeName.PROPERTY_NAME_PREFIX);
532         if(ai==null && !isProperty)
533         {
534             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.setattribute_undefined_attribute", externalName );
535             throw new AttributeNotFoundException( msg );
536         }
537         if(ai!=null && !ai.isWritable())
538         {
539             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.setattribute_attribute_not_writable", externalName );
540             throw new MBeanException(new MBeanConfigException( msg ));
541         }
542         
543         try
544         {
545             if(isProperty)
546             {
547                 boolean bAllowsEmptyValue = true;
548                 if(ai!=null)
549                 {
550                     AttrDescriptor descr = getDescriptor(externalName);
551                     if(descr!=null)
552                     {
553                        bAllowsEmptyValue = descr.isEmptyValueAllowed();
554                     }
555                 }
556                 // set property value
557
setPropertyElementValue(new Attribute(externalName.substring(ConfigAttributeName.PROPERTY_NAME_PREFIX.length()), value), bAllowsEmptyValue);
558                 return;
559             }
560             else
561             { //normal attribute
562
// check type (now only for exception)
563
// left check for Verifyer now (bug #4725686)
564
// MBeanEasyConfig.convertStringValueToProperType(value.toString(), ai.getType());
565

566                 AttrDescriptor descr = getDescriptor(externalName);
567                 ConfigBean bean = getConfigBean(externalName);
568                 if(descr.isElement())
569                 {
570                     //it looks that now we have no Elements with values
571
//bean.set???Value(descr.getAttributeName(), value.toString());
572
String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.getattribute_not_implemented_for_xml" );
573                     throw new MBeanException(new MBeanConfigException( msg ));
574
575     // descr.getNode().setContent(value.toString());
576
// m_configContext.flush();
577
}
578                 else
579                 {
580
581                     //descr.getNode().setAttribute(descr.getAttributeName(), value.toString());
582
if(value==null || (value.equals("") && !descr.isEmptyValueAllowed()) )
583                        bean.setAttributeValue(descr.getAttributeName(), null);
584                     else
585                        bean.setAttributeValue(descr.getAttributeName(), value.toString());
586                     m_configContext.flush();
587                 }
588             }
589             
590         }
591 // catch (MBeanConfigException mce)
592
// {
593
// String msg = localStrings.getString( "admin.server.core.mbean.config.setattribute_attribute_exception", externalName, mce.getMessage() );
594
// throw new MBeanException(new MBeanConfigException( msg ));
595
// }
596
catch (ConfigException e)
597         {
598             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.setAttribute_exception_for_externalname_basexpath", externalName, m_BasePath, e.getMessage() );
599             throw new MBeanException(new MBeanConfigException( msg ));
600         }
601     }
602     
603     /****************************************************************************************************************
604      * Gets MBean's attribute values.
605      * @param attributeNames A list of the attributes names to be retrieved.
606      * @return The list of attributes retrieved.
607      */

608     public AttributeList getAttributes(String JavaDoc[] attributeNames)
609     {
610         AttributeList attrs = new AttributeList();
611         for(int i=0; i<attributeNames.length; i++)
612         {
613 // try
614
{
615                 if(attributeNames[i].length()==0)
616                 {
617                     String JavaDoc[] names = getAllAttributeNames();
618                     if(names!=null)
619                         attrs.addAll(getAttributes(names));
620                 }
621                 else
622                 {
623                     if(attributeNames[i].equals(ConfigAttributeName.PROPERTY_NAME_PREFIX))
624                     {
625                         String JavaDoc[] names = getAllPropertyNames(true);
626                         if(names!=null)
627                             attrs.addAll(getAttributes(names));
628                     }
629                     else
630                     {
631                         try
632                         {
633                             Object JavaDoc value = getAttribute(attributeNames[i]);
634                             attrs.add(new Attribute(attributeNames[i], value));
635                         }
636                         catch (MBeanException ce)
637                         {
638                             attrs.add(new Attribute(attributeNames[i], null));
639                         }
640                         catch (AttributeNotFoundException ce)
641                         {
642                             attrs.add(new Attribute(attributeNames[i], null));
643                         }
644                         catch (NullPointerException JavaDoc npe) //ConfigBean returns this exception by many reasons
645
{
646                             attrs.add(new Attribute(attributeNames[i], null));
647                         }
648                     }
649                 }
650             }
651 // catch (Throwable t)
652
// {
653
// }
654
}
655         return attrs;
656     }
657     
658     /****************************************************************************************************************
659      * Sets the values of several MBean's attributes.
660      * @param attrList A list of attributes: The identification of the attributes to be set and the values they are to be set to.
661      * @return The list of attributes that were set, with their new values.
662      */

663     public AttributeList setAttributes(AttributeList attrList)
664     {
665         AttributeList attrs = new AttributeList();
666         Iterator JavaDoc it = attrList.iterator();
667         while (it.hasNext())
668         {
669             try
670             {
671                 Attribute attribute = (Attribute) it.next();
672                 setAttribute(attribute);
673                 attrs.add(attribute);
674             }
675             catch (MBeanException mbe)
676             {
677             }
678             catch (AttributeNotFoundException anfe)
679             {
680             }
681             catch (NullPointerException JavaDoc npe)
682             {
683             }
684         }
685         return attrs;
686     }
687     /**
688     Every resource MBean should override this method to execute specific
689     operations on the MBean.
690      */

691     public Object JavaDoc invoke(String JavaDoc methodName, Object JavaDoc[] methodParams,
692     String JavaDoc[] methodSignature) throws MBeanException, ReflectionException
693     {
694         //return null;
695
/*
696          * New for 8.0
697         */

698         return ( super.invoke(methodName, methodParams, methodSignature) );
699     }
700     
701     //****************************************************************************************************************
702
private Hashtable JavaDoc createAttrsDescriptors(String JavaDoc[][] attrs) throws MBeanConfigException
703     {
704         Hashtable JavaDoc ht = new Hashtable JavaDoc();
705         if (attrs != null)
706         {
707             for(int i=0; i<attrs.length; i++)
708             {
709                 ht.put(attrs[i][0],
710                        new AttrDescriptor(attrs[i][1]));
711             }
712         }
713         return ht;
714     }
715     
716     //****************************************************************************************************************
717
private AttrDescriptor getDescriptor(String JavaDoc externalName)
718     {
719         return (AttrDescriptor)m_Attrs.get(externalName);
720     }
721     
722     // THE FOLLOWING METHOD IS LUDO's CODE
723
/**
724      * Convert a DTD name into a bean name:
725      *
726      * Any - or _ character is removed. The letter following - and _
727      * is changed to be upper-case.
728      * If the user mixes upper-case and lower-case, the case is not
729      * changed.
730      * If the Word is entirely in upper-case, the word is changed to
731      * lower-case (except the characters following - and _)
732      * The first letter is always upper-case.
733      */

734     public static String JavaDoc convertName(String JavaDoc name)
735     {
736         CharacterIterator JavaDoc ci;
737         StringBuffer JavaDoc n = new StringBuffer JavaDoc();
738         boolean up = true;
739         boolean keepCase = false;
740         char c;
741         
742         ci = new StringCharacterIterator JavaDoc(name);
743         c = ci.first();
744         
745         // If everything is uppercase, we'll lowercase the name.
746
while (c != CharacterIterator.DONE)
747         {
748             if (Character.isLowerCase(c))
749             {
750                 keepCase = true;
751                 break;
752             }
753             c = ci.next();
754         }
755         
756         c = ci.first();
757         while (c != CharacterIterator.DONE)
758         {
759             if (c == '-' || c == '_')
760                 up = true;
761             else
762             {
763                 if (up)
764                     c = Character.toUpperCase(c);
765                 else
766                     if (!keepCase)
767                         c = Character.toLowerCase(c);
768                 n.append(c);
769                 up = false;
770             }
771             c = ci.next();
772         }
773         return n.toString();
774     }
775     
776     //****************************************************************************************************
777
private MBeanAttributeInfo getAttrInfo(String JavaDoc attrName)
778     {
779         if(attrName==null || m_MBeanInfo==null)
780             return null;
781         
782         MBeanAttributeInfo[] ai = m_MBeanInfo.getAttributes();
783         if(ai!=null)
784             for(int i=0; i<ai.length; i++)
785             {
786                 String JavaDoc name = ai[i].getName();
787                 if(attrName.equals(ai[i].getName()))
788                     return ai[i];
789             }
790         return null;
791     }
792     
793     //****************************************************************************************************
794
private String JavaDoc[] getAllAttributeNames()
795     {
796         if(m_Attrs==null )
797             return null;
798         Enumeration JavaDoc keys = m_Attrs.keys();
799         ArrayList JavaDoc list = new ArrayList JavaDoc();
800         while(keys.hasMoreElements())
801         {
802             list.add(keys.nextElement());
803         }
804         return (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
805     }
806
807     //****************************************************************************************************
808
private String JavaDoc[] getAllPropertyNames(boolean bAddPropertyPrefix)
809     {
810
811         ConfigBean baseBean = getBaseConfigBean();
812         Class JavaDoc cl = baseBean.getClass();
813         ElementProperty[] props;
814         try
815         {
816            Method JavaDoc method = cl.getDeclaredMethod("getElementProperty", null);
817            props = (ElementProperty[])method.invoke(baseBean, null);
818            String JavaDoc[] names = new String JavaDoc[props.length];
819            for(int i=0; i<props.length; i++)
820            {
821                if(bAddPropertyPrefix)
822                    names[i] = ConfigAttributeName.PROPERTY_NAME_PREFIX+props[i].getName();
823                else
824                    names[i] = props[i].getName();
825            }
826            return names;
827         }
828         catch (java.lang.NoSuchMethodException JavaDoc nsme)
829         {
830             return null;
831         }
832         catch (java.lang.IllegalAccessException JavaDoc iae)
833         {
834             return null;
835         }
836         catch (java.lang.reflect.InvocationTargetException JavaDoc ite)
837         {
838             return null;
839         }
840     }
841
842     //****************************************************************************************************
843
/**
844      * This method allows to append another bean attributes (usually child ones) to base attributes;
845      * so for MBean user they all will be as base
846      */

847     static Object JavaDoc[] MergeAttributesWithAnotherMbean(
848              String JavaDoc[][] maplist1, String JavaDoc[] attributes1,
849              String JavaDoc[][] maplist2, String JavaDoc[] attributes2,
850              String JavaDoc relativeXPath2, String JavaDoc attrNamesPrefix2)
851     {
852         int size = 0;
853         if(maplist1!=null)
854             size += maplist1.length;
855         if(maplist2!=null)
856             size += maplist2.length;
857         String JavaDoc[][] new_maplist = new String JavaDoc[size][];
858         String JavaDoc[] new_attributes = new String JavaDoc[size];
859         String JavaDoc[] mapelem;
860         int i = 0;
861         if(maplist1!=null)
862             for(i=0; i<maplist1.length; i++)
863             {
864                 new_maplist[i] = (String JavaDoc[])maplist1[i].clone();
865                 new_attributes[i] = attributes1[i];
866             }
867         if(maplist2!=null)
868             for(int j=0; j<maplist2.length; j++)
869             {
870                 mapelem = (String JavaDoc[])maplist2[j].clone();
871                 new_attributes[i+j] = attributes2[j];
872                 if(attrNamesPrefix2!=null)
873                 {
874                    mapelem[0] = attrNamesPrefix2 + mapelem[0];
875                    new_attributes[i+j] = attrNamesPrefix2 + attributes2[j].trim();
876                 }
877                 mapelem[1] = relativeXPath2 + "/" + mapelem[1];
878                 new_maplist[i+j] = mapelem;
879             }
880         return new Object JavaDoc[]{new_maplist, new_attributes};
881         
882     }
883
884     //****************************************************************************************************
885
private static Class JavaDoc getConfigBeanClass(String JavaDoc xPath)
886     {
887         // get ConfigBean classname from XPath
888
String JavaDoc beanName = ConfigBeansFactory.getConfigBeanNameByXPath(xPath);
889         //getting the class object
890
try
891         {
892             Class JavaDoc cl = Class.forName("com.sun.enterprise.config.serverbeans."+beanName);
893             return cl;
894         }
895         catch(Exception JavaDoc e)
896         {
897             return null;
898         }
899     }
900
901     /** Abstract method that subclasses have to implement. This is the way for
902      * invoke method to work, through reflection.
903      */

904     protected Class JavaDoc getImplementingClass() {
905         return ( this.getClass() );
906     }
907     
908     /** Reflection requires the implementing object. */
909     protected Object JavaDoc getImplementingMBean() {
910         return ( this );
911     }
912
913      //****************************************************************************************************************
914
private class AttrDescriptor
915     {
916         public String JavaDoc m_xPath = "";
917         public String JavaDoc m_attributeName;
918         public boolean m_bAllowsEmptyValue = false;
919         
920         //*******************************************************************************************************
921
public AttrDescriptor(String JavaDoc description) throws MBeanConfigException
922         {
923             try
924             {
925                 //first investigate: is it element value or attriibute value
926
int lastSlashIdx = description.lastIndexOf(ServerXPathHelper.XPATH_SEPARATOR);
927                 if(description.charAt(lastSlashIdx+1)==ATTRIBUTE_CHAR)
928                 { //element's attribute
929
if(description.charAt(lastSlashIdx+2)==ALLOWS_EMPTY_CHAR)
930                     {
931                         m_attributeName = description.substring(lastSlashIdx+3);
932                         m_bAllowsEmptyValue = true;
933                     }
934                     else
935                        m_attributeName = description.substring(lastSlashIdx+2);
936                     
937                     if(lastSlashIdx>0)
938                         m_xPath = description.substring(0,lastSlashIdx);
939                 }
940                 else
941                 { //element's content
942
m_attributeName = null; //not attribute
943
}
944             }
945             catch (Throwable JavaDoc e)
946             {
947                 String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.attrdescriptor_constructor_exception", description, e.getMessage() );
948                 throw new MBeanConfigException( msg );
949             }
950         }
951         
952         //*******************************************************************************************************
953
public String JavaDoc getAttributeName()
954         {
955             return m_attributeName;
956         }
957         
958         //*******************************************************************************************************
959
public String JavaDoc getXPath()
960         {
961             if(m_BasePath==null || m_BasePath.length()==0 ||
962                m_xPath.indexOf(ServerXPathHelper.XPATH_SEPARATOR)==0)
963                 return m_xPath;
964             if(m_xPath.length()==0)
965                return m_BasePath;
966             return m_BasePath + ServerXPathHelper.XPATH_SEPARATOR + m_xPath;
967         }
968         
969         //*******************************************************************************************************
970
public boolean isEmptyValueAllowed()
971         {
972             return m_bAllowsEmptyValue;
973         }
974         //*******************************************************************************************************
975
public ConfigBean getConfigBean() throws Exception JavaDoc
976         {
977             return getConfigBeanByXPath(getXPath());
978         }
979         
980         //****************************************************************************************************
981
public String JavaDoc getDefaultValue()
982         {
983             Class JavaDoc cl = getConfigBeanClass(getXPath());
984             //4. initiate class and bind with the node
985
try
986             {
987                 Method JavaDoc method = cl.getMethod("getDefaultAttributeValue", new Class JavaDoc[] {Class.forName("java.lang.String")} );
988                 return (String JavaDoc)method.invoke(null, new Object JavaDoc[] {m_attributeName});
989             }
990             catch(Exception JavaDoc e)
991             {
992                 return null;
993             }
994         }
995         //*******************************************************************************************************
996
/* public ConfigNode getNode() throws ConfigException
997         {
998             return m_configContext.exactLookup(getXPath());
999         }
1000*/

1001        //*******************************************************************************************************
1002
public boolean isElement()
1003        {
1004            return (m_attributeName==null);
1005        }
1006    }
1007    
1008}
1009
Popular Tags