KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Set JavaDoc;
37 import java.util.Collections JavaDoc;
38
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.MBeanInfo JavaDoc;
41 import javax.management.MBeanOperationInfo JavaDoc;
42 import javax.management.MBeanAttributeInfo JavaDoc;
43
44 import com.sun.appserv.management.base.AMX;
45 import com.sun.appserv.management.base.XTypes;
46 import com.sun.appserv.management.base.SystemInfo;
47 import com.sun.appserv.management.base.Util;
48 import com.sun.appserv.management.util.misc.StringUtil;
49 import com.sun.appserv.management.util.jmx.JMXUtil;
50
51 import com.sun.appserv.management.util.misc.ClassUtil;
52 import com.sun.appserv.management.base.Container;
53
54
55 /**
56     Convert MBeanInfo which was derived from public client-side interface into
57     MBeanInfo useable by server-side MBeans.
58  */

59 final class MBeanInfoConverter
60 {
61     private final Map JavaDoc<Class JavaDoc,MBeanInfo JavaDoc> mConvertedInfos;
62     private static MBeanInfoConverter INSTANCE = null;
63     
64         private
65     MBeanInfoConverter( )
66     {
67         mConvertedInfos = Collections.synchronizedMap( new HashMap JavaDoc<Class JavaDoc,MBeanInfo JavaDoc>() );
68     }
69     
70         public static synchronized MBeanInfoConverter
71     getInstance()
72     {
73         if ( INSTANCE == null )
74         {
75             INSTANCE = new MBeanInfoConverter();
76         }
77         
78         return( INSTANCE );
79     }
80     
81         protected static Class JavaDoc
82     toClass( final String JavaDoc className )
83     {
84         try
85         {
86             return( ClassUtil.getClassFromName( className ) );
87         }
88         catch( ClassNotFoundException JavaDoc e )
89         {
90             assert( false );
91             throw new RuntimeException JavaDoc( e );
92         }
93     }
94     
95     private final static String JavaDoc OBJECT_NAME_SUFFIX = "ObjectName";
96     private final static String JavaDoc SET_SUFFIX = "Set";
97     private final static String JavaDoc MAP_SUFFIX = "Map";
98     private final static String JavaDoc LIST_SUFFIX = "List";
99     private final static String JavaDoc OBJECT_NAME_MAP_SUFFIX = OBJECT_NAME_SUFFIX + MAP_SUFFIX;
100     private final static String JavaDoc OBJECT_NAME_SET_SUFFIX = OBJECT_NAME_SUFFIX + SET_SUFFIX;
101     private final static String JavaDoc OBJECT_NAME_LIST_SUFFIX = OBJECT_NAME_SUFFIX + LIST_SUFFIX;
102         
103         
104         protected void
105     trace( Object JavaDoc o )
106     {
107         //System.out.println( com.sun.appserv.management.util.stringifier.SmartStringifier.toString( o ) );
108
}
109     
110         private final MBeanAttributeInfo JavaDoc
111     convert( final MBeanAttributeInfo JavaDoc info )
112     {
113         MBeanAttributeInfo JavaDoc result = info;
114         final String JavaDoc name = info.getName();
115         
116         final Class JavaDoc type = toClass( info.getType() );
117         
118         String JavaDoc newName = null;
119         Class JavaDoc newType = type;
120         
121         if ( AMX.class.isAssignableFrom( type ) )
122         {
123             newName = name + OBJECT_NAME_SUFFIX;
124             newType = ObjectName JavaDoc.class;
125         }
126         else if ( name.endsWith( SET_SUFFIX ) &&
127              ! name.endsWith( OBJECT_NAME_SET_SUFFIX ) )
128         {
129             newName = convertMethodName( name, SET_SUFFIX, OBJECT_NAME_SET_SUFFIX );
130         }
131         else if ( name.endsWith( MAP_SUFFIX ) &&
132              ! name.endsWith( OBJECT_NAME_MAP_SUFFIX )
133             )
134         {
135             newName = convertMethodName( name, MAP_SUFFIX, OBJECT_NAME_MAP_SUFFIX );
136         }
137         else if ( name.endsWith( LIST_SUFFIX ) &&
138              ! name.endsWith( OBJECT_NAME_LIST_SUFFIX )
139             )
140         {
141             newName = convertMethodName( name, LIST_SUFFIX, OBJECT_NAME_LIST_SUFFIX );
142         }
143         
144         if ( newName != null )
145         {
146             trace( ClassUtil.stripPackageName( type.getName() ) + " " + name +
147                 " => " + ClassUtil.stripPackageName( newType.getName() ) + " " + newName );
148                 
149             result = new MBeanAttributeInfo JavaDoc( newName, newType.getName(),
150                 info.getDescription(), info.isReadable(), info.isWritable(), info.isIs() );
151         }
152         
153         return( result );
154     }
155     
156         protected static String JavaDoc
157     convertMethodName(
158         final String JavaDoc srcName,
159         final String JavaDoc srcSuffix,
160         final String JavaDoc resultSuffix )
161     {
162         return( StringUtil.replaceSuffix( srcName, srcSuffix, resultSuffix ) );
163     }
164     
165         private final MBeanOperationInfo JavaDoc
166     convert( final MBeanOperationInfo JavaDoc info )
167     {
168         MBeanOperationInfo JavaDoc result = info;
169         final String JavaDoc name = info.getName();
170         
171         final Class JavaDoc returnClass = toClass( info.getReturnType() );
172         
173         String JavaDoc newName = null;
174         Class JavaDoc newReturnClass = returnClass;
175         
176         if ( AMX.class.isAssignableFrom( returnClass ) )
177         {
178             /*
179                 Anything returning an AMX (or sub-interface) must necessarily
180                 return an ObjectName from the MBean.
181              */

182             newReturnClass = ObjectName JavaDoc.class;
183             
184             /*
185                 Except for create() and createAbc() methods, we tack on
186                 OBJECT_NAME_SUFFIX to the MBean operation. AMXProxyHandler
187                 expects this convention.
188              */

189             if ( name.startsWith( "create" ) )
190             {
191                 newName = name;
192             }
193             else
194             {
195                 newName = name + OBJECT_NAME_SUFFIX;
196             }
197         }
198         else if ( Map JavaDoc.class.isAssignableFrom( returnClass ) &&
199             name.endsWith( MAP_SUFFIX ) &&
200             ! name.endsWith( OBJECT_NAME_MAP_SUFFIX ) )
201         {
202             newName = convertMethodName( name, MAP_SUFFIX, OBJECT_NAME_MAP_SUFFIX );
203         }
204         else if ( Set JavaDoc.class.isAssignableFrom( returnClass ) &&
205             name.endsWith( SET_SUFFIX ) &&
206             ! name.endsWith( OBJECT_NAME_SET_SUFFIX ))
207         {
208             newName = convertMethodName( name, SET_SUFFIX, OBJECT_NAME_SET_SUFFIX );
209         }
210         else if ( Set JavaDoc.class.isAssignableFrom( returnClass ) &&
211             name.endsWith( LIST_SUFFIX ) &&
212             ! name.endsWith( OBJECT_NAME_LIST_SUFFIX ))
213         {
214             newName = convertMethodName( name, LIST_SUFFIX, OBJECT_NAME_LIST_SUFFIX );
215         }
216         
217         if ( newName != null )
218         {
219             trace( ClassUtil.stripPackageName( returnClass.getName() ) + " " + name + "(...)" +
220                 " => " + ClassUtil.stripPackageName( newReturnClass.getName() ) + " " + newName + "(...)" );
221                 
222             result = new MBeanOperationInfo JavaDoc(
223                 newName,
224                 info.getDescription(),
225                 info.getSignature(),
226                 newReturnClass.getName(),
227                 info.getImpact() );
228         }
229         
230         return( result );
231     }
232
233         private final MBeanAttributeInfo JavaDoc[]
234     convertAttributes( final MBeanAttributeInfo JavaDoc[] origInfos )
235     {
236         final MBeanAttributeInfo JavaDoc[] infos = new MBeanAttributeInfo JavaDoc[ origInfos.length ];
237         
238         for( int i = 0; i < infos.length; ++i )
239         {
240             infos[ i ] = convert( origInfos[ i ] );
241         }
242         
243         return( infos );
244     }
245     
246         private final MBeanOperationInfo JavaDoc[]
247     convertOperations( final MBeanOperationInfo JavaDoc[] origInfos )
248     {
249         final MBeanOperationInfo JavaDoc[] infos = new MBeanOperationInfo JavaDoc[ origInfos.length ];
250         
251         for( int i = 0; i < infos.length; ++i )
252         {
253             infos[ i ] = convert( origInfos[ i ] );
254         }
255         
256         return( infos );
257     }
258     
259     
260         private final MBeanInfo JavaDoc
261     find( final Class JavaDoc theInterface )
262     {
263         return( mConvertedInfos.get( theInterface ) );
264     }
265     
266         public final MBeanInfo JavaDoc
267     convert(
268         final Class JavaDoc theInterface,
269         final MBeanAttributeInfo JavaDoc[] extraAttributeInfos )
270     {
271         MBeanInfo JavaDoc result = null;
272         
273         // synchronize on theInterface so that we only ever do it once
274
synchronized( theInterface )
275         {
276             result = find( theInterface );
277             if ( result == null )
278             {
279                 final MBeanInfo JavaDoc origInfo = JMXUtil.interfaceToMBeanInfo( theInterface );
280                 
281                 final MBeanAttributeInfo JavaDoc[] origAttrInfos = origInfo.getAttributes();
282                 final MBeanAttributeInfo JavaDoc[] attrInfos = extraAttributeInfos == null ?
283                     origAttrInfos :
284                     JMXUtil.mergeMBeanAttributeInfos( origAttrInfos, extraAttributeInfos);
285                 
286                 result = new MBeanInfo JavaDoc(
287                         origInfo.getClassName(),
288                         origInfo.getDescription(),
289                         convertAttributes( attrInfos ),
290                         origInfo.getConstructors(),
291                         convertOperations( origInfo.getOperations() ),
292                         origInfo.getNotifications() );
293                 
294                 mConvertedInfos.put( theInterface, result );
295             }
296         }
297         
298         return( result );
299     }
300 }
301
302
303
304
305
306
307
308
309
Popular Tags