KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > util > jmx > AttributeNameMapperImpl


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.util.jmx;
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
35 /**
36     Maps Attribute names to legal Java identifiers, so that they can
37     be exposed in a proxy with get/set routines
38  */

39 public class AttributeNameMapperImpl implements AttributeNameMapper
40 {
41     final Map JavaDoc<String JavaDoc,String JavaDoc> mDerivedToOriginal;
42     final Map JavaDoc<String JavaDoc,String JavaDoc> mOriginalToDerived;
43     final Set JavaDoc<String JavaDoc> mAllNames;
44     final AttributeNameMangler mMangler;
45     
46     private Output mDebug;
47     
48     /**
49         Create a new instance which will map nothing by default.
50      */

51         public
52     AttributeNameMapperImpl()
53     {
54         this( null, null);
55     }
56     
57         public void
58     setDebugOutput( final Output debugOutput )
59     {
60         mDebug = debugOutput;
61     }
62     
63         protected final void
64     debug(final Object JavaDoc o)
65     {
66         if ( mDebug != null )
67         {
68             mDebug.println( o );
69         }
70     }
71     
72         public String JavaDoc
73     matchName(
74         final String JavaDoc derivedName,
75         final String JavaDoc[] candidates )
76     {
77         throw new UnsupportedOperationException JavaDoc( "matchName" );
78     }
79     
80     /**
81         Create a new instance which will map (as necessary) the specified
82         Attribute names.
83         Same as AttributeNameMapperImpl( originalNames, new AttributeNameManglerImpl() )
84      */

85         public
86     AttributeNameMapperImpl( final String JavaDoc[] originalNames )
87     {
88         this( originalNames, new AttributeNameManglerImpl( true, null ) );
89     }
90     
91     /**
92         Create a new instance which will map (as necessary) the specified
93         Attribute names
94      */

95         public
96     AttributeNameMapperImpl( final AttributeNameMangler mangler )
97     {
98         this( null, mangler );
99     }
100     
101     /**
102         Create a new instance which will map (as necessary) the specified
103         Attribute names
104      */

105         public
106     AttributeNameMapperImpl(
107         final String JavaDoc[] originalNames,
108         AttributeNameMangler mangler )
109     {
110         mDebug = null;
111         mDerivedToOriginal = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
112         mOriginalToDerived = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
113         mAllNames = new HashSet JavaDoc<String JavaDoc>();
114         mMangler = mangler == null ? new AttributeNameManglerImpl( true, null ) : mangler ;
115         
116         if ( originalNames != null )
117         {
118             deriveAll( originalNames );
119         }
120     }
121     
122     
123         public void
124     deriveAll( final String JavaDoc[] originalNames )
125     {
126         final Set JavaDoc<String JavaDoc> notRequired = new HashSet JavaDoc<String JavaDoc>();
127         final Set JavaDoc<String JavaDoc> required = new HashSet JavaDoc<String JavaDoc>();
128         
129         // first determine all names we (a) need to map and (b) don't need to map.
130
// we must not generate any mapped names that conflict with names
131
// that don't require mapping
132
for( final String JavaDoc originalName : originalNames )
133         {
134             if ( mOriginalToDerived.containsKey( originalName ) )
135             {
136                 // continue; already present (explicit mapping)
137
}
138             else if ( ! requiresMapping( originalName ) )
139             {
140                 notRequired.add( originalName );
141             }
142             else
143             {
144                 required.add( originalName );
145             }
146         }
147         
148         // add all ones that don't require mapping to map to themselves
149
for ( final String JavaDoc name : notRequired )
150         {
151             addMapping( name, name );
152         }
153         
154         // now create a mapped name for each one that requires it.
155
for( final String JavaDoc originalName : required )
156         {
157             // our preferred derivation
158
final String JavaDoc preferredDerivation = originalToDerived( originalName );
159             
160             // ensure that the derived name is not already used
161
String JavaDoc derivedName = preferredDerivation;
162             int id = 0;
163             while ( mAllNames.contains( derivedName ) ||
164                     required.contains( derivedName ) )
165             {
166                 derivedName = preferredDerivation + "_" + id;
167                 ++id;
168             }
169             addMapping( originalName, derivedName );
170         }
171     }
172     
173     
174         public boolean
175     derivedSameAsOriginal( String JavaDoc derivedName )
176     {
177         final String JavaDoc original = derivedToOriginal( derivedName );
178         final boolean theSame = original != null && derivedName.equals( original );
179             
180         return( theSame );
181     }
182     
183         public void
184     addMapping(
185         final String JavaDoc originalName,
186         final String JavaDoc derivedName )
187     {
188         mDerivedToOriginal.put( derivedName, originalName );
189         mOriginalToDerived.put( originalName, derivedName );
190         
191         mAllNames.add( derivedName );
192     }
193     
194         public void
195     dontMap( final String JavaDoc originalName )
196     {
197         debug( "dontMap: " + originalName );
198         addMapping( originalName, originalName );
199     }
200     
201         public boolean
202     requiresMapping( final String JavaDoc originalName )
203     {
204         boolean requiresMapping = true ;
205         
206         // must start with upper-case first-letter
207
final char firstChar = originalName.charAt( 0 );
208         if ( Character.isUpperCase( firstChar ) )
209         {
210             if ( Character.isJavaIdentifierStart( firstChar ) )
211             {
212                 final int length = originalName.length();
213                 
214                 requiresMapping = false;
215                 for( int i = 1; i < length; ++i )
216                 {
217                     if ( ! Character.isJavaIdentifierPart( originalName.charAt( i ) ) )
218                     {
219                         requiresMapping = true;
220                         break;
221                     }
222                 }
223             }
224         }
225         
226         return( requiresMapping );
227     }
228     
229     
230         public String JavaDoc
231     originalToDerived( String JavaDoc originalName )
232     {
233         String JavaDoc derivedName = (String JavaDoc)mOriginalToDerived.get( originalName );
234         
235         if ( derivedName == null )
236         {
237             derivedName = mMangler.mangleAttributeName( originalName );
238         }
239         
240         return( derivedName );
241     }
242     
243         public String JavaDoc
244     derivedToOriginal( String JavaDoc derivedName )
245     {
246         String JavaDoc original = (String JavaDoc)mDerivedToOriginal.get( derivedName );
247         
248         if ( original == null )
249         {
250             original = derivedName;
251         }
252         
253         return( original );
254     }
255     
256     
257         public Set JavaDoc<String JavaDoc>
258     getAttributeNames( )
259     {
260         return( mAllNames );
261     }
262     
263         public String JavaDoc
264     toString()
265     {
266         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
267         
268         buf.append( "AttributeNameMapperImpl mappings:\n" );
269         final Iterator JavaDoc iter = getAttributeNames().iterator();
270         while ( iter.hasNext() )
271         {
272             final String JavaDoc name = (String JavaDoc)iter.next();
273             buf.append( name );
274             buf.append( " => " );
275             buf.append( derivedToOriginal( name ) + "\n" );
276         }
277         
278         return( buf.toString() );
279     }
280 }
281
Popular Tags