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    package org.apache.james.repository.file;
021    
022    import java.io.InputStream;
023    import java.io.ObjectInputStream;
024    import java.io.ObjectOutputStream;
025    import java.io.OutputStream;
026    
027    import org.apache.james.repository.api.ObjectRepository;
028    
029    /**
030     * This is a simple implementation of persistent object store using object
031     * serialization on the file system.
032     */
033    public class FilePersistentObjectRepository extends AbstractFileRepository implements ObjectRepository {
034    
035        /**
036         * @see
037         * org.apache.james.repository.file.AbstractFileRepository#getExtensionDecorator()
038         */
039        protected String getExtensionDecorator() {
040            return ".FileObjectStore";
041        }
042    
043        /**
044         * @see
045         * org.apache.james.repository.api.ObjectRepository#get(java.lang.String)
046         */
047        public synchronized Object get(final String key) {
048            try {
049                final InputStream inputStream = getInputStream(key);
050    
051                if (inputStream == null)
052                    throw new NullPointerException("Null input stream returned for key: " + key);
053                try {
054                    final ObjectInputStream stream = new ObjectInputStream(inputStream);
055    
056                    if (stream == null)
057                        throw new NullPointerException("Null stream returned for key: " + key);
058    
059                    final Object object = stream.readObject();
060                    if (DEBUG) {
061                        getLogger().debug("returning object " + object + " for key " + key);
062                    }
063                    return object;
064                } finally {
065                    inputStream.close();
066                }
067            } catch (final Throwable e) {
068                throw new RuntimeException("Exception caught while retrieving an object, cause: " + e.toString());
069            }
070        }
071    
072        /**
073         * @see
074         * org.apache.james.repository.api.ObjectRepository#get(java.lang.String,
075         * java.lang.ClassLoader)
076         */
077        public synchronized Object get(final String key, final ClassLoader classLoader) {
078            try {
079                final InputStream inputStream = getInputStream(key);
080    
081                if (inputStream == null)
082                    throw new NullPointerException("Null input stream returned for key: " + key);
083    
084                try {
085                    final ObjectInputStream stream = new ClassLoaderObjectInputStream(classLoader, inputStream);
086    
087                    if (stream == null)
088                        throw new NullPointerException("Null stream returned for key: " + key);
089    
090                    final Object object = stream.readObject();
091    
092                    if (DEBUG) {
093                        getLogger().debug("returning object " + object + " for key " + key);
094                    }
095                    return object;
096                } finally {
097                    inputStream.close();
098                }
099            } catch (final Throwable e) {
100                throw new RuntimeException("Exception caught while retrieving an object: " + e);
101            }
102    
103        }
104    
105        /**
106         * @see
107         * org.apache.james.repository.api.ObjectRepository#put(java.lang.String,
108         * java.lang.Object)
109         */
110        public synchronized void put(final String key, final Object value) {
111            try {
112                final OutputStream outputStream = getOutputStream(key);
113    
114                try {
115                    final ObjectOutputStream stream = new ObjectOutputStream(outputStream);
116                    stream.writeObject(value);
117                    if (DEBUG)
118                        getLogger().debug("storing object " + value + " for key " + key);
119                } finally {
120                    outputStream.close();
121                }
122            } catch (final Exception e) {
123                throw new RuntimeException("Exception caught while storing an object: " + e);
124            }
125        }
126    
127    }