Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2009 - 2012 ScalAgent Distributed Technologies
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package fr.dyade.aaa.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamConstants;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Hashtable;
import org.objectweb.util.monolog.api.BasicLevel;
import fr.dyade.aaa.common.encoding.ByteBufferDecoder;
import fr.dyade.aaa.common.encoding.ByteBufferEncoder;
import fr.dyade.aaa.common.encoding.Encodable;
import fr.dyade.aaa.common.encoding.EncodableFactory;
import fr.dyade.aaa.common.encoding.EncodableFactoryRepository;
import fr.dyade.aaa.common.encoding.SerializableWrapper;
/**
* The AbstractTransaction class implements the common part of the Transaction
* Transaction interface. A transaction implementation only needs to define several
* methods: saveInLog, loadByteArray, delete and commit.
*
* @see Transaction
*/
public abstract class AbstractTransaction extends BaseTransaction {
private static final byte JAVA_SERIALIZATION_TAG = 0x0;
private static final byte ENCODING_TAG = 0x1;
private boolean onlyUseJavaSerialization;
protected long startTime = 0L;
/**
* Returns the starting time.
*
* @return The starting time.
*/
public long getStartTime() {
return startTime;
}
public AbstractTransaction() {}
// State of the transaction monitor.
protected int phase;
/**
* Returns the transaction state.
* @return the transaction state.
*
* @see fr.dyade.aaa.util.Transaction#getPhase()
*/
public final int getPhase() {
return phase;
}
/**
* Returns a string representation of the transaction state.
* @return the string representation of the transaction state.
*
* @see fr.dyade.aaa.util.Transaction#getPhaseInfo()
*/
public final String getPhaseInfo() {
return PhaseInfo[phase];
}
/**
* Changes the transaction state.
*
* @param newPhase the new transaction state.
* @throws IOException
*/
protected abstract void setPhase(int newPhase) throws IOException;
protected File dir = null;
/**
* ThreadLocal variable used to get the log to associate state with each
* thread. The log contains all operations do by the current thread since
* the last commit. On commit, its content is added to current
* log (clog, memory + disk), then it is freed.
*/
protected ThreadLocal perThreadContext = null;
public abstract void initRepository() throws IOException;
public final void init(String path) throws IOException {
phase = INIT;
if (logmon.isLoggable(BasicLevel.INFO))
logmon.log(BasicLevel.INFO, "Transaction, init():");
dir = new File(path);
if (!dir.exists()) dir.mkdirs();
if (!dir.isDirectory())
throw new FileNotFoundException(path + " is not a directory.");
// Saves the transaction classname in order to prevent use of a
// different one after restart (see AgentServer.init).
DataOutputStream ldos = null;
try {
File tfc = new File(dir, "TFC");
if (! tfc.exists()) {
ldos = new DataOutputStream(new FileOutputStream(tfc));
ldos.writeUTF(getClass().getName());
ldos.flush();
}
} finally {
if (ldos != null) ldos.close();
}
loadProperties(dir);
onlyUseJavaSerialization = getBoolean("Transaction.OnlyUseJavaSerialization");
initRepository();
saveProperties(dir);
perThreadContext = new ThreadLocal() {
protected synchronized Context initialValue() {
return new Context();
}
};
startTime = System.currentTimeMillis();
if (logmon.isLoggable(BasicLevel.INFO))
logmon.log(BasicLevel.INFO, "Transaction, initialized " + startTime);
/* The Transaction subsystem is ready */
setPhase(FREE);
}
/**
* Start a transaction validation, the validation phase needs 3 phases: begin, commit
* and release. The begin ensure the mutual exclusion of the current transaction.
*
* @see fr.dyade.aaa.util.Transaction#begin()
*/
public final synchronized void begin() throws IOException {
while (phase != FREE) {
try {
wait();
} catch (InterruptedException exc) {
}
}
// Change the transaction state.
setPhase(RUN);
}
public class Context {
private Hashtable log = null;
private ByteArrayOutputStream bos = null;
private ObjectOutputStream oos = null;
public final Hashtable getLog() {
return log;
}
Context() {
log = new Hashtable