1 23 24 29 30 31 package com.sun.cli.jmx.cmd; 32 33 import java.util.Properties ; 34 import java.util.Iterator ; 35 import java.util.Enumeration ; 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 import java.io.FileOutputStream ; 39 import java.io.FileNotFoundException ; 40 import java.io.IOException ; 41 42 class ValueData 43 { 44 Object mValue; 45 boolean mAllowPersistence; 46 47 ValueData( Object value ) 48 { 49 mValue = value; 50 mAllowPersistence = false; 51 } 52 53 ValueData( Object value, boolean allowPersistence ) 54 { 55 mValue = value; 56 mAllowPersistence = allowPersistence; 57 } 58 } 59 60 public final class CmdEnvImpl implements CmdEnv 61 { 62 private final java.util.HashMap mEnv; 63 64 65 private final static void 66 dm( Object o ) 67 { 68 System.out.println( o.toString() ); 69 } 70 71 public 72 CmdEnvImpl() 73 { 74 mEnv = new java.util.HashMap (); 75 } 76 77 public void 78 put( String key, Object value, boolean allowPersistence ) 79 { 80 final ValueData data = new ValueData( value, allowPersistence); 81 82 mEnv.put( key, data ); 83 } 84 85 public Object 86 get( String key ) 87 { 88 final ValueData data = (ValueData)mEnv.get( key ); 89 final Object value = (data != null) ? data.mValue : null; 90 91 return( value ); 92 } 93 94 public void 95 remove( String key ) 96 { 97 mEnv.remove( key ); 98 } 99 100 public java.util.Set 101 getKeys() 102 { 103 return( mEnv.keySet( ) ); 104 } 105 106 public boolean 107 isPersistable( String key ) 108 { 109 final ValueData data = (ValueData)mEnv.get( key ); 110 final boolean isPersistable = data != null && data.mAllowPersistence; 111 112 return( isPersistable ); 113 } 114 115 Properties 116 exportProperties( ) 117 { 118 final Properties props = new Properties (); 119 120 final Iterator iter = getKeys().iterator(); 121 while( iter.hasNext() ) 122 { 123 final String key = (String )iter.next(); 124 125 if ( isPersistable( key ) ) 126 { 127 props.put( key, get( key ).toString() ); 128 } 129 } 130 131 return( props ); 132 } 133 134 void 135 importProperties( Properties props ) 136 { 137 final Enumeration propertyNames = props.propertyNames(); 138 139 while ( propertyNames.hasMoreElements() ) 140 { 141 final String name = (String )propertyNames.nextElement(); 142 final String value = props.getProperty( name ); 143 144 put( name, value, true ); 145 } 146 } 147 148 private final static String HEADER = "jmxadmin environment properties"; 149 150 void 151 load( File file ) 152 throws IOException 153 { 154 FileInputStream inputStream = null; 155 156 try 157 { 158 inputStream = new FileInputStream ( file ); 159 160 final Properties props = new Properties (); 161 props.load( inputStream ); 162 inputStream.close(); 163 164 importProperties( props ); 165 } 166 catch( FileNotFoundException e ) 167 { 168 if ( inputStream != null ) 170 { 171 inputStream.close(); 172 } 173 } 174 } 175 176 void 177 store( File file ) 178 throws FileNotFoundException , IOException 179 { 180 final Properties props = exportProperties( ); 181 182 FileOutputStream outputStream = null; 183 184 try 185 { 186 outputStream = new FileOutputStream ( file ); 187 props.store( outputStream, HEADER ); 188 outputStream.close(); 189 } 190 catch( Exception e ) 191 { 192 System.err.println( "can't store environment" ); 193 } 194 finally 195 { 196 if ( outputStream != null ) 197 { 198 outputStream.close(); 199 } 200 } 201 } 202 203 }; 204 205 206 207 208 209 | Popular Tags |