Apache Muse - Create a Resource (Programmatically)
You can create new instances of a resource type programmatically using the org.apache.muse.core.ResourceManager API. This section describes the steps you must take to instantitate, initialize, and expose the resource so it can start handling requests.
Note
It is also possible to create new resources at application startup by
specifying their EPRs at design time.
The deployment descriptor overview covers the relationship between a resource type and its context path - we will now see how the context path value can be used to create an instance of a resource without referencing its implementation class(es). There is more than one step required to create a resource:
- Create the resource object - This is done by passing the resource's context path
to ResourceManager.createResource(), like so:
ResourceManager manager = getEnvironment().getResourceManager();
Resource resource = manager.createResource("database-server");
ResourceManager manager = getEnvironment().getResourceManager();
String contextPath = manager.getResourceContextPath(DatabaseServer.class);
Resource resource = manager.createResource(contextPath);
- Initialize the resource object - Once constructed, you need to invoke
the initialize() method:
resource.initialize();
- Add the resource object - You resource is ready to handle requests, but
you must store it in the ResourceManager in order for SOAP requests to
find their way to it. If you do not take this final step, requests send to the
resource's EPR will be returned with a WS-A DestinationUnreachable fault. Also
note that the addResource() method used for this task takes an EPR as its
first parameter even though Resource has a getEndpointReference()
method; this seemingly redundant parameter provides you with the opportunity to
map multiple EPRs to the same resource object, something that may be very useful
if your environments has an extremely large number of resources.
EndpointReference epr = resource.getEndpointReference(); // alternative: create another unique EPR
manager.addResource(epr, resource);