jadex.component.ComponentComponentFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-kernel-component Show documentation
Show all versions of jadex-kernel-component Show documentation
The Jadex component kernel provides basic support for active components. A component may be composed of subcomponents.
package jadex.component;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import jadex.bridge.ComponentIdentifier;
import jadex.bridge.IInternalAccess;
import jadex.bridge.IResourceIdentifier;
import jadex.bridge.component.IComponentFeatureFactory;
import jadex.bridge.modelinfo.IModelInfo;
import jadex.bridge.service.BasicService;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.ServiceQuery;
import jadex.bridge.service.types.cms.IBootstrapFactory;
import jadex.bridge.service.types.factory.IComponentFactory;
import jadex.bridge.service.types.factory.SComponentFactory;
import jadex.bridge.service.types.library.ILibraryService;
import jadex.bridge.service.types.library.ILibraryServiceListener;
import jadex.commons.LazyResource;
import jadex.commons.SUtil;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
/**
* Factory for default contexts.
* No special properties supported, yet.
*/
public class ComponentComponentFactory extends BasicService implements IComponentFactory, IBootstrapFactory
{
//-------- constants --------
/** The supported component types (file extensions).
* Convention used by platform config panel. */
public static final String[] FILETYPES = new String[]{ComponentModelLoader.FILE_EXTENSION_COMPONENT};
/** The component component file type. */
public static final String FILETYPE_COMPONENT = "Component";
// /** The application file extension. */
// public static final String FILE_EXTENSION_APPLICATION = ".application.xml";
/** The image icon. */
protected static final LazyResource ICON = new LazyResource(ComponentComponentFactory.class, "/jadex/component/images/component.png");
//-------- attributes --------
/** The application model loader. */
protected ComponentModelLoader loader;
/** The provider. */
protected IInternalAccess provider;
// /** The library service. */
// protected ILibraryService libservice;
/** The library service listener */
protected ILibraryServiceListener libservicelistener;
/** The standard + XML component features. */
protected Collection features;
//-------- constructors --------
/**
* Create a new application factory for startup.
* @param platform The platform.
* @param mappings The XML reader mappings of supported spaces (if any).
*/
// This constructor is used by the Starter class and the ADFChecker plugin.
public ComponentComponentFactory(String providerid)
{
super(new ComponentIdentifier(providerid), IComponentFactory.class, null);
this.loader = new ComponentModelLoader(null);
this.features = SComponentFactory.DEFAULT_FEATURES;
}
/**
* Create a new application factory.
* @param platform The platform.
* @param mappings The XML reader mappings of supported spaces (if any).
*/
public ComponentComponentFactory(IInternalAccess provider)
{
super(provider.getId(), IComponentFactory.class, null);
this.provider = provider;
this.features = SComponentFactory.DEFAULT_FEATURES;
}
/**
* Start the service.
*/
public IFuture startService(IInternalAccess component, IResourceIdentifier rid)
{
this.provider = component;
this.providerid = provider.getId();
setServiceIdentifier(createServiceIdentifier(provider, "BootstrapFactory", IComponentFactory.class, IComponentFactory.class, rid, null));
return startService();
}
/**
* Start the service.
*/
public IFuture startService()
{
final Future ret = new Future();
super.startService().addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
loader = new ComponentModelLoader(null);
libservicelistener = new ILibraryServiceListener()
{
public IFuture resourceIdentifierRemoved(IResourceIdentifier parid, IResourceIdentifier rid)
{
loader.clearModelCache();
return IFuture.DONE;
}
public IFuture resourceIdentifierAdded(IResourceIdentifier parid, IResourceIdentifier rid, boolean rem)
{
loader.clearModelCache();
return IFuture.DONE;
}
};
ILibraryService ls = getLibraryService();
if(ls!=null)
{
// System.out.println("library listener on: "+this);
ls.addLibraryServiceListener(libservicelistener);
}
// else
// {
// System.out.println("no library listener on: "+this);
// }
ret.setResult(null);
}
});
return ret;
}
/**
* Shutdown the service.
* @param listener The listener.
*/
public synchronized IFuture shutdownService()
{
final Future ret = new Future();
getLibraryService().removeLibraryServiceListener(libservicelistener)
.addResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(Void result)
{
ComponentComponentFactory.super.shutdownService()
.addResultListener(new DelegationResultListener(ret));
}
});
return ret;
}
//-------- IComponentFactory interface --------
/**
* Load a model.
* @param model The model (e.g. file name).
* @param The imports (if any).
* @return The loaded model.
*/
public IFuture loadModel(final String model, Object pojo, final String[] imports, final IResourceIdentifier rid)
{
final Future ret = new Future();
if(getLibraryService()!=null)
{
getLibraryService().getClassLoader(rid).addResultListener(
new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(ClassLoader cl)
{
try
{
ret.setResult(loader.loadComponentModel(model, pojo, imports, cl,
new Object[]{rid, getServiceId().getProviderId().getRoot()}).getModelInfo());
}
catch(Exception e)
{
ret.setException(e);
}
}
});
}
else
{
try
{
ClassLoader cl = getClass().getClassLoader();
ret.setResult(loader.loadComponentModel(model, pojo, imports, cl,
new Object[]{rid, getProviderId().getRoot()}).getModelInfo());
}
catch(Exception e)
{
ret.setException(e);
}
}
return ret;
// System.out.println("filename: "+filename);
// try
// {
// ret.setResult(loader.loadComponentModel(model, imports,
// libservice==null? getClass().getClassLoader(): libservice.getClassLoader(rid), rid).getModelInfo());
// }
// catch(Exception e)
// {
// ret.setException(e);
// }
//
// return ret;
}
/**
* Test if a model can be loaded by the factory.
* @param model The model (e.g. file name).
* @param The imports (if any).
* @return True, if model can be loaded.
*/
public IFuture isLoadable(String model, Object pojo, String[] imports, IResourceIdentifier rid)
{
return model.endsWith(ComponentModelLoader.FILE_EXTENSION_COMPONENT) ? IFuture.TRUE : IFuture.FALSE;
}
/**
* Test if a model is startable (e.g. an component).
* @param model The model (e.g. file name).
* @param The imports (if any).
* @return True, if startable (and loadable).
*/
public IFuture isStartable(String model, Object pojo, String[] imports, IResourceIdentifier rid)
{
return model.endsWith(ComponentModelLoader.FILE_EXTENSION_COMPONENT) ? IFuture.TRUE : IFuture.FALSE;
}
/**
* Get the names of ADF file types supported by this factory.
*/
public String[] getComponentTypes()
{
return new String[]{FILETYPE_COMPONENT};
}
public String[] getComponentAnnotationTypes()
{
return SUtil.EMPTY_STRING_ARRAY;
}
/**
* Get a default icon for a file type.
*/
public IFuture getComponentTypeIcon(String type)
{
Future ret = new Future();
if(type.equals(FILETYPE_COMPONENT))
{
try
{
ret.setResult(ICON.getData());
}
catch(IOException e)
{
ret.setException(e);
}
}
else
{
ret.setResult(null);
}
return ret;
}
/**
* Get the component type of a model.
* @param model The model (e.g. file name).
* @param The imports (if any).
*/
public IFuture getComponentType(String model, String[] imports, IResourceIdentifier rid)
{
return new Future(model.toLowerCase().endsWith(ComponentModelLoader.FILE_EXTENSION_COMPONENT)? FILETYPE_COMPONENT: null);
}
/**
* Get the properties.
* Arbitrary properties that can e.g. be used to
* define kernel-specific settings to configure tools.
* @param type The component type.
* @return The properties or null, if the component type is not supported by this factory.
*/
public Map getProperties(String type)
{
return FILETYPE_COMPONENT.equals(type)
? Collections.EMPTY_MAP : null;
}
/**
* Get the component features for a model.
* @param model The component model.
* @return The component features.
*/
public IFuture> getComponentFeatures(IModelInfo model, Object pojo)
{
return new Future>(features);
}
/**
* Get the library service
*/
protected ILibraryService getLibraryService()
{
return internalaccess==null? null: internalaccess.getFeature(IRequiredServicesFeature.class).getLocalService(new ServiceQuery<>(ILibraryService.class).setMultiplicity(0));
}
}