Class Feature
- java.lang.Object
-
- ej.kf.Module
-
- ej.kf.Feature
-
public class Feature extends Module
A Feature represents an optional part of an application that adds new features and services.
A Feature only depends on Kernel code. A Feature is considered as unreliable code from Kernel point of view.
Instances of this class are owned by the Kernel.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFeature.StateA Feature state.
-
Field Summary
Fields Modifier and Type Field Description static intMAX_CRITICALITYThe maximum criticality that a feature can have.static intMIN_CRITICALITYThe minimum criticality that a feature can have.static intNORM_CRITICALITYThe default criticality that is assigned to a feature.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Thread[]getAllAliveThreads()Gets a snapshot of all alive threads owned by this Feature (some threads included in the returned array may have been terminated when this method returns, some new threads may have been created when this method returns).intgetCriticality()Returns the criticality of this feature.longgetMemoryLimit()Gets the maximum amount of memory heap that can be allocated by this Feature.InputStreamgetResourceAsStream(String name)Finds in this feature the resource with the given name.Feature.StategetState()Returns the current Feature state.voidsetCriticality(int criticality)Sets the criticality of this feature.booleansetMemoryLimit(long size)Sets the maximum amount of memory heap that can be allocated by this Feature.voidstart()Causes this Feature to start.voidstop()Causes this Feature to stop.-
Methods inherited from class ej.kf.Module
getAllocatedMemory, getExecutionCounter, getExecutionQuota, getName, getProvider, getUID, getVersion, setExecutionQuota
-
-
-
-
Field Detail
-
MIN_CRITICALITY
public static final int MIN_CRITICALITY
The minimum criticality that a feature can have.- See Also:
getCriticality(), Constant Field Values
-
NORM_CRITICALITY
public static final int NORM_CRITICALITY
The default criticality that is assigned to a feature.- See Also:
getCriticality(), Constant Field Values
-
MAX_CRITICALITY
public static final int MAX_CRITICALITY
The maximum criticality that a feature can have.- See Also:
getCriticality(), Constant Field Values
-
-
Method Detail
-
getAllAliveThreads
public Thread[] getAllAliveThreads()
Gets a snapshot of all alive threads owned by this Feature (some threads included in the returned array may have been terminated when this method returns, some new threads may have been created when this method returns).- Returns:
- the threads owned by this Feature
- Throws:
IllegalStateException- if the Feature is not in theFeature.State.STARTEDstate
-
getState
public Feature.State getState()
Returns the current Feature state.- Returns:
- this Feature's state.
- See Also:
Feature.State
-
start
public void start()
Causes this Feature to start. A new thread owned by the Feature is created and started. The Feature is switched in theFeature.State.STARTEDstate and this method returns. Next steps are executed asynchronously within the new thread context:- Feature clinits are executed
- Entry point is instantiated
FeatureEntryPoint.start()is called
- Throws:
IllegalStateException- if the Feature state is not inFeature.State.INSTALLEDstate
-
stop
public void stop()
Causes this Feature to stop. The following steps are executed:- A new thread owned by the Feature is created and
FeatureEntryPoint.stop()is executed. - Wait until this thread is normally terminated or timeout occurred.
ej.lang.Resource.reclaim()is called for each resource that remains open by the Feature.- A
DeadFeatureExceptionis thrown in threads that are running Feature code or in threads that want to call Feature code. - Wait until all threads owned by this Feature are terminated.
- Feature state is set to
Feature.State.STOPPED. - Objects owned by the Feature are reclaimed. If there are no remaining alive objects, the Feature state is set
to
Feature.State.INSTALLED.
When the new Feature state is
Feature.State.INSTALLED, the Feature runtime has been fully reclaimed (threads and objects). Otherwise, the new Feature state isFeature.State.STOPPEDand there are some remaining Feature objects references from Kernel.This method can be called multiple times by the Kernel to reclaim objects again and thus to try to switch the Feature in the
Feature.State.INSTALLEDstate.When Feature state is set to
Feature.State.STOPPED, Kernel application shall remove all its references to objects owned by this Feature, through the calls ofFeatureStateListener.stateChanged(ej.kf.Feature, ej.kf.Feature.State).- Throws:
IllegalStateException- if the Feature is in theFeature.State.INSTALLEDorFeature.State.UNINSTALLEDstate
- A new thread owned by the Feature is created and
-
getCriticality
public int getCriticality()
Returns the criticality of this feature.The criticality is used by the system to determine which feature to kill when there is memory pressure. The features with the lowest criticality will be killed first. The features with a criticality set to
MAX_CRITICALITYare not killed automatically by the system when there is memory pressure.The default criticality of a feature is
NORM_CRITICALITY.- Returns:
- the criticality.
- Throws:
IllegalStateException- if this feature is inFeature.State.UNINSTALLEDstate.
-
setCriticality
public void setCriticality(int criticality)
Sets the criticality of this feature.- Parameters:
criticality- the new criticality of this feature.- Throws:
IllegalArgumentException- If the criticality is not in the rangeMIN_CRITICALITYtoMAX_CRITICALITY.IllegalStateException- if this feature is inFeature.State.UNINSTALLEDstate.- See Also:
getCriticality()
-
getResourceAsStream
@Nullable public InputStream getResourceAsStream(String name)
Finds in this feature the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed from code.The name of a resource is a '/'-separated path name that identifies the resource. This path must be absolute (i.e., begins with a '
/'); if not, this method returnsnull.This method will search this feature for the resource. It will not search the Kernel for the resource.
The returned input stream is automatically closed when this feature is uninstalled.
- Parameters:
name- The resource name- Returns:
- An input stream for reading the resource, or
nullif the resource could not be found. - Throws:
IllegalStateException- if this feature is inFeature.State.UNINSTALLEDstate.
-
setMemoryLimit
public boolean setMemoryLimit(long size)
Sets the maximum amount of memory heap that can be allocated by this Feature.
This method may trigger the garbage collector. The maximum amount of memory heap is not updated if the given limit is lower thanModule.getAllocatedMemory().
AnOutOfMemoryErroris thrown during an allocation made by a Feature if its memory usage would exceed this limit.- Parameters:
size- the maximum amount of memory heap that can be allocated by this Feature (in bytes)- Returns:
trueif the maximum amount of memory heap has been updated,falseotherwise.- Throws:
IllegalArgumentException- if the requested size is negative or greater than the memory heap (Runtime.maxMemory())IllegalStateException- if this feature is inFeature.State.UNINSTALLEDstate.- Since:
- 1.7
- See Also:
getMemoryLimit(),Module.getAllocatedMemory()
-
getMemoryLimit
public long getMemoryLimit()
Gets the maximum amount of memory heap that can be allocated by this Feature. By default, the maximum amount of memory heap is equal toRuntime.maxMemory()(disabled).- Returns:
- the maximum amount of memory heap that can be allocated by this Feature (in bytes)
- Throws:
IllegalStateException- if this feature is inFeature.State.UNINSTALLEDstate.- Since:
- 1.7
- See Also:
setMemoryLimit(long)
-
-