1 17 18 package org.apache.avalon.cornerstone.blocks.masterstore.xml; 19 20 import java.beans.XMLDecoder ; 21 import java.beans.XMLEncoder ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import org.apache.avalon.cornerstone.blocks.masterstore.AbstractFileRepository; 25 import org.apache.avalon.cornerstone.services.store.ObjectRepository; 26 27 46 public class XMLFilePersistentObjectRepository 47 extends AbstractFileRepository 48 implements ObjectRepository 49 { 50 protected String getExtensionDecorator() 51 { 52 return ".FileObjectStore"; 53 } 54 55 58 public synchronized Object get( final String key ) 59 { 60 try 61 { 62 final InputStream inputStream = getInputStream( key ); 63 64 try 65 { 66 final XMLDecoder decoder = new XMLDecoder ( inputStream ); 67 final Object object = decoder.readObject(); 68 if( DEBUG ) 69 { 70 getLogger().debug( "returning object " + object + " for key " + key ); 71 } 72 return object; 73 } 74 finally 75 { 76 inputStream.close(); 77 } 78 } 79 catch( final Exception e ) 80 { 81 throw new RuntimeException ( "Exception caught while retrieving an object: " + e ); 82 } 83 } 84 85 public synchronized Object get( final String key, final ClassLoader classLoader ) 86 { 87 try 88 { 89 final InputStream inputStream = getInputStream( key ); 90 final ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 91 Thread.currentThread().setContextClassLoader( classLoader ); 92 try 93 { 94 final XMLDecoder decoder = new XMLDecoder ( inputStream ); 95 final Object object = decoder.readObject(); 96 if( DEBUG ) 97 { 98 getLogger().debug( "returning object " + object + " for key " + key ); 99 } 100 return object; 101 } 102 finally 103 { 104 Thread.currentThread().setContextClassLoader( oldCL ); 105 inputStream.close(); 106 } 107 } 108 catch( final Exception e ) 109 { 110 e.printStackTrace(); 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 XMLEncoder encoder = new XMLEncoder ( outputStream ); 129 encoder.writeObject( value ); 130 encoder.flush(); 131 if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key ); 132 } 133 finally 134 { 135 outputStream.close(); 136 } 137 } 138 catch( final Exception e ) 139 { 140 throw new RuntimeException ( "Exception caught while storing an object: " + e ); 141 } 142 } 143 } 144 | Popular Tags |