KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > support > AliasMgr


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  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/support/AliasMgr.java,v 1.3 2005/12/25 03:45:43 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:43 $
28  */

29  
30
31 package com.sun.cli.jmx.support;
32
33 import java.util.Set JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.Arrays JavaDoc;
37 import java.util.HashSet JavaDoc;
38
39
40 /*
41     This MBean must be modified to store its aliases within domain.xml. For now, it uses
42     an internal implementation.
43  */

44 public final class AliasMgr implements AliasMgrMBean
45 {
46     final AliasMgrSPI mImpl;
47     
48         public
49     AliasMgr( AliasMgrSPI impl )
50     {
51         mImpl = impl;
52     }
53     
54         private static void
55     checkLegalAlias( String JavaDoc aliasName )
56     {
57         if ( ! isValidAlias( aliasName ) )
58         {
59             throw new IllegalArgumentException JavaDoc( "illegal alias name: \"" + aliasName + "\"");
60         }
61     }
62     
63         public void
64     createAlias( String JavaDoc aliasName, String JavaDoc objectName ) throws Exception JavaDoc
65     {
66         checkLegalAlias( aliasName );
67         
68         if ( resolveAlias( aliasName ) != null )
69         {
70             // illegal to create an already-existing alias
71
throw new IllegalArgumentException JavaDoc( "alias already exists: " + aliasName );
72         }
73         
74         mImpl.create( aliasName, objectName );
75     }
76     
77         public String JavaDoc
78     resolveAlias( String JavaDoc aliasName )
79     {
80         checkLegalAlias( aliasName );
81         
82         final String JavaDoc result = (String JavaDoc)mImpl.get( aliasName );
83         
84         return( result );
85     }
86     
87         public void
88     deleteAlias( String JavaDoc aliasName ) throws Exception JavaDoc
89     {
90         checkLegalAlias( aliasName );
91         
92         mImpl.delete( aliasName );
93     }
94     
95         public String JavaDoc []
96     getAliases() throws Exception JavaDoc
97     {
98         return( listAliases( ) );
99     }
100     
101         public String JavaDoc []
102     listAliases( ) throws Exception JavaDoc
103     {
104         return( listAliases( false ) );
105     }
106     
107         public String JavaDoc []
108     listAliases( boolean showValues ) throws Exception JavaDoc
109     {
110         final Set JavaDoc keys = mImpl.getNames();
111         final int numKeys = keys.size();
112         
113         final String JavaDoc [] aliases = new String JavaDoc[ numKeys ];
114         final Iterator JavaDoc iter = keys.iterator();
115         
116         for( int i = 0; i < numKeys; ++i )
117         {
118             final String JavaDoc key = (String JavaDoc)iter.next();
119             
120             if ( showValues )
121             {
122                 aliases[ i ] = key + "=" + resolveAlias( key );
123             }
124             else
125             {
126                 aliases[ i ] = key;
127             }
128         }
129         
130         Arrays.sort( aliases );
131         
132         return( aliases );
133     }
134
135      
136         public static boolean
137     isValidAlias( final String JavaDoc str )
138     {
139         final int strLength = str.length();
140
141         boolean isValid = strLength != 0;
142         if ( isValid )
143         {
144             // this can be done more efficiently with a BitMap or Set, but who cares
145
for( int i = 0; i < strLength; ++i )
146             {
147                 final char theChar = str.charAt( i );
148                 
149                 if ( ! LegalAliasChars.isLegalAliasChar( theChar ) )
150                 {
151                     isValid = false;
152                     break;
153                 }
154                 
155             }
156         }
157         
158         return( isValid );
159     }
160 }
161
162
163
164 final class LegalAliasChars
165 {
166         private
167     LegalAliasChars()
168     {
169         // disallow instantiation
170
}
171
172     /*
173         Aliases must not allow delimiters used by ObjectNames.
174      */

175      private final static String JavaDoc LEGAL_ALIAS_CHARS =
176         "abcdefghijklmnopqrstuvwxyz" + // lower-case letters
177
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // upper-case letters
178
"0123456789" + // digits
179
"-_."; // useful separators
180

181      private final static boolean [] LEGAL_ALIAS_CHARS_BITMAP = initLegalAliasChars();
182
183         private static boolean []
184     initLegalAliasChars()
185     {
186         final boolean [] legals = new boolean [ 128 ];
187         
188         for( int i = 0; i < LEGAL_ALIAS_CHARS.length(); ++i )
189         {
190             legals[ i ] = false;
191         }
192         
193         for( int i = 0; i < LEGAL_ALIAS_CHARS.length(); ++i )
194         {
195             final char theChar = LEGAL_ALIAS_CHARS.charAt( i );
196             
197             legals[ (int)theChar ] = true;
198         }
199         return( legals );
200     }
201     
202         public static boolean
203     isLegalAliasChar( char theChar )
204     {
205         final int intValue = (int)theChar;
206         
207         return( intValue < LEGAL_ALIAS_CHARS_BITMAP.length &&
208                 LEGAL_ALIAS_CHARS_BITMAP[ intValue ] );
209     }
210 }
211
212
Popular Tags