KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > support > DottedNamesBase


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 package com.sun.enterprise.management.support;
24
25 import java.util.Map JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 import javax.management.ObjectName JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanServer JavaDoc;
36 import javax.management.MBeanAttributeInfo JavaDoc;
37 import javax.management.MBeanOperationInfo JavaDoc;
38 import javax.management.Attribute JavaDoc;
39 import javax.management.AttributeList JavaDoc;
40 import javax.management.AttributeChangeNotification JavaDoc;
41 import javax.management.InstanceNotFoundException JavaDoc;
42 import javax.management.ReflectionException JavaDoc;
43 import javax.management.MBeanException JavaDoc;
44 import javax.management.IntrospectionException JavaDoc;
45 import javax.management.AttributeNotFoundException JavaDoc;
46 import javax.management.InvalidAttributeValueException JavaDoc;
47
48 import javax.management.MBeanServerInvocationHandler JavaDoc;
49
50
51
52 import com.sun.appserv.management.util.misc.ClassUtil;
53 import com.sun.appserv.management.util.misc.GSetUtil;
54 import com.sun.appserv.management.util.jmx.JMXUtil;
55
56 import com.sun.appserv.management.config.PropertiesAccess;
57 import com.sun.appserv.management.base.XTypesMapper;
58 import com.sun.appserv.management.base.Util;
59
60 import com.sun.appserv.management.base.DottedNames;
61 import com.sun.appserv.management.base.AMX;
62
63 import com.sun.enterprise.management.support.ObjectNames;
64
65
66
67 /**
68     Base class from which the DottedNames (config and monitoring) MBeans
69     inherit.
70     <p>
71  */

72 public abstract class DottedNamesBase extends AMXImplBase
73     implements DottedNames
74 {
75     private static final String JavaDoc DOTTED_NAMES =
76         "com.sun.appserv:name=dotted-name-get-set,type=dotted-name-support";
77     
78     protected OldDottedNamesProxy mOldDottedNamesProxy = null;
79     
80     private MBeanInfo JavaDoc mMBeanInfo;
81     private Map JavaDoc<String JavaDoc,Attribute JavaDoc> mAttributes;
82     
83     private Set JavaDoc<String JavaDoc> mParentAttributeNames;
84     
85         public
86     DottedNamesBase()
87     {
88         mMBeanInfo = null;
89         
90         mParentAttributeNames = null;
91     }
92     
93     
94     public abstract Object JavaDoc[] dottedNameGet( String JavaDoc[] names );
95     public abstract Object JavaDoc dottedNameGet( String JavaDoc name );
96     public abstract Object JavaDoc[] dottedNameList( String JavaDoc[] names );
97     public abstract Object JavaDoc[] dottedNameSet( String JavaDoc[] nameValuePairs );
98     
99     protected abstract boolean isWriteableDottedName( String JavaDoc name );
100
101     
102     /**
103         MBeanInfo Attributes change whenever the dotted-namespace changes.
104      */

105         public boolean
106     getMBeanInfoIsInvariant()
107     {
108         return( false );
109     }
110     
111     /**
112         Note the during registration, we won't have setup our delegate, and our
113         MBeanInfo will therefore not contain any Attributes.
114      */

115         public MBeanInfo JavaDoc
116     getMBeanInfo()
117     {
118         MBeanInfo JavaDoc mbeanInfo = null;
119         
120         if ( getOldDottedNames() != null )
121         {
122             ensureMBeanInfo();
123             mbeanInfo = mMBeanInfo;
124         }
125         else
126         {
127             mbeanInfo = super.getMBeanInfo();
128         }
129         
130         return( mbeanInfo );
131     }
132     
133         public void
134     preRegisterDone()
135         throws Exception JavaDoc
136     {
137         super.preRegisterDone();
138         
139         setupOldDottedNamesProxy( );
140     }
141     
142     
143     /**
144         Proxy representing the "old" DottedNames
145     */

146     protected interface OldDottedNamesProxy
147     {
148         public Object JavaDoc[] dottedNameGet( String JavaDoc[] names );
149         public Object JavaDoc dottedNameGet( String JavaDoc name );
150         public Object JavaDoc[] dottedNameMonitoringGet( String JavaDoc[] names );
151         public Object JavaDoc dottedNameMonitoringGet( String JavaDoc names );
152         public String JavaDoc[] dottedNameList( String JavaDoc[] names );
153         public String JavaDoc[] dottedNameMonitoringList( String JavaDoc[] names );
154         public Object JavaDoc[] dottedNameSet( String JavaDoc[] names );
155     }
156
157         protected void
158     setupOldDottedNamesProxy( )
159     {
160         setupOldDottedNamesProxy( Util.newObjectName( DOTTED_NAMES ) );
161     }
162
163         protected void
164     setupOldDottedNamesProxy( ObjectName JavaDoc target )
165     {
166         final MBeanServer JavaDoc server = getMBeanServer();
167         
168         if ( ! server.isRegistered( target ) )
169         {
170         logSevere( "setupOldDottedNamesProxy: old dotted names MBeans is not registered" );
171         }
172         
173         mOldDottedNamesProxy = (OldDottedNamesProxy)
174             MBeanServerInvocationHandler.newProxyInstance(
175                 server, target, OldDottedNamesProxy.class, false );
176     }
177     
178         protected OldDottedNamesProxy
179     getOldDottedNames()
180     {
181         return( mOldDottedNamesProxy );
182     }
183     
184         protected final boolean
185     isDottedName( final String JavaDoc name )
186     {
187         return( getAttributes().keySet().contains( name ) );
188     }
189     
190         protected final boolean
191     isParentAttributeName( final String JavaDoc name )
192     {
193         return( mParentAttributeNames.contains( name ) );
194     }
195     
196         protected final void
197     filterNames(
198         final String JavaDoc[] in,
199         Set JavaDoc<String JavaDoc> dotted,
200         Set JavaDoc<String JavaDoc> parent )
201     {
202         for( int i = 0; i < in.length; ++i )
203         {
204             final String JavaDoc name = in[ i ];
205             
206             if ( isDottedName( name ) )
207             {
208                 dotted.add( name );
209             }
210             else if ( isParentAttributeName( name ) )
211             {
212                 parent.add( name );
213             }
214         }
215     }
216     
217         public AttributeList JavaDoc
218     getAttributes( final String JavaDoc[] names )
219     {
220         mCoverage.attributesWereRead( names );
221         
222         final Set JavaDoc<String JavaDoc> dotted = new HashSet JavaDoc<String JavaDoc>();
223         final Set JavaDoc<String JavaDoc> parent = new HashSet JavaDoc<String JavaDoc>();
224         filterNames( names, dotted, parent);
225
226         final Object JavaDoc[] dottedResults =
227             dottedNameGet( (String JavaDoc[])dotted.toArray( new String JavaDoc[ dotted.size() ] ) );
228         
229         final String JavaDoc[] namesForParent = new String JavaDoc[ parent.size() ];
230         final AttributeList JavaDoc parentResults =
231             super.getAttributes( (String JavaDoc[])parent.toArray( namesForParent ) );
232         
233         final AttributeList JavaDoc successList = new AttributeList JavaDoc();
234         successList.addAll( parentResults );
235         
236         // add all the dotted name results
237
for( int i = 0; i < dottedResults.length; ++i )
238         {
239             if ( dottedResults[ i ] instanceof Attribute JavaDoc )
240             {
241                 successList.add( (Attribute JavaDoc)dottedResults[ i ] );
242             }
243             else
244             {
245                 assert( dottedResults[ i ] instanceof Exception JavaDoc );
246             }
247         }
248         
249         
250         return( successList );
251     }
252     
253         public Object JavaDoc
254     getAttribute( final String JavaDoc name )
255         throws AttributeNotFoundException JavaDoc
256     {
257         checkLegalName( name );
258         mCoverage.attributeWasRead( name );
259         
260         Object JavaDoc result = null;
261         
262         if ( isDottedName( name ) )
263         {
264             result = dottedNameGet( name );
265             assert( !(result instanceof Attribute JavaDoc) );
266         }
267         else if ( isParentAttributeName( name ) )
268         {
269             result = super.getAttribute( name );
270         }
271         else
272         {
273             throw new AttributeNotFoundException JavaDoc( name );
274         }
275         
276         return( result );
277     }
278     
279     
280         public void
281     setAttribute( final Attribute JavaDoc attr )
282         throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc
283     {
284         final String JavaDoc name = attr.getName();
285         
286         if ( isParentAttributeName( name ) )
287         {
288             super.setAttribute( attr );
289         }
290         else
291         {
292             checkLegalName( name );
293             mCoverage.attributeWasWritten( name );
294             
295             final AttributeList JavaDoc inList = new AttributeList JavaDoc();
296             inList.add( attr );
297             final AttributeList JavaDoc result = setAttributes( inList );
298             if ( result.size() != 1)
299             {
300                 throw new InvalidAttributeValueException JavaDoc( attr.getName() );
301             }
302         }
303     }
304     
305         public AttributeList JavaDoc
306     setAttributes( AttributeList JavaDoc attributes )
307     {
308         
309         /*
310             Convert each attribute to a name/value pair.
311             Omit any attributes that don't have a legal attribute name
312          */

313         final int numAttrsIn = attributes.size();
314         final List JavaDoc<String JavaDoc> legalPairs = new ArrayList JavaDoc<String JavaDoc>();
315         for( int i = 0; i < numAttrsIn; ++i )
316         {
317             final Attribute JavaDoc attr = (Attribute JavaDoc)attributes.get( i );
318             
319             final String JavaDoc name = attr.getName();
320             mCoverage.attributeWasWritten( name );
321             
322             if ( isLegalAttributeName( name ) )
323             {
324                 legalPairs.add( attributeToNamePair( attr ) );
325             }
326         }
327         
328         final String JavaDoc[] pairs = (String JavaDoc[])legalPairs.toArray( new String JavaDoc[ legalPairs.size() ] );
329
330         final Object JavaDoc[] results = dottedNameSet( pairs );
331         
332         final AttributeList JavaDoc attributeList = new AttributeList JavaDoc();
333         for( int i = 0; i < results.length; ++i )
334         {
335             if ( results[ i ] instanceof Attribute JavaDoc )
336             {
337                 attributeList.add( (Attribute JavaDoc)results[ i ] );
338             }
339             else
340             {
341                 assert( results[ i ] instanceof Exception JavaDoc );
342                 // it's an exception
343
}
344         }
345         
346         return( attributeList );
347     }
348     
349     
350         public Object JavaDoc
351     invokeManually(
352         String JavaDoc operationName,
353         Object JavaDoc[] params,
354         String JavaDoc[] types)
355         throws ReflectionException JavaDoc, MBeanException JavaDoc, NoSuchMethodException JavaDoc,
356         AttributeNotFoundException JavaDoc
357     {
358         final boolean noParams = params == null || params.length == 0;
359         Object JavaDoc result = null;
360         
361         if ( operationName.equals( "refresh" ) && noParams )
362         {
363             refresh();
364             result = null;
365         }
366         else
367         {
368             result = super.invokeManually( operationName, params, types );
369         }
370         return( result );
371     }
372     
373         private synchronized void
374     ensureMBeanInfo()
375     {
376         if ( mMBeanInfo == null )
377         {
378             trace( "##### DottedNamesBase.ensureMBeanInfo" );
379             refresh();
380             assert( mMBeanInfo != null );
381         }
382     }
383     
384     
385         protected MBeanAttributeInfo JavaDoc[]
386     buildAttributeInfos( final MBeanAttributeInfo JavaDoc[] parentAttributeInfos )
387     {
388         final String JavaDoc[] parentAttributeNames =
389             JMXUtil.getAttributeNames( parentAttributeInfos );
390         mParentAttributeNames =
391             GSetUtil.newUnmodifiableStringSet( parentAttributeNames );
392         
393         final Map JavaDoc<String JavaDoc,Attribute JavaDoc> attributes = getAttributes();
394         
395         final MBeanAttributeInfo JavaDoc[] infos =
396             new MBeanAttributeInfo JavaDoc[ attributes.size() + parentAttributeInfos.length ];
397         
398         // make info for every Attribute
399

400         int i = 0;
401         for( final String JavaDoc name : attributes.keySet() )
402         {
403             final Attribute JavaDoc attr = attributes.get( name );
404         
405             final Object JavaDoc value = attr.getValue();
406             final Class JavaDoc theClass = value == null ? String JavaDoc.class : attr.getValue().getClass();
407             
408             infos[ i ] = new MBeanAttributeInfo JavaDoc( name, theClass.getName(), "",
409                 true, isWriteableDottedName( name ), false );
410             ++i;
411         }
412         
413         System.arraycopy( parentAttributeInfos, 0,
414             infos, attributes.size(), parentAttributeInfos.length );
415
416         return( infos );
417     }
418     
419         protected MBeanOperationInfo JavaDoc[]
420     buildOperationInfos( final MBeanOperationInfo JavaDoc[] existing )
421     {
422         final MBeanOperationInfo JavaDoc refreshInfo = new MBeanOperationInfo JavaDoc( "refresh",
423             "update MBeanInfo to reflect all available dotted names",
424             null,
425             Void JavaDoc.class.getName(),
426             MBeanOperationInfo.ACTION );
427         
428         final MBeanOperationInfo JavaDoc[] infos = new MBeanOperationInfo JavaDoc[ existing.length + 1 ];
429         System.arraycopy( existing, 0, infos, 0, existing.length );
430         infos[ infos.length -1 ] = refreshInfo;
431         
432         return( infos );
433     }
434     
435         protected MBeanInfo JavaDoc
436     buildMBeanInfo()
437     {
438         final MBeanInfo JavaDoc superMBeanInfo = super.getMBeanInfo();
439         
440         final MBeanAttributeInfo JavaDoc[] attributeInfos =
441                 buildAttributeInfos( superMBeanInfo.getAttributes() );
442         final MBeanOperationInfo JavaDoc[] operationInfos =
443                 buildOperationInfos( superMBeanInfo.getOperations() );
444         
445         final MBeanInfo JavaDoc info = new MBeanInfo JavaDoc( this.getClass().getName(),
446                                     "exposes dotted-names as Attributes",
447                                     attributeInfos,
448                                     superMBeanInfo.getConstructors(),
449                                     operationInfos,
450                                     superMBeanInfo.getNotifications() );
451         
452         return( info );
453     }
454     
455     
456     
457     
458         private static Attribute JavaDoc
459     namePairToAttribute( String JavaDoc pair )
460     {
461         final int delimIndex = pair.indexOf( "=" );
462         assert( delimIndex >= 1 );
463         final String JavaDoc name = pair.substring( 0, delimIndex );
464         final String JavaDoc value = pair.substring( delimIndex + 1, pair.length() );
465         
466         return( new Attribute JavaDoc( name, value ) );
467     }
468     
469         private static String JavaDoc
470     attributeToNamePair( Attribute JavaDoc attr )
471     {
472         return( attr.getName() + "=" + attr.getValue() );
473     }
474     
475         protected final boolean
476     isLegalAttributeName( final String JavaDoc name )
477     {
478         ensureAttributes();
479         return( getAttributes().keySet().contains( name ) ||
480                 mParentAttributeNames.contains( name ) );
481     }
482     
483         protected final void
484     ensureAttributes()
485     {
486         if ( mAttributes == null )
487         {
488             refreshAttributes();
489         }
490     }
491
492         protected final Map JavaDoc<String JavaDoc,Attribute JavaDoc>
493     getAttributes()
494     {
495         ensureAttributes();
496         
497         return( mAttributes );
498     }
499
500         protected final String JavaDoc[]
501     getDottedNamesArray()
502     {
503         final Map JavaDoc<String JavaDoc,Attribute JavaDoc> map = getAttributes();
504         
505         final Set JavaDoc<String JavaDoc> keySet = map.keySet();
506         
507         return( (String JavaDoc[])keySet.toArray( new String JavaDoc[ keySet.size() ] ) );
508     }
509     
510     
511         private final void
512     checkLegalName( String JavaDoc name )
513         throws AttributeNotFoundException JavaDoc
514     {
515         if ( ! isLegalAttributeName( name ) )
516         {
517             throw new AttributeNotFoundException JavaDoc( "illegal attribute name: " + name );
518         }
519     }
520     
521
522     /**
523         Refresh the MBeanInfo to reflect the currently available attributes.
524      */

525     private static final String JavaDoc ALL = "*";
526         final void
527     refreshAttributes()
528     {
529         Object JavaDoc result = null;
530         
531         //trace( "##### DottedNamesBase.refreshAttributeNames" );
532
result = dottedNameGet( ALL );
533         
534         // results is an array of length 1. It should contain an Object[] containining
535
// everything obtained from "ALL"
536
final Attribute JavaDoc[] values = (Attribute JavaDoc[])result;
537         
538         // extract the name of each attribute
539
final Map JavaDoc<String JavaDoc,Attribute JavaDoc> map = new HashMap JavaDoc<String JavaDoc,Attribute JavaDoc>();
540         for( final Attribute JavaDoc attr : values )
541         {
542             map.put( attr.getName(), attr );
543         }
544         
545         mAttributes = map;
546     }
547     
548         public final void
549     refresh()
550     {
551         trace( "##### DottedNamesBase.refresh" );
552         refreshAttributes();
553         mMBeanInfo = buildMBeanInfo();
554     }
555 }
556
557
558
559
560
561
562
563
564
Popular Tags