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