better lifecycle mgmnt

This commit is contained in:
kimchy 2010-04-03 02:20:42 +03:00
parent d633b3dfbb
commit 65acc0cfa9
2 changed files with 47 additions and 3 deletions

View File

@ -59,13 +59,14 @@ public abstract class AbstractLifecycleComponent<T> extends AbstractComponent im
}
@SuppressWarnings({"unchecked"}) @Override public T start() throws ElasticSearchException {
if (!lifecycle.moveToStarted()) {
if (!lifecycle.canMoveToStarted()) {
return (T) this;
}
for (LifecycleListener listener : listeners) {
listener.beforeStart();
}
doStart();
lifecycle.moveToStarted();
for (LifecycleListener listener : listeners) {
listener.afterStart();
}
@ -75,12 +76,13 @@ public abstract class AbstractLifecycleComponent<T> extends AbstractComponent im
protected abstract void doStart() throws ElasticSearchException;
@SuppressWarnings({"unchecked"}) @Override public T stop() throws ElasticSearchException {
if (!lifecycle.moveToStopped()) {
if (!lifecycle.canMoveToStopped()) {
return (T) this;
}
for (LifecycleListener listener : listeners) {
listener.beforeStop();
}
lifecycle.moveToStopped();
doStop();
for (LifecycleListener listener : listeners) {
listener.afterStop();
@ -94,12 +96,13 @@ public abstract class AbstractLifecycleComponent<T> extends AbstractComponent im
if (lifecycle.started()) {
stop();
}
if (!lifecycle.moveToClosed()) {
if (!lifecycle.canMoveToClosed()) {
return;
}
for (LifecycleListener listener : listeners) {
listener.beforeClose();
}
lifecycle.moveToClosed();
doClose();
for (LifecycleListener listener : listeners) {
listener.afterClose();

View File

@ -104,6 +104,21 @@ public class Lifecycle {
return state == State.CLOSED;
}
public boolean canMoveToStarted() throws ElasticSearchIllegalStateException {
State localState = this.state;
if (localState == State.INITIALIZED || localState == State.STOPPED) {
return true;
}
if (localState == State.STARTED) {
return false;
}
if (localState == State.CLOSED) {
throw new ElasticSearchIllegalStateException("Can't move to started state when closed");
}
throw new ElasticSearchIllegalStateException("Can't move to started with unknown state");
}
public boolean moveToStarted() throws ElasticSearchIllegalStateException {
State localState = this.state;
if (localState == State.INITIALIZED || localState == State.STOPPED) {
@ -119,6 +134,20 @@ public class Lifecycle {
throw new ElasticSearchIllegalStateException("Can't move to started with unknown state");
}
public boolean canMoveToStopped() throws ElasticSearchIllegalStateException {
State localState = state;
if (localState == State.STARTED) {
return true;
}
if (localState == State.INITIALIZED || localState == State.STOPPED) {
return false;
}
if (localState == State.CLOSED) {
throw new ElasticSearchIllegalStateException("Can't move to started state when closed");
}
throw new ElasticSearchIllegalStateException("Can't move to started with unknown state");
}
public boolean moveToStopped() throws ElasticSearchIllegalStateException {
State localState = state;
if (localState == State.STARTED) {
@ -134,6 +163,18 @@ public class Lifecycle {
throw new ElasticSearchIllegalStateException("Can't move to started with unknown state");
}
public boolean canMoveToClosed() throws ElasticSearchIllegalStateException {
State localState = state;
if (localState == State.CLOSED) {
return false;
}
if (localState == State.STARTED) {
throw new ElasticSearchIllegalStateException("Can't move to closed before moving to stopped mode");
}
return true;
}
public boolean moveToClosed() throws ElasticSearchIllegalStateException {
State localState = state;
if (localState == State.CLOSED) {