View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 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;
21  import java.io.ObjectInputStream;
22  import java.io.ObjectOutputStream;
23  import java.io.OutputStream;
24  import org.apache.avalon.cornerstone.services.store.ObjectRepository;
25  import org.apache.james.util.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 getExtensionDecorator()
37      {
38          return ".FileObjectStore";
39      }
40  
41      /***
42       * Get the object associated to the given unique key.
43       */
44      public synchronized Object get( final String key )
45      {
46          try
47          {
48              final InputStream inputStream = getInputStream( key );
49  
50              if( inputStream == null )
51                    throw new NullPointerException("Null input stream returned for key: " + key );
52              try
53              {
54                  final ObjectInputStream stream = new ObjectInputStream( inputStream );
55  
56                  if( stream == null )
57                    throw new NullPointerException("Null stream returned for key: " + key );
58  
59                  final Object 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 e )
72          {
73              throw new RuntimeException(
74                "Exception caught while retrieving an object, cause: " + e.toString() );
75          }
76      }
77  
78      public synchronized Object get( final String key, final ClassLoader classLoader )
79      {
80          try
81          {
82              final InputStream inputStream = getInputStream( key );
83  
84              if( inputStream == null )
85                    throw new NullPointerException("Null input stream returned for key: " + key );
86  
87              try
88              {
89                  final ObjectInputStream stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
90  
91                  if( stream == null )
92                    throw new NullPointerException("Null stream returned for key: " + key );
93  
94                  final Object 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 e )
108         {
109             throw new RuntimeException( "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 key, final Object value )
118     {
119         try
120         {
121             final OutputStream outputStream = getOutputStream( key );
122 
123             try
124             {
125                 final ObjectOutputStream stream = new ObjectOutputStream( 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 e )
135         {
136             throw new RuntimeException( "Exception caught while storing an object: " + e );
137         }
138     }
139 
140 }