
org.refcodes.remoting.ext.observer.impls.ObservableRemoteServerImpl 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.io.Serializable;
import java.util.concurrent.ExecutorService;
import org.refcodes.component.ext.observer.events.ClosedEvent;
import org.refcodes.component.ext.observer.events.OpenedEvent;
import org.refcodes.component.ext.observer.events.impls.ClosedEventImpl;
import org.refcodes.component.ext.observer.events.impls.OpenedEventImpl;
import org.refcodes.connection.Transceiver;
import org.refcodes.controlflow.consts.ExecutionStrategy;
import org.refcodes.controlflow.utils.ControlFlowUtility;
import org.refcodes.exception.traps.VetoException;
import org.refcodes.observer.events.MetaDataEvent;
import org.refcodes.observer.impls.AbstractObservable;
import org.refcodes.remoting.ext.observer.ObservableRemoteServer;
import org.refcodes.remoting.ext.observer.ProviderRemoteObserver;
import org.refcodes.remoting.ext.observer.events.SubjectPublishedEvent;
import org.refcodes.remoting.ext.observer.events.SubjectSignedOffEvent;
import org.refcodes.remoting.ext.observer.events.impls.SubjectPublishedEventImpl;
import org.refcodes.remoting.ext.observer.events.impls.SubjectSignedOffEventImpl;
import org.refcodes.remoting.impls.RemoteServerImpl;
/**
* @author steiner
*/
public class ObservableRemoteServerImpl extends RemoteServerImpl implements ObservableRemoteServer {
// /////////////////////////////////////////////////////////////////////////
// CONSTANTS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// VARIABLES:
// /////////////////////////////////////////////////////////////////////////
private ProviderRemoteObservableImpl _observable;
// /////////////////////////////////////////////////////////////////////////
// CONSTRUCTORS:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
public ObservableRemoteServerImpl( Transceiver aTransceiver ) {
this( aTransceiver, null );
}
/**
* {@inheritDoc}
*/
public ObservableRemoteServerImpl( Transceiver aTransceiver, ExecutorService aExecutorService ) {
super( aTransceiver, aExecutorService );
_observable = new ProviderRemoteObservableImpl( getExecutorService() );
}
// /////////////////////////////////////////////////////////////////////////
// METHODS:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
public boolean hasObserverSubscription( ProviderRemoteObserver aObserver ) {
ControlFlowUtility.throwIllegalStateException( isDestroyed() );
return _observable.hasObserverSubscription( aObserver );
}
/**
* {@inheritDoc}
*/
@Override
public boolean subscribeObserver( ProviderRemoteObserver aObserver ) {
ControlFlowUtility.throwIllegalStateException( isDestroyed() );
return _observable.subscribeObserver( aObserver );
}
/**
* {@inheritDoc}
*/
@Override
public boolean unsubscribeObserver( ProviderRemoteObserver aObserver ) {
ControlFlowUtility.throwIllegalStateException( isDestroyed() );
return _observable.unsubscribeObserver( aObserver );
}
/**
* {@inheritDoc}
*/
@Override
public void destroy() {
if ( !isDestroyed() ) {
super.destroy();
_observable.clear();
_observable = null;
}
}
// /////////////////////////////////////////////////////////////////////////
// HOOKS:
// /////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////
// SIGNALS:
// /////////////////////////////////////////////////////////////////////////
/**
* {@inheritDoc}
*/
@Override
protected void onOpened() {
if ( !_observable.isEmpty() ) {
try {
_observable.fireEvent( new OpenedEventImpl( this ) );
}
catch ( VetoException aException ) {
/* not thrown by observer */
}
}
}
/**
* {@inheritDoc}
*/
@Override
protected void onSubjectPublished( Object aSubject ) {
if ( !_observable.isEmpty() ) {
try {
_observable.fireEvent( new SubjectPublishedEventImpl( aSubject, this ) );
}
catch ( VetoException aException ) {
/* not thrown by observer */
}
}
}
/**
* @param eObjectDescriptor
*/
@Override
protected void onSubjectSignedOff( Object aSubject ) {
if ( !_observable.isEmpty() ) {
SubjectSignedOffEvent eSubjectSignedOffEvent = new SubjectSignedOffEventImpl( aSubject, this );
try {
_observable.fireEvent( eSubjectSignedOffEvent );
}
catch ( VetoException ve ) {
/* not thrown by observer */
}
}
}
/**
* {@inheritDoc}
*/
@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 ProviderRemoteObservableImpl extends AbstractObservable {
/**
* {@inheritDoc}
*/
@Override
public int size() {
return super.size();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEmpty() {
return super.isEmpty();
}
/**
* {@inheritDoc}
*/
@Override
public void clear() {
super.clear();
}
/**
*
* @param aExecutorService
*/
public ProviderRemoteObservableImpl( ExecutorService aExecutorService ) {
super( aExecutorService );
}
/**
* 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 );
}
/**
* {@inheritDoc}
*/
@Override
protected boolean fireEvent( MetaDataEvent aEvent, ProviderRemoteObserver aObserver, ExecutionStrategy aEventExecutionStrategy ) throws VetoException {
if ( aEvent instanceof SubjectPublishedEvent ) {
aObserver.onSubjectPublishedEvent( (SubjectPublishedEvent) aEvent );
}
else if ( aEvent instanceof SubjectSignedOffEvent ) {
aObserver.onSubjectSignedOffEvent( (SubjectSignedOffEvent) 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