KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import java.util.Collections JavaDoc;
32
33 import com.sun.appserv.management.util.misc.Output;
34 import com.sun.appserv.management.util.misc.GSetUtil;
35 import com.sun.appserv.management.util.misc.StringUtil;
36 import com.sun.appserv.management.util.misc.MapUtil;
37 import com.sun.appserv.management.util.misc.CollectionUtil;
38
39 /**
40     Maps Attribute names to legal Java identifiers, so that they can
41     be exposed in a proxy with get/set routines
42  */

43 public class AMXAttributeNameMapperImpl implements AMXAttributeNameMapper
44 {
45     final Map JavaDoc<String JavaDoc,String JavaDoc> mDerivedToOriginal;
46     final Map JavaDoc<String JavaDoc,String JavaDoc> mOriginalToDerived;
47     final Set JavaDoc<String JavaDoc> mDerivedNames;
48     
49     final Map JavaDoc<String JavaDoc,String JavaDoc> mUnmappedOriginals;
50     
51     private Output mDebug;
52     
53     /**
54         Create a new instance which will map nothing by default.
55      */

56         public
57     AMXAttributeNameMapperImpl()
58     {
59         mDebug = null;
60         mDerivedToOriginal = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
61         mOriginalToDerived = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
62         mDerivedNames = new HashSet JavaDoc<String JavaDoc>();
63         
64         mUnmappedOriginals = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
65     }
66     
67         public void
68     setDebugOutput( final Output debugOutput )
69     {
70         mDebug = debugOutput;
71     }
72     
73         protected final void
74     debug(final Object JavaDoc o)
75     {
76         if ( mDebug != null )
77         {
78             mDebug.println( o );
79         }
80     }
81     
82     private static final char DASH_CHAR = '-';
83     private static final String JavaDoc DASH = "" + DASH_CHAR;
84     
85         private String JavaDoc
86     stripDashes( final String JavaDoc name )
87     {
88         String JavaDoc result = name;
89         
90         if ( name.indexOf( DASH ) >= 0 )
91         {
92             final StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
93             final int length = name.length();
94             
95             boolean upperNext = false;
96             for( int i = 0; i < length; ++i )
97             {
98                 final char c = name.charAt( i );
99                 
100                 if ( c == DASH_CHAR )
101                 {
102                     upperNext = true;
103                 }
104                 else
105                 {
106                     if ( upperNext )
107                     {
108                         builder.append( ("" + c ).toUpperCase() );
109                         upperNext = false;
110                     }
111                     else
112                     {
113                         builder.append( c );
114                     }
115                 }
116             }
117             
118             result = builder.toString();
119         }
120         
121         return result;
122     }
123     
124         private String JavaDoc
125     normalize( final String JavaDoc s )
126     {
127         return stripDashes( s ).toLowerCase();
128     }
129     
130     
131         private Map JavaDoc<String JavaDoc,String JavaDoc>
132     normalize( final Set JavaDoc<String JavaDoc> s )
133     {
134         final Map JavaDoc<String JavaDoc,String JavaDoc> result = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
135         
136         for( final String JavaDoc name : s )
137         {
138             result.put( normalize( name ), name );
139         }
140
141         return result;
142     }
143     
144     
145     /**
146         Attempt to match the derived name to one of the candidates.
147         This facility is used when different runtime conditions
148         present different original names which must be mapped to the
149         same derived name.
150         <p>
151         If a name is matched it is added as a mapping and the
152         original name is returned, otherwise an exception is thrown.
153      */

154         public String JavaDoc
155     matchName(
156         final String JavaDoc derivedName,
157         final String JavaDoc[] candidates )
158     {
159         String JavaDoc result = null;
160         
161         final String JavaDoc existingOriginal = derivedToOriginal( derivedName );
162         if ( existingOriginal != null )
163         {
164             throw new IllegalArgumentException JavaDoc( "Already mapped: " +
165                 derivedName + " => " + existingOriginal );
166         }
167         if ( mUnmappedOriginals.keySet().size() == 0 )
168         {
169             throw new IllegalArgumentException JavaDoc(
170                 "There are no unmapped originals, existing mappings: " +
171                 MapUtil.toString( mOriginalToDerived, ", " ) );
172         }
173         
174         for( final String JavaDoc candidate : candidates )
175         {
176             final String JavaDoc candidateNormalized = normalize( candidate );
177             
178             if ( mUnmappedOriginals.keySet().contains( candidateNormalized ) )
179             {
180                 result = mUnmappedOriginals.get( candidateNormalized );
181                 mUnmappedOriginals.remove( candidateNormalized );
182                 addMapping( result, derivedName );
183                 break;
184             }
185         }
186         
187         if ( result == null )
188         {
189             final String JavaDoc msg = derivedName + " can't be matched to " +
190                 StringUtil.toString( ", " , (Object JavaDoc[])candidates ) +
191                 ", unmapped originals: " +
192                     CollectionUtil.toString( mUnmappedOriginals.values(), ", " );
193             throw new IllegalArgumentException JavaDoc( msg );
194         }
195         
196         return result;
197     }
198     
199         public String JavaDoc
200     matchName(
201         final String JavaDoc derivedName,
202         final String JavaDoc candidate1,
203         final String JavaDoc candidate2 )
204     {
205         return matchName( derivedName, new String JavaDoc[] { candidate1, candidate2 } );
206     }
207     
208         public String JavaDoc
209     matchName(
210         final String JavaDoc derivedName,
211         final String JavaDoc candidate1 )
212     {
213         return matchName( derivedName, new String JavaDoc[] { candidate1 } );
214     }
215     
216     
217     /**
218         Attempt to match an original name with a derived one by performing
219         a case-insensitive comparison. It is OK if there are more derived
220         names than originals, or if there are more original names than
221         derived names.
222         @param derivedNames
223         @param originalNames
224      */

225         public void
226     matchNames(
227         final String JavaDoc[] derivedNames,
228         final String JavaDoc[] originalNames )
229     {
230         final Set JavaDoc<String JavaDoc> originals = GSetUtil.newSet( originalNames );
231         final Set JavaDoc<String JavaDoc> deriveds = GSetUtil.newSet( derivedNames );
232         
233         // first, eliminate identical names
234
for( final String JavaDoc original : originalNames )
235         {
236             if ( deriveds.contains( original ) )
237             {
238                 //System.out.println( "IDENTITY: " + original );
239
addMapping( original, original );
240                 originals.remove( original );
241                 deriveds.remove( original );
242             }
243         }
244         
245         // now we have remaining only those names that differ.
246
if ( originals.size() != 0 )
247         {
248             final Map JavaDoc<String JavaDoc,String JavaDoc> originalsMap = normalize( originals );
249             final Map JavaDoc<String JavaDoc,String JavaDoc> derivedsMap = normalize( deriveds );
250             
251             for( final String JavaDoc originalLowerCase : originalsMap.keySet() )
252             {
253                 final String JavaDoc original = originalsMap.get( originalLowerCase );
254                 final String JavaDoc derived = derivedsMap.get( originalLowerCase );
255                 if ( derived != null )
256                 {
257                     //System.out.println( "MATCHED: " + original + " => " + derived );
258
addMapping( original, derived );
259                 }
260                 else
261                 {
262                    // System.out.println( "NO MATCH: " + original );
263
mUnmappedOriginals.put( normalize( original ), original );
264                 }
265             }
266         }
267     }
268     
269         public Set JavaDoc<String JavaDoc>
270     getUnmappedOriginals()
271     {
272         Set JavaDoc<String JavaDoc> unmapped = Collections.emptySet();
273         
274         if ( mUnmappedOriginals.keySet().size() != 0 )
275         {
276             unmapped = new HashSet JavaDoc<String JavaDoc>( mUnmappedOriginals.values());
277         }
278         
279         return unmapped;
280     }
281     
282         public void
283     addMapping(
284         final String JavaDoc originalName,
285         final String JavaDoc derivedName )
286     {
287         mDerivedToOriginal.put( derivedName, originalName );
288         mOriginalToDerived.put( originalName, derivedName );
289         
290         if ( mUnmappedOriginals.keySet().contains( originalName ) )
291         {
292             mUnmappedOriginals.remove( originalName );
293         }
294         
295         mDerivedNames.add( derivedName );
296     }
297     
298         public void
299     dontMap( final String JavaDoc originalName )
300     {
301         addMapping( originalName, originalName );
302     }
303     
304         public String JavaDoc
305     originalToDerived( final String JavaDoc originalName )
306     {
307         final String JavaDoc derivedName = mOriginalToDerived.get( originalName );
308         return( derivedName );
309     }
310     
311         public String JavaDoc
312     derivedToOriginal( final String JavaDoc derivedName )
313     {
314         final String JavaDoc original = mDerivedToOriginal.get( derivedName );
315         return( original );
316     }
317     
318         public Set JavaDoc<String JavaDoc>
319     getDerivedNames( )
320     {
321         return( mDerivedNames );
322     }
323     
324         public String JavaDoc
325     toString()
326     {
327         final StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
328         
329         final String JavaDoc LINE_SEPARATOR = System.getProperty( "line.separator" );
330         for( final String JavaDoc name : getDerivedNames() )
331         {
332             builder.append( name );
333             builder.append( " => " );
334             builder.append( derivedToOriginal( name ) );
335             builder.append( LINE_SEPARATOR );
336         }
337         
338         return( builder.toString() );
339     }
340 }
341
342
343
344
345
346
347
348
349
350
351
352
Popular Tags