
jadex.platform.service.cms.PlatformComponent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-platform Show documentation
Show all versions of jadex-platform Show documentation
The Jadex platform package contains implementations of platform services as well as the platform component itself.
package jadex.platform.service.cms;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IComponentStep;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.ImmediateComponentStep;
import jadex.bridge.component.ComponentCreationInfo;
import jadex.bridge.component.FeatureNotAvailableException;
import jadex.bridge.component.IComponentFeature;
import jadex.bridge.component.IComponentFeatureFactory;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.component.IMonitoringComponentFeature;
import jadex.bridge.component.IPropertiesFeature;
import jadex.bridge.component.impl.IInternalExecutionFeature;
import jadex.bridge.modelinfo.IModelInfo;
import jadex.bridge.modelinfo.ModelInfo;
import jadex.bridge.modelinfo.SubcomponentTypeInfo;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.cms.IComponentDescription;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.bridge.service.types.factory.IPlatformComponentAccess;
import jadex.bridge.service.types.monitoring.IMonitoringEvent;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishEventLevel;
import jadex.bridge.service.types.monitoring.IMonitoringService.PublishTarget;
import jadex.bridge.service.types.monitoring.MonitoringEvent;
import jadex.commons.IParameterGuesser;
import jadex.commons.IValueFetcher;
import jadex.commons.SReflect;
import jadex.commons.future.CollectionResultListener;
import jadex.commons.future.CounterResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.kernelbase.ExternalAccess;
/**
* Standalone platform component implementation.
*/
public class PlatformComponent implements IPlatformComponentAccess, IInternalAccess
{
//-------- attributes --------
/** The creation info. */
protected ComponentCreationInfo info;
/** The features. */
protected Map, IComponentFeature> features;
/** The feature instances as list (for reverse execution, cached for speed). */
protected List lfeatures;
/** The inited feature instances as list (for shutdown after failed init). */
protected List ifeatures;
/** The logger. */
protected Logger logger;
/** The failure reason (if any). */
protected Exception exception;
/** The combined value fetcher (cached for speed). */
protected IValueFetcher fetcher;
// /** The component lifecycle state (init, body, end). */
// protected ComponentLifecycleState state = ComponentLifecycleState.CREATE;
//-------- IPlatformComponentAccess interface --------
/**
* Create the component, i.e. instantiate its features.
* This is the first method that is called by the platform.
*
* @param info The component creation info.
* @param platformdata The shared objects for all components of the same platform (registry etc.). See starter for available data.
* @param facs The factories for component features to be instantiated for this component.
*/
public void create(ComponentCreationInfo info, Collection facs)
{
// state = ComponentLifecycleState.CREATE;
this.info = info;
this.features = new LinkedHashMap, IComponentFeature>();
this.lfeatures = new ArrayList();
for(IComponentFeatureFactory fac: facs)
{
// System.out.println("feature: "+fac);
IComponentFeature instance = fac.createInstance(getInternalAccess(), info);
features.put((Class>)fac.getType(), instance);
for(Class> ltype: fac.getLookupTypes())
features.put(ltype, instance);
lfeatures.add(instance);
}
}
/**
* Perform the initialization of the component.
* Tries to switch to a separate thread for the component as soon as possible.
*
* @return A future to indicate when the initialization is done.
*/
public IFuture init()
{
// state = ComponentLifecycleState.INIT;
// Run init on component thread (hack!!! requires that execution feature works before its init)
IExecutionFeature exe = getComponentFeature(IExecutionFeature.class);
return exe.scheduleStep(new ImmediateComponentStep()
{
public IFuture execute(IInternalAccess ia)
{
ifeatures = new ArrayList();
return executeInitOnFeatures(lfeatures.iterator());
}
});
}
/**
* Perform the main execution of the component (if any).
*
* @return A future to indicate when the body is done.
*/
public IFuture body()
{
// state = ComponentLifecycleState.BODY;
IExecutionFeature exe = getComponentFeature(IExecutionFeature.class);
return exe.scheduleStep(new IComponentStep()
{
public IFuture execute(IInternalAccess ia)
{
return executeBodyOnFeatures(lfeatures.iterator());
}
});
}
/**
* Perform the shutdown of the component (if any).
*
* @return A future to indicate when the shutdown is done.
*/
public IFuture shutdown()
{
// state = ComponentLifecycleState.END;
// System.out.println("shutdown component features start: "+getComponentIdentifier());
IExecutionFeature exe = getComponentFeature(IExecutionFeature.class);
return exe.scheduleStep(new ImmediateComponentStep()
{
public IFuture execute(IInternalAccess ia)
{
final Future ret = new Future();
executeShutdownOnFeatures(ifeatures!=null ? ifeatures : lfeatures, 0)
.addResultListener(new IResultListener()
{
public void resultAvailable(Void result)
{
proceed(null);
}
public void exceptionOccurred(Exception exception)
{
proceed(exception);
}
public void proceed(final Exception ex)
{
// System.out.println("shutdown component features end: "+getComponentIdentifier());
if(getComponentFeature0(IMonitoringComponentFeature.class)!=null
&& getComponentFeature(IMonitoringComponentFeature.class).hasEventTargets(PublishTarget.TOALL, PublishEventLevel.COARSE))
{
MonitoringEvent event = new MonitoringEvent(getComponentDescription().getName(), getComponentDescription().getCreationTime(),
IMonitoringEvent.TYPE_COMPONENT_DISPOSED, getComponentDescription().getCause(), System.currentTimeMillis(), PublishEventLevel.COARSE);
event.setProperty("details", getComponentDescription());
getComponentFeature(IMonitoringComponentFeature.class).publishEvent(event, PublishTarget.TOALL).addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
if(ex!=null)
ret.setException(ex);
else
ret.setResult(null);
}
public void exceptionOccurred(Exception exception)
{
ret.setException(exception);
}
});
}
else
{
ret.setResult(null);
}
}
});
return ret;
}
});
}
/**
* Recursively init the features.
*/
protected IFuture executeInitOnFeatures(final Iterator features)
{
IFuture fut = IFuture.DONE;
while(fut.isDone() && fut.getException()==null && features.hasNext())
{
IComponentFeature cf = features.next();
// if(getComponentIdentifier().getName().indexOf("Interceptor")!=-1)
// System.out.println("Initing "+cf+" of "+getComponentIdentifier());
ifeatures.add(cf);
fut = cf.init();
}
// fut.addResultListener(new IResultListener()
// {
// public void resultAvailable(Void result)
// {
// System.out.println("ini fin: "+getComponentIdentifier());
// }
//
// public void exceptionOccurred(Exception exception)
// {
// System.out.println("ini ex: "+getComponentIdentifier());
// }
// });
if(!fut.isDone())
{
final Future ret = new Future();
fut.addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
executeInitOnFeatures(features).addResultListener(new DelegationResultListener(ret));
}
});
return ret;
}
else
{
if(fut.getException()!=null)
{
// System.out.println("Initing of "+getComponentIdentifier()+" failed due to "+fut.getException());
// Init failed: remove failed feature.
ifeatures.remove(ifeatures.size()-1);
}
else
{
// System.out.println("Initing of "+getComponentIdentifier()+" done.");
// Init succeeded: list no longer needed.
ifeatures = null;
}
return fut;
}
}
/**
* Execute feature bodies in parallel.
*/
protected IFuture executeBodyOnFeatures(final Iterator features)
{
List> undones = new ArrayList>();
IFuture fut = IFuture.DONE;
while(fut.getException()==null && features.hasNext())
{
IComponentFeature cf = features.next();
// if(getComponentIdentifier().getName().indexOf("Interceptor")!=-1)
// System.out.println("Starting "+cf+" of "+getComponentIdentifier());
fut = cf.body();
if(!fut.isDone())
{
undones.add(fut);
}
}
if(fut.getException()==null && !undones.isEmpty())
{
final Future ret = new Future();
IResultListener crl = new CounterResultListener(undones.size(), new DelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
Boolean keepalive = getModel().getKeepalive(getConfiguration());
if(keepalive!=null && !keepalive.booleanValue())
{
killComponent();
}
ret.setResult(null);
}
});
for(IFuture undone: undones)
{
undone.addResultListener(crl);
}
return ret;
}
else
{
if(fut.getException()==null)
{
Boolean keepalive = getModel().getKeepalive(getConfiguration());
if(keepalive!=null && !keepalive.booleanValue())
{
killComponent();
}
}
return fut;
}
}
/**
* Recursively shutdown the features in inverse order.
*/
protected IFuture executeShutdownOnFeatures(final List features, int cnt)
{
IFuture fut = IFuture.DONE;
while(fut.isDone() && cnt ret = new Future();
fut.addResultListener(new IResultListener()
{
public void resultAvailable(Void result)
{
executeShutdownOnFeatures(features, fcnt+1).addResultListener(new DelegationResultListener(ret));
}
public void exceptionOccurred(Exception exception)
{
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
getLogger().warning("Exception during component cleanup of "+getComponentIdentifier()+": "+exception);
getLogger().info(sw.toString());
executeShutdownOnFeatures(features, fcnt+1).addResultListener(new DelegationResultListener(ret));
}
});
return ret;
}
else
{
return IFuture.DONE;
}
}
/**
* Get the user view of this platform component.
*
* @return An internal access exposing user operations of the component.
*/
public IInternalAccess getInternalAccess()
{
return this;
}
/**
* Get the exception, if any.
* @return The failure reason for use during cleanup, if any.
*/
public Exception getException()
{
return exception;
}
// /**
// * Get the shared platform data.
// *
// * @return The objects shared by all components of the same platform (registry etc.). See starter for available data.
// */
// public Map getPlatformData()
// {
// return platformdata;
// }
//-------- IInternalAccess interface --------
/**
* Get the model of the component.
* @return The model.
*/
public IModelInfo getModel()
{
return info.getModel();
}
/**
* Get the start configuration or the default configuration if any.
* @return The configuration.
*/
public String getConfiguration()
{
String ret = info.getConfiguration();
// if(ret==null)
// {
// ConfigurationInfo[] configs = getModel().getConfigurations();
// if(configs.length>0)
// {
// ret = configs[0].getName();
// }
// }
return ret;
}
/**
* Get the id of the component.
* @return The component id.
*/
public IComponentIdentifier getComponentIdentifier()
{
return info.getComponentDescription().getName();
}
/**
* Get the component description.
* @return The component description.
*/
// Todo: hack??? should be internal to CMS!?
public IComponentDescription getComponentDescription()
{
return info.getComponentDescription();
}
/**
* Get a feature of the component.
* @param feature The type of the feature.
* @return The feature instance.
*/
public T getComponentFeature(Class extends T> type)
{
if(!features.containsKey(type))
{
throw new FeatureNotAvailableException("No such feature: "+type);
}
else
{
return type.cast(features.get(type));
}
}
/**
* Get a feature of the component.
* @param feature The type of the feature.
* @return The feature instance.
*/
public T getComponentFeature0(Class extends T> type)
{
return type.cast(features.get(type));
}
/**
* Kill the component.
*/
public IFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy