KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 import javax.management.ObjectName JavaDoc;
33
34 import com.sun.appserv.management.base.AllTypesMapper;
35 import com.sun.appserv.management.base.Util;
36 import com.sun.appserv.management.base.AMX;
37 import com.sun.appserv.management.util.misc.ClassUtil;
38 import com.sun.appserv.management.util.misc.GSetUtil;
39 import com.sun.appserv.management.util.misc.ListUtil;
40 import com.sun.appserv.management.util.misc.ObjectUtil;
41 import com.sun.appserv.management.util.jmx.JMXUtil;
42
43 import com.sun.appserv.management.util.stringifier.SmartStringifier;
44
45 /**
46     Information mapping a j2eeType to other necessary information.
47  */

48 public final class TypeInfo
49 {
50     private final TypeData mTypeData;
51     private final Set JavaDoc<String JavaDoc> mChildJ2EETypes;
52     private final Set JavaDoc<String JavaDoc> mNonChildJ2EETypes;
53     
54     private final Class JavaDoc mInterface;
55     private final Class JavaDoc mImplClass;
56     
57         public
58     TypeInfo( final TypeData typeData )
59         throws ClassNotFoundException JavaDoc
60     {
61         mTypeData = typeData;
62         mInterface = deriveInterface( typeData.getJ2EEType() );
63         mImplClass = deriveImplClass( mInterface );
64         
65         mChildJ2EETypes = new HashSet JavaDoc<String JavaDoc>();
66         mNonChildJ2EETypes = new HashSet JavaDoc<String JavaDoc>();
67     }
68     
69         public String JavaDoc
70     toString()
71     {
72         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
73         
74         final String JavaDoc PR = "\n ";
75         
76         buf.append( getJ2EEType() + ": " );
77         buf.append( PR + "parent type = " + SmartStringifier.toString( getLegalParentJ2EETypes() ) );
78         buf.append( PR + "child types = " + SmartStringifier.toString( mChildJ2EETypes ) );
79         buf.append( PR + "mbean = " + mInterface.getName() );
80         buf.append( PR + "impl = " + mImplClass.getName() );
81         buf.append( "\n" );
82         
83         return( buf.toString() );
84     }
85     
86     
87         private static Class JavaDoc
88     deriveInterface( final String JavaDoc j2eeType )
89         throws ClassNotFoundException JavaDoc
90     {
91         return( AllTypesMapper.getInstance().getInterfaceForType( j2eeType ) );
92     }
93     
94         private static String JavaDoc
95     getBaseName( String JavaDoc mbeanClassName )
96     {
97         final String JavaDoc classname = ClassUtil.stripPackageName( mbeanClassName );
98         final String JavaDoc baseName = classname;
99         
100         return( baseName );
101     }
102     
103         private static Class JavaDoc
104     deriveImplClass( final Class JavaDoc mbeanInterface )
105         throws ClassNotFoundException JavaDoc
106     {
107         final String JavaDoc baseName = getBaseName( mbeanInterface.getName() );
108         
109         final String JavaDoc implClass = baseName;
110         
111         return( locateImplClass( implClass ) );
112     }
113     
114     
115     private static final String JavaDoc[] IMPL_PACKAGES = new String JavaDoc[]
116     {
117         "com.sun.enterprise.management.ext.lb",
118         "com.sun.enterprise.management.ext.wsmgmt",
119         "com.sun.enterprise.management.ext.logging",
120                 "com.sun.enterprise.management.config",
121         "com.sun.enterprise.management.j2ee",
122         "com.sun.enterprise.management.monitor",
123         "com.sun.enterprise.management.support",
124         "com.sun.enterprise.management.deploy",
125         "com.sun.enterprise.management.ext",
126         "com.sun.enterprise.management",
127     };
128     
129     private final static String JavaDoc IMPL = "Impl";
130     
131         private static Class JavaDoc
132     locateImplClass(
133         final String JavaDoc packageName,
134         final String JavaDoc baseName )
135     {
136         if ( ! packageName.startsWith( "com.sun.enterprise.management" ) )
137         {
138             throw new RuntimeException JavaDoc( "Illegal implementation package for AMX" );
139         }
140
141         final String JavaDoc implClassname = packageName + "." + baseName + IMPL;
142         
143         Class JavaDoc implClass = null;
144         try
145         {
146             implClass = ClassUtil.getClassFromName( implClassname );
147         }
148         catch( ClassNotFoundException JavaDoc e )
149         {
150             // ignore, we'll return null
151
}
152         
153         return( implClass );
154     }
155     
156     
157         private static Class JavaDoc
158     locateImplClass(
159         final String JavaDoc classname )
160         throws ClassNotFoundException JavaDoc
161     {
162         Class JavaDoc implClass = null;
163         
164         for( int i = 0; i < IMPL_PACKAGES.length; ++i )
165         {
166             implClass = locateImplClass( IMPL_PACKAGES[ i ], classname );
167             
168             if ( implClass != null )
169             {
170                 break;
171             }
172         }
173         
174         if ( implClass == null )
175         {
176             throw new ClassNotFoundException JavaDoc(
177                 "Excpected to find implementation class " + classname + IMPL );
178         }
179         
180         return( implClass );
181     }
182     
183     
184         public void
185     addChildJ2EEType( final String JavaDoc j2eeType )
186     {
187         assert( ! j2eeType.equals( getJ2EEType() ) );
188         assert( ! mNonChildJ2EETypes.contains( j2eeType ) );
189         
190         mChildJ2EETypes.add( j2eeType );
191     }
192     
193         public void
194     addContaineeJ2EEType( final String JavaDoc j2eeType )
195     {
196         assert( ! j2eeType.equals( getJ2EEType() ) );
197         assert( ! mChildJ2EETypes.contains( j2eeType ) );
198         
199         mNonChildJ2EETypes.add( j2eeType );
200     }
201     
202         public String JavaDoc
203     getJ2EEType()
204     {
205         return( mTypeData.getJ2EEType() );
206     }
207     
208         public Set JavaDoc<String JavaDoc>
209     getLegalParentJ2EETypes()
210     {
211         return( mTypeData.getLegalParentJ2EETypes() );
212     }
213     
214         public String JavaDoc
215     getContainedByJ2EEType()
216     {
217         return( mTypeData.getContaineeByJ2EEType() );
218     }
219     
220         public boolean
221     isSubType()
222     {
223         return( mTypeData.isSubType() );
224     }
225     
226         public Class JavaDoc
227     getInterface()
228     {
229         return( mInterface );
230     }
231     
232         public Class JavaDoc
233     getImplClass()
234     {
235         return( mImplClass );
236     }
237     
238         public String JavaDoc
239     getParentJ2EEType()
240     {
241         final Set JavaDoc<String JavaDoc> legalParentJ2EETypes = getLegalParentJ2EETypes();
242         
243         if ( legalParentJ2EETypes == null )
244         {
245             throw new IllegalArgumentException JavaDoc( "no legal parent types for: " + getJ2EEType() );
246         }
247         
248         if ( legalParentJ2EETypes.size() != 1 )
249         {
250             throw new IllegalArgumentException JavaDoc( "expecting single parent for " +
251             getJ2EEType() + ", have: " + toString( legalParentJ2EETypes ) );
252         }
253         
254         return( GSetUtil.getSingleton( legalParentJ2EETypes ) );
255     }
256
257
258         public Set JavaDoc<String JavaDoc>
259     getChildJ2EETypes()
260     {
261         return( Collections.unmodifiableSet( mChildJ2EETypes ) );
262     }
263     
264         public Set JavaDoc<String JavaDoc>
265     getNonChildJ2EETypes()
266     {
267         return( Collections.unmodifiableSet( mNonChildJ2EETypes ) );
268     }
269     
270         public Set JavaDoc<String JavaDoc>
271     getContaineeJ2EETypes()
272     {
273         final Set JavaDoc<String JavaDoc> all =
274             GSetUtil.newSet( mChildJ2EETypes, mNonChildJ2EETypes );
275         
276         return( Collections.unmodifiableSet( all ) );
277     }
278     
279         public int
280     hashCode()
281     {
282         return ObjectUtil.hashCode(
283                 mTypeData, mInterface, mChildJ2EETypes, mNonChildJ2EETypes );
284     }
285     
286         public boolean
287     equals( final Object JavaDoc o )
288     {
289         if ( o == this )
290         {
291             return( true );
292         }
293         else if ( ! (o instanceof TypeInfo ) )
294         {
295             return( false );
296         }
297         
298         final TypeInfo rhs = (TypeInfo)o;
299         boolean equals = false;
300         if ( mTypeData.equals( rhs.mTypeData ) &&
301             mInterface == rhs.mInterface &&
302             mImplClass == rhs.mImplClass &&
303             mChildJ2EETypes.equals( rhs.mChildJ2EETypes ) &&
304             getLegalParentJ2EETypes().equals( rhs.getLegalParentJ2EETypes() )
305                 )
306         {
307             equals = true;
308         }
309         
310         return( equals );
311     }
312     
313     
314         private static String JavaDoc
315     toString( final Object JavaDoc o )
316     {
317         return( SmartStringifier.toString( o ) );
318     }
319     
320 }
321
322
323
324
325
326
327
328
329
Popular Tags