org.refcodes.component.ext.observer.ObservableLifecycleRequestAutomatonImpl 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 ObservableLifecycleRequestAutomatonImpl extends LifecycleAutomatonImpl implements ObservableLifecycleRequestAutomaton, Loggable {
// /////////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////////
private LifecycleRequestObservable _observable;
private EventMetaData _eventMetaData;
private Object _source;
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////////
/**
* Instantiates a new observable life cycle request automaton impl.
*/
public ObservableLifecycleRequestAutomatonImpl() {
super();
_eventMetaData = new EventMetaDataImpl();
_source = this;
_observable = new LifecycleRequestObservable();
}
/**
* {@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 ObservableLifecycleRequestAutomatonImpl( Object aSource ) {
super();
_eventMetaData = new EventMetaDataImpl();
_source = aSource;
_observable = new LifecycleRequestObservable();
}
/**
* {@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 ObservableLifecycleRequestAutomatonImpl( EventMetaData aEventMetaData ) {
super();
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleRequestObservable();
}
/**
* {@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 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 ObservableLifecycleRequestAutomatonImpl( EventMetaData aEventMetaData, Object aSource ) {
super();
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleRequestObservable();
}
/**
* {@inheritDoc}
*
* @param aLifecycleComponent The {@link LifecycleComponent} to which the
* life-cycle method calls are to be delegated to.
*/
public ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = this;
_observable = new LifecycleRequestObservable();
}
/**
* {@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 ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent, Object aSource ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = aSource;
_observable = new LifecycleRequestObservable();
}
/**
* {@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 ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleRequestObservable();
}
/**
* {@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 ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData, Object aSource ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleRequestObservable();
}
// 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 ObservableLifecycleRequestAutomatonImpl( ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = new EventMetaDataImpl();
_source = this;
_observable = new LifecycleRequestObservable( 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 ObservableLifecycleRequestAutomatonImpl( Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = new EventMetaDataImpl();
_source = aSource;
_observable = new LifecycleRequestObservable( 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 ObservableLifecycleRequestAutomatonImpl( EventMetaData aEventMetaData, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleRequestObservable( 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 ObservableLifecycleRequestAutomatonImpl( EventMetaData aEventMetaData, Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super();
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleRequestObservable( 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 ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = this;
_observable = new LifecycleRequestObservable( 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 ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent, Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = new EventMetaDataImpl( this.getClass() );
_source = aSource;
_observable = new LifecycleRequestObservable( 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 ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = this;
_observable = new LifecycleRequestObservable( 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 ObservableLifecycleRequestAutomatonImpl( LifecycleComponent aLifecycleComponent, EventMetaData aEventMetaData, Object aSource, ExecutorService aExecutorService, ExecutionStrategy aExecutionStrategy ) {
super( aLifecycleComponent );
_eventMetaData = aEventMetaData;
_source = aSource;
_observable = new LifecycleRequestObservable( aExecutorService, aExecutionStrategy );
}
// /////////////////////////////////////////////////////////////////////////
// OBSERVERS:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
public boolean hasObserverSubscription( LifecycleRequestObserver aObserver ) {
return _observable.hasObserverSubscription( aObserver );
}
/**
* {@inheritDoc}
*/
@Override
public boolean subscribeObserver( LifecycleRequestObserver aObserver ) {
return _observable.subscribeObserver( aObserver );
}
/**
* {@inheritDoc}
*/
@Override
public boolean unsubscribeObserver( LifecycleRequestObserver 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())) + "\" ..." );
if ( _observable != null ) {
try {
_observable.fireEvent( new InitializeEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
throw new InitializeException( e );
}
}
super.initialize();
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())) + "\" ..." );
if ( _observable != null ) {
try {
_observable.fireEvent( new StartEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
throw new StartException( e );
}
}
super.start();
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())) + "\" ..." );
if ( _observable != null ) {
try {
_observable.fireEvent( new PauseEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
throw new PauseException( e );
}
}
super.pause();
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())) + "\" ..." );
if ( _observable != null ) {
try {
_observable.fireEvent( new ResumeEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
throw new ResumeException( e );
}
}
super.resume();
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())) + "\" ..." );
if ( _observable != null ) {
try {
_observable.fireEvent( new StopEvent( _eventMetaData, _source ) );
}
catch ( VetoException e ) {
throw new StopException( e );
}
}
super.stop();
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())) + "\" ..." );
if ( _observable != null ) {
try {
_observable.fireEvent( new DestroyEvent( _eventMetaData, _source ) );
}
catch ( VetoException ignore ) {
/* Cannot happen here */
}
}
super.destroy();
_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 LifecycleRequestObservable extends AbstractObservable {
// /////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////
private ExecutionStrategy _executionStrategy;
// /////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////
/**
* Instantiates a new life cycle observable.
*/
public LifecycleRequestObservable() {
super();
_executionStrategy = ExecutionStrategy.SEQUENTIAL;
}
/**
* Instantiates a new life cycle observable.
*
* @param aExecutorService the executor service
* @param aExecutionStrategy the execution strategy
*/
public LifecycleRequestObservable( 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( LifecycleRequestEvent aEvent, LifecycleRequestObserver aEventListener, ExecutionStrategy aEventExecutionStrategy ) throws Exception {
if ( aEvent instanceof InitializeEvent ) {
aEventListener.onInitialize( (InitializeEvent) aEvent );
}
else if ( aEvent instanceof StartEvent ) {
aEventListener.onStart( (StartEvent) aEvent );
}
else if ( aEvent instanceof PauseEvent ) {
aEventListener.onPause( (PauseEvent) aEvent );
}
else if ( aEvent instanceof ResumeEvent ) {
aEventListener.onResume( (ResumeEvent) aEvent );
}
else if ( aEvent instanceof StopEvent ) {
aEventListener.onStop( (StopEvent) aEvent );
}
else if ( aEvent instanceof DestroyEvent ) {
aEventListener.onDestroy( (DestroyEvent) 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( LifecycleRequestEvent aEvent ) throws VetoException {
return super.fireEvent( aEvent, _executionStrategy );
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy