KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > mbeans > DottedNameGetSetMBeanBase


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.mbeans;
25
26 import com.sun.enterprise.admin.dottedname.*;
27 import com.sun.enterprise.admin.dottedname.valueaccessor.*;
28
29 import javax.management.*;
30 import java.util.*;
31 import java.util.regex.Pattern JavaDoc;
32
33
34
35 /*
36  * Base class for dotted name access. This was originally an inner class within
37  * DottedNameGetSetMBeanImpl. It was moved out as a separate class for
38  * extensibility reasons.
39  *
40  * @author <a <a HREF=mailto:lloyd.chambers@sun.com>Lloyd Chambers</a>
41  * @author <a HREF=mailto:shreedhar.ganapathy@sun.com>Shreedhar Ganapathy</a>
42  * Date: Jun 9, 2004
43  * @version $Revision: 1.3 $
44  */

45 abstract public class DottedNameGetSetMBeanBase{
46     final MBeanServerConnection mConn;
47     final DottedNameRegistry mRegistry;
48     protected final ValueAccessor mValueAccessor;
49     final DottedNameServerInfo mServerInfo;
50
51     public final static char ASSIGNMENT_DELIM = '=';
52
53     /*
54         Instantiate with a reference to an MBeanServerConnection which will be used
55         as the server when searching for required objects (which is possibly different
56         than the MBeanServer in which this object will be registered).
57
58         Due to a bug in the server startup sequence, this is the only allowed
59         constructor; avoiding the bug requires the SunoneInterceptor as 'conn'.com
60         Ideally the only constructor would be one that takes no arguments, and obtains
61         its MBeanServerConnection from preRegister().
62      */

63         public
64     DottedNameGetSetMBeanBase(
65         final MBeanServerConnection conn,
66         final DottedNameRegistry registry,
67         final DottedNameServerInfo serverInfo )
68     {
69         mConn = conn;
70
71         mRegistry = registry;
72
73         mValueAccessor = new AnyValueAccessor( mConn );
74
75         mServerInfo = serverInfo;
76     }
77
78
79     abstract DottedNameQuery createQuery( );
80     abstract DottedNameResolver getResolver();
81
82
83
84         protected MBeanServerConnection
85     getMBS()
86     {
87         return( mConn );
88     }
89
90     protected DottedNameRegistry
91     getRegistry( )
92     {
93         return( mRegistry );
94     }
95
96         protected static ObjectName
97     getTarget( final DottedNameForValue dottedName, final DottedNameResolver resolver)
98         throws Exception JavaDoc
99     {
100         final DottedName prefixDN = dottedName.getPrefix();
101
102         final ObjectName theObject = resolver.resolveDottedName( prefixDN.toString() );
103
104         if ( theObject == null )
105         {
106             final String JavaDoc msg = DottedNameStrings.getString(
107                     DottedNameStrings.OBJECT_INSTANCE_NOT_FOUND_KEY, dottedName.toString() );
108
109             throw new InstanceNotFoundException( msg );
110         }
111
112         return( theObject );
113     }
114
115
116     /*
117         static void
118     dm( Object o )
119     {
120         System.out.println( o.toString() );
121     }
122         static void
123     dm( Object o )
124     {
125         DottedNameLogger.dm( o );
126     }
127     */

128
129     protected static void
130     logException( final Exception JavaDoc e )
131     {
132         DottedNameLogger.logException( e );
133     }
134
135
136     /*
137         Can override the creation of the attribute eg filtering the output.
138      */

139         Attribute
140     formAttribute( final DottedName prefix, final String JavaDoc valueName, Object JavaDoc value )
141     {
142         return( new Attribute( prefix + "." + valueName, value ) );
143     }
144
145
146     /*
147       get the value for a single dotted name which must not be a wildcard name
148      */

149     protected void
150     doGet( final String JavaDoc dottedNameString, final AttributeList attrsOut )
151         throws Exception JavaDoc
152     {
153         // NOTE: this name includes the value-name
154
final DottedName dn = getDottedName( dottedNameString );
155         final DottedNameForValue dnv = new DottedNameForValue( dn );
156
157         final ObjectName target = getTarget( dnv, getResolver( ) );
158         final String JavaDoc valueName = dnv.getValueName();
159
160         final Attribute attr = mValueAccessor.getValue( target, valueName);
161
162         if ( attr != null )
163         {
164             // emit the name in its full form
165
attrsOut.add( formAttribute( dnv.getPrefix(), valueName, attr.getValue() ) );
166         }
167     }
168
169     public void
170     doGet( final Set dottedNames, final AttributeList attrsOut ) throws Exception JavaDoc
171     {
172         final Iterator iter = dottedNames.iterator();
173
174         while ( iter.hasNext() )
175         {
176             try
177             {
178                 doGet( (String JavaDoc)iter.next(), attrsOut );
179             }
180             catch( Exception JavaDoc e )
181             {
182                 // propogate up up if it's a single item
183
if ( dottedNames.size() == 1 )
184                 {
185                     throw e;
186                 }
187                 else
188                 {
189                     // do not log the exception if the attribute wasn't found;
190
// this is very common with wildcards
191
if ( ! (e instanceof AttributeNotFoundException) )
192                     {
193                         logException( e );
194                     }
195                 }
196             }
197         }
198     }
199
200     static private final char BACKSLASH = '\\';
201     /*
202         We support only '*" and '?'-- the '.' is a literal character
203      */

204         protected static String JavaDoc
205     convertWildcardStringToJavaFormat( final String JavaDoc wildcardString )
206     {
207         final int length = wildcardString.length();
208         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
209
210         for( int i = 0; i < length; ++i )
211         {
212             final char theChar = wildcardString.charAt( i );
213
214             if ( theChar == '.' )
215             {
216                 buf.append( "[.]" );
217             }
218             else if ( theChar == '*' )
219             {
220                 buf.append( ".*" );
221             }
222             else if ( theChar == BACKSLASH )
223             {
224                 buf.append( "" + BACKSLASH + BACKSLASH );
225             }
226             else
227             {
228                 buf.append( theChar );
229             }
230         }
231         return( buf.toString() );
232     }
233
234
235         private boolean
236     startsWithDomain( final String JavaDoc dottedNameExpr )
237     {
238         return( dottedNameExpr.startsWith( DottedNameAliasSupport.DOMAIN_SCOPE ) );
239     }
240
241     protected boolean
242     startsWithConfigName( final String JavaDoc dottedNameExpr )
243     {
244         boolean startsWithConfig = false;
245
246         try
247         {
248             final Iterator iter = mServerInfo.getConfigNames().iterator();
249             while ( iter.hasNext() )
250             {
251                 final String JavaDoc configName = (String JavaDoc)iter.next();
252
253                 if ( dottedNameExpr.startsWith( configName ) )
254                 {
255                     startsWithConfig = true;
256                     break;
257                 }
258             }
259         }
260         catch( DottedNameServerInfo.UnavailableException e )
261         {
262             logException( e );
263         }
264
265         return( startsWithConfig );
266     }
267
268     /*
269         Get the starting set to search, given a dottedName.
270
271         The dotted name may be wildcarded or not; different sets may be selected
272         based on special rules for aliasing:
273
274         (1) if it starts with DOMAIN_SCOPE or starts with a config name,
275         then the non-aliased registry is selected.
276
277         (2) otherwise, the aliased server names are used
278
279         The caller will likely filter the resulting Set.
280      */

281     protected Set
282     getSearchSet( final String JavaDoc dottedNameExpr )
283     {
284         final DottedName dn = getDottedName( dottedNameExpr );
285         final String JavaDoc scope = dn.getScope();
286         Set s = null;
287
288         // consider it to be a domain if it starts with "domain"
289
final boolean isDomain = scope.startsWith( DottedNameAliasSupport.DOMAIN_SCOPE );
290
291         // consider it to be a config if it starts with a config name
292
boolean isConfig = startsWithConfigName( dottedNameExpr );
293
294         if ( isDomain || isConfig )
295         {
296             s = getRegistry().allDottedNameStrings();
297         }
298         else
299         {
300             // this will create a search set consisting of everything "server."
301
s = createQuery().allDottedNameStrings();
302         }
303
304         return( s );
305     }
306
307     /*
308         Resolve the (possibly wildcarded) dotted name prefix to a Set of dotted name Strings.
309      */

310     protected Set
311     resolveWildcardPrefix( final String JavaDoc dottedNamePrefix)
312         throws DottedNameServerInfo.UnavailableException
313     {
314         Set resolvedSet = Collections.EMPTY_SET;
315
316         if ( DottedName.isWildcardName( dottedNamePrefix ) )
317         {
318             if ( dottedNamePrefix.equals( "*" ) ) // optimization
319
{
320                 resolvedSet = createQuery().allDottedNameStrings();
321             }
322             else
323             {
324                 final Set searchSet = getSearchSet( dottedNamePrefix );
325
326                 final String JavaDoc regex = convertWildcardStringToJavaFormat( dottedNamePrefix );
327
328                 final DottedNameWildcardMatcher matcher =
329                         new DottedNameWildcardMatcherImpl( searchSet );
330
331                 resolvedSet = matcher.matchDottedNames( regex );
332             }
333         }
334         else
335         {
336             resolvedSet = Collections.singleton( dottedNamePrefix );
337         }
338
339         return( resolvedSet );
340     }
341
342     /*
343         Given a dotted-name prefix, generate the dotted names for all values belonging to that
344         prefix, as specified by the suffix, which is some wildcarded form.
345     */

346     protected Set
347     prefixToValueDottedNamesWild(
348         final DottedNameResolver resolver,
349         final String JavaDoc prefix,
350         final String JavaDoc suffix)
351     {
352         final Set all = new HashSet();
353
354         try
355         {
356             final ObjectName objectName = resolver.resolveDottedName( prefix );
357
358             if ( objectName != null )
359             {
360                 Set allValueNames = null;
361                 PropertyValueAccessorBase prop_accessor = null;
362                 // wildcarded properties must not wildcard the "property" part
363
// eg "property.<regex>"
364
if ( suffix.equals( "*" ) )
365                 {
366                     // all attributes *and* all properties
367
allValueNames = getAllPropertyNames( new PropertyValueAccessor(getMBS()), objectName );
368                     allValueNames.addAll(getAllPropertyNames( new SystemPropertyValueAccessor(getMBS()), objectName ));
369                     allValueNames.addAll( getAllValueNames( getMBS(), objectName ) );
370                 }
371                 else if ((prop_accessor=(new PrefixedValueSupport(getMBS()).getPrefixedValueAccessor(suffix)))!=null)
372                 {
373                     allValueNames = getAllPropertyNames( prop_accessor, objectName );
374                 }
375                 else
376                 {
377                     // any other expression should match just attributes
378
allValueNames = getAllValueNames( getMBS(), objectName );
379                 }
380
381                 final Set valuesDottedNames = generateDottedNamesForValues( allValueNames, prefix, suffix );
382
383                 all.addAll( valuesDottedNames );
384             }
385         }
386         catch( Exception JavaDoc e )
387         {
388             logException( e );
389         }
390
391         return( all );
392     }
393
394     /*
395         Given a Set of dotted name prefixes, generate the Set of all dotted names for
396         appropriate values on those prefixes.
397     */

398         Set
399     prefixesToValueDottedNames(
400         final DottedNameResolver resolver,
401         final Set prefixes,
402         final String JavaDoc suffix )
403     {
404         final Set all = new HashSet();
405         final Iterator iter = prefixes.iterator();
406
407         while ( iter.hasNext() )
408         {
409             final String JavaDoc prefix = (String JavaDoc)iter.next();
410
411             if ( DottedName.isWildcardName( suffix ) )
412             {
413                 all.addAll( prefixToValueDottedNamesWild( resolver, prefix, suffix ) );
414             }
415             else
416             {
417                 // a fixed non-wildcard value-name--just append it
418
final String JavaDoc dottedName = prefix + "." + suffix;
419                 all.add( dottedName );
420
421             }
422         }
423
424         return( all );
425     }
426
427         String JavaDoc
428     setToString( final Set s )
429     {
430         final Iterator iter = s.iterator();
431         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
432
433         while ( iter.hasNext() )
434         {
435             buf.append( (String JavaDoc)iter.next() + "\n" );
436         }
437
438         return( buf.toString() );
439     }
440
441
442         String JavaDoc
443     normalizeWildcardName( final String JavaDoc name )
444     {
445         String JavaDoc normalizedName = name;
446
447         if ( name.equals( "*" ) )
448         {
449             normalizedName = "*.*";
450         }
451         return( normalizedName );
452     }
453
454         protected Set
455     resolveInputNames( final String JavaDoc [] names )
456         throws DottedNameServerInfo.UnavailableException
457     {
458         final Set all = new HashSet();
459
460         for( int i = 0; i < names.length; ++i )
461         {
462             String JavaDoc name = names[ i ];
463
464             if ( DottedName.isWildcardName( name ) )
465             {
466                 name = normalizeWildcardName( name );
467
468                 final DottedName dn = getDottedName( name );
469                 final DottedNameForValue dnv = new DottedNameForValue( dn );
470
471                 final String JavaDoc prefix = dnv.getPrefix().toString();
472                 final String JavaDoc valueName = dnv.getValueName();
473
474                 final Set resolvedPrefixes = resolveWildcardPrefix( prefix );
475
476
477                 final Set newDottedNames = prefixesToValueDottedNames( getResolver( ),
478                                                     resolvedPrefixes, valueName );
479
480
481                 all.addAll( newDottedNames );
482             }
483             else
484             {
485                 all.add( name );
486             }
487
488         }
489
490         return( all );
491     }
492
493     protected DottedName
494     getDottedName( final String JavaDoc s )
495     {
496         return( DottedNameFactory.getInstance().get( s ) );
497     }
498
499     private final class AttributeComparator implements java.util.Comparator JavaDoc
500     {
501             public int
502         compare( Object JavaDoc o1, Object JavaDoc o2 )
503         {
504             final Attribute attr1 = (Attribute)o1;
505             final Attribute attr2 = (Attribute)o2;
506
507             return( attr1.getName().compareTo( attr2.getName() ) );
508         }
509
510             public boolean
511         equals( Object JavaDoc other )
512         {
513             return( other instanceof AttributeComparator );
514         }
515     }
516
517         protected Attribute []
518     sortAttributeList( final AttributeList attrsIn )
519     {
520         final Attribute [] attrs = new Attribute[ attrsIn.size() ];
521         attrsIn.toArray( attrs );
522
523         Arrays.sort( attrs, new AttributeComparator() );
524
525         return( attrs );
526     }
527
528
529         Object JavaDoc []
530     dottedNameGet( final String JavaDoc [] names )
531     {
532         final Object JavaDoc [] results = new Object JavaDoc[ names.length ];
533
534         for( int i = 0; i < names.length; ++i )
535         {
536             results[ i ] = dottedNameGet( names[ i ] );
537         }
538
539         return( results );
540     }
541
542         Object JavaDoc
543     dottedNameGet( final String JavaDoc name )
544     {
545         Object JavaDoc result = null;
546
547         try
548         {
549             final Set all = resolveInputNames( new String JavaDoc [] { name } );
550
551             final AttributeList attrs = new AttributeList();
552             doGet( all, attrs );
553
554             if ( ! DottedName.isWildcardName( name ) )
555             {
556                 // return an Attribute if the input was a single dotted-name
557
assert( attrs.size() == 1 );
558                 result = (Attribute)attrs.get( 0 );
559             }
560             else
561             {
562                 result = sortAttributeList( attrs );
563             }
564         }
565         catch( Exception JavaDoc e )
566         {
567             logException( e );
568             // the result will be the exception itself
569
result = e;
570         }
571
572         assert( result != null );
573         return( result );
574     }
575
576
577     /*
578         Return a Set of String of the names of all properties
579         (INCLUDING the "property." prefix)
580      */

581         protected static Set
582     getAllPropertyNames( PropertyValueAccessorBase accessor, final ObjectName objectName )
583         throws Exception JavaDoc
584     {
585         final Set allNames = new HashSet();
586
587         // add the property names
588
final String JavaDoc [] propNames = accessor.getAllPropertyNames( objectName, true );
589         for( int i = 0; i < propNames.length; ++i )
590         {
591             // prepend the "properties." prefix
592
allNames.add( propNames[ i ] );
593         }
594
595         return( allNames );
596     }
597
598
599     /*
600         Return a Set of String of the names of all attributes and properties available within the MBean.com
601
602         Properties are prefixed by "property" (DOTTED_NAME_PROPERTIES_PREFIX).
603      */

604         protected static Set
605     getAllValueNames( final MBeanServerConnection conn, final ObjectName objectName )
606         throws Exception JavaDoc
607     {
608         final Set allNames = new HashSet();
609
610         allNames.addAll(getAllPropertyNames( new PropertyValueAccessor(conn), objectName ));
611         allNames.addAll(getAllPropertyNames( new SystemPropertyValueAccessor(conn), objectName ));
612         allNames.addAll( AttributeValueAccessor.getAllAttributeNames( conn, objectName ) );
613
614         return( allNames );
615     }
616
617     /*
618         Return a Set of String (dotted names)
619      */

620         protected static Set
621     generateDottedNamesForValues(
622         final Set valueNames,
623         final String JavaDoc prefix,
624         final String JavaDoc suffix )
625     {
626         final Iterator iter = valueNames.iterator();
627         final Set allDottedNameStrings = new HashSet();
628
629         final Pattern JavaDoc pattern = Pattern.compile( convertWildcardStringToJavaFormat( suffix ) );
630
631         while ( iter.hasNext() )
632         {
633             final String JavaDoc valueName = (String JavaDoc)iter.next();
634
635             if ( pattern.matcher( valueName ).matches() )
636             {
637                 allDottedNameStrings.add( prefix + "." + valueName );
638             }
639         }
640
641         return( allDottedNameStrings );
642     }
643
644
645     /*
646         Get all children of a name prefix.
647      */

648     protected Set
649     getAllDescendants( final String JavaDoc namePrefix )
650     {
651         final Set searchSet = getSearchSet( namePrefix );
652
653         // a child must be prefix.xxx
654
final String JavaDoc searchPrefix = namePrefix + ".";
655
656         final Set resultSet = new HashSet();
657         final Iterator iter = searchSet.iterator();
658         while ( iter.hasNext() )
659         {
660             final String JavaDoc candidateString = (String JavaDoc)iter.next();
661
662             if ( candidateString.startsWith( searchPrefix ) )
663             {
664                 resultSet.add( candidateString );
665             }
666         }
667
668         return( resultSet );
669     }
670
671
672     /*
673         Find all immediate children of the prefix. An "immediate child" must have
674         one more name part than its parent.
675      */

676     protected Set
677     getAllImmediateChildren( final String JavaDoc namePrefix )
678     {
679         final Set allChildren = getAllDescendants( namePrefix );
680
681         final int numParentParts = getDottedName( namePrefix ).getParts().size();
682         final Iterator iter = allChildren.iterator();
683
684         final Set resultSet = new HashSet();
685         while ( iter.hasNext() )
686         {
687             final String JavaDoc descendant = (String JavaDoc)iter.next();
688
689             if ( getDottedName( descendant ).getParts().size() == numParentParts + 1 )
690             {
691                 resultSet.add( descendant );
692             }
693         }
694
695         return( resultSet );
696     }
697
698         protected Set
699     getAllTopLevelNames()
700     {
701         final Set all = new HashSet();
702         final Set searchSet = createQuery().allDottedNameStrings();
703
704         // a child must be prefix.xxx
705
final Iterator iter = searchSet.iterator();
706         while ( iter.hasNext() )
707         {
708             final String JavaDoc candidateString = (String JavaDoc)iter.next();
709             final DottedName dn = getDottedName( candidateString );
710
711             if ( dn.getParts().size() == 0 )
712             {
713                 all.add( candidateString );
714             }
715         }
716         return( all );
717     }
718
719     protected Set
720     doList( final String JavaDoc [] namePrefixes )
721         throws DottedNameServerInfo.UnavailableException
722     {
723         final Set all = new HashSet();
724
725         for( int i = 0; i < namePrefixes.length; ++i )
726         {
727             final String JavaDoc dottedNamePrefix = namePrefixes[ i ];
728
729             Set resolved = null;
730             if ( DottedName.isWildcardName( dottedNamePrefix ) )
731             {
732                 resolved = resolveWildcardPrefix( dottedNamePrefix );
733             }
734             else
735             {
736                 // no wildcard means to list all immediate children of the prefix
737
resolved = getAllImmediateChildren( dottedNamePrefix );
738             }
739
740             all.addAll( resolved );
741         }
742
743         return( all );
744     }
745
746     public String JavaDoc []
747     dottedNameList( final String JavaDoc [] namePrefixes )
748     {
749         Set all = Collections.EMPTY_SET;
750
751         try
752         {
753             // if nothing specified, get top-level names
754
all = (namePrefixes.length == 0) ?
755                     getAllTopLevelNames() : doList( namePrefixes );
756         }
757         catch( Exception JavaDoc e )
758         {
759             logException( e );
760         }
761
762         final String JavaDoc [] allArray = new String JavaDoc [ all.size() ];
763         all.toArray( allArray );
764
765         Arrays.sort( allArray );
766         return( allArray );
767     }
768
769
770         protected DottedNameServerInfo
771     getServerInfo( )
772     {
773         return( mServerInfo );
774     }
775 }
776
777
778
779
780
781
782
Popular Tags