1 17 18 package org.apache.avalon.cornerstone.blocks.masterstore; 19 20 import java.io.InputStream ; 21 import java.io.ObjectInputStream ; 22 import java.io.ObjectOutputStream ; 23 import java.io.OutputStream ; 24 import org.apache.avalon.cornerstone.services.store.ObjectRepository; 25 26 34 public class File_Persistent_Object_Repository 35 extends AbstractFileRepository 36 implements ObjectRepository 37 { 38 protected String getExtensionDecorator() 39 { 40 return ".FileObjectStore"; 41 } 42 43 46 public synchronized Object get( final String key ) 47 { 48 try 49 { 50 final InputStream inputStream = getInputStream( key ); 51 52 if( inputStream == null ) 53 throw new NullPointerException ( "Null input stream returned for key: " + key ); 54 try 55 { 56 final ObjectInputStream stream = new ObjectInputStream ( inputStream ); 57 58 if( stream == null ) 59 throw new NullPointerException ( "Null stream returned for key: " + key ); 60 61 final Object object = stream.readObject(); 62 if( DEBUG ) 63 { 64 getLogger().debug( "returning object " + object + " for key " + key ); 65 } 66 return object; 67 } 68 finally 69 { 70 inputStream.close(); 71 } 72 } 73 catch( final Throwable e ) 74 { 75 throw new RuntimeException ( 76 "Exception caught while retrieving an object, cause: " + e.toString() ); 77 } 78 } 79 80 public synchronized Object get( final String key, final ClassLoader classLoader ) 81 { 82 try 83 { 84 final InputStream inputStream = getInputStream( key ); 85 86 if( inputStream == null ) 87 throw new NullPointerException ( "Null input stream returned for key: " + key ); 88 89 try 90 { 91 final ObjectInputStream stream = new ClassLoaderObjectInputStream( classLoader, inputStream ); 92 93 if( stream == null ) 94 throw new NullPointerException ( "Null stream returned for key: " + key ); 95 96 final Object object = stream.readObject(); 97 98 if( DEBUG ) 99 { 100 getLogger().debug( "returning object " + object + " for key " + key ); 101 } 102 return object; 103 } 104 finally 105 { 106 inputStream.close(); 107 } 108 } 109 catch( final Throwable e ) 110 { 111 throw new RuntimeException ( "Exception caught while retrieving an object: " + e ); 112 } 113 114 } 115 116 119 public synchronized void put( final String key, final Object value ) 120 { 121 try 122 { 123 final OutputStream outputStream = getOutputStream( key ); 124 125 try 126 { 127 final ObjectOutputStream stream = new ObjectOutputStream ( outputStream ); 128 stream.writeObject( value ); 129 if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key ); 130 } 131 finally 132 { 133 outputStream.close(); 134 } 135 } 136 catch( final Exception e ) 137 { 138 throw new RuntimeException ( "Exception caught while storing an object: " + e ); 139 } 140 } 141 142 } 143 | Popular Tags |