apache > ws.apache

## 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");
  > `

  Of course, you will not want to hardcode deployment data into your Java code, so the ResourceManager provides a lookup system for resource context paths:

  > `ResourceManager manager = getEnvironment().getResourceManager();
  > String contextPath = manager.getResourceContextPath(DatabaseServer.class);
  > Resource resource = manager.createResource(contextPath);
  > `

  In the example above, we're asking the manager for the context path of a resource type that implements the given capability (a custom DatabaseServer capability). Note that we're using a capability interface - not an implementation class - as our query object. Since Muse encourages the reuse of capability interfaces, this is a robust way to get the context path without creating a dependency on implementation details (Java or XML). If you know the differentiating capabilities that make up your resource type, you can find its path and instantiate it via createResource().
- Initialize the resource object - Once constructed, you need to invoke the initialize() method:

  > `resource.initialize();`

  This will tell the resource to create and initialize all of its capabilities.
- 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);
  > `

  Once added, your resource will continue to service requests until you call Resource.shutdown(), which will remove the resource from the manager.