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  package org.apache.james.mailrepository.filepair;
21  
22  import java.io.InputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.io.OutputStream;
26  import org.apache.avalon.cornerstone.services.store.ObjectRepository;
27  import org.apache.james.util.io.ClassLoaderObjectInputStream;
28  
29  /***
30   * This is a simple implementation of persistent object store using
31   * object serialization on the file system.
32   *
33   */
34  public class File_Persistent_Object_Repository
35      extends AbstractFileRepository
36      implements ObjectRepository
37  {
38      protected String getExtensionDecorator()
39      {
40          return ".FileObjectStore";
41      }
42  
43      /***
44       * Get the object associated to the given unique key.
45       */
46      public synchronized Object get( final String key )
47      {
48          try
49          {
50              final InputStream inputStream = getInputStream( key );
51  
52              if( inputStream == null )
53                    throw new NullPointerException("Null input stream returned for key: " + key );
54              try
55              {
56                  final ObjectInputStream stream = new ObjectInputStream( inputStream );
57  
58                  if( stream == null )
59                    throw new NullPointerException("Null stream returned for key: " + key );
60  
61                  final Object object = stream.readObject();
62                  if( DEBUG )
63                  {
64                      getLogger().debug( "returning object " + object + " for key " + key );
65                  }
66                  return object;
67              }
68              finally
69              {
70                  inputStream.close();
71              }
72          }
73          catch( final Throwable e )
74          {
75              throw new RuntimeException(
76                "Exception caught while retrieving an object, cause: " + e.toString() );
77          }
78      }
79  
80      public synchronized Object get( final String key, final ClassLoader classLoader )
81      {
82          try
83          {
84              final InputStream inputStream = getInputStream( key );
85  
86              if( inputStream == null )
87                    throw new NullPointerException("Null input stream returned for key: " + key );
88  
89              try
90              {
91                  final ObjectInputStream stream = new ClassLoaderObjectInputStream( classLoader, inputStream );
92  
93                  if( stream == null )
94                    throw new NullPointerException("Null stream returned for key: " + key );
95  
96                  final Object object = stream.readObject();
97  
98                  if( DEBUG )
99                  {
100                     getLogger().debug( "returning object " + object + " for key " + key );
101                 }
102                 return object;
103             }
104             finally
105             {
106                 inputStream.close();
107             }
108         }
109         catch( final Throwable e )
110         {
111             throw new RuntimeException( "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 key, final Object value )
120     {
121         try
122         {
123             final OutputStream outputStream = getOutputStream( key );
124 
125             try
126             {
127                 final ObjectOutputStream stream = new ObjectOutputStream( outputStream );
128                 stream.writeObject( value );
129                 if( DEBUG ) getLogger().debug( "storing object " + value + " for key " + key );
130             }
131             finally
132             {
133                 outputStream.close();
134             }
135         }
136         catch( final Exception e )
137         {
138             throw new RuntimeException( "Exception caught while storing an object: " + e );
139         }
140     }
141 
142 }