HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Enabling A File System As A Transactional Resource"
Vol. 8, Issue 11, p. 28

	



Listing 1

public void close() throws ResourceException
{
if (mConn != null)
{
mConn.sendEvent(ConnectionEvent.CONNECTION_CLOSED,
null, this);
if (localTransaction != null &&
localTransaction.inTransaction) {
localTransaction.rollback();
localTransaction = null;
}
mConn = null;
}
return;
}

Listing 2

public class FSInRecord extends HashMap
implements MappedRecord
{
static final String SOURCE_DIR
= new String("SOURCE_DIR");
static final String SOURCE_FILENAME
= new String("SOURCE_FILENAME");
static final String DESTINATION_DIR
= new String("DESTINATION_DIR");
static final String DESTINATION_FILENAME
= new String("DESTINATION_FILENAME");
static final String DATA
= new String("DATA");
static final String SIZE
= new String("SIZE");
public FSInRecord(String fileName,
String newName, String sourceDir,
String destinationDir, int size)
{
this.put(SOURCE_DIR, sourceDir);
this.put(SOURCE_FILENAME, fileName);
this.put(DESTINATION_DIR, destinationDir);
this.put(DESTINATION_FILENAME, newName);
this.put(SIZE, new Integer(size));
}
...
...
}

Listing 3

boolean execute(InteractionSpec ispec, Record input,
Record output) throws ResourceException
{
FSOutRecord out = (FSOutRecord)output;
FSInRecord in = (FSInRecord)input;
String sourceDir = (String)in.get(FSInRecord.SOURCE_DIR);
sourceDir = (sourceDir != null &&
sourceDir.endsWith(File.separator)) ? sourceDir :
sourceDir + File.separator;
String sourceFileName =
(String)in.get(FSInRecord.SOURCE_FILENAME);
String destDir =
(String)in.get(FSInRecord.DESTINATION_DIR);
destDir = (destDir != null && destDir.endsWith(File.separator))
? destDir : destDir + File.separator;
String destFileName = (String)in.get(FSInRecord.DESTINATION_
FILENAME);
String destFile;
if((sourceDir == null) || (sourceDir.equals("")))
throw new ResourceException("Invalid File Directory");
if((sourceFileName == null) ||
(sourceFileName.equals("")))
throw new ResourceException("Invalid File Name");
String sourceFile = new String(sourceDir +
sourceFileName);
if(sourceDir.equals(workingFolder))

throw new ResourceException("Invalid File Directory. It
can not be same as working folder directory.");
try
{
switch(((InteractionSpecImpl)ispec).getAction())
{
case InteractionSpecImpl.CREATE: {
File file = new File(sourceFile);
boolean success = file.createNewFile();
if(!success) throw new ResourceException("File could
not be created");
addWork(sourceDir, sourceFileName, null, null,
WorkItem.CREATE);
break;}
case InteractionSpecImpl.DELETE: {
destFileName = sourceFileName + new
java.util.Date().getTime();
destFile = new String(workingFolder + destFileName);
closeFile(sourceFile);
File destFileHandle = new File(destFile);
File sourceFileHandle = new File(sourceFile);
boolean status =
sourceFileHandle.renameTo(destFileHandle);
if(! status)
throw new ResourceException("File can not be deleted");
addWork(sourceDir, sourceFileName, workingFolder,
destFileName, WorkItem.DELETE);
break;}
case InteractionSpecImpl.MOVE: {
if((destDir == null) || (destDir.equals("")))
throw new ResourceException("Invalid Destination File
Directory");
destFile = destFileName != null ? destDir +
destFileName : destDir + sourceFileName;
closeFile(sourceFile);
File destFileHandle = new File(destFile);
File sourceFileHandle = new File(sourceFile);
boolean status =
sourceFileHandle.renameTo(destFileHandle);
if(! status)
throw new ResourceException("File can not be moved");
String renameTo = destFileName != null ? destFileName
: sourceFileName;
addWork(sourceDir, sourceFileName, destDir, renameTo,
WorkItem.MOVE);
break;}
...
...
}
}
catch(IOException ie)
{
throw new ResourceException(ie.getMessage());
}
return true;
}

Listing 4

public WorkItem(String sourceDir, String sourceFile,
String destinationDir, String destinationFile, int
workName, ManagedConnection mConn) {
this.workName = workName;
this.sourceDir = (sourceDir.endsWith(File.separator)) ?
sourceDir : sourceDir + File.separator;
...
...
}
public void executeCommit() throws ResourceException {
switch(workName) {
case CREATE: break;
case UPDATE:
case DELETE: //delete the backed up file
String fullNameWithPath = new String(destinationDir +
destinationFile);
File toBeDeleted = new File(fullNameWithPath);
closeFile(fullNameWithPath);
if (!toBeDeleted.delete()) throw new
ResourceException("Clean Failure: failed to delete ... " +
toBeDeleted.getPath());
break;
case MOVE: break;
}
}

Listing 5

public void start(Xid xid, int flags) throws XAException {
if (mConn.localTransactionCCI != null) {
throw new XAException(XAException.XAER_OUTSIDE);
}
activate(xid);
BitSet states = null;
switch (flags) {
case TMNOFLAGS:
states = getTransactionState(associatedXID, true);
break;
case TMRESUME:
states = getTransactionState(associatedXID, false);
states = updateState(states, SUSPENDED, false);
break;
case TMJOIN:
states = getTransactionState(associatedXID, false);
break;
}
XIDStates.put(associatedXID, states);
}

Listing 6

public int prepare(Xid xid) throws XAException {
BitSet states = getTransactionState(xid, false);
boolean suspended = currentState(states, SUSPENDED);
boolean markedForRollback = currentState(states,
MARKED_FOR_ROLLBACK);
boolean readOnly = currentState(states, READ_ONLY);
if (suspended) {
throw new XAException(XAException.XAER_PROTO);
} else if (markedForRollback) {
throw new XAException(XAException.XA_RBOTHER);
} else if (readOnly) {
return XA_RDONLY;
}
states = updateState(states, PREPARED, true);
XIDStates.put(xid, states);
return XA_OK;
}

Listing 7

public void commit(Xid xid, boolean onePhase) throws
XAException {
BitSet states = getTransactionState(xid, false);
boolean prepared = currentState(states, PREPARED);
if (!onePhase) {
if (!prepared) {
throw new XAException(XAException.XAER_PROTO);
}
}
ArrayList workList = (ArrayList)XIDWork.get(xid);
executeActionsList(workList, true);
removeTransationHistory(xid);
}

Listing 8

public void setWorkingFolder(String workingFolder)
{
if (workingFolder == null ||
workingFolder.equals("")) throw new
RuntimeException("Invalid value for Working folder in the
Deployment Descriptor.");
File workingDir = new File(workingFolder);
if (!workingDir.isDirectory()) throw new
RuntimeException("Working folder specified is not a valid
Directory.");
if (!workingDir.canWrite()) throw new
RuntimeException("Working folder specified does not have
write permissions.");
this.workingFolder = workingFolder;
}

Listing 9

public void setWorkingFolder(String workingFolder)
{
if (workingFolder == null ||
workingFolder.equals("")) throw new
RuntimeException("Invalid value for Working folder in the
Deployment Descriptor.");
File workingDir = new File(workingFolder);
if (!workingDir.isDirectory()) throw new
RuntimeException("Working folder specified is not a valid
Directory.");
if (!workingDir.canWrite()) throw new
RuntimeException("Working folder specified does not have
write permissions.");
this.workingFolder = workingFolder;
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.