KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > masterstore > xml > AbstractXMLFilePersistentObjectRepository


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.xml;
20
21 import org.apache.avalon.cornerstone.blocks.masterstore.AbstractFileRepository;
22
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.beans.XMLDecoder JavaDoc;
26 import java.beans.XMLEncoder JavaDoc;
27
28 /**
29  * @author Paul Hammant
30  * @version $Revision: 1.8 $
31  */

32 public abstract class AbstractXMLFilePersistentObjectRepository extends AbstractFileRepository {
33
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             try
44             {
45                 final XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc( inputStream );
46                 final Object JavaDoc 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 JavaDoc e )
59         {
60             throw new RuntimeException JavaDoc( "Exception caught while retrieving an object: " + e );
61         }
62     }
63
64     public synchronized Object JavaDoc get( final String JavaDoc key, final ClassLoader JavaDoc classLoader )
65     {
66         try
67         {
68             final InputStream JavaDoc inputStream = getInputStream( key );
69             final ClassLoader JavaDoc oldCL = Thread.currentThread().getContextClassLoader();
70             Thread.currentThread().setContextClassLoader( classLoader );
71             try
72             {
73                 final XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc( inputStream );
74                 final Object JavaDoc 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 JavaDoc e )
88         {
89             e.printStackTrace();
90             throw new RuntimeException JavaDoc( "Exception caught while retrieving an object: " + e );
91         }
92
93     }
94
95     /**
96      * Store the given object and associates it to the given key
97      */

98     public synchronized void put( final String JavaDoc key, final Object JavaDoc value )
99     {
100         try
101         {
102             final OutputStream JavaDoc outputStream = getOutputStream( key );
103
104             try
105             {
106                 //System.out.println("Putting key!:" + key + " " + value + " " + value.getClass().getName());
107
final XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc( 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 JavaDoc e )
118         {
119             throw new RuntimeException JavaDoc( "Exception caught while storing an object: " + e );
120         }
121     }
122
123     protected String JavaDoc getExtensionDecorator()
124     {
125         return ".FileObjectStore";
126     }
127 }
128
Popular Tags