1 22 23 package apollo; 24 25 import java.io.*; 26 import java.net.*; 27 import java.util.*; 28 29 public class MuffinStoreHandler 30 { 31 private static MuffinStoreHandler _handler; 32 33 private BasicService _basicService; 34 private URL _codebase; 35 private PersistenceService _persistenceService; 36 37 public MuffinStoreHandler() 38 { 39 _basicService = ServiceManager.lookupBasicService(); 40 _persistenceService = ServiceManager.lookupPersistenceService(); 41 42 _codebase = _basicService.getCodeBase(); 43 } 44 45 public void setTag( URL url, int tag ) throws MalformedURLException, IOException 46 { 47 _persistenceService.setTag( url, tag ); 48 } 49 50 public static MuffinStoreHandler getHandler() 51 { 52 if( _handler == null ) 53 _handler = new MuffinStoreHandler(); 54 return _handler; 55 } 56 57 public FileContents get( URL url ) throws MalformedURLException, FileNotFoundException, IOException 58 { 59 return _persistenceService.get( url ); 60 } 61 62 public FileContents get( String name ) throws MalformedURLException, FileNotFoundException, IOException 63 { 64 return _persistenceService.get( new URL( getCodeBase(), name ) ); 65 } 66 67 public URL getCodeBase() 68 { 69 return _codebase; 70 } 71 72 public String [] getNames() throws MalformedURLException, IOException 73 { 74 return _persistenceService.getNames( getCodeBase() ); 75 } 76 77 public String [] getNames( URL url ) throws MalformedURLException, IOException 78 { 79 return _persistenceService.getNames( url ); 80 } 81 82 public String [] getNames( String name ) throws MalformedURLException, IOException 83 { 84 return _persistenceService.getNames( new URL( getCodeBase(), name ) ); 85 } 86 87 public int getTag( URL url ) throws MalformedURLException, IOException 88 { 89 return _persistenceService.getTag( url ); 90 } 91 92 98 private String getUrlFileName( URL url ) 99 { 100 String path = url.getPath(); 101 102 if( path.endsWith( "/" ) ) 103 return ""; 105 106 String name = ""; 107 int index = path.lastIndexOf( '/' ); 108 if( index != -1 ) 109 name = path.substring( index + 1 ); 110 111 return name; 112 } 113 114 public long create( URL url, long maxSize ) throws MalformedURLException, IOException 115 { 116 return _persistenceService.create( url, maxSize ); 117 } 118 119 public long create( String name, long maxSize ) throws MalformedURLException, IOException 120 { 121 return _persistenceService.create( new URL( getCodeBase(), name ), maxSize ); 122 } 123 124 public void delete( URL url ) throws MalformedURLException, IOException 125 { 126 _persistenceService.delete( url ); 127 } 128 129 public void delete( String name ) throws MalformedURLException, IOException 130 { 131 _persistenceService.delete( new URL( getCodeBase(), name ) ); 132 } 133 134 public boolean exists( URL url ) throws MalformedURLException, IOException 135 { 136 String names[] = getNames( url ); 137 138 140 String key = getUrlFileName( url ); 142 143 for( int i = 0; i < names.length; i++ ) 144 if( names[i].equals( key ) ) 145 return true; 146 147 return false; 148 } 149 150 public boolean exists( String name ) throws MalformedURLException, IOException 151 { 152 return exists( new URL( getCodeBase(), name ) ); 153 } 154 155 public Properties loadProperties( String name ) throws MalformedURLException, FileNotFoundException, IOException 156 { 157 160 FileContents contents = get( name ); 161 162 Properties props = new Properties(); 163 InputStream in = contents.getInputStream(); 164 props.load( in ); 165 in.close(); 166 167 return props; 168 } 169 170 public String loadText( String name ) throws MalformedURLException, FileNotFoundException, IOException 171 { 172 175 FileContents contents = get( name ); 176 177 StringBuffer text = new StringBuffer (); 178 179 InputStreamReader in = new InputStreamReader( contents.getInputStream() ); 180 char buffer[] = new char[4096]; 181 int bytes_read; 182 while( ( bytes_read = in.read( buffer ) ) != -1 ) 183 text.append( new String ( buffer, 0, bytes_read ) ); 184 in.close(); 185 186 return text.toString(); 187 } 188 189 public void saveProperties( String name, Properties props ) throws MalformedURLException, FileNotFoundException, IOException 190 { 191 saveProperties( name, props, 2056 ); 192 } 193 194 public void saveProperties( String name, Properties props, long maxSize ) throws MalformedURLException, FileNotFoundException, IOException 195 { 196 URL muffinUrl = new URL( getCodeBase(), name ); 197 if( !exists( muffinUrl ) ) 198 create( muffinUrl, maxSize ); 199 200 FileContents contents = get( muffinUrl ); 201 202 OutputStream out = contents.getOutputStream( true ); 203 206 props.store( out, new Date().toString() ); 207 out.flush(); 208 out.close(); 209 } 210 211 public void saveText( String name, String text ) throws MalformedURLException, FileNotFoundException, IOException 212 { 213 saveText( name, text, 2056 ); 214 } 215 216 public void saveText( String name, String text, long maxSize ) throws MalformedURLException, FileNotFoundException, IOException 217 { 218 URL muffinUrl = new URL( getCodeBase(), name ); 219 if( !exists( muffinUrl ) ) 220 create( muffinUrl, maxSize ); 221 222 FileContents contents = get( muffinUrl ); 223 224 OutputStreamWriter out = new OutputStreamWriter( contents.getOutputStream( true ) ); 225 228 out.write( text ); 229 out.flush(); 230 out.close(); 231 } 232 } 233 | Popular Tags |