1 23 24 29 30 31 package com.sun.cli.jmx.support; 32 33 import java.util.Set ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Arrays ; 37 import java.util.HashSet ; 38 39 40 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 aliasName ) 56 { 57 if ( ! isValidAlias( aliasName ) ) 58 { 59 throw new IllegalArgumentException ( "illegal alias name: \"" + aliasName + "\""); 60 } 61 } 62 63 public void 64 createAlias( String aliasName, String objectName ) throws Exception 65 { 66 checkLegalAlias( aliasName ); 67 68 if ( resolveAlias( aliasName ) != null ) 69 { 70 throw new IllegalArgumentException ( "alias already exists: " + aliasName ); 72 } 73 74 mImpl.create( aliasName, objectName ); 75 } 76 77 public String 78 resolveAlias( String aliasName ) 79 { 80 checkLegalAlias( aliasName ); 81 82 final String result = (String )mImpl.get( aliasName ); 83 84 return( result ); 85 } 86 87 public void 88 deleteAlias( String aliasName ) throws Exception 89 { 90 checkLegalAlias( aliasName ); 91 92 mImpl.delete( aliasName ); 93 } 94 95 public String [] 96 getAliases() throws Exception 97 { 98 return( listAliases( ) ); 99 } 100 101 public String [] 102 listAliases( ) throws Exception 103 { 104 return( listAliases( false ) ); 105 } 106 107 public String [] 108 listAliases( boolean showValues ) throws Exception 109 { 110 final Set keys = mImpl.getNames(); 111 final int numKeys = keys.size(); 112 113 final String [] aliases = new String [ numKeys ]; 114 final Iterator iter = keys.iterator(); 115 116 for( int i = 0; i < numKeys; ++i ) 117 { 118 final String key = (String )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 str ) 138 { 139 final int strLength = str.length(); 140 141 boolean isValid = strLength != 0; 142 if ( isValid ) 143 { 144 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 } 171 172 175 private final static String LEGAL_ALIAS_CHARS = 176 "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "-_."; 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 |