1 19 package org.apache.avalon.cornerstone.blocks.masterstore.xml; 20 21 import org.apache.avalon.cornerstone.blocks.masterstore.AbstractFileRepository; 22 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.beans.XMLDecoder ; 26 import java.beans.XMLEncoder ; 27 28 32 public abstract class AbstractXMLFilePersistentObjectRepository extends AbstractFileRepository { 33 34 37 public synchronized Object get( final String key ) 38 { 39 try 40 { 41 final InputStream inputStream = getInputStream( key ); 42 43 try 44 { 45 final XMLDecoder decoder = new XMLDecoder ( inputStream ); 46 final Object object = decoder.readObject(); 47 if( DEBUG ) 48 { 49 monitor.returningKey(XMLFilePersistentObjectRepository.class, key); 50 } 51 return object; 52 } 53 finally 54 { 55 inputStream.close(); 56 } 57 } 58 catch( final Exception e ) 59 { 60 throw new RuntimeException ( "Exception caught while retrieving an object: " + e ); 61 } 62 } 63 64 public synchronized Object get( final String key, final ClassLoader classLoader ) 65 { 66 try 67 { 68 final InputStream inputStream = getInputStream( key ); 69 final ClassLoader oldCL = Thread.currentThread().getContextClassLoader(); 70 Thread.currentThread().setContextClassLoader( classLoader ); 71 try 72 { 73 final XMLDecoder decoder = new XMLDecoder ( inputStream ); 74 final Object object = decoder.readObject(); 75 if( DEBUG ) 76 { 77 monitor.returningObjectForKey(XMLFilePersistentObjectRepository.class, object, key); 78 } 79 return object; 80 } 81 finally 82 { 83 Thread.currentThread().setContextClassLoader( oldCL ); 84 inputStream.close(); 85 } 86 } 87 catch( final Exception e ) 88 { 89 e.printStackTrace(); 90 throw new RuntimeException ( "Exception caught while retrieving an object: " + e ); 91 } 92 93 } 94 95 98 public synchronized void put( final String key, final Object value ) 99 { 100 try 101 { 102 final OutputStream outputStream = getOutputStream( key ); 103 104 try 105 { 106 final XMLEncoder encoder = new XMLEncoder ( outputStream ); 108 encoder.writeObject( value ); 109 encoder.flush(); 110 if( DEBUG ) monitor.storingObjectForKey(XMLFilePersistentObjectRepository.class, value, key); 111 } 112 finally 113 { 114 outputStream.close(); 115 } 116 } 117 catch( final Exception e ) 118 { 119 throw new RuntimeException ( "Exception caught while storing an object: " + e ); 120 } 121 } 122 123 protected String getExtensionDecorator() 124 { 125 return ".FileObjectStore"; 126 } 127 } 128 | Popular Tags |