1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.james.mailrepository.filepair;
23
24 import java.io.InputStream;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.OutputStream;
28 import org.apache.avalon.cornerstone.services.store.ObjectRepository;
29 import org.apache.james.util.io.ClassLoaderObjectInputStream;
30
31
32
33
34
35
36 public class File_Persistent_Object_Repository
37 extends AbstractFileRepository
38 implements ObjectRepository
39 {
40
41
42
43
44 protected String getExtensionDecorator()
45 {
46 return ".FileObjectStore";
47 }
48
49
50
51
52 public synchronized Object get( final String key )
53 {
54 try
55 {
56 final InputStream inputStream = getInputStream( key );
57
58 if( inputStream == null )
59 throw new NullPointerException("Null input stream returned for key: " + key );
60 try
61 {
62 final ObjectInputStream stream = new ObjectInputStream( inputStream );
63
64 if( stream == null )
65 throw new NullPointerException("Null stream returned for key: " + key );
66
67 final Object object = stream.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 Throwable e )
80 {
81 throw new RuntimeException(
82 "Exception caught while retrieving an object, cause: " + e.toString() );
83 }
84 }
85
86
87
88
89 public synchronized Object get( final String key, final ClassLoader classLoader )
90 {
91 try
92 {
93 final InputStream inputStream = getInputStream( key );
94
95 if( inputStream == null )
96 throw new NullPointerException("Null input stream returned for key: " + key );
97
98 try
99 {
100 final ObjectInputStream stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
101
102 if( stream == null )
103 throw new NullPointerException("Null stream returned for key: " + key );
104
105 final Object object = stream.readObject();
106
107 if( DEBUG )
108 {
109 getLogger().debug( "returning object " + object + " for key " + key );
110 }
111 return object;
112 }
113 finally
114 {
115 inputStream.close();
116 }
117 }
118 catch( final Throwable e )
119 {
120 throw new RuntimeException( "Exception caught while retrieving an object: " + e );
121 }
122
123 }
124
125
126
127
128
129
130 public synchronized void put( final String key, final Object value )
131 {
132 try
133 {
134 final OutputStream outputStream = getOutputStream( key );
135
136 try
137 {
138 final ObjectOutputStream stream = new ObjectOutputStream( outputStream );
139 stream.writeObject( value );
140 if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key );
141 }
142 finally
143 {
144 outputStream.close();
145 }
146 }
147 catch( final Exception e )
148 {
149 throw new RuntimeException( "Exception caught while storing an object: " + e );
150 }
151 }
152
153 }