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