KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You 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.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.cornerstone.blocks.masterstore;
20
21 import org.apache.avalon.cornerstone.services.store.ObjectRepository;
22
23 import java.io.InputStream JavaDoc;
24 import java.io.ObjectInputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27
28 /**
29  * @author Paul Hammant
30  * @version $Revision: 1.8 $
31  */

32 public abstract class AbstractFilePersistentObjectRepository extends AbstractFileRepository
33         implements ObjectRepository {
34     /**
35      * Get the object associated to the given unique key.
36      */

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

110     public synchronized void put( final String JavaDoc key, final Object JavaDoc value )
111     {
112         try
113         {
114             final OutputStream JavaDoc outputStream = getOutputStream( key );
115
116             try
117             {
118                 final ObjectOutputStream JavaDoc stream = new ObjectOutputStream JavaDoc( outputStream );
119                 stream.writeObject( value );
120                 if( DEBUG ) monitor.storingObjectForKey(File_Persistent_Object_Repository.class, value, key);
121             }
122             finally
123             {
124                 outputStream.close();
125             }
126         }
127         catch( final Exception JavaDoc e )
128         {
129             throw new RuntimeException JavaDoc( "Exception caught while storing an object: " + e );
130         }
131     }
132
133     protected String JavaDoc getExtensionDecorator()
134     {
135         return ".FileObjectStore";
136     }
137 }
138
Popular Tags