View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.mailrepository.filepair;
23  
24  import java.io.InputStream;
25  import java.io.ObjectInputStream;
26  import java.io.ObjectOutputStream;
27  import java.io.OutputStream;
28  import org.apache.avalon.cornerstone.services.store.ObjectRepository;
29  import org.apache.james.util.io.ClassLoaderObjectInputStream;
30  
31  /**
32   * This is a simple implementation of persistent object store using
33   * object serialization on the file system.
34   *
35   */
36  public class File_Persistent_Object_Repository
37      extends AbstractFileRepository
38      implements ObjectRepository
39  {
40      
41      /**
42       * @see org.apache.james.mailrepository.filepair.AbstractFileRepository#getExtensionDecorator()
43       */
44      protected String getExtensionDecorator()
45      {
46          return ".FileObjectStore";
47      }
48  
49      /**
50       * @see org.apache.avalon.cornerstone.services.store.ObjectRepository#get(String)
51       */
52      public synchronized Object get( final String key )
53      {
54          try
55          {
56              final InputStream inputStream = getInputStream( key );
57  
58              if( inputStream == null )
59                    throw new NullPointerException("Null input stream returned for key: " + key );
60              try
61              {
62                  final ObjectInputStream stream = new ObjectInputStream( inputStream );
63  
64                  if( stream == null )
65                    throw new NullPointerException("Null stream returned for key: " + key );
66  
67                  final Object object = stream.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 Throwable e )
80          {
81              throw new RuntimeException(
82                "Exception caught while retrieving an object, cause: " + e.toString() );
83          }
84      }
85  
86      /**
87       * @see org.apache.avalon.cornerstone.services.store.ObjectRepository#get(String, ClassLoader)
88       */
89      public synchronized Object get( final String key, final ClassLoader classLoader )
90      {
91          try
92          {
93              final InputStream inputStream = getInputStream( key );
94  
95              if( inputStream == null )
96                    throw new NullPointerException("Null input stream returned for key: " + key );
97  
98              try
99              {
100                 final ObjectInputStream stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
101 
102                 if( stream == null )
103                   throw new NullPointerException("Null stream returned for key: " + key );
104 
105                 final Object object = stream.readObject();
106 
107                 if( DEBUG )
108                 {
109                     getLogger().debug( "returning object " + object + " for key " + key );
110                 }
111                 return object;
112             }
113             finally
114             {
115                 inputStream.close();
116             }
117         }
118         catch( final Throwable e )
119         {
120             throw new RuntimeException( "Exception caught while retrieving an object: " + e );
121         }
122 
123     }
124 
125     /**
126      * Store the given object and associates it to the given key
127      * 
128      * @see org.apache.avalon.cornerstone.services.store.ObjectRepository#put(String, Object)
129      */
130     public synchronized void put( final String key, final Object value )
131     {
132         try
133         {
134             final OutputStream outputStream = getOutputStream( key );
135 
136             try
137             {
138                 final ObjectOutputStream stream = new ObjectOutputStream( outputStream );
139                 stream.writeObject( value );
140                 if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key );
141             }
142             finally
143             {
144                 outputStream.close();
145             }
146         }
147         catch( final Exception e )
148         {
149             throw new RuntimeException( "Exception caught while storing an object: " + e );
150         }
151     }
152 
153 }