KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > masterstore > File_Persistent_Object_Repository


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

17
18 package org.apache.avalon.cornerstone.blocks.masterstore;
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
26 /**
27  * This is a simple implementation of persistent object store using
28  * object serialization on the file system.
29  *
30  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
31  * @author <a HREF="mailto:fede@apache.org">Federico Barbieri</a>
32  * @author <a HREF="mailto:paul_hammant@yahoo.com">Paul Hammant</a>
33  */

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

46     public synchronized Object JavaDoc get( final String JavaDoc key )
47     {
48         try
49         {
50             final InputStream JavaDoc inputStream = getInputStream( key );
51
52             if( inputStream == null )
53                 throw new NullPointerException JavaDoc( "Null input stream returned for key: " + key );
54             try
55             {
56                 final ObjectInputStream JavaDoc stream = new ObjectInputStream JavaDoc( inputStream );
57
58                 if( stream == null )
59                     throw new NullPointerException JavaDoc( "Null stream returned for key: " + key );
60
61                 final Object JavaDoc object = stream.readObject();
62                 if( DEBUG )
63                 {
64                     getLogger().debug( "returning object " + object + " for key " + key );
65                 }
66                 return object;
67             }
68             finally
69             {
70                 inputStream.close();
71             }
72         }
73         catch( final Throwable JavaDoc e )
74         {
75             throw new RuntimeException JavaDoc(
76                 "Exception caught while retrieving an object, cause: " + e.toString() );
77         }
78     }
79
80     public synchronized Object JavaDoc get( final String JavaDoc key, final ClassLoader JavaDoc classLoader )
81     {
82         try
83         {
84             final InputStream JavaDoc inputStream = getInputStream( key );
85
86             if( inputStream == null )
87                 throw new NullPointerException JavaDoc( "Null input stream returned for key: " + key );
88
89             try
90             {
91                 final ObjectInputStream JavaDoc stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
92
93                 if( stream == null )
94                     throw new NullPointerException JavaDoc( "Null stream returned for key: " + key );
95
96                 final Object JavaDoc object = stream.readObject();
97
98                 if( DEBUG )
99                 {
100                     getLogger().debug( "returning object " + object + " for key " + key );
101                 }
102                 return object;
103             }
104             finally
105             {
106                 inputStream.close();
107             }
108         }
109         catch( final Throwable JavaDoc e )
110         {
111             throw new RuntimeException JavaDoc( "Exception caught while retrieving an object: " + e );
112         }
113
114     }
115
116     /**
117      * Store the given object and associates it to the given key
118      */

119     public synchronized void put( final String JavaDoc key, final Object JavaDoc value )
120     {
121         try
122         {
123             final OutputStream JavaDoc outputStream = getOutputStream( key );
124
125             try
126             {
127                 final ObjectOutputStream JavaDoc stream = new ObjectOutputStream JavaDoc( outputStream );
128                 stream.writeObject( value );
129                 if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key );
130             }
131             finally
132             {
133                 outputStream.close();
134             }
135         }
136         catch( final Exception JavaDoc e )
137         {
138             throw new RuntimeException JavaDoc( "Exception caught while storing an object: " + e );
139         }
140     }
141
142 }
143
Popular Tags