FileDocCategorySizeDatePackage
Locked.javaAPI DocGlassfish v2 API5529Fri May 04 22:33:32 BST 2007com.sun.enterprise.admin.common.domains.registry

Locked

public class Locked extends Object implements LockingStore

Fields Summary
private PersistentStore
store
private RandomAccessFile
raf
Constructors Summary
Locked(PersistentStore s, RandomAccessFile r)

	checkNotNull(s, "null store not allowed");
	checkNotNull(r, "null Random Access File not allowed");
	store = s;
	raf = r;
  
Methods Summary
private voidcheckNotNull(java.lang.Object o, java.lang.String m)

	if (o == null){
	  throw new NullPointerException(m);
	}
  
protected voidfinalize()

	this.unlock();
  
public longlastModified()

return 0L;
public voidlock()

public java.lang.ObjectreadObject()
Read the object from the store.

precondition - true

postcondition - store has not been modified

return
the single object that was in the store (or null if the store is empty). Releases all resources except lock. State is unchanged.
throws
TimeoutException if a lock on the store couldn't be obtained in a reasonable time.
throws
IOException if there was a problem reading from the store. Release all resources. State is unlocked
throws
ClassNotFoundException if the object could not be restored from the store. Release all resources. State is unlocked

	try {
	  Object o = null;
	  ObjectInputStream ois = null;
	  FileInputStream fis = null;
	  try {
		fis = new FileInputStream(store.getStore());
		if (fis.available() > 0){
		  ois = new ObjectInputStream(fis);
		  o = ois.readObject();
		} else {
		  o = null;
		  fis.close();
		}
	  }
	  catch (EOFException e){	// this occurs if the file is empty
		o = null;
	  }
	  finally{
		if (fis != null){
		  fis.close();
		}
		if (ois != null){
		  ois.close();
		}
	  }
	  return o;
	}
	catch (IOException ioe){
	  unlock();
	  throw ioe;
	}
	catch (ClassNotFoundException cnfe){
	  unlock();
	  throw cnfe;
	}
	
  
public voidunlock()

	try {
	  raf.close();
	}
	catch (IOException ioe){
		  // stomp on exceptions during closure - we don't care!
	}
	store.setState(new Unlocked(store));
  
public voidwriteObject(java.lang.Object o)
Write the given object to the store via serialization.

Precondition - store is locked

Postcondition - store contains this object, and only this object. All resources except lock are released. State is unchanged.

param
o the object to be put into the store. Must implement {@link Serializable}
throws
IOException if there was a problem writing to the store. All resources released. State is unlocked
throws
IllegalStateException if the store is not locked. All resources released. State is unlocked

	ObjectOutputStream oos = null;
	FileOutputStream fos = null;
	try {
	  oos = new ObjectOutputStream(new FileOutputStream(store.getStore(),
														false));
	  oos.writeObject(o);
	  oos.flush();
	}
	catch (IOException ioe){
	  this.unlock();
	  throw ioe;
	}
	catch (IllegalStateException ise){
	  this.unlock();
	  throw ise;
	}
	finally {
	  if (fos != null) fos.close();
	  if (oos != null) oos.close();
	}