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