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