1 8 9 package mx4j.persist; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileNotFoundException ; 14 import java.io.FileOutputStream ; 15 import java.io.IOException ; 16 import java.io.NotSerializableException ; 17 import java.io.ObjectInputStream ; 18 import java.io.ObjectOutputStream ; 19 import java.io.Serializable ; 20 import javax.management.InstanceNotFoundException ; 21 import javax.management.MBeanException ; 22 import javax.management.RuntimeOperationsException ; 23 24 import mx4j.loading.ClassLoaderObjectInputStream; 25 26 31 public class FilePersister extends Persister 32 { 33 private File m_store; 34 35 42 public FilePersister(String location, String name) throws MBeanException 43 { 44 if (name == null) 45 { 46 throw new MBeanException (new IllegalArgumentException ("Persist name cannot be null")); 47 } 48 49 if (location != null) 50 { 51 File dir = new File (location); 52 if (!dir.exists()) 53 { 54 throw new MBeanException (new FileNotFoundException (location)); 55 } 56 m_store = new File (dir, name); 57 } 58 else 59 { 60 m_store = new File (name); 61 } 62 } 63 64 67 public String getFileName() 68 { 69 return m_store.getAbsolutePath(); 70 } 71 72 public Object load() throws MBeanException , RuntimeOperationsException , InstanceNotFoundException 73 { 74 FileInputStream fin = null; 75 ObjectInputStream clois = null; 76 Object result = null; 77 78 synchronized (this) 79 { 80 try 81 { 82 fin = new FileInputStream (m_store); 84 85 clois = new ClassLoaderObjectInputStream(fin, Thread.currentThread().getContextClassLoader()); 87 try 88 { 89 result = clois.readObject(); 91 } 92 catch (ClassNotFoundException ex) 93 { 94 try 97 { 98 clois.close(); 99 } 100 catch (IOException ignored) 101 { 102 } 103 104 throw new MBeanException (ex); 105 118 } 119 } 120 catch (IOException ex) 121 { 122 throw new MBeanException (ex); 123 } 124 finally 125 { 126 try 127 { 128 if (clois != null) clois.close(); 129 } 130 catch (IOException ignored) 131 { 132 } 133 } 134 } 135 return result; 136 } 137 138 public void store(Object data) throws MBeanException , RuntimeOperationsException , InstanceNotFoundException 139 { 140 if (data == null) throw new RuntimeOperationsException (new IllegalArgumentException ("Cannot store a null object.")); 141 if (!(data instanceof Serializable )) throw new MBeanException (new NotSerializableException (data.getClass().getName() + " must implement java.io.Serializable")); 142 143 FileOutputStream fos = null; 144 ObjectOutputStream oos = null; 145 synchronized (this) 146 { 147 try 148 { 149 fos = new FileOutputStream (m_store); 150 oos = new ObjectOutputStream (fos); 151 } 152 catch (IOException ex) 153 { 154 throw new MBeanException (ex); 155 } 156 try 157 { 158 oos.writeObject(data); 160 oos.flush(); 162 } 163 catch (IOException ex) 164 { 165 throw new MBeanException (ex); 166 } 167 finally 168 { 169 try 170 { 171 oos.close(); 172 } 173 catch (IOException ex) 174 { 175 } 176 } 177 } 178 } 179 } 180 | Popular Tags |