KickJava   Java API By Example, From Geeks To Geeks.

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


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  * $Header: /cvs/glassfish/admin/mbeanapi-impl/src/java/com/sun/enterprise/management/support/MappedDelegate.java,v 1.6 2005/12/25 03:40:47 tcfujii Exp $
31  * $Revision: 1.6 $
32  * $Date: 2005/12/25 03:40:47 $
33  */

34
35 package com.sun.enterprise.management.support;
36
37 import java.util.Set JavaDoc;
38
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.MBeanServer JavaDoc;
41 import javax.management.MBeanInfo JavaDoc;
42 import javax.management.MBeanAttributeInfo JavaDoc;
43 import javax.management.Attribute JavaDoc;
44 import javax.management.AttributeList JavaDoc;
45 import javax.management.InstanceNotFoundException JavaDoc;
46 import javax.management.AttributeNotFoundException JavaDoc;
47 import javax.management.InvalidAttributeValueException JavaDoc;
48 import javax.management.ReflectionException JavaDoc;
49 import javax.management.MBeanException JavaDoc;
50 import javax.management.ReflectionException JavaDoc;
51
52 import com.sun.enterprise.management.support.AMXAttributeNameMapper;
53 import com.sun.appserv.management.util.stringifier.SmartStringifier;
54
55 import com.sun.appserv.management.util.jmx.JMXUtil;
56 import com.sun.appserv.management.util.misc.ArrayConversion;
57 import com.sun.appserv.management.util.misc.CollectionUtil;
58 import com.sun.appserv.management.util.misc.StringUtil;
59
60
61 /**
62     Delegate class which wraps another, and maps its Attributes
63     to different names.
64  */

65 public class MappedDelegate extends DelegateBase
66 {
67     /**
68         MBeanInfo in which names have not been mapped (original).
69      */

70     private final Delegate mDelegate;
71     
72     /**
73         MBeanInfo in which names have not been mapped (original).
74      */

75     private final MBeanInfo JavaDoc mUnmappedMBeanInfo;
76     
77     /**
78         MBeanInfo in which names have been mapped.
79      */

80     private final MBeanInfo JavaDoc mMappedMBeanInfo;
81     
82     /**
83         the Attribute names, before mapping
84      */

85     private final Set JavaDoc<String JavaDoc> mUnmappedAttributeNames;
86     
87     /**
88         the mapper
89      */

90     private final AMXAttributeNameMapper mAttributeNameMapper;
91     
92     
93     /**
94         New instance which wraps the supplied delegate using the specified mapper.
95      */

96         public
97     MappedDelegate(
98         final Delegate delegate,
99         final AMXAttributeNameMapper mapper )
100     {
101         super( "MappedDelegate." + (delegate == null ? "null" : delegate.getID()), null);
102         
103         mDelegate = delegate;
104         
105         mUnmappedMBeanInfo = getUnmappedMBeanInfoFresh();
106         
107         final String JavaDoc[] unmappedAttributeNames =
108             JMXUtil.getAttributeNames( mUnmappedMBeanInfo.getAttributes() );
109         mUnmappedAttributeNames = ArrayConversion.arrayToSet( unmappedAttributeNames );
110         
111         mAttributeNameMapper = mapper;
112         //mAttributeNameMapper.deriveAll( unmappedAttributeNames );
113

114         mMappedMBeanInfo = mapMBeanInfo( mDelegate.getMBeanInfo() );
115     }
116     
117     /**
118         Get the unmapped name for a mapped Attribute name. If the name has
119         not been mapped, it is returned unchanged.
120      */

121         protected String JavaDoc
122     derivedToOriginal( final String JavaDoc mappedName )
123     {
124         return( mAttributeNameMapper.derivedToOriginal( mappedName ) );
125     }
126     
127     /**
128         Return the unmapped version of an mapped Attribute.
129      */

130         private Attribute JavaDoc
131     derivedToOriginal( final Attribute JavaDoc mappedAttr )
132     {
133         final String JavaDoc unmappedName = derivedToOriginal( mappedAttr.getName() );
134         
135         return( new Attribute JavaDoc( unmappedName, mappedAttr.getValue() ) );
136     }
137     
138     /**
139         Call derivedToOriginal( name ) for each mapped name.
140      */

141         protected String JavaDoc[]
142     derivedToOriginal( final String JavaDoc[] mappedNames )
143     {
144         final String JavaDoc[] unmappedNames = new String JavaDoc[ mappedNames.length ];
145         
146         for( int i = 0; i < unmappedNames.length; ++i )
147         {
148             unmappedNames[ i ] = derivedToOriginal( mappedNames[ i ] );
149         }
150         
151         return( unmappedNames );
152     }
153     
154     
155     /**
156         Get the mapped name for an unmapped name. If the name has
157         not been mapped, it is returned unchanged.
158      */

159         private String JavaDoc
160     originalToDerived( final String JavaDoc unmappedName )
161     {
162         return( mAttributeNameMapper.originalToDerived( unmappedName ) );
163     }
164     
165     
166     /**
167      */

168         private Attribute JavaDoc
169     originalToDerived( final Attribute JavaDoc unmappedAttr )
170     {
171         final String JavaDoc mappedName = originalToDerived( unmappedAttr.getName() );
172         return( new Attribute JavaDoc( mappedName, unmappedAttr.getValue() ) );
173     }
174     
175     
176         protected AttributeList JavaDoc
177     derivedToOriginal( final AttributeList JavaDoc mappedAttrs )
178     {
179         // first, create a new list which uses the unmapped names
180
final int numAttrs = mappedAttrs.size();
181         final AttributeList JavaDoc unmappedAttrs = new AttributeList JavaDoc();
182         for( int i = 0; i < numAttrs; ++i )
183         {
184             unmappedAttrs.add( derivedToOriginal( (Attribute JavaDoc)mappedAttrs.get( i ) ) );
185         }
186         
187         return( unmappedAttrs );
188     }
189     
190         protected AttributeList JavaDoc
191     originalToDerived( final AttributeList JavaDoc unmappedAttrs )
192     {
193         // first, create a new list which uses the unmapped names
194
final int numAttrs = unmappedAttrs.size();
195         final AttributeList JavaDoc mappedAttrs = new AttributeList JavaDoc();
196         for( int i = 0; i < numAttrs; ++i )
197         {
198             mappedAttrs.add( originalToDerived( (Attribute JavaDoc)unmappedAttrs.get( i ) ) );
199         }
200         
201         
202         return( unmappedAttrs );
203     }
204     
205         public boolean
206     supportsAttribute( final String JavaDoc mappedName )
207     {
208         return( mAttributeNameMapper.getDerivedNames().contains( mappedName ) );
209     }
210     
211         private void
212     checkSupported( final String JavaDoc mappedName )
213         throws AttributeNotFoundException JavaDoc
214     {
215         if ( ! supportsAttribute( mappedName ) )
216         {
217             debug( "Attribute " + mappedName + " is not supported, have: " +
218                 CollectionUtil.toString( mAttributeNameMapper.getDerivedNames(), ", ") );
219             throw new AttributeNotFoundException JavaDoc( mappedName );
220         }
221     }
222     
223         public Object JavaDoc
224     getAttribute( final String JavaDoc mappedName )
225         throws AttributeNotFoundException JavaDoc
226     {
227         checkSupported( mappedName );
228         
229         final String JavaDoc unmappedName = mAttributeNameMapper.derivedToOriginal( mappedName );
230         
231         return( mDelegate.getAttribute( unmappedName ) );
232     }
233
234         public AttributeList JavaDoc
235     getAttributes( final String JavaDoc[] mappedNames )
236     {
237         final AttributeList JavaDoc unmappedResults =
238             mDelegate.getAttributes( derivedToOriginal( mappedNames ) );
239         
240         return( originalToDerived( unmappedResults ) );
241     }
242     
243         public void
244     setAttribute( final Attribute JavaDoc mappedAttr )
245         throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc
246     {
247         checkSupported( mappedAttr.getName() );
248         
249         final Attribute JavaDoc unmappedAttr = derivedToOriginal( mappedAttr );
250         
251         mDelegate.setAttribute( unmappedAttr );
252     }
253     
254         public AttributeList JavaDoc
255     setAttributes( final AttributeList JavaDoc mappedAttrs )
256     {
257         // first, create a new list which uses the unmapped names
258
final AttributeList JavaDoc unmappedAttrs = derivedToOriginal( mappedAttrs );
259         
260         // get the Attributes using the unmapped names
261
final AttributeList JavaDoc unmappedResults =
262                         mDelegate.setAttributes( unmappedAttrs );
263         
264         return( originalToDerived( unmappedResults ) );
265     }
266     
267         protected MBeanInfo JavaDoc
268     getUnmappedMBeanInfoFresh()
269     {
270         return( mDelegate.getMBeanInfo( ) );
271     }
272     
273         protected MBeanInfo JavaDoc
274     getUnmappedMBeanInfo()
275     {
276         return( mUnmappedMBeanInfo );
277     }
278     
279         private MBeanAttributeInfo JavaDoc
280     mapAttributeInfo( final MBeanAttributeInfo JavaDoc unmappedInfo )
281     {
282         String JavaDoc mappedName = originalToDerived( unmappedInfo.getName() );
283         
284         if ( mappedName == null )
285         {
286             mappedName = unmappedInfo.getName();
287         }
288         
289         final MBeanAttributeInfo JavaDoc mappedInfo =
290                             new MBeanAttributeInfo JavaDoc(
291                                 mappedName,
292                                 unmappedInfo.getType(),
293                                 unmappedInfo.getDescription(),
294                                 unmappedInfo.isReadable(),
295                                 unmappedInfo.isWritable(),
296                                 unmappedInfo.isIs()
297                             );
298         
299         return( mappedInfo );
300     }
301     
302         private MBeanAttributeInfo JavaDoc[]
303     mapAttributeInfos( final MBeanAttributeInfo JavaDoc[] unmappedInfos )
304     {
305         final MBeanAttributeInfo JavaDoc[] mappedInfos =
306                 new MBeanAttributeInfo JavaDoc[ unmappedInfos.length ];
307         
308         for ( int i = 0; i < unmappedInfos.length; ++i )
309         {
310             final String JavaDoc attrName = unmappedInfos[ i ].getName();
311             assert( attrName != null );
312             
313             mappedInfos[ i ] = mapAttributeInfo( unmappedInfos[ i ] );
314             assert( mappedInfos[ i ].getName() != null );
315             debug( "Mapped " + unmappedInfos[ i ].getName() + " to " + mappedInfos[i].getName() );
316         }
317         return( mappedInfos );
318     }
319
320     /**
321         Create an MBeanInfo which contains the mapped names.
322      */

323         private MBeanInfo JavaDoc
324     mapMBeanInfo( final MBeanInfo JavaDoc unmappedMBeanInfo )
325     {
326         final MBeanAttributeInfo JavaDoc[] mappedAttributeInfos =
327                 mapAttributeInfos( unmappedMBeanInfo.getAttributes() );
328                 
329         final MBeanInfo JavaDoc mappedInfo = new MBeanInfo JavaDoc(
330             unmappedMBeanInfo.getClassName(),
331             unmappedMBeanInfo.getDescription(),
332             mappedAttributeInfos,
333             unmappedMBeanInfo.getConstructors(),
334             unmappedMBeanInfo.getOperations(),
335             unmappedMBeanInfo.getNotifications()
336             );
337         
338         return( mappedInfo );
339     }
340     
341     
342         protected MBeanInfo JavaDoc
343     getMappedMBeanInfo()
344     {
345         return( mMappedMBeanInfo );
346     }
347     
348         public MBeanInfo JavaDoc
349     getMBeanInfo()
350     {
351         return( getMappedMBeanInfo() );
352     }
353     
354     /**
355      */

356         public String JavaDoc[]
357     getAttributeNames( )
358     {
359         final String JavaDoc[] names =
360             JMXUtil.getAttributeNames( getMBeanInfo().getAttributes() );
361             
362         return( names );
363     }
364     
365
366     
367     /**
368         Just pass it along to the Delegate
369      */

370         public final Object JavaDoc
371     invoke(
372         String JavaDoc operationName,
373         Object JavaDoc[] args,
374         String JavaDoc[] types )
375     {
376         final Object JavaDoc result = mDelegate.invoke( operationName, args, types );
377         
378         return( result );
379     }
380 }
381
382
383
384
385
386
387
388
389
Popular Tags