KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28
29 /**
30 */

31
32 package com.sun.enterprise.management.support;
33
34 import java.util.Set JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Collections JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 import java.lang.reflect.Field JavaDoc;
41
42 import javax.management.ObjectName JavaDoc;
43 import javax.management.InstanceNotFoundException JavaDoc;
44 import javax.management.MalformedObjectNameException JavaDoc;
45 import javax.management.MBeanServer JavaDoc;
46
47 import com.sun.appserv.management.base.AMX;
48 import com.sun.appserv.management.base.AMXAttributes;
49 import com.sun.appserv.management.base.AMXDebug;
50
51 import com.sun.appserv.management.base.Util;
52 import com.sun.appserv.management.DomainRoot;
53 import com.sun.appserv.management.base.QueryMgr;
54 import com.sun.appserv.management.deploy.DeploymentMgr;
55 import com.sun.appserv.management.base.XTypes;
56 import com.sun.appserv.management.config.*;
57 import com.sun.appserv.management.monitor.*;
58 import com.sun.appserv.management.j2ee.J2EETypes;
59
60 import com.sun.appserv.management.util.misc.ListUtil;
61 import com.sun.appserv.management.util.jmx.JMXUtil;
62 import com.sun.appserv.management.base.AllTypesMapper;
63
64 import com.sun.appserv.management.util.misc.ClassUtil;
65 import com.sun.appserv.management.util.misc.GSetUtil;
66 import com.sun.appserv.management.util.stringifier.SmartStringifier;
67
68 /**
69     Class used to build ObjectNames for AMX MBeans. <b>This class does not assume
70     that any MBeans are loaded</b>--and no code should be added which does; such code
71     belongs in the {@link QueryMgr}.
72     <p>
73     Note that most of the time, using the
74     {@link QueryMgr} is a better choice.
75     In particular, the ObjectNames returned from here are the minimal ones required to
76     uniquely identify the ; the actual registered MBean may well have
77     additional properties.
78 */

79 public final class ObjectNames
80 {
81     private final String JavaDoc mJMXDomain;
82     
83         private
84     ObjectNames( final String JavaDoc jmxDomain )
85     {
86         mJMXDomain = jmxDomain;
87     }
88     
89         private void
90     debug( final Object JavaDoc o )
91     {
92         AMXDebug.getInstance().getOutput(
93             "com.sun.enterprise.management.support.ObjectNames" ).println( o );
94     }
95     
96         public static ObjectNames
97     getInstance( final String JavaDoc jmxDomain )
98     {
99         return( new ObjectNames( jmxDomain ) );
100     }
101     
102     
103         public String JavaDoc
104     getJMXDomain()
105     {
106         return( mJMXDomain );
107     }
108     
109     private static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[ 0 ];
110     
111     
112     /**
113      */

114         public static String JavaDoc
115     getJ2EEType( Class JavaDoc theInterface )
116     {
117         return( (String JavaDoc)ClassUtil.getFieldValue( theInterface, "J2EE_TYPE" ) );
118     }
119     
120
121     /**
122         Append the formatted props to the JMX domain and return the ObjectName
123      */

124         private ObjectName JavaDoc
125     newObjectName( String JavaDoc props )
126     {
127         return( Util.newObjectName( getJMXDomain(), props ) );
128     }
129     
130         public static String JavaDoc
131     makeWild( String JavaDoc props )
132     {
133         return( Util.concatenateProps( props, JMXUtil.WILD_PROP ) );
134     }
135     
136     /**
137         Build an ObjectName for an MBean logically contained within the parent MBean.
138         The child may be a true child (a subtype), or simply logically contained
139         within the parent.
140         
141         @param parentObjectName
142         @param parentFullType
143         @param childJ2EEType
144         @param childName
145         @return ObjectName
146      */

147         public ObjectName JavaDoc
148     buildContaineeObjectName(
149         final ObjectName JavaDoc parentObjectName,
150         final String JavaDoc parentFullType,
151         final String JavaDoc childJ2EEType,
152         final String JavaDoc childName )
153     {
154         final String JavaDoc domain = parentObjectName.getDomain();
155         
156         String JavaDoc props = "";
157         
158         final TypeInfo info = TypeInfos.getInstance().getInfo( childJ2EEType );
159         if ( info.isSubType() )
160         {
161             // extract j2eeType and name
162
final String JavaDoc parentProp = Util.getSelfProp( parentObjectName );
163             
164             // extract the remaining ancestors
165
final String JavaDoc[] parentFullTypes = Util.getTypeArray( parentFullType );
166             final Set JavaDoc<String JavaDoc> ancestorKeys = GSetUtil.newSet( parentFullTypes, 0, parentFullTypes.length - 1 );
167             final String JavaDoc ancestorProps = JMXUtil.getProps( parentObjectName, ancestorKeys, true );
168             props = Util.concatenateProps( parentProp, ancestorProps );
169         }
170         else
171         {
172             // it's logically contained within the parent, nothing more to do
173
}
174         
175         
176         final String JavaDoc requiredProps = Util.makeRequiredProps( childJ2EEType, childName );
177         final String JavaDoc allProps = Util.concatenateProps( requiredProps, props );
178         
179         return( Util.newObjectName( domain, allProps ) );
180     }
181     
182         public ObjectName JavaDoc
183     buildContaineeObjectName(
184         final ObjectName JavaDoc parentObjectName,
185         final String JavaDoc parentFullType,
186         final String JavaDoc childJ2EEType )
187     {
188         final String JavaDoc name = getSingletonName( childJ2EEType );
189         
190         return( buildContaineeObjectName( parentObjectName, parentFullType, childJ2EEType, name ) );
191     }
192     
193         public ObjectName JavaDoc
194     getDomainRootObjectName()
195     {
196         return( newObjectName( Util.makeRequiredProps( XTypes.DOMAIN_ROOT, getJMXDomain() ) ) );
197     }
198     
199     /**
200         Get the ObjectName for a singleton object.
201      */

202         public ObjectName JavaDoc
203     getSingletonObjectName( final String JavaDoc j2eeType )
204     {
205         final TypeInfo info = TypeInfos.getInstance().getInfo( j2eeType );
206         if ( info.isSubType() )
207         {
208             throw new IllegalArgumentException JavaDoc( "singletons may not be sub-types: " + j2eeType );
209         }
210         
211         final String JavaDoc props = Util.makeRequiredProps( j2eeType, getSingletonName( j2eeType ) );
212         
213         return( newObjectName( props ) );
214     }
215     
216
217     
218     /**
219         In the ObjectName, what should the "name" property contain if the object
220         is a singleton or otherwise not named? The name property is always required.
221         Possible choices include:
222         <ul>
223         <li>"none" (or similar idea)</li>
224         <li>the same as its j2eeType"</li>
225         <li>empty</li>
226         <li>random</li>
227         </ul>
228      */

229         public static String JavaDoc
230     getSingletonName( String JavaDoc j2eeType )
231     {
232         if ( TypeInfos.getInstance().getInfo( j2eeType ) == null )
233         {
234             throw new IllegalArgumentException JavaDoc( j2eeType );
235         }
236         
237         assert( ! AMX.NO_NAME.equals( MISSING_PARENT_NAME ) );
238         return( AMX.NO_NAME );
239     }
240     
241     /**
242         The name that should be used for a missing parent such as J2EEApplication=null,
243         as defined by JSR 77 specification.
244         Must be distinct from the name returned by getSingletonName( j2eeType ).
245      */

246     public static final String JavaDoc MISSING_PARENT_NAME = AMX.NULL_NAME;
247
248     
249     
250     /**
251         Get a parent ObjectName pattern using a child's ObjectName. The "parent" in
252         this context means the MBean that logically contains the child. The caller
253         must query for the actual ObjectName.
254         
255         @param childObjectName the child ObjectName
256         @return an ObjectName pattern for the parent.
257     */

258         public ObjectName JavaDoc
259     getContainerObjectNamePattern(
260         final ObjectName JavaDoc childObjectName,
261         final String JavaDoc childFullType )
262     {
263         ObjectName JavaDoc parentPattern = null;
264         
265         final String JavaDoc domain = childObjectName.getDomain();
266         
267         final String JavaDoc[] fullType = Util.getTypeArray( childFullType );
268         assert( fullType.length >= 1 );
269         
270         final String JavaDoc childType = fullType[ fullType.length - 1 ];
271         
272         if ( fullType.length == 1 )
273         {
274             final TypeInfo info = TypeInfos.getInstance().getInfo( childType );
275             
276             final String JavaDoc containedByJ2EEType = info.getContainedByJ2EEType();
277             if ( containedByJ2EEType != null )
278             {
279                 String JavaDoc parentProps = "";
280                 
281                 // special case--DomainRoot and J2EEDomain must have a name
282
// equal to the domain name
283
if ( containedByJ2EEType.equals( XTypes.DOMAIN_ROOT ) ||
284                     containedByJ2EEType.equals( J2EETypes.J2EE_DOMAIN ) )
285                 {
286                     parentProps = Util.makeRequiredProps( containedByJ2EEType, domain );
287                 }
288                 else
289                 {
290                     parentProps = Util.makeRequiredProps( containedByJ2EEType,
291                         getSingletonName( containedByJ2EEType ) );
292                 }
293                 
294                 parentPattern = Util.newObjectNamePattern( domain, parentProps );
295             }
296             else
297             {
298                 parentPattern = null; // no parent
299
}
300         }
301         else
302         {
303             /*
304                  It's a subType. A subtype may have one or more "missing" ancestors. For example,
305                  the FullType of EJBModule is J2EEServer.J2EEApplication.EJBModule. Since an EJBModule
306                  may be standalone, the J2EEApplication may not actually exist. We need to account for
307                  this, and we do so by using the convention that a name of "null" indicates such a missing
308                  ancestor.
309                  
310                  The parent is handled differently than the other properties because we have to extract
311                  j2eeType=<type>,name=<name> whereas the other properties are of the form
312                  <type>=<name>. So first, we must find the first real parent.
313              */

314             String JavaDoc parentJ2EEType = null;
315             String JavaDoc parentName = null;
316             Set JavaDoc<String JavaDoc> remainingKeys = Collections.emptySet();
317             for( int i = fullType.length - 2; i >= 0; --i )
318             {
319                 final String JavaDoc tempType = fullType[ i ];
320                 final String JavaDoc tempName = childObjectName.getKeyProperty( tempType );
321                 assert( tempName != null ) : "missing ObjectName property: " + tempType;
322                 if ( ! MISSING_PARENT_NAME.equals( tempName ) )
323                 {
324                     parentJ2EEType = tempType;
325                     parentName = tempName;
326                     final int numItems = i;
327                     remainingKeys = GSetUtil.newSet( fullType, 0, numItems );
328                     break;
329                 }
330             }
331             /*
332             trace( "\n---------------------- getContainerObjectNamePattern ---------------------------" );
333             trace( childFullType + " = " + childObjectName );
334             trace( "parentJ2EEType = " + parentJ2EEType );
335             trace( "parentName = " + parentName );
336             trace( "remainingKeys = " + toString( remainingKeys ) );
337             trace( "-------------------------------------------------" );
338             */

339             final String JavaDoc parentProps = Util.makeRequiredProps( parentJ2EEType, parentName );
340             final String JavaDoc ancestorProps = JMXUtil.getProps( childObjectName, remainingKeys );
341             
342             final String JavaDoc props = Util.concatenateProps( parentProps, ancestorProps );
343             
344             parentPattern = Util.newObjectNamePattern( domain, props );
345         }
346         
347         return( parentPattern );
348     }
349     
350
351         private static String JavaDoc
352     getFullType( final MBeanServer JavaDoc server, final ObjectName JavaDoc objectName )
353     {
354         try
355         {
356             final String JavaDoc fullType = (String JavaDoc)
357                 server.getAttribute( objectName, AMXAttributes.ATTR_FULL_TYPE );
358                 
359             return( fullType );
360         }
361         catch( Exception JavaDoc e )
362         {
363             throw new RuntimeException JavaDoc( e );
364         }
365     }
366     
367     /**
368         Get the ObjectName of the MBean logically containing the specified MBean.
369         
370         @param server
371         @param containedObjectName
372         @return the ObjectName of the MBean logically containing the child
373      */

374         public ObjectName JavaDoc
375     getContainerObjectName(
376         final MBeanServer JavaDoc server,
377         final ObjectName JavaDoc containedObjectName )
378         throws InstanceNotFoundException JavaDoc
379     {
380         debug( "getContainerObjectName: containedObjectName = " + containedObjectName );
381             
382         final String JavaDoc containedFullType = getFullType( server, containedObjectName );
383         
384         debug( "getContainerObjectName: containedFullType = " + containedFullType );
385         
386         ObjectName JavaDoc parentPattern = getContainerObjectNamePattern( containedObjectName, containedFullType );
387         ObjectName JavaDoc containingObjectName = null;
388         
389         if ( parentPattern != null )
390         {
391             debug( "getContainerObjectName: parentPattern = " + parentPattern );
392             final Set JavaDoc<ObjectName JavaDoc> names = JMXUtil.queryNames( server, parentPattern, null );
393             
394             if ( names.size() == 0 )
395             {
396                 throw new InstanceNotFoundException JavaDoc( parentPattern.toString() );
397             }
398             
399             containingObjectName = (ObjectName JavaDoc)GSetUtil.getSingleton( names );
400         }
401         else
402         {
403             final String JavaDoc j2eeType = Util.getJ2EEType( containedObjectName );
404             
405             if ( ! j2eeType.equals( XTypes.DOMAIN_ROOT ) )
406             {
407                 throw new IllegalArgumentException JavaDoc( containedObjectName.toString() );
408             }
409         }
410         
411         return( containingObjectName );
412     }
413     
414     
415     
416     
417         private static String JavaDoc
418     toString( final Object JavaDoc o )
419     {
420         return( SmartStringifier.toString( o ) );
421     }
422 }
423
424
425
426
427
428
429
430
431
432
Popular Tags