KickJava   Java API By Example, From Geeks To Geeks.

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


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     PROPRIETARY/CONFIDENTIAL. Use of this product is subject
26     to license terms. Copyright ? 2001 Sun Microsystems, Inc.
27         All rights reserved.
28  
29     $Id: GenericConfigurator.java,v 1.3 2005/12/25 04:14:29 tcfujii Exp $
30  */

31
32 package com.sun.enterprise.admin.server.core.mbean.config;
33
34 import com.sun.enterprise.admin.util.HostAndPort;
35 import com.sun.enterprise.admin.util.Debug;
36 import com.sun.enterprise.admin.util.ArgChecker;
37 import com.sun.enterprise.admin.common.OperationProgress;
38 import com.sun.enterprise.admin.common.RequestID;
39 import com.sun.enterprise.admin.common.ObjectNames;
40 import com.sun.enterprise.admin.common.*;
41 import com.sun.enterprise.admin.common.MBeanServerFactory;
42 import com.sun.enterprise.admin.common.constant.ConfigAttributeName;
43 import com.sun.enterprise.admin.common.constant.AdminConstants;
44 import com.sun.enterprise.admin.server.core.mbean.meta.MBeanEasyConfig;
45 import com.sun.enterprise.admin.common.exception.MBeanConfigException;
46 import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeanNamingInfo;
47 import com.sun.enterprise.admin.monitor.CommandMapper;
48 import com.sun.enterprise.admin.monitor.MonitorCommand;
49 import com.sun.enterprise.admin.monitor.MonitorGetCommand;
50 import com.sun.enterprise.admin.monitor.MonitorSetCommand;
51 import com.sun.enterprise.admin.monitor.MonitorListCommand;
52 import com.sun.enterprise.admin.event.MonitoringEvent;
53 import com.sun.enterprise.admin.event.AdminEventMulticaster;
54 import com.sun.enterprise.admin.event.AdminEventResult;
55
56 import com.sun.enterprise.admin.server.core.mbean.config.naming.ConfigMBeansNaming;
57
58 //JMX imports
59
import javax.management.ObjectName JavaDoc;
60 import javax.management.MBeanServer JavaDoc;
61 import javax.management.DynamicMBean JavaDoc;
62 import javax.management.AttributeList JavaDoc;
63 import javax.management.MBeanInfo JavaDoc;
64 import javax.management.MBeanAttributeInfo JavaDoc;
65 import javax.management.Attribute JavaDoc;
66 import javax.management.AttributeNotFoundException JavaDoc;
67 import javax.management.MBeanException JavaDoc;
68 import javax.management.ReflectionException JavaDoc;
69 import javax.management.InvalidAttributeValueException JavaDoc;
70 import javax.management.MalformedObjectNameException JavaDoc;
71 import javax.management.InstanceNotFoundException JavaDoc;
72 import javax.management.IntrospectionException JavaDoc;
73 import javax.management.JMRuntimeException JavaDoc;
74 import javax.management.ServiceNotFoundException JavaDoc;
75
76 import java.util.Set JavaDoc;
77 import java.util.Hashtable JavaDoc;
78 import java.util.Iterator JavaDoc;
79 import java.util.ArrayList JavaDoc;
80 import java.util.logging.Level JavaDoc;
81 import java.util.logging.Logger JavaDoc;
82
83 //i18n import
84
import com.sun.enterprise.util.i18n.StringManager;
85
86 /**
87  * Transparent deliverer of get/set requests to proper MBeans, according
88  * to dot-notated attribute name, which includes MBean name synonym in prefix part.
89  * e.g name="server.ias1.orb.port" represents attribute "port" in
90  * MBean named "ias:instance-name=ias1,component=orb"
91  * <p>
92  * ObjectName of this MBean is: ias:type=configurator
93  */

94
95 public class GenericConfigurator extends AdminBase
96 {
97     public static final Logger JavaDoc sLogger = Logger.getLogger(AdminConstants.kLoggerName);
98     private final static String JavaDoc MSG_ATTR_NOT_FOUND = "mbean.config.attribute_not_found";
99     private final static String JavaDoc MSG_GETMBEANINFO_FAILED = "mbean.config.getmbeaninfo_failed";
100     private final static String JavaDoc MSG_GET_ATTRIBUTE = "mbean.config.get_attribute";
101     private final static String JavaDoc MSG_GET_ATTRIBUTE_DEFAULT = "mbean.config.get_attribute_default";
102     private final static String JavaDoc MSG_SET_ATTRIBUTE = "mbean.config.set_attribute";
103     private final static String JavaDoc MSG_LIST_NAMES_CONTS = "mbean.config.list_names_continuations";
104     // i18n StringManager
105
private static StringManager localStrings =
106         StringManager.getManager( GenericConfigurator.class );
107
108     MBeanServer JavaDoc mServer = null;
109     public GenericConfigurator() {
110     }
111     
112     /**
113      * Obtains the Dynamic MBean Attribute object(s) for given attribute pattern (in dotted notation).
114      *
115      * @param dottedName - The name of the attribute to be retrieved (in dotted notation)
116      * @throws MBeanException - Wraps a java.lang.Exception thrown by the MBean's getter.
117      * @throws ReflectionException - Wraps a java.lang.Exception thrown while trying to invoke the getter.
118      * @throws MalformedNameException - if MBean name incorrect
119      */

120     public AttributeList JavaDoc getGenericAttribute(String JavaDoc dottedName) throws
121     InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc,
122     MalformedNameException, MalformedObjectNameException JavaDoc, IntrospectionException JavaDoc,MBeanConfigException
123     {
124         sLogger.log(Level.FINEST, MSG_GET_ATTRIBUTE, dottedName);
125         if(mServer==null)
126             mServer=MBeanServerFactory.getMBeanServer();
127         dottedName = dottedName.trim();
128         String JavaDoc attrName = extractAttrNameFromDotted(dottedName);
129         int prefixLen = dottedName.length() - attrName.length();
130         String JavaDoc namePrefix = null;
131         if(prefixLen>0)
132             namePrefix = dottedName.substring(0, prefixLen);
133        
134         ArrayList JavaDoc objectNames = getMBeanNamesFromDottedPattern(dottedName);
135         
136         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
137         for(int i=0; i<objectNames.size(); i++)
138         {
139             
140             addAttributeToList(attrList, (ObjectName JavaDoc)objectNames.get(i), attrName);
141         }
142         if(namePrefix!=null && attrList!=null)
143         {
144             for(int i=0; i<attrList.size(); i++)
145             {
146                 Attribute JavaDoc attr = (Attribute JavaDoc)attrList.get(i);
147                 attrList.set(i, new Attribute JavaDoc(namePrefix+attr.getName(),attr.getValue()));
148             }
149         }
150         return attrList;
151     }
152     
153     /**
154      * Obtains the Default MBean Attributes values
155      *
156      * @param instanceName - The server instance name
157      * @param mbeanType - Type of mbean (from config naming)
158      * @param attrNames - Atring array of attribute names
159      * @throws MBeanConfigException - if mbean creation failed.
160      */

161     public AttributeList JavaDoc getGenericAttributeDefaultValues(String JavaDoc instanceName, String JavaDoc mbeanType, String JavaDoc[] attrNames) throws MBeanConfigException
162     {
163         sLogger.log(Level.FINEST, MSG_GET_ATTRIBUTE_DEFAULT, new String JavaDoc[]{instanceName, mbeanType});
164         ConfigMBeanNamingInfo info = new ConfigMBeanNamingInfo(mbeanType, new String JavaDoc[] {instanceName}, false);
165         ConfigMBeanBase mbean = info.constructConfigMBean();
166         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
167         for(int i=0; i<attrNames.length; i++)
168         {
169             Object JavaDoc defaultValue;
170             try
171             {
172                 defaultValue = mbean.getDefaultAttributeValue(attrNames[i]);
173             }
174             catch (AttributeNotFoundException JavaDoc e)
175             {
176                 defaultValue = null;
177             }
178             catch (MBeanConfigException e)
179             {
180                 defaultValue = null;
181             }
182             if(defaultValue!=null)
183                 attrList.add(new Attribute JavaDoc(attrNames[i], defaultValue));
184         }
185         return attrList;
186     }
187     
188
189     /**
190      */

191     public String JavaDoc[] listGenericDottedNameContinuiations(String JavaDoc dottedName) throws MalformedNameException
192     {
193         sLogger.log(Level.FINEST, MSG_LIST_NAMES_CONTS, dottedName);
194         dottedName = dottedName.trim();
195         String JavaDoc instanceName = extractInstanceNameFromDotted(dottedName);
196         return ConfigMBeansNaming.findNameContinuation(instanceName, dottedName);
197     }
198     
199     /**
200      * Obtains the Dynamic MBean Attribute objects for given attribute patterns (in dotted notation).
201      *
202      * @param dottedNames - The arry of attribute names(patterns) to be retrieved (in dotted notation)
203      * @throws MBeanException - Wraps a java.lang.Exception thrown by the MBean's getter.
204      * @throws ReflectionException - Wraps a java.lang.Exception thrown while trying to invoke the getter.
205      * @throws MalformedNameException - if MBean name incorrect
206      */

207     public AttributeList JavaDoc getGenericAttributes(String JavaDoc[] dottedNames) throws
208     InstanceNotFoundException JavaDoc,AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc,
209     MalformedNameException, MalformedObjectNameException JavaDoc, IntrospectionException JavaDoc,MBeanConfigException
210     {
211         AttributeList JavaDoc list = new AttributeList JavaDoc();
212         for(int i=0; i<dottedNames.length; i++)
213         {
214             list.addAll(getGenericAttribute(dottedNames[i]));
215         }
216         return list;
217     }
218     
219     /**
220      * Set Dynamic MBean attribute values for given attribute pattern (in dotted notation).
221      *
222      * @param dottedName - The name of the attribute to be set (in dotted notation)
223      * @throws MBeanException - Wraps a java.lang.Exception thrown by the MBean's getter.
224      * @throws ReflectionException - Wraps a java.lang.Exception thrown while trying to invoke the getter.
225      * @throws MalformedNameException - if MBean name incorrect
226      */

227     public AttributeList JavaDoc setGenericAttribute(String JavaDoc dottedName, Object JavaDoc value) throws
228     MBeanConfigException,InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
229     MBeanException JavaDoc, ReflectionException JavaDoc, MalformedNameException, MalformedObjectNameException JavaDoc, IntrospectionException JavaDoc
230     {
231         sLogger.log(Level.FINEST, MSG_SET_ATTRIBUTE, new Object JavaDoc[]{dottedName, value});
232         if(mServer==null)
233             mServer=MBeanServerFactory.getMBeanServer();
234         String JavaDoc attrName = extractAttrNameFromDotted(dottedName);
235         
236         ArrayList JavaDoc objectNames = getMBeanNamesFromDottedPattern(dottedName);
237         
238         AttributeList JavaDoc attrList = new AttributeList JavaDoc();
239         for(int i=0; i<objectNames.size(); i++)
240             setMatchedAttributesToValue(attrList, (ObjectName JavaDoc)objectNames.get(i), attrName, value);
241         return attrList;
242     }
243     
244     
245     /**
246      * @return Returns the list of attributes that have been set. This method
247      * doesnot throw AttributeNotFoundException for any attribute that is not
248      * found. The caller has to obtain this information from the returned list
249      * of attributes.
250      */

251     public AttributeList JavaDoc setGenericAttributes(AttributeList JavaDoc al)
252     throws InvalidAttributeValueException JavaDoc,InstanceNotFoundException JavaDoc,InvalidAttributeValueException JavaDoc, MBeanConfigException,
253     MBeanException JavaDoc, ReflectionException JavaDoc, MalformedNameException, MalformedObjectNameException JavaDoc, IntrospectionException JavaDoc
254     {
255         AttributeList JavaDoc list = new AttributeList JavaDoc();
256         Iterator JavaDoc it = al.iterator();
257         while (it.hasNext())
258         {
259             Attribute JavaDoc attribute = (Attribute JavaDoc) it.next();
260             String JavaDoc name = attribute.getName();
261             Object JavaDoc value = attribute.getValue();
262             try
263             {
264                 list.addAll(setGenericAttribute(name, value));
265             }
266             catch (AttributeNotFoundException JavaDoc anfe)
267             {
268                 sLogger.log(Level.FINE, MSG_ATTR_NOT_FOUND, name);
269             }
270         }
271         return list;
272     }
273     
274     /**
275      * Method that returns the monitorable params as an attribute list. This
276      * method will contact the running administered instance that is represented
277      * in the name of the attribute and will return the values from various
278      * monitoring data providers in the administered instance.
279      * <p>
280      * Note that this method gets the data as a whole. Returned list will have
281      * all the attributes that represent the MBean attributes.
282      * <p>
283      * The semantics are similar to the generic get command so that
284      * <tt>(server-name).application.(app-name).ejb-module.(mod-name).ejb.
285      * (bean-name).pool.*</tt> will give all the monitorable attributes of
286      * MBean corresponding to this name.
287      * <p>
288      * @return AttributeList that contains the Attribute instances. If there are
289      * no attributes an empty AttributeList is returned, which means the
290      * name does not correspond to any MBean or there are no attributes.
291      * Never returns a null.
292      * @param String representing the dotted name of the monitor data provider.
293      */

294     
295     public AttributeList JavaDoc getMonitor(String JavaDoc dottedName) throws Exception JavaDoc
296     {
297         Name name = new Name(dottedName);
298         String JavaDoc instanceName = name.getNamePart(0).toString();
299         
300         // 1. create MonitorCommand
301
CommandMapper cm = CommandMapper.getInstance(instanceName);
302         MonitorGetCommand command = cm.mapGetCommand(dottedName); //throws InvalidDottedNameException
303
// 2. create correspondent MonitoringEvent
304
MonitoringEvent event = new MonitoringEvent(instanceName, dottedName, MonitoringEvent.GET_MONITOR_DATA, command);
305         // 3. send/receive event to instance
306
AdminEventResult result = AdminEventMulticaster.multicastEvent(event);
307         // 4. analyse the result
308
if(!result.getResultCode().equals(result.SUCCESS))
309         {
310             handleMonitoringError(result, instanceName, dottedName);
311         }
312         // 5. extract result list
313
return (AttributeList JavaDoc)result.getAttribute(event.getEffectiveDestination(), MonitorCommand.MONITOR_RESULT);
314     }
315     
316     public AttributeList JavaDoc getMonitor(String JavaDoc[] dottedNames) throws Exception JavaDoc
317     {
318         AttributeList JavaDoc list = new AttributeList JavaDoc();
319         for(int i=0; i<dottedNames.length; i++)
320         {
321             list.addAll(getMonitor(dottedNames[i]));
322         }
323         return list;
324     }
325
326     /**
327      * Returns the list of immediate children of a node that represents
328      * a component in a hierarchy of nodes. Note that this method will return
329      * the list of names of children only. The idea is to aid in reaching the
330      * leaf nodes in the hierarchy (Semantics of unix list command). The name
331      * of the node is represented by a dotted name.
332      * @param String representing the dotted name of an (intermediate) node.
333      * @return String[] each of whose elements represents the name of the
334      * immediate child that would aid in forming a deeper path.
335      * Returns an empty array if there are no children which essentially
336      * means that one has reached the leaf node or the hierachy is invalid.
337      * Never returns a null.
338      */

339     
340     public String JavaDoc[] listMonitor(String JavaDoc dottedName) throws Exception JavaDoc
341     {
342         Name name = new Name(dottedName);
343         String JavaDoc instanceName = name.getNamePart(0).toString();
344         
345         // 1. create MonitorCommand
346
CommandMapper cm = CommandMapper.getInstance(instanceName);
347         MonitorListCommand command = cm.mapListCommand(dottedName); //throws InvalidDottedNameException
348
// 2. create correspondent MonitoringEvent
349
MonitoringEvent event = new MonitoringEvent(instanceName, dottedName, MonitoringEvent.LIST_MONITORABLE, command);
350         // 3. send/receive event to instance
351
AdminEventResult result = AdminEventMulticaster.multicastEvent(event);
352         // 4. analyse the result
353
if(!result.getResultCode().equals(result.SUCCESS))
354         {
355             handleMonitoringError(result, instanceName, dottedName);
356         }
357         // 5. extract result list
358
return (String JavaDoc[])result.getAttribute(event.getEffectiveDestination(), MonitorCommand.MONITOR_RESULT);
359     }
360     
361     public AttributeList JavaDoc setMonitor(AttributeList JavaDoc al) throws Exception JavaDoc
362     {
363         AttributeList JavaDoc list = new AttributeList JavaDoc();
364         Iterator JavaDoc it = al.iterator();
365         while (it.hasNext())
366         {
367             Attribute JavaDoc attribute = (Attribute JavaDoc) it.next();
368             String JavaDoc name = attribute.getName();
369             Object JavaDoc value = attribute.getValue();
370             try
371             {
372                 list.addAll(setMonitor(name, value));
373             }
374             catch (AttributeNotFoundException JavaDoc anfe)
375             {
376                 sLogger.log(Level.FINE, MSG_ATTR_NOT_FOUND, name);
377             }
378         }
379         
380         return list;
381     }
382     
383     public AttributeList JavaDoc setMonitor(String JavaDoc dottedName, Object JavaDoc value) throws Exception JavaDoc
384     {
385         Name name = new Name(dottedName);
386         String JavaDoc instanceName = name.getNamePart(0).toString();
387         
388         // 1. create MonitorCommand
389
CommandMapper cm = CommandMapper.getInstance(instanceName);
390         MonitorSetCommand command = cm.mapSetCommand(dottedName, value); //throws InvalidDottedNameException
391
// 2. create correspondent MonitoringEvent
392
MonitoringEvent event = new MonitoringEvent(instanceName, dottedName, MonitoringEvent.SET_MONITOR_DATA, command);
393         // 3. send/receive event to instance
394
AdminEventResult result = AdminEventMulticaster.multicastEvent(event);
395         // 4. analyse the result
396
if(!result.getResultCode().equals(result.SUCCESS))
397         {
398             handleMonitoringError(result, instanceName, dottedName);
399         }
400         // 5. extract result list
401
AttributeList JavaDoc resultList = null;
402         AttributeList JavaDoc tmp = (AttributeList JavaDoc)result.getAttribute(event.getEffectiveDestination(), MonitorCommand.MONITOR_RESULT);
403         Iterator JavaDoc it = tmp.iterator();
404         while (it.hasNext())
405         {
406             Attribute JavaDoc attribute = (Attribute JavaDoc) it.next();
407             resultList = (AttributeList JavaDoc)attribute.getValue();
408         }
409         return resultList;
410     }
411    
412     //******************************************************************************************************************
413
private int setMatchedAttributesToValue(AttributeList JavaDoc attrList, ObjectName JavaDoc objName, String JavaDoc attrPattern, Object JavaDoc value ) throws
414     InstanceNotFoundException JavaDoc,AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
415     MBeanException JavaDoc, ReflectionException JavaDoc, MalformedNameException, IntrospectionException JavaDoc
416     {
417         ArrayList JavaDoc attrNames = getAttrNames(objName, attrPattern);
418         if(attrNames.size()==0)
419         {
420             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.no_attributes_matched_to_pattern", attrPattern );
421             throw new AttributeNotFoundException JavaDoc( msg );
422         }
423         for(int i=0; i<attrNames.size(); i++)
424         {
425             String JavaDoc name = (String JavaDoc)attrNames.get(i);
426             Object JavaDoc obj = convertValueToAttributeType(objName, name, value);
427             mServer.setAttribute(objName, new Attribute JavaDoc(name, obj));
428             attrList.add(new Attribute JavaDoc(name, obj));
429         }
430         return attrNames.size(); //number of added elements
431
}
432     
433     //***************************************************************************************************
434
private void addAttributeToList(AttributeList JavaDoc attrList, ObjectName JavaDoc objName, String JavaDoc attrPattern ) throws
435     InstanceNotFoundException JavaDoc,AttributeNotFoundException JavaDoc, MBeanException JavaDoc,
436     ReflectionException JavaDoc, MalformedNameException, IntrospectionException JavaDoc
437     {
438         // only single "*"/"property.*" masks available now and they are implemented
439
// in ConfigMBeanBase.getAttributes() with "empty" values
440
AttributeList JavaDoc list = null;
441         if(attrPattern.equals(""+Tokens.kWildCardChar))
442         {
443             list = mServer.getAttributes(objName, new String JavaDoc[]{""});
444             if(list!=null)
445                 attrList.addAll(list);
446         }
447         else
448             if(attrPattern.equals(ConfigAttributeName.PROPERTY_NAME_PREFIX+Tokens.kWildCardChar))
449             {
450                 list = mServer.getAttributes(objName, new String JavaDoc[]{ConfigAttributeName.PROPERTY_NAME_PREFIX});
451                 if(list!=null)
452                     attrList.addAll(list);
453             }
454             else
455             {
456                 Object JavaDoc attValue = mServer.getAttribute(objName, attrPattern);
457                 attrList.add(new Attribute JavaDoc(attrPattern, attValue));
458             }
459         
460 /* //the following code maybe will be used for non-config mbeans
461         ArrayList attrNames = getAttrNames(objName, attrPattern);
462         if(attrNames.size()==0)
463         {
464             String msg = localStrings.getString( "admin.server.core.mbean.config.no_attributes_matched_to_pattern", attrPattern );
465             throw new AttributeNotFoundException( msg );
466         }
467         for(int i=0; i<attrNames.size(); i++)
468         {
469             String name = (String)attrNames.get(i);
470             Object attValue;
471             if(attrNames.size()==1)
472                 attValue = mServer.getAttribute(objName, name);
473             else
474             {
475                 try
476                 {
477                     attValue = mServer.getAttribute(objName, name);
478                 }
479                 catch (Exception e)
480                 {
481                     attValue = null;
482                 }
483             }
484             attrList.add(new Attribute(name, attValue));
485         }
486         return attrNames.size(); //number of added elements
487 */

488     }
489
490     /**
491      * Handle monitoring error.
492      * @param result monitoring result.
493      * @param instance instance on which monitoring was attempted
494      * @param compName component on which monitoring was attempted
495      * @throws ServiceNotFoundException if failure was caused by inability to
496      * connect to instance
497      * @throws InstanceNotFoundException if mbean corresponding to the specified
498      * name could not be found
499      * @throws AttributeNotFoundException if mbean attribute corresponding to
500      * specified name could not be found
501      * @throws JMRuntimeException Other runtime error in monitoring
502      */

503     private void handleMonitoringError(AdminEventResult result, String JavaDoc instance,
504             String JavaDoc compName)
505             throws AttributeNotFoundException JavaDoc, InstanceNotFoundException JavaDoc,
506             ServiceNotFoundException JavaDoc {
507         String JavaDoc resultCode = result.getResultCode();
508         if (AdminEventResult.TRANSMISSION_ERROR.equals(resultCode)) {
509             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.failed_to_connect_instance", instance );
510             throw new ServiceNotFoundException JavaDoc( msg );
511         } else if (AdminEventResult.MBEAN_NOT_FOUND.equals(resultCode)) {
512             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.unmatched_mbean", compName );
513             throw new InstanceNotFoundException JavaDoc( msg );
514         } else if (AdminEventResult.MBEAN_ATTR_NOT_FOUND.equals(resultCode)) {
515             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.unmatched_attribute", compName );
516             throw new AttributeNotFoundException JavaDoc( msg );
517         } else if (!AdminEventResult.SUCCESS.equals(resultCode)) {
518             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.other_monitoring_error", resultCode );
519             throw new JMRuntimeException JavaDoc( msg );
520         }
521     }
522
523     //***************************************************************************************************
524
private ArrayList JavaDoc getAttrNames(ObjectName JavaDoc objName, String JavaDoc attrPattern ) throws
525     InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc, ReflectionException JavaDoc
526     {
527         ArrayList JavaDoc list = new ArrayList JavaDoc();
528         if(attrPattern==null)
529             return list;
530         
531         if(attrPattern.indexOf(Tokens.kWildCardChar)>=0 || attrPattern.indexOf(ObjectNames.kSingleMatchChar)>=0)
532         {
533             MBeanInfo JavaDoc mi = mServer.getMBeanInfo(objName);
534             if(mi!=null)
535             {
536                 MBeanAttributeInfo JavaDoc[] ai = mi.getAttributes();
537                 if(ai!=null)
538                     for(int i=0; i<ai.length; i++)
539                     {
540                         String JavaDoc name = ai[i].getName();
541                         try
542                         {
543                             if((new CombinedPatternMatcher(attrPattern, name)).matches())
544                                 list.add(ai[i].getName());
545                         } catch (Throwable JavaDoc e)
546                         {
547                         }
548                     }
549             }
550         }
551         else
552             list.add(attrPattern); //for now
553
return list;
554     }
555     
556     //***************************************************************************************************
557
private String JavaDoc getAttrType(ObjectName JavaDoc objName, String JavaDoc attrName) throws
558     InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc, ReflectionException JavaDoc
559     {
560         return getAttrType(attrName, mServer.getMBeanInfo(objName));
561     }
562     
563     //**?*************************************************************************************************
564
private String JavaDoc getAttrType(String JavaDoc attrName, MBeanInfo JavaDoc mi)
565     {
566         if(attrName==null && mi==null)
567             return null;
568         
569         MBeanAttributeInfo JavaDoc[] ai = mi.getAttributes();
570         if(ai!=null)
571             for(int i=0; i<ai.length; i++)
572             {
573                 String JavaDoc name = ai[i].getName();
574                 if(attrName.equals(ai[i].getName()))
575                     return ai[i].getType();
576             }
577         return null;
578     }
579     
580     //***************************************************************************************************
581
private ArrayList JavaDoc getTargetObjectNames(ObjectName JavaDoc objectNamePattern ) throws MalformedObjectNameException JavaDoc
582     {
583         ArrayList JavaDoc list = new ArrayList JavaDoc();
584         
585         if(objectNamePattern==null)
586             return list;
587         
588         // call to MBeanServer.queryNames()
589
Set JavaDoc mNames = mServer.queryMBeans(objectNamePattern, null);
590         Iterator JavaDoc it = mNames.iterator();
591         while (it.hasNext())
592         {
593             list.add(it.next());
594         }
595         
596         // because of lazy loading we can have no registered mbeans, in this case - add pattern
597
// FIXME: add check is ObjectName -> non-wildcarded pattern
598
// NOTE: it will be added only if no one registered bean with such pattern otherwise it will work as before
599
if(list.size()==0)
600         {
601             list.add(objectNamePattern);
602         }
603         // list.add(new ObjectName(objectNamePattern)); //for now
604
return list;
605     }
606     
607     //***************************************************************************************************
608
String JavaDoc extractAttrNameFromDotted(String JavaDoc dottedStringName) throws MalformedNameException
609     {
610         int idx = dottedStringName.lastIndexOf(Tokens.kDelimiterChar);
611         if(idx<=0 || idx==(dottedStringName.length()-1))
612         {
613             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.cannot_extract_attribute_name_from_dotted_notation", dottedStringName );
614             throw new MalformedNameException( msg );
615         }
616         //property?
617
if(dottedStringName.substring(0, idx+1).endsWith(ConfigAttributeName.PROPERTY_NAME_PREFIX))
618         {
619             int idx2 = idx-ConfigAttributeName.PROPERTY_NAME_PREFIX.length();
620             if(dottedStringName.charAt(idx2)==Tokens.kDelimiterChar)
621             {
622                 idx = idx2;
623                 if(idx<1) {
624                     String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.cannot_extract_attribute_name_from_dotted_notation", dottedStringName );
625                     throw new MalformedNameException( msg );
626                 }
627             }
628         }
629         return dottedStringName.substring(idx+1);
630     }
631     
632     //***************************************************************************************************
633
String JavaDoc extractInstanceNameFromDotted(String JavaDoc dottedName) throws MalformedNameException
634     {
635         Name name = new Name(dottedName);
636         return name.getNamePart(0).toString();
637     }
638
639     //***************************************************************************************************
640
ArrayList JavaDoc getMBeanNamesFromDottedPattern(String JavaDoc dottedStringName) throws MBeanConfigException,MalformedNameException, MalformedObjectNameException JavaDoc
641     {
642         String JavaDoc attrName = extractAttrNameFromDotted(dottedStringName);
643         int idx = dottedStringName.length() - attrName.length() - 1;
644         if(idx<1) {
645             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.config.genericconfigurator_cannot_extract_attribute_name_from_dotted_notation", dottedStringName );
646             throw new MalformedNameException( msg );
647         }
648        
649         ConfigMBeanNamingInfo info = new ConfigMBeanNamingInfo(dottedStringName.substring(0,idx));
650         ArrayList JavaDoc list = new ArrayList JavaDoc();
651         list.add(info.getObjectName());
652         return list;
653     }
654     
655     //******************************************************************************************************************
656
private Object JavaDoc convertValueToAttributeType(ObjectName JavaDoc objName, String JavaDoc attrName, Object JavaDoc value )
657     {
658         try
659         {
660             if(value instanceof String JavaDoc) //only for string now
661
{
662                 String JavaDoc type = getAttrType(objName, attrName);
663                 return MBeanEasyConfig.convertStringValueToProperType((String JavaDoc)value, type);
664             }
665         }
666         catch (Throwable JavaDoc t)
667         {
668         }
669         return value; //no conversion if error
670
}
671     
672     
673     //***************************************************
674
//static MBean attributes and opeartions descriptions
675
/* For 8.0 since we are having the JMX 1.2 RI MBeanServer, it needs all
676      * the mbeans to be JMX compliant mbeans. The earlier implementation was
677      * rather lenient in that it was not checking for the MBean being compliant
678      * per se. */

679     
680     private static final String JavaDoc[] mAttrs = new String JavaDoc[0];
681     private static final String JavaDoc[] mOpers =
682     {
683         "getGenericAttribute(String name), INFO",
684         "getGenericAttributes(String[] attributeNames), INFO",
685         "setGenericAttribute(String name, Object objValue), ACTION_INFO",
686         "setGenericAttributes(javax.management.AttributeList al), ACTION_INFO",
687         "getMonitor(String[] name), INFO",
688         "setMonitor(javax.management.AttributeList al), ACTION",
689         "listMonitor(String name), INFO",
690         "listGenericDottedNameContinuiations(String dottedName), INFO",
691     };
692     
693     /** Implementation of <code>getMBeanInfo()</code>
694      * Uses helper class <code>MBeanEasyConfig</code> to construct whole MBeanXXXInfo tree.
695      * @return <code>MBeanInfo</code> objects containing full MBean description.
696      */

697     public MBeanInfo JavaDoc getMBeanInfo()
698     {
699         
700         try
701         {
702             return (new MBeanEasyConfig(getClass(), mAttrs, mOpers, null)).getMBeanInfo();
703         }
704         catch(Throwable JavaDoc e)
705         {
706             sLogger.log(Level.FINE, MSG_GETMBEANINFO_FAILED, e);
707             return null;
708         }
709     }
710     
711     /** Every resource MBean should override this method to execute specific
712      * operations on the MBean. This method is enhanced in 8.0. It was a no-op
713      * in 7.0. In 8.0, it is modified to invoke the actual method through
714      * reflection.
715      * @since 8.0
716      * @see javax.management.MBeanServer#invoke
717      * @see #getImplementingClass
718      */

719     protected Class JavaDoc getImplementingClass() {
720         return ( this.getClass() );
721     }
722     
723     /** Reflection requires the implementing object. */
724     protected Object JavaDoc getImplementingMBean() {
725         return ( this );
726     }
727  
728 }
729
Popular Tags