KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > base > TypesMapper


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.appserv.management.base;
24
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.lang.reflect.Field JavaDoc;
31
32
33 /**
34     Map all types from XTypes to their respective MBean interfaces.
35     
36     @see AllTypesMapper
37     @see com.sun.appserv.management.j2ee.J2EETypesMapper
38  */

39 public class TypesMapper
40 {
41     private final Map JavaDoc<String JavaDoc,Class JavaDoc> mMap;
42     
43         public
44     TypesMapper( final Class JavaDoc[] interfaces )
45     {
46         mMap = init( interfaces );
47     }
48     
49     /**
50         Map all types to interfaces.
51      */

52         private Map JavaDoc<String JavaDoc,Class JavaDoc>
53     init( final Class JavaDoc[] interfaces )
54     {
55         final Map JavaDoc<String JavaDoc,Class JavaDoc> m = new HashMap JavaDoc<String JavaDoc,Class JavaDoc>();
56
57         for( int i = 0; i < interfaces.length; ++i )
58         {
59             final Class JavaDoc theInterface = interfaces[ i ];
60             
61             try
62             {
63                 final Field JavaDoc field = theInterface.getField( "J2EE_TYPE" );
64                 final String JavaDoc value = (String JavaDoc)field.get( theInterface );
65                 if ( m.containsKey( value ) )
66                 {
67                     final String JavaDoc msg =
68                         "TypesMapper: key already present: " +
69                         value + " for " + theInterface.getName();
70                     
71                     assert( false ): msg;
72                     throw new RuntimeException JavaDoc( msg );
73                 }
74                 m.put( value, theInterface );
75             }
76             catch( Exception JavaDoc e )
77             {
78                 e.printStackTrace();
79                 assert( false );
80                 throw new IllegalArgumentException JavaDoc( theInterface.getName() );
81             }
82         }
83         
84         return( m );
85     }
86     
87     /**
88         Return the Class associated with a given type.
89      */

90         public Class JavaDoc
91     getInterfaceForType( final String JavaDoc type )
92     {
93         final Class JavaDoc theClass = mMap.get( type );
94         
95         return( theClass );
96     }
97     
98         public Set JavaDoc<String JavaDoc>
99     getJ2EETypes()
100     {
101         return Collections.unmodifiableSet( mMap.keySet() );
102     }
103     
104         public Set JavaDoc<Class JavaDoc>
105     getClasses()
106     {
107         final Set JavaDoc<Class JavaDoc> classes = new HashSet JavaDoc<Class JavaDoc>();
108         classes.addAll( mMap.values() );
109         return Collections.unmodifiableSet( classes );
110     }
111 }
112
113
114
115
116
117
118
119
120
121
122
123
Popular Tags