Class Kernel
- java.lang.Object
-
- ej.kf.Module
-
- ej.kf.Kernel
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidaddConverter(Converter<?> converter)Adds theConverterto the list of converters.static voidaddFeatureStateListener(FeatureStateListener listener)Adds the givenFeatureStateListenerto the list of listeners that are notified when the state of a Feature has changed.static voidaddResourceControlListener(ResourceControlListener listener)Adds the givenResourceControlListenerto the list of listeners that are notified when a Feature is stopped by the Resource Control Manager.static booleanareEquivalentSharedInterfaces(Class<?> si1, Class<?> si2)Tells whether the given classes are equivalent shared interfaces.static <T> Tbind(T o, Class<T> targetType, Feature targetOwner)Binds anObjectowned by a Feature to another Feature.static booleancanUninstall(Feature f)Tells whether the given Feature can be uninstalled.static <T> Tclone(T from, Module toOwner)Creates and returns a copy of the given object, so that the newly created object is owned by the givenModule.static voidenter()Enters in Kernel mode: the current thread context is switched to be owned by the Kernel.static voidexit()Exits from Kernel mode: the current thread context is restored to the owner of the caller of the method (which can remain the Kernel).static FeatureStateListener[]getAllFeatureStateListeners()Returns an array containing all theFeatureStateListenerthat are notified when the state of a Feature has changed.static Feature[]getAllLoadedFeatures()Returns the set of Features currently loaded.static ModulegetContextOwner()Returns the owner of the current thread context.static Class<?>getEquivalentSharedInterface(Class<?> si, Feature target)Gets the equivalent shared interface in the given target Feature.static Class<?>getImplementedSharedInterface(Class<?> fromClass, Class<?> topInterface)Gets the first shared interface implemented by the given class under the hierarchy oftopInterface.static KernelgetInstance()Returns the singleton instance representing the Kernel.static ModulegetOwner(Object o)Returns the owner of the givenObject.longgetReservedMemory()Gets the minimum amount of memory heap reserved for the Kernel.static Class<?>getSharedInterface(Class<?> si, Class<?> topInterface, Feature target)From a shared interface, gets the closest shared interface in the given target Feature.static Featureinstall(InputStream is)Installs a Feature from anInputStream.static booleanisAPI(Class<?> c)Tells whether the given class is a Kernel API.static booleanisInKernelMode()Tells whether the current thread context is currently in Kernel mode.static booleanisSharedInterface(Class<?> c)Tells whether the given class is a shared interface (i.e.static Featureload(InputStream is)Installs a Feature from anInputStreamand starts it.static <T> Proxy<T>newProxy(T ref, Module owner)Allocates a newProxyand sets its reference to the given object.static voidremoveConverter(Converter<?> converter)Removes theConverterto the list of converters.static voidremoveFeatureStateListener(FeatureStateListener listener)Removes theFeatureStateListenerto the list of listeners that are notified when the state of a Feature has changed.static voidremoveResourceControlListener(ResourceControlListener listener)Removes theResourceControlListenerto the list of listeners that are notified when a Feature is stopped by the Resource Control Manager.static voidrunUnderContext(Module contextOwner, Runnable runnable)Calls theRunnable.run()method with current context set to the givenModule.booleansetReservedMemory(long size)Sets the minimum amount of memory heap reserved for the Kernel.static voiduninstall(Feature f)Uninstalls a Feature.static booleanunload(Feature f)Stops a Feature and uninstalls it if its state isFeature.State.INSTALLEDafterFeature.stop().-
Methods inherited from class ej.kf.Module
getAllocatedMemory, getExecutionCounter, getExecutionQuota, getName, getProvider, getUID, getVersion, setExecutionQuota
-
-
-
-
Method Detail
-
getInstance
public static Kernel getInstance()
Returns the singleton instance representing the Kernel.- Returns:
- the singleton instance representing the Kernel
-
load
public static Feature load(InputStream is) throws IOException, InvalidFormatException, IncompatibleFeatureException, AlreadyLoadedFeatureException
Installs a Feature from anInputStreamand starts it.- Parameters:
is- the input stream from where the Feature data is loaded.- Returns:
- the loaded Feature, in the
Feature.State.STARTEDstate. - Throws:
IOException- if something occurs when readingInputStreamAlreadyLoadedFeatureException- if Feature is already loadedIncompatibleFeatureException- if Feature is not compatible with the current KernelInvalidFormatException- if Feature content is invalid- See Also:
install(InputStream),Feature.start()
-
install
public static Feature install(InputStream is) throws IOException, InvalidFormatException, IncompatibleFeatureException, AlreadyLoadedFeatureException
Installs a Feature from anInputStream. Feature shall have been generated against the current Kernel classes. Feature data is read and linked to the Kernel. The Feature is added to the list of loaded features and its state is set toFeature.State.INSTALLED. The given input stream is let open.- Parameters:
is- the input stream from where the Feature data is loaded.- Returns:
- the loaded Feature, in the
Feature.State.INSTALLEDstate. - Throws:
IOException- if something occurs when readingInputStreamAlreadyLoadedFeatureException- if Feature is already loadedIncompatibleFeatureException- if Feature is not compatible with the current KernelInvalidFormatException- if Feature content is invalid- See Also:
getAllLoadedFeatures()
-
uninstall
public static void uninstall(Feature f)
Uninstalls a Feature. When this method returns, the Feature code has been unlinked from the Kernel and reclaimed. The Feature is removed from the list of loaded features and its state is set toFeature.State.UNINSTALLED.- Parameters:
f- the feature to be uninstalled.- Throws:
IllegalStateException- if Feature state is notFeature.State.INSTALLED- See Also:
getAllLoadedFeatures()
-
canUninstall
public static boolean canUninstall(Feature f)
Tells whether the given Feature can be uninstalled.- Parameters:
f- the Feature to test- Returns:
trueif the Feature can be uninstalled,falseotherwise.- Since:
- 1.6
-
unload
public static boolean unload(Feature f) throws UnknownFeatureException
Stops a Feature and uninstalls it if its state isFeature.State.INSTALLEDafterFeature.stop().- Parameters:
f- the feature to be unloaded.- Returns:
trueif Feature state isFeature.State.UNINSTALLED,falseotherwise.- Throws:
UnknownFeatureException- if the given Feature is unknown.IllegalStateException- if Feature state isFeature.State.UNINSTALLED- See Also:
uninstall(Feature),Feature.stop()
-
getAllLoadedFeatures
public static Feature[] getAllLoadedFeatures()
Returns the set of Features currently loaded.- Returns:
- all Features that are not in the state
Feature.State.UNINSTALLED.
-
enter
public static void enter()
Enters in Kernel mode: the current thread context is switched to be owned by the Kernel. If the current context was already in Kernel mode, this method does nothing.The context owner is automatically restored when returning from the method (equivalent to calling
exit()before returning).- See Also:
exit()
-
exit
public static void exit()
Exits from Kernel mode: the current thread context is restored to the owner of the caller of the method (which can remain the Kernel). If the restored context is owned by a Feature, all the locals that refer to an object owned by another Feature are reset tonull.- See Also:
enter()
-
isInKernelMode
public static boolean isInKernelMode()
Tells whether the current thread context is currently in Kernel mode.- Returns:
- the result of
Kernel.getContextOwner() == Kernel.getInstance()
-
getOwner
public static Module getOwner(Object o)
Returns the owner of the givenObject.- Parameters:
o- the object.- Returns:
- the owner of the object.
-
getContextOwner
public static Module getContextOwner()
Returns the owner of the current thread context.- Returns:
- the context owner.
-
addFeatureStateListener
public static void addFeatureStateListener(FeatureStateListener listener)
Adds the givenFeatureStateListenerto the list of listeners that are notified when the state of a Feature has changed.- Parameters:
listener- the new listener to add- Throws:
NullPointerException- if listener isnull
-
addResourceControlListener
public static void addResourceControlListener(ResourceControlListener listener)
Adds the givenResourceControlListenerto the list of listeners that are notified when a Feature is stopped by the Resource Control Manager.- Parameters:
listener- the new listener to add- Throws:
NullPointerException- if listener isnull
-
removeFeatureStateListener
public static void removeFeatureStateListener(FeatureStateListener listener)
Removes theFeatureStateListenerto the list of listeners that are notified when the state of a Feature has changed.Does nothing if the listener is not registered or
null.- Parameters:
listener- the listener to be removed
-
removeResourceControlListener
public static void removeResourceControlListener(ResourceControlListener listener)
Removes theResourceControlListenerto the list of listeners that are notified when a Feature is stopped by the Resource Control Manager.Does nothing if the listener is not registered or
null.- Parameters:
listener- the listener to be removed
-
getAllFeatureStateListeners
public static FeatureStateListener[] getAllFeatureStateListeners()
Returns an array containing all theFeatureStateListenerthat are notified when the state of a Feature has changed.- Returns:
- an array of
FeatureStateListener[]with all the listeners
-
runUnderContext
public static void runUnderContext(Module contextOwner, Runnable runnable)
Calls theRunnable.run()method with current context set to the givenModule.- Parameters:
contextOwner- the context owner that will execute the methodrunnable- theRunnableinstance to run under the givenFeaturecontext.- Throws:
IllegalAccessError- if theRunnableinstance is not accessible to the context owner, or if theRunnableis owned by a Feature and must run in Kernel context.IllegalStateException- ifcontextOwneris a Feature and not in theFeature.State.STARTEDstate.
-
clone
public static <T> T clone(T from, Module toOwner) throws CloneNotSupportedExceptionCreates and returns a copy of the given object, so that the newly created object is owned by the givenModule. The source object class must beStringor must implementCloneable. Otherwise, aCloneNotSupportedExceptionis thrown. If the source object owner and the target owner are the same, this method is equivalent toObject.clone()method applied on the source object. Otherwise, the object can be cloned if the source object class is owned by the Kernel and all its object references are accessible to the new owner. In all other cases, anIllegalAccessErroris thrown.- Type Parameters:
T- the Kernel type of the object to clone- Parameters:
from- the object to clonetoOwner- the owner of the cloned object- Returns:
- the cloned object
- Throws:
CloneNotSupportedException- if the source object cannot be clonedIllegalAccessError- if the creation of the new object would break access rulesIllegalStateException- iftoOwneris a Feature and not in theFeature.State.STARTEDstate.NullPointerException- if one of the arguments isnull
-
addConverter
public static void addConverter(Converter<?> converter)
Adds theConverterto the list of converters. Registered converters are used bybind(Object, Class, Feature).- Parameters:
converter- the new converter to add- Throws:
NullPointerException- if converter isnullIllegalArgumentException- if a converter managing the same type is already registered- See Also:
Converter.getType()
-
removeConverter
public static void removeConverter(Converter<?> converter)
Removes theConverterto the list of converters.Does nothing if the converter is not registered or
null.- Parameters:
converter- the converter to be removed
-
bind
@Nullable public static <T> T bind(@Nullable T o, Class<T> targetType, Feature targetOwner)
Binds an
Objectowned by a Feature to another Feature.When the target type is owned by the Kernel, the object is converted using the most accurate registered converter.
When the target type is owned by the Feature, it must be a shared interface. In this case, a
Proxyinstance is returned.Objectidentity is preserved across Features: calling multiple times this method with the same parameters returns the same object.- Type Parameters:
T- the Kernel type of the object to bind- Parameters:
o- the object to be convertedtargetType- the type of the converted objecttargetOwner- the owner of the converted object- Returns:
- an object owned by the target owner, or
nullif o isnullor is aProxythat refers to a dead object - Throws:
IllegalAccessError- if the given object cannot be bounded to the given typeIllegalArgumentException- if the given type is not a shared interfaceIllegalStateException- iftargetOwneris not in theFeature.State.STARTEDstate.
-
isSharedInterface
public static boolean isSharedInterface(Class<?> c)
Tells whether the given class is a shared interface (i.e. an interface owned by a Feature and defined as shared).- Parameters:
c- the class to test- Returns:
trueif the class is a shared interface,falseotherwise
-
newProxy
public static <T> Proxy<T> newProxy(T ref, Module owner)
Allocates a newProxyand sets its reference to the given object.- Type Parameters:
T- the reference type- Parameters:
ref- theProxyreferenceowner- the owner of theProxyinstance- Returns:
- the new
Proxyinstance initialized with the given reference - Throws:
IllegalStateException- ifowneris a Feature and not in theFeature.State.STARTEDstate.
-
isAPI
public static boolean isAPI(Class<?> c)
Tells whether the given class is a Kernel API.- Parameters:
c- the class to test- Returns:
trueif the class is a Kernel API,falseotherwise
-
getSharedInterface
@Nullable public static Class<?> getSharedInterface(Class<?> si, Class<?> topInterface, Feature target)
From a shared interface, gets the closest shared interface in the given target Feature.
The closest shared interface is computed by returning the first equivalent shared interface in the target Feature, starting from the interface
sito the interfacetopInterface(included).If the owner of the given shared interface is the target, the same shared interface is returned.
- Parameters:
si- a shared interface that extendstopInterfaceinterfacetopInterface- a shared interface or an interface owned by the Kernel, which is assignable fromsi.target- the target Feature where to find the closest equivalent shared interface ofsi- Returns:
- the closest shared interface as described,
nullif not found. - Throws:
IllegalArgumentException- ifsiis not a shared interface or iftopInterfaceis not an interface or iftopInterfaceis not assignable fromfromClass- See Also:
getEquivalentSharedInterface(Class, Feature)
-
getEquivalentSharedInterface
@Nullable public static Class<?> getEquivalentSharedInterface(Class<?> si, Feature target)
Gets the equivalent shared interface in the given target Feature.
The equivalent shared interface is the interface owned by the target Feature such as
areEquivalentSharedInterfaces(Class, Class)istrue.- Parameters:
si- a shared interfacetarget- the target Feature where to find the equivalent shared interface ofsi- Returns:
- the equivalent shared interface,
nullif not found - Throws:
IllegalArgumentException- ifsiis not a shared interface
-
areEquivalentSharedInterfaces
public static boolean areEquivalentSharedInterfaces(Class<?> si1, Class<?> si2)
Tells whether the given classes are equivalent shared interfaces.
Two classes are equivalent shared interfaces if they are shared interfaces and have the same fully qualified name.
- Parameters:
si1- a class to testsi2- a class to test- Returns:
trueif the given classes are equivalent shared interfaces,falseotherwise
-
getImplementedSharedInterface
@Nullable public static Class<?> getImplementedSharedInterface(Class<?> fromClass, Class<?> topInterface)
Gets the first shared interface implemented by the given class under the hierarchy of
topInterface.If
fromClassis a shared interface it is directly returned.- Parameters:
fromClass- a class or an interface owned by aFeaturethat implementstopInterfacetopInterface- an interface implemented byfromClass- Returns:
- the shared interface type as described or
nullif no shared interface found - Throws:
IllegalArgumentException- if the given class is an array or is owned by the Kernel or iftopInterfaceis not an interface or iftopInterfaceis not assignable fromfromClass
-
setReservedMemory
public boolean setReservedMemory(long size)
Sets the minimum amount of memory heap reserved for the Kernel.
This method may trigger the garbage collector. The minimum amount of memory heap reserved for the Kernel is not updated if the given value is greater thanModule.getAllocatedMemory()+Runtime.freeMemory().
AnOutOfMemoryErroris thrown during an allocation made by a Feature if the memory available to the Kernel is lower than this limit. The memory available for the Kernel is equal toModule.getAllocatedMemory()+Runtime.freeMemory().- Parameters:
size- the minimum amount of memory heap reserved for the Kernel (in bytes)- Returns:
trueif minimum amount of memory heap reserved for the Kernel has been updated,falseotherwise.- Throws:
IllegalArgumentException- if the requested size is negative or greater than the memory heap (Runtime.maxMemory())- Since:
- 1.7
- See Also:
getReservedMemory()
-
getReservedMemory
public long getReservedMemory()
Gets the minimum amount of memory heap reserved for the Kernel. By default, the minimum amount of memory heap is equal to0(disabled).- Returns:
- the minimum amount of memory heap reserved for the Kernel (in bytes)
- Since:
- 1.7
- See Also:
setReservedMemory(long)
-
-