001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    
021    
022    package org.apache.james.repository.file;
023    
024    import java.io.BufferedOutputStream;
025    import java.io.IOException;
026    import java.io.InputStream;
027    import java.io.OutputStream;
028    
029    import org.apache.james.repository.api.StreamRepository;
030    
031    /**
032     * Implementation of a StreamRepository to a File.<br>
033     * TODO: -retieve(String key) should return a FilterInputStream to allow
034     * mark and reset methods. (working not like BufferedInputStream!!!)
035     */
036    public class FilePersistentStreamRepository
037        extends AbstractFileRepository
038        implements StreamRepository
039    {
040    
041        /**
042         * @see org.apache.james.repository.file.AbstractFileRepository#getExtensionDecorator()
043         */
044        protected String getExtensionDecorator()
045        {
046            return ".FileStreamStore";
047        }
048    
049    
050        /**
051         * @see org.apache.james.repository.api.StreamRepository#get(java.lang.String)
052         */
053        public synchronized InputStream get( final String key )
054        {
055            try
056            {
057                return getInputStream( key );
058            }
059            catch( final IOException ioe )
060            {
061                final String message = "Exception caught while retrieving a stream ";
062                getLogger().warn( message, ioe );
063                throw new RuntimeException( message + ": " + ioe );
064            }
065        }
066    
067    
068        /**
069         * @see org.apache.james.repository.api.StreamRepository#put(java.lang.String)
070         */
071        public synchronized OutputStream put( final String key )
072        {
073            try
074            {
075                final OutputStream outputStream = getOutputStream( key );
076                return new BufferedOutputStream( outputStream );
077            }
078            catch( final IOException ioe )
079            {
080                final String message = "Exception caught while storing a stream ";
081                getLogger().warn( message, ioe );
082                throw new RuntimeException( message + ": " + ioe );
083            }
084        }
085    
086        /**
087         * Return the size of the file which belongs to the given key
088         * 
089         * @param key the key to get the size for
090         * @return size the Size which belongs to the givens keys file
091         */
092        public long getSize(final String key) {
093            try {
094                return getFile(key).length();
095            }
096            catch(IOException e) {
097                return 0;
098            }
099        }
100    }