001: /*
002: * hammurapi-rules @mesopotamia.version@
003: * Hammurapi rules engine.
004: * Copyright (C) 2005 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.rules;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileNotFoundException;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.util.Map;
033:
034: import biz.hammurapi.config.ConfigurationException;
035:
036: public class FileObjectStorage implements ObjectStorage {
037: private Map objects;
038:
039: public void put(String key, Object value) {
040: objects.put(key, value);
041:
042: }
043:
044: public void start() throws ConfigurationException {
045: if (file == null) {
046: throw new ConfigurationException(
047: "Storage file name is not set");
048: }
049:
050: if (file.exists()) {
051: if (file.isFile()) {
052: try {
053: ObjectInputStream ois = new ObjectInputStream(
054: new FileInputStream(file));
055: objects = (Map) ois.readObject();
056: ois.close();
057: } catch (ClassNotFoundException e) {
058: throw new ConfigurationException(
059: "Could not load objects from: "
060: + file.getAbsolutePath()
061: + ", class not found " + e, e);
062: } catch (FileNotFoundException e) {
063: throw new ConfigurationException(
064: "Storage file does not exist: "
065: + file.getAbsolutePath(), e);
066: } catch (IOException e) {
067: throw new ConfigurationException(
068: "Could not load objects from: "
069: + file.getAbsolutePath(), e);
070: }
071: } else {
072: throw new ConfigurationException("Not a file: "
073: + file.getAbsolutePath());
074: }
075: }
076: }
077:
078: public void stop() throws ConfigurationException {
079: try {
080: ObjectOutputStream oos = new ObjectOutputStream(
081: new FileOutputStream(file));
082: oos.writeObject(objects);
083: oos.close();
084: } catch (FileNotFoundException e) {
085: throw new ConfigurationException(
086: "Collections file does not exist: "
087: + file.getAbsolutePath(), e);
088: } catch (IOException e) {
089: throw new ConfigurationException(
090: "Could not load collections from: "
091: + file.getAbsolutePath(), e);
092: }
093: }
094:
095: private File file;
096:
097: public void setFile(File file) {
098: this .file = file;
099: }
100:
101: public Object get(String name) {
102: return objects.get(name);
103: }
104: }
|