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
020package org.apache.james.repository.file;
021
022import java.io.InputStream;
023import java.io.ObjectInputStream;
024import java.io.ObjectOutputStream;
025import java.io.OutputStream;
026
027import 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 */
033public 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(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                final Object object = stream.readObject();
057                if (DEBUG) {
058                    getLogger().debug("returning object " + object + " for key " + key);
059                }
060                return object;
061            } finally {
062                inputStream.close();
063            }
064        } catch (Throwable e) {
065            throw new RuntimeException("Exception caught while retrieving an object, cause: " + e.toString());
066        }
067    }
068
069    /**
070     * @see
071     * org.apache.james.repository.api.ObjectRepository#get(java.lang.String,
072     * java.lang.ClassLoader)
073     */
074    public synchronized Object get(String key, ClassLoader classLoader) {
075        try {
076            final InputStream inputStream = getInputStream(key);
077
078            if (inputStream == null) {
079                throw new NullPointerException("Null input stream returned for key: " + key);
080            }
081
082            final ObjectInputStream stream = new ClassLoaderObjectInputStream(classLoader, inputStream);
083            try {
084                final Object object = stream.readObject();
085
086                if (DEBUG) {
087                    getLogger().debug("returning object " + object + " for key " + key);
088                }
089                return object;
090            } finally {
091                stream.close();
092                inputStream.close();
093            }
094        } catch (Throwable e) {
095            throw new RuntimeException("Exception caught while retrieving an object: " + e);
096        }
097
098    }
099
100    /**
101     * @see
102     * org.apache.james.repository.api.ObjectRepository#put(java.lang.String,
103     * java.lang.Object)
104     */
105    public synchronized void put(String key, Object value) {
106        try {
107            final OutputStream outputStream = getOutputStream(key);
108
109            try {
110                final ObjectOutputStream stream = new ObjectOutputStream(outputStream);
111                stream.writeObject(value);
112                if (DEBUG)
113                    getLogger().debug("storing object " + value + " for key " + key);
114            } finally {
115                outputStream.close();
116            }
117        } catch (Exception e) {
118            throw new RuntimeException("Exception caught while storing an object: " + e);
119        }
120    }
121
122}