All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.refcodes.remoting.ext.observer.impls.ObservableRemoteClientImpl Maven / Gradle / Ivy

// /////////////////////////////////////////////////////////////////////////////
// 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/LICENSE-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.remoting.ext.observer.impls;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.refcodes.component.ext.observer.ClosedEvent;
import org.refcodes.component.ext.observer.OpenedEvent;
import org.refcodes.component.ext.observer.impls.ClosedEventImpl;
import org.refcodes.component.ext.observer.impls.OpenedEventImpl;
import org.refcodes.controlflow.ControlFlowUtility;
import org.refcodes.controlflow.ExecutionStrategy;
import org.refcodes.exception.VetoException;
import org.refcodes.observer.MetaDataEvent;
import org.refcodes.observer.impls.AbstractObservable;
import org.refcodes.remoting.ext.observer.ConsumerRemoteObserver;
import org.refcodes.remoting.ext.observer.ObservableRemoteClient;
import org.refcodes.remoting.ext.observer.ProxyPublishedEvent;
import org.refcodes.remoting.ext.observer.ProxySignedOffEvent;
import org.refcodes.remoting.ext.observer.PublishProxyEvent;
import org.refcodes.remoting.ext.observer.SignOffProxyEvent;
import org.refcodes.remoting.impls.RemoteClientImpl;
import org.refcodes.remoting.impls.RemoteServerImpl;

/**
 * @author steiner
 */
public class ObservableRemoteClientImpl extends RemoteClientImpl implements ObservableRemoteClient {

	// /////////////////////////////////////////////////////////////////////////
	// CONSTANTS:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// VARIABLES:
	// /////////////////////////////////////////////////////////////////////////

	private ConsumerRemoteObservableImpl _observable;

	// /////////////////////////////////////////////////////////////////////////
	// CONSTRUCTORS:
	// /////////////////////////////////////////////////////////////////////////

	public ObservableRemoteClientImpl() {
		this( null );
	}

	public ObservableRemoteClientImpl( ExecutorService aExecutorService ) {
		super( aExecutorService );
		_observable = new ConsumerRemoteObservableImpl( getExecutorService() );
	}

	// /////////////////////////////////////////////////////////////////////////
	// METHODS:
	// /////////////////////////////////////////////////////////////////////////

	@Override
	public boolean hasObserverSubscription( ConsumerRemoteObserver aObserver ) {
		ControlFlowUtility.throwIllegalStateException( isDestroyed() );
		return _observable.hasObserverSubscription( aObserver );
	}

	@Override
	public boolean subscribeObserver( ConsumerRemoteObserver aObserver ) {
		ControlFlowUtility.throwIllegalStateException( isDestroyed() );
		return _observable.subscribeObserver( aObserver );
	}

	@Override
	public boolean unsubscribeObserver( ConsumerRemoteObserver aObserver ) {
		ControlFlowUtility.throwIllegalStateException( isDestroyed() );
		return _observable.unsubscribeObserver( aObserver );
	}

	@Override
	public void destroy() {
		if ( !isDestroyed() ) {
			super.destroy();
			_observable.clear();
			_observable = null;
		}
	}

	// /////////////////////////////////////////////////////////////////////////
	// HOOKS:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// SIGNALS:
	// /////////////////////////////////////////////////////////////////////////

	@Override
	protected void onOpened() {
		if ( !_observable.isEmpty() ) {
			try {
				_observable.fireEvent( new OpenedEventImpl( this ) );
			}
			catch ( VetoException aException ) {
				/* not thrown by observer */
			}
		}
	}

	@Override
	protected void onPublishProxy( Class aType ) throws VetoException {
		if ( !_observable.isEmpty() ) {
			_observable.fireEvent( new PublishProxyEventImpl( aType, this ) );
		}
	}

	@Override
	protected void onProxyPublished( Object aProxy ) {
		try {
			_observable.fireEvent( new ProxyPublishedEventImpl( aProxy, this ) );
		}
		catch ( VetoException aException ) {
			/* not thrown by observer */
		}
	}

	@Override
	protected void onProxySignedOff( Object aProxy ) {
		if ( !_observable.isEmpty() ) {
			ProxySignedOffEventImpl theProxySignedOffEvent = new ProxySignedOffEventImpl( aProxy, this );
			try {
				_observable.fireEvent( theProxySignedOffEvent );
			}
			catch ( VetoException aException ) {
				/* not thrown by observer */
			}
		}
	}

	@Override
	protected void onClosed() {
		if ( !_observable.isEmpty() ) {
			try {
				_observable.fireEvent( new ClosedEventImpl( this ) );
			}
			catch ( VetoException aException ) {
				/* not thrown by observer */
			}
		}
	}

	// /////////////////////////////////////////////////////////////////////////
	// HELPER:
	// /////////////////////////////////////////////////////////////////////////

	// /////////////////////////////////////////////////////////////////////////
	// INNER CLASSES:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Implementation of the {@link AbstractObservable} for the
	 * {@link RemoteServerImpl} class.
	 */
	private static class ConsumerRemoteObservableImpl extends AbstractObservable> {

		// /////////////////////////////////////////////////////////////////////
		// CONSTRUCTORS:
		// /////////////////////////////////////////////////////////////////////

		/**
		 * Constructs a {@link ConsumerRemoteObservableImpl} configured with the
		 * given {@link ExecutorService}.
		 * 
		 * @param aExecutorService The {@link ExecutorService} to be used, when
		 *        null then an {@link ExecutorService} something line
		 *        {@link Executors#newCachedThreadPool()} is then retrieved.
		 */
		public ConsumerRemoteObservableImpl( ExecutorService aExecutorService ) {
			super( aExecutorService );
		}

		// /////////////////////////////////////////////////////////////////////
		// METHDOS:
		// /////////////////////////////////////////////////////////////////////

		@Override
		public int size() {
			return super.size();
		}

		@Override
		public boolean isEmpty() {
			return super.isEmpty();
		}

		@Override
		public void clear() {
			super.clear();
		}

		/**
		 * Same as {@link #fireEvent(MetaDataEvent, ExecutionStrategy)} with a
		 * predefined {@link ExecutionStrategy#JOIN}.
		 * 
		 * @see #fireEvent(MetaDataEvent, ExecutionStrategy)
		 */
		protected boolean fireEvent( MetaDataEvent aEvent ) throws VetoException {
			return super.fireEvent( aEvent, ExecutionStrategy.PARALLEL );
		}

		@Override
		protected boolean fireEvent( MetaDataEvent aEvent, ConsumerRemoteObserver aObserver, ExecutionStrategy aEventExecutionStrategy ) throws VetoException {
			if ( aEvent instanceof PublishProxyEvent ) {
				aObserver.onPublishProxyEvent( (PublishProxyEvent) aEvent );
			}
			else if ( aEvent instanceof ProxyPublishedEvent ) {
				aObserver.onProxyPublishedEvent( (ProxyPublishedEvent) aEvent );
			}
			else if ( aEvent instanceof SignOffProxyEvent ) {
				aObserver.onSignOffProxyEvent( (SignOffProxyEvent) aEvent );
			}
			else if ( aEvent instanceof ProxySignedOffEvent ) {
				aObserver.onProxySignedOffEvent( (ProxySignedOffEvent) aEvent );
			}
			else if ( aEvent instanceof OpenedEvent ) {
				aObserver.onOpendEvent( (OpenedEvent) aEvent );
			}
			else if ( aEvent instanceof ClosedEvent ) {
				aObserver.onClosedEvent( (ClosedEvent) aEvent );
			}
			aObserver.onEvent( aEvent );
			return true;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy