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.io.File ; 37 import java.io.IOException ; 38 import java.io.FileWriter ; 39 import java.io.FileReader ; 40 41 public final class AliasMgrHashMapImpl implements AliasMgrSPI 42 { 43 final HashMap mMap; 44 String mFilename; 45 46 public final static String DEFAULT_FILENAME = "aliases.txt"; 47 48 public 49 AliasMgrHashMapImpl() 50 { 51 mMap = new HashMap (); 52 } 53 54 public void 55 create( String aliasName, String value ) throws Exception  56 { 57 mMap.put( aliasName, value ); 58 59 save(); 60 } 61 62 public String  63 get( String aliasName ) 64 { 65 return( (String )mMap.get( aliasName ) ); 66 } 67 68 public void 69 delete( String aliasName ) 70 throws Exception  71 { 72 mMap.remove( aliasName ); 73 save(); 74 } 75 76 public Set  77 getNames() 78 { 79 return( mMap.keySet() ); 80 } 81 82 public void 83 save( ) 84 throws IOException  85 { 86 if ( mFilename != null ) 87 { 88 save( new File ( mFilename ) ); 89 } 90 } 91 92 public void 93 save( File theFile ) 94 throws IOException  95 { 96 mFilename = theFile.getPath(); 97 98 final Set names = getNames(); 99 final Iterator iter = names.iterator(); 100 final String [] pairs = new String [ names.size() ]; 101 102 for( int i = 0; i < pairs.length; ++i ) 103 { 104 final String name = (String )iter.next(); 105 106 pairs[ i ] = name + "=" + get( name ); 107 } 108 109 final FileWriter out = new FileWriter ( theFile ); 110 111 for( int i = 0; i < pairs.length; ++i ) 112 { 113 out.write( pairs[ i ] + "\n" ); 114 } 115 116 out.close(); 117 } 118 119 private String  120 readLine( FileReader in ) 121 throws IOException  122 { 123 StringBuffer buf = new StringBuffer (); 124 125 while ( true ) 126 { 127 final int i =in.read(); 128 if ( i < 0 ) 129 { 130 return( null ); 131 } 132 133 final char theChar = (char)i; 134 if ( theChar == '\n' || theChar == '\r' ) 135 { 136 if ( buf.length() == 0 ) 138 continue; 139 break; 140 } 141 142 buf.append( theChar ); 143 } 144 return( buf.toString() ); 145 } 146 147 public void 148 load( File theFile ) 149 throws Exception  150 { 151 mFilename = theFile.getPath(); 152 153 final FileReader in = new FileReader ( theFile ); 154 155 while ( true ) 156 { 157 final String pair = readLine( in ); 158 if ( pair == null ) 159 break; 160 161 final int separatorOffset = pair.indexOf( '=' ); 162 163 final String name = pair.substring( 0, separatorOffset ); 164 final String value = pair.substring( separatorOffset + 1, pair.length() ); 165 166 create( name, value ); 167 } 168 169 in.close(); 170 } 171 }; 172 173 174 | Popular Tags |