KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml;
19
20 import java.beans.XMLDecoder JavaDoc;
21 import java.beans.XMLEncoder JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import org.apache.avalon.cornerstone.blocks.masterstore.AbstractFileRepository;
25 import org.apache.avalon.cornerstone.services.store.ObjectRepository;
26
27 /**
28  * This is a simple implementation of persistent object store using
29  * XML serialization from JDK 1.4 to a file system.
30  *
31  * This implementation of ObjectRepository comes with the following warning:
32  * "XMLEncoder provides suitable persistence delegates
33  * for all public subclasses of java.awt.Component in J2SE and the types of
34  * all of their properties, recursively. All other classes will be handled
35  * with the default persistence delegate which assumes the class follows
36  * the beans conventions" (snipped from the BugParade)
37  *
38  * Basically, don't use this block for anything other than Swing component
39  * serialization. Sun will have to do a lot of work writing a
40  * "PersistenceDelegate" to handle other JDK types let alone custom classes.
41  *
42  * @author <a HREF="mailto:paul_hammant@yahoo.com">Paul Hammant</a>
43  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
44  * @author <a HREF="mailto:fede@apache.org">Federico Barbieri</a>
45  */

46 public class XMLFilePersistentObjectRepository
47     extends AbstractFileRepository
48     implements ObjectRepository
49 {
50     protected String JavaDoc getExtensionDecorator()
51     {
52         return ".FileObjectStore";
53     }
54
55     /**
56      * Get the object associated to the given unique key.
57      */

58     public synchronized Object JavaDoc get( final String JavaDoc key )
59     {
60         try
61         {
62             final InputStream JavaDoc inputStream = getInputStream( key );
63
64             try
65             {
66                 final XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc( inputStream );
67                 final Object JavaDoc object = decoder.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 Exception JavaDoc e )
80         {
81             throw new RuntimeException JavaDoc( "Exception caught while retrieving an object: " + e );
82         }
83     }
84
85     public synchronized Object JavaDoc get( final String JavaDoc key, final ClassLoader JavaDoc classLoader )
86     {
87         try
88         {
89             final InputStream JavaDoc inputStream = getInputStream( key );
90             final ClassLoader JavaDoc oldCL = Thread.currentThread().getContextClassLoader();
91             Thread.currentThread().setContextClassLoader( classLoader );
92             try
93             {
94                 final XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc( inputStream );
95                 final Object JavaDoc object = decoder.readObject();
96                 if( DEBUG )
97                 {
98                     getLogger().debug( "returning object " + object + " for key " + key );
99                 }
100                 return object;
101             }
102             finally
103             {
104                 Thread.currentThread().setContextClassLoader( oldCL );
105                 inputStream.close();
106             }
107         }
108         catch( final Exception JavaDoc e )
109         {
110             e.printStackTrace();
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                 //System.out.println("Putting key!:" + key + " " + value + " " + value.getClass().getName());
128
final XMLEncoder JavaDoc encoder = new XMLEncoder JavaDoc( outputStream );
129                 encoder.writeObject( value );
130                 encoder.flush();
131                 if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key );
132             }
133             finally
134             {
135                 outputStream.close();
136             }
137         }
138         catch( final Exception JavaDoc e )
139         {
140             throw new RuntimeException JavaDoc( "Exception caught while storing an object: " + e );
141         }
142     }
143 }
144
Popular Tags