KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > mailrepository > filepair > File_Persistent_Object_Repository


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.mailrepository.filepair;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.ObjectInputStream JavaDoc;
22 import java.io.ObjectOutputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import org.apache.avalon.cornerstone.services.store.ObjectRepository;
25 import org.apache.avalon.excalibur.io.ClassLoaderObjectInputStream;
26
27 /**
28  * This is a simple implementation of persistent object store using
29  * object serialization on the file system.
30  *
31  */

32 public class File_Persistent_Object_Repository
33     extends AbstractFileRepository
34     implements ObjectRepository
35 {
36     protected String JavaDoc getExtensionDecorator()
37     {
38         return ".FileObjectStore";
39     }
40
41     /**
42      * Get the object associated to the given unique key.
43      */

44     public synchronized Object JavaDoc get( final String JavaDoc key )
45     {
46         try
47         {
48             final InputStream JavaDoc inputStream = getInputStream( key );
49
50             if( inputStream == null )
51                   throw new NullPointerException JavaDoc("Null input stream returned for key: " + key );
52             try
53             {
54                 final ObjectInputStream JavaDoc stream = new ObjectInputStream JavaDoc( inputStream );
55
56                 if( stream == null )
57                   throw new NullPointerException JavaDoc("Null stream returned for key: " + key );
58
59                 final Object JavaDoc 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 JavaDoc e )
72         {
73             throw new RuntimeException JavaDoc(
74               "Exception caught while retrieving an object, cause: " + e.toString() );
75         }
76     }
77
78     public synchronized Object JavaDoc get( final String JavaDoc key, final ClassLoader JavaDoc classLoader )
79     {
80         try
81         {
82             final InputStream JavaDoc inputStream = getInputStream( key );
83
84             if( inputStream == null )
85                   throw new NullPointerException JavaDoc("Null input stream returned for key: " + key );
86
87             try
88             {
89                 final ObjectInputStream JavaDoc stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
90
91                 if( stream == null )
92                   throw new NullPointerException JavaDoc("Null stream returned for key: " + key );
93
94                 final Object JavaDoc 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 JavaDoc e )
108         {
109             throw new RuntimeException JavaDoc( "Exception caught while retrieving an object: " + e );
110         }
111
112     }
113
114     /**
115      * Store the given object and associates it to the given key
116      */

117     public synchronized void put( final String JavaDoc key, final Object JavaDoc value )
118     {
119         try
120         {
121             final OutputStream JavaDoc outputStream = getOutputStream( key );
122
123             try
124             {
125                 final ObjectOutputStream JavaDoc stream = new ObjectOutputStream JavaDoc( 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 JavaDoc e )
135         {
136             throw new RuntimeException JavaDoc( "Exception caught while storing an object: " + e );
137         }
138     }
139
140 }
141
Popular Tags