1 23 24 package com.sun.enterprise.admin.common.domains.registry; 25 26 import java.io.RandomAccessFile ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.lang.ClassNotFoundException ; 30 import java.io.IOException ; 31 import java.io.ObjectInputStream ; 32 import java.io.ObjectOutputStream ; 33 import java.io.File ; 34 import java.nio.channels.FileChannel ; 35 import java.io.EOFException ; 36 37 38 class Locked implements LockingStore 39 { 40 Locked(PersistentStore s, RandomAccessFile r){ 41 checkNotNull(s, "null store not allowed"); 42 checkNotNull(r, "null Random Access File not allowed"); 43 store = s; 44 raf = r; 45 } 46 47 public long lastModified() {return 0L;} 48 49 66 67 public Object readObject() throws IOException , ClassNotFoundException { 68 try { 69 Object o = null; 70 ObjectInputStream ois = null; 71 FileInputStream fis = null; 72 try { 73 fis = new FileInputStream (store.getStore()); 74 if (fis.available() > 0){ 75 ois = new ObjectInputStream (fis); 76 o = ois.readObject(); 77 } else { 78 o = null; 79 fis.close(); 80 } 81 } 82 catch (EOFException e){ o = null; 84 } 85 finally{ 86 if (fis != null){ 87 fis.close(); 88 } 89 if (ois != null){ 90 ois.close(); 91 } 92 } 93 return o; 94 } 95 catch (IOException ioe){ 96 unlock(); 97 throw ioe; 98 } 99 catch (ClassNotFoundException cnfe){ 100 unlock(); 101 throw cnfe; 102 } 103 104 } 105 119 120 public void writeObject(Object o) throws IOException , IllegalStateException { 121 ObjectOutputStream oos = null; 122 FileOutputStream fos = null; 123 try { 124 oos = new ObjectOutputStream (new FileOutputStream (store.getStore(), 125 false)); 126 oos.writeObject(o); 127 oos.flush(); 128 } 129 catch (IOException ioe){ 130 this.unlock(); 131 throw ioe; 132 } 133 catch (IllegalStateException ise){ 134 this.unlock(); 135 throw ise; 136 } 137 finally { 138 if (fos != null) fos.close(); 139 if (oos != null) oos.close(); 140 } 141 } 142 143 public void lock() throws IOException , TimeoutException{} 144 145 public void unlock(){ 146 try { 147 raf.close(); 148 } 149 catch (IOException ioe){ 150 } 152 store.setState(new Unlocked(store)); 153 } 154 155 protected void finalize() { 156 this.unlock(); 157 } 158 159 160 private void checkNotNull(Object o, String m) throws NullPointerException { 161 if (o == null){ 162 throw new NullPointerException (m); 163 } 164 } 165 166 private PersistentStore store; 167 private RandomAccessFile raf; 168 169 } 170 171 172 173 174 | Popular Tags |