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("Storage file name is not set"); 047 } 048 049 if (file.exists()) { 050 if (file.isFile()) { 051 try { 052 ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file)); 053 objects=(Map) ois.readObject(); 054 ois.close(); 055 } catch (ClassNotFoundException e) { 056 throw new ConfigurationException("Could not load objects from: "+file.getAbsolutePath()+", class not found "+e, e); 057 } catch (FileNotFoundException e) { 058 throw new ConfigurationException("Storage file does not exist: "+file.getAbsolutePath(), e); 059 } catch (IOException e) { 060 throw new ConfigurationException("Could not load objects from: "+file.getAbsolutePath(), e); 061 } 062 } else { 063 throw new ConfigurationException("Not a file: "+file.getAbsolutePath()); 064 } 065 } 066 } 067 068 public void stop() throws ConfigurationException { 069 try { 070 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file)); 071 oos.writeObject(objects); 072 oos.close(); 073 } catch (FileNotFoundException e) { 074 throw new ConfigurationException("Collections file does not exist: "+file.getAbsolutePath(), e); 075 } catch (IOException e) { 076 throw new ConfigurationException("Could not load collections from: "+file.getAbsolutePath(), e); 077 } 078 } 079 080 private File file; 081 082 public void setFile(File file) { 083 this.file = file; 084 } 085 086 public Object get(String name) { 087 return objects.get(name); 088 } 089 }