bitronix.tm.resource.common.AbstractXAStatefulHolder Maven / Gradle / Ivy
package bitronix.tm.resource.common;
import java.util.*;
import org.slf4j.*;
import bitronix.tm.utils.Decoder;
/**
* Implementation of all services required by a {@link XAStatefulHolder}.
*
*
* @author lorban
*/
public abstract class AbstractXAStatefulHolder implements XAStatefulHolder {
private final static Logger log = LoggerFactory.getLogger(AbstractXAStatefulHolder.class);
private int state = STATE_IN_POOL;
private List stateChangeEventListeners = new ArrayList();
public synchronized int getState() {
return state;
}
public void setState(int state) {
int oldState = this.state;
fireStateChanging(oldState, state);
synchronized (this) {
if (oldState == state)
throw new IllegalArgumentException("cannot switch state from " + Decoder.decodeXAStatefulHolderState(oldState) +
" to " + Decoder.decodeXAStatefulHolderState(state));
if (log.isDebugEnabled()) log.debug("state changing from " + Decoder.decodeXAStatefulHolderState(oldState) +
" to " + Decoder.decodeXAStatefulHolderState(state) + " in " + this);
this.state = state;
}
fireStateChanged(oldState, state);
}
public void addStateChangeEventListener(StateChangeListener listener) {
stateChangeEventListeners.add(listener);
}
public void removeStateChangeEventListener(StateChangeListener listener) {
stateChangeEventListeners.remove(listener);
}
private void fireStateChanging(int currentState, int futureState) {
if (log.isDebugEnabled()) log.debug("notifying " + stateChangeEventListeners.size() +
" stateChangeEventListener(s) about state changing from " + Decoder.decodeXAStatefulHolderState(currentState) +
" to " + Decoder.decodeXAStatefulHolderState(futureState) + " in " + this);
for (int i = 0; i < stateChangeEventListeners.size(); i++) {
StateChangeListener stateChangeListener = (StateChangeListener) stateChangeEventListeners.get(i);
stateChangeListener.stateChanging(this, currentState, futureState);
}
}
private void fireStateChanged(int oldState, int newState) {
if (log.isDebugEnabled()) log.debug("notifying " + stateChangeEventListeners.size() +
" stateChangeEventListener(s) about state changed from " + Decoder.decodeXAStatefulHolderState(oldState) +
" to " + Decoder.decodeXAStatefulHolderState(newState) + " in " + this);
for (int i = 0; i < stateChangeEventListeners.size(); i++) {
StateChangeListener stateChangeListener = (StateChangeListener) stateChangeEventListeners.get(i);
stateChangeListener.stateChanged(this, oldState, newState);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy