org.refcodes.component.ext.observer.ObservableLifecycleStatusAutomatonImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of refcodes-component-ext-observer Show documentation
Show all versions of refcodes-component-ext-observer Show documentation
Artifact for providing event based extended functionality for the
refcodes-component artifact.
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// =============================================================================
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// =============================================================================
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// together with the GPL linking exception applied; as being applied by the GNU
// Classpath ("http://www.gnu.org/software/classpath/license.html")
// =============================================================================
// Apache License, v2.0 ("http://www.apache.org/licenses/TEXT-2.0")
// =============================================================================
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////
package org.refcodes.component.ext.observer;
import java.util.concurrent.ExecutorService;
import org.refcodes.component.InitializeException;
import org.refcodes.component.LifecycleAutomatonImpl;
import org.refcodes.component.LifecycleComponent;
import org.refcodes.component.LifecycleRequest;
import org.refcodes.component.LifecycleStatus;
import org.refcodes.component.PauseException;
import org.refcodes.component.ResumeException;
import org.refcodes.component.StartException;
import org.refcodes.component.StopException;
import org.refcodes.controlflow.ExecutionStrategy;
import org.refcodes.exception.VetoException;
import org.refcodes.mixin.EventMetaData;
import org.refcodes.mixin.EventMetaDataImpl;
import org.refcodes.mixin.Loggable;
import org.refcodes.observer.AbstractObservable;
import org.refcodes.observer.GenericActionEvent;
/**
* Abstract implementation of the {@link ObservableLifecycleStatusAutomaton}
* interface with the according hook methods for you to create your domain
* driven {@link ObservableLifecycleStatusAutomaton}.
*/
public class ObservableLifecycleStatusAutomatonImpl extends LifecycleAutomatonImpl implements ObservableLifecycleStatusAutomaton, Loggable {
// /////////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////////
private LifecycleStatusObservable _observable;
private EventMetaData _eventMetaData;
private Object _source;
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////////
/**
* Instantiates a new observable life cycle automaton impl.
*/
public ObservableLifecycleStatusAutomatonImpl() {
super();
_eventMetaData = new EventMetaDataImpl();
_source = this;
_observable = new LifecycleStatusObservable();
}
/**
* {@inheritDoc}
*
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
*/
public ObservableLifecycleStatusAutomatonImpl( Object aSource ) {
super();
_eventMetaData = new EventMetaDataImpl();
_source = aSource;
_observable = new LifecycleStatusObservable();
}
/**
* {@inheritDoc}
*
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
*/
public ObservableLifecycleStatusAutomatonImpl( EventMetaData aEventMetaData ) {
super();
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleStatusObservable();
}
/**
* {@inheritDoc}
*
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
*/
public ObservableLifecycleStatusAutomatonImpl( EventMetaData aEventMetaData, Object aSource ) {
super();
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleStatusObservable();
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = this;
_observable = new LifecycleStatusObservable();
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent, Object aSource ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = aSource;
_observable = new LifecycleStatusObservable();
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleStatusObservable();
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData, Object aSource ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleStatusObservable();
}
// WITH EXECUTOR-SERVICE:
/**
* {@inheritDoc}
*
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = new EventMetaDataImpl();
_source = this;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
/**
* {@inheritDoc}
*
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = new EventMetaDataImpl();
_source = aSource;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
/**
* {@inheritDoc}
*
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( EventMetaData aEventMetaData, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
/**
* {@inheritDoc}
*
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( EventMetaData aEventMetaData, Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = this;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent, Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = aSource;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
* @param aEventMetaData The {@link EventMetaData} to be used when firing
* events in case the {@link EventMetaData} is to be different from
* the auto-generated {@link EventMetaData}.
* @param aSource The source instance to be used when firing events in case
* the source is to be different from this class' instance.
* @param aExecutorService The executor service to be used when firing
* {@link GenericActionEvent} instances in multiple threads (if null
* then a default one is used).
* @param aExecutionStrategy The {@link ExecutionStrategy} to be used when
* firing {@link GenericActionEvent} instance (if null then the
* default {@link ExecutionStrategy#SEQUENTIAL} is used).
*/
public ObservableLifecycleStatusAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData, Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleStatusObservable( aExecutorService, aExecutionStrategy );
}
// /////////////////////////////////////////////////////////////////////////
// OBSERVERS:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
public boolean hasObserverSubscription( LifecycleStatusObserver aObserver ) {
return _observable.hasObserverSubscription( aObserver );
}
/**
* {@inheritDoc}
*/
@Override
public boolean subscribeObserver( LifecycleStatusObserver aObserver ) {
return _observable.subscribeObserver( aObserver );
}
/**
* {@inheritDoc}
*/
@Override
public boolean unsubscribeObserver( LifecycleStatusObserver aObserver ) {
return _observable.unsubscribeObserver( aObserver );
}
// /////////////////////////////////////////////////////////////////////////
// LIFE-CYCLE:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
public synchronized void initialize() throws InitializeException {
info( "About to " + LifecycleRequest.INITIALIZE + " component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" ..." );
boolean isFireEvent = isInitalizable();
super.initialize();
if ( isFireEvent && isInitialized() ) {
try {
_observable.fireEvent( new InitializedEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
/* Cannot happen here */
}
}
info( "Component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" is " + LifecycleStatus.INITIALIZED + "." );
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void start() throws StartException {
info( "About to " + LifecycleRequest.START + " component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" ..." );
boolean isFireEvent = isStartable();
super.start();
if ( isFireEvent && isRunning() ) {
try {
_observable.fireEvent( new StartedEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
/* Cannot happen here */
}
}
info( "Component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" is " + LifecycleStatus.STARTED + "." );
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void pause() throws PauseException {
info( "About to " + LifecycleRequest.PAUSE + " component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" ..." );
boolean isFireEvent = isPausable();
super.pause();
if ( isFireEvent && isPaused() ) {
try {
_observable.fireEvent( new PausedEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
/* Cannot happen here */
}
}
info( "Component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" is " + LifecycleStatus.PAUSED + "." );
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void resume() throws ResumeException {
info( "About to " + LifecycleRequest.RESUME + " component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" ..." );
boolean isFireEvent = isResumable();
super.resume();
if ( isFireEvent && isRunning() ) {
try {
_observable.fireEvent( new ResumedEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
/* Cannot happen here */
}
}
info( "Component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" is " + LifecycleStatus.STARTED + "." );
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void stop() throws StopException {
info( "About to " + LifecycleRequest.STOP + " component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" ..." );
boolean isFireEvent = isStoppable();
super.stop();
if ( isFireEvent && isStopped() ) {
try {
_observable.fireEvent( new StoppedEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
/* Cannot happen here */
}
}
info( "Component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" is " + LifecycleStatus.STOPPED + "." );
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void destroy() {
info( "About to " + LifecycleRequest.DESTROY + " component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" ..." );
boolean isFireEvent = isDestroyable();
super.destroy();
if ( isFireEvent && isDestroyed() ) {
try {
_observable.fireEvent( new DestroyedEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
/* Cannot happen here */
}
}
_observable.clear();
_observable = null;
_source = null;
_eventMetaData = null;
info( "Component \"" + ((getLifecycleComponent() != null) ? (getLifecycleComponent().getClass().getName()) : (getClass().getName())) + "\" is " + LifecycleStatus.DESTROYED + "." );
}
// /////////////////////////////////////////////////////////////////////////
// INNER CLASSES:
// /////////////////////////////////////////////////////////////////////////
/**
* Implementation of the {@link AbstractObservable} for event distribution.
*/
private class LifecycleStatusObservable extends AbstractObservable {
// /////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////
private ExecutionStrategy _executionStrategy;
// /////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////
/**
* Instantiates a new life cycle observable.
*/
public LifecycleStatusObservable() {
super();
_executionStrategy = ExecutionStrategy.SEQUENTIAL;
}
/**
* Instantiates a new life cycle observable.
*
* @param aExecutorService the executor service
* @param aExecutionStrategy the execution strategy
*/
public LifecycleStatusObservable( ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aExecutorService );
_executionStrategy = (aExecutionStrategy != null) ? aExecutionStrategy : ExecutionStrategy.SEQUENTIAL;
}
// /////////////////////////////////////////////////////////////////////
// METHODS:
// /////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
public int size() {
return super.size();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEmpty() {
return super.isEmpty();
}
/**
* {@inheritDoc}
*/
@Override
public void clear() {
super.clear();
}
// /////////////////////////////////////////////////////////////////////
// HOOKS:
// /////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
protected boolean fireEvent( LifecycleStatusEvent aEvent, LifecycleStatusObserver aEventListener, ExecutionStrategy aEventExecutionStrategy ) throws Exception {
if ( aEvent instanceof InitializedEvent ) {
aEventListener.onInitialized( (InitializedEvent) aEvent );
}
else if ( aEvent instanceof StartedEvent ) {
aEventListener.onStarted( (StartedEvent) aEvent );
}
else if ( aEvent instanceof PausedEvent ) {
aEventListener.onPaused( (PausedEvent) aEvent );
}
else if ( aEvent instanceof ResumedEvent ) {
aEventListener.onResumed( (ResumedEvent) aEvent );
}
else if ( aEvent instanceof StoppedEvent ) {
aEventListener.onStopped( (StoppedEvent) aEvent );
}
else if ( aEvent instanceof DestroyedEvent ) {
aEventListener.onDestroyed( (DestroyedEvent) aEvent );
}
return true;
}
/**
* Fires the according event.
*
* @param aEvent The {@link GenericActionEvent} to be fired.
*
* @return true, if successful
*
* @throws VetoException Thrown in case of a veto.
*/
protected boolean fireEvent( LifecycleStatusEvent aEvent ) throws VetoException {
return super.fireEvent( aEvent, _executionStrategy );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy