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 org.apache.avalon.cornerstone.services.store.StreamRepository;
21  
22  import java.io.BufferedOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  /***
28   * Implementation of a StreamRepository to a File.
29   * TODO: -retieve(String key) should return a FilterInputStream to allow
30   * mark and reset methods. (working not like BufferedInputStream!!!)
31   *
32   */
33  public class File_Persistent_Stream_Repository
34      extends AbstractFileRepository
35      implements StreamRepository
36  {
37      protected String getExtensionDecorator()
38      {
39          return ".FileStreamStore";
40      }
41  
42      /***
43       * Get the object associated to the given unique key.
44       */
45      public synchronized InputStream get( final String key )
46      {
47          try
48          {
49              return getInputStream( key );
50          }
51          catch( final IOException ioe )
52          {
53              final String message = "Exception caught while retrieving a stream ";
54              getLogger().warn( message, ioe );
55              throw new RuntimeException( message + ": " + ioe );
56          }
57      }
58  
59      /***
60       * Store the given object and associates it to the given key
61       */
62      public synchronized OutputStream put( final String key )
63      {
64          try
65          {
66              final OutputStream outputStream = getOutputStream( key );
67              return new BufferedOutputStream( outputStream );
68          }
69          catch( final IOException ioe )
70          {
71              final String message = "Exception caught while storing a stream ";
72              getLogger().warn( message, ioe );
73              throw new RuntimeException( message + ": " + ioe );
74          }
75      }
76  
77      public long getSize(final String key) {
78          try {
79              return getFile(key).length();
80          }
81          catch(IOException e) {
82              return 0;
83          }
84      }
85  }