jadex.bridge.service.component.interceptors.ServiceGetter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-platform-bridge Show documentation
Show all versions of jadex-platform-bridge Show documentation
Jadex bridge is a base package for kernels and platforms, i.e., it is used by both and provides commonly used interfaces and classes for active components and their management.
package jadex.bridge.service.component.interceptors;
import jadex.bridge.IInternalAccess;
import jadex.bridge.service.ServiceScope;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.ServiceQuery;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
/**
* The service getter allows for getting a service
*/
public class ServiceGetter
{
/** The internal access. */
protected IInternalAccess component;
/** The service type. */
protected Class type;
/** The cached service. */
protected T service;
/** The scope. */
protected ServiceScope scope;
/** The time of the last search. */
protected long lastsearch;
/** The delay between searches when no service was found. */
protected long delay = 30000;
/** Ongoing call future. */
protected Future callfut;
/**
* Create a new service getter.
*/
public ServiceGetter(IInternalAccess component, Class type, ServiceScope scope)
{
this(component, 30000, type, scope);
}
/**
* Create a new service getter.
*/
public ServiceGetter(IInternalAccess component, long delay, Class type, ServiceScope scope)
{
this.component = component;
this.delay = delay;
this.type = type;
if(ServiceScope.EXPRESSION.equals(scope))
throw new IllegalArgumentException("Cannot use scope 'expression' directly.");
this.scope = scope;
}
/**
* Get or search the service with a delay in case not found.
*/
public IFuture getService()
{
// System.out.println("getMon");
// final Future ret = new Future();
// component.getServiceContainer().searchService( new ServiceQuery<>( type, scope))
// .addResultListener(component.createResultListener(new IResultListener()
// {
// public void resultAvailable(T result)
// {
// service = result;
// ret.setResult(service);
// }
//
// public void exceptionOccurred(Exception exception)
// {
// // exception.printStackTrace();
// ret.setResult(null);
// }
// }));
Future ret;
// Must use a call future to ensure that all calls get a result if one can be found
if(callfut==null)
{
callfut = new Future();
ret = callfut;
if(service==null)
{
if(lastsearch==0 || System.currentTimeMillis()>lastsearch+delay)
{
lastsearch = System.currentTimeMillis();
component.getFeature(IRequiredServicesFeature.class).searchService(new ServiceQuery<>(type, scope))
.addResultListener(new IResultListener()
{
public void resultAvailable(T result)
{
service = result;
// ret.setResult(service);
Future fut = callfut;
callfut = null;
fut.setResult(service);
}
public void exceptionOccurred(Exception exception)
{
// exception.printStackTrace();
// ret.setResult(null);
Future fut = callfut;
callfut = null;
fut.setResult(null);
}
});
}
else
{
Future fut = callfut;
callfut = null;
fut.setResult(null);
}
}
else
{
Future fut = callfut;
callfut = null;
fut.setResult(service);
}
}
else
{
ret = callfut;
}
return ret;
}
/**
* Set the service to null, if e.g. broken.
*/
public void resetService()
{
this.service = null;
}
/**
* Get last service.
*/
public T getLastService()
{
return this.service;
}
}