public abstract class Resource
extends java.lang.Object
Resources model named, memory-managed objects that may have dependencies onto other resources and may have a non-trivial life cycle. For example all component factories are required to provide extensions of this class.
The resource management system is the underpinning of z2's on-demand behavior. The typical life cycle of a resource implementation instance looks as follows
as(Class)
calls may occur. In order to provide the required result or to implement some side-effect, a
resource implementation may build up state and use other resources:
handle().addDependency(<handle of required resource>)See also
IResourceHandle.addDependency(IResourceHandle)
.
handle().attach(Object)if it is desired that the resource instance be kept as long as the returned objects (see
IResourceHandle.attach(Object)
).
synchronized
as invalidations may happen at any timehandle().adjust(long, long, short)See also
IResourceHandle.adjust(long, long, short)
.
Reasons for this vary. For example Web Applications require explicit "stop" calls, so that a resource representing a Web application may not be silently collected.
IResourceHandle.adjust(long, long, short)
), the resource may be collected once no other reference holds on to a resource handle for it.
During invalidation, a resource should clean up any state built up since provisioning and return to a freshly provisioned status.
Constructor | Description |
---|---|
Resource() |
Modifier and Type | Method | Description |
---|---|---|
<T> T |
as(TypeRef<T> typeRef) |
Retrieve a resource representation adhering to the passed-on type reference.
|
<T> T |
as(java.lang.Class<T> clz) |
Retrieve a typed representation of the resource.
|
IResourceHandle |
handle() |
Return the handle for this resource.
|
void |
init() |
Called at initialization time with the resource management.
|
void |
init(IResourceHandle c) |
|
void |
invalidate() |
This method gets called whenever a dependency resource has been invalidated or this resource needs
to be invalidated.
|
public final void init(IResourceHandle c)
public void init()
public final IResourceHandle handle()
public void invalidate() throws ResourceBusyException
This code should be executed in a life cycle code block of this resource instance, i.e. where dependencies are effectively managed, so that race conditions can be avoided.
State changing methods of a resource should always be synchronized (e.g. on this). This is
in particular true for cross-resource dependencies. In order to assure consistency under race conditions,
a dependent resource should first declare its dependency and then retrieve the resource implementation.
In case of failures, resources should clean up by calling handle().invalidate(true);
ResourceBusyException
public <T> T as(java.lang.Class<T> clz)
clz
- expected return typenull
if the type facade is not supported.ResourceTypeNotAvailableException
- if the type is supported but cannot be made
available due to an error situationpublic <T> T as(TypeRef<T> typeRef)
as(Class)
whenever possible.
In order to check for a TypeRef
to match something expected, compare with a local type reference. E.g.
private TypeRef> ref = new TypeRef<>(){};
public T as(TypeRef typeRef) {
if (ref.equals(typeRef)) {
return typeRef.cast((Supplier) ()->"Hello World");
}
return super.as(typeRef);
}
typeRef
- the expected return type specified via TypeRef
(that also supports parameterized types)