29.4. Using a contract resolver
Overview
The most involved mechanism for resolving WSDL document locations at runtime is to implement your own custom contract resolver. This requires that you provide an implementation of the Apache CXF specific
ServiceContractResolver
interface. You also need to register your custom resolver with the bus.
Once properly registered, the custom contract resolver will be used to resolve the location of any required WSDL and schema documents.
Implementing the contract resolver
A contract resolver is an implementation of the
org.apache.cxf.endpoint.ServiceContractResolver
interface. As shown in Example 29.3, “ServiceContractResolver
Interface”, this interface has a single method, getContractLocation()
, that needs to be implemented. getContractLocation()
takes the QName of a service and returns the URI for the service's WSDL contract.
Example 29.3. ServiceContractResolver
Interface
public interface ServiceContractResolver { URI getContractLocation(QName qname); }
The logic used to resolve the WSDL contract's location is application specific. You can add logic that resolves contract locations from a UDDI registry, a database, a custom location on a file system, or any other mechanism you choose.
Registering the contract resolver programmatically
Before the Apache CXF runtime will use your contract resolver, you must register it with a contract resolver registry. Contract resolver registries implement the
org.apache.cxf.endpoint.ServiceContractResolverRegistry
interface. However, you do not need to implement your own registry. Apache CXF provides a default implementation in the org.apache.cxf.endpoint.ServiceContractResolverRegistryImpl
class.
To register a contract resolver with the default registry you do the following:
- Get a reference to the default bus object.
- Get the service contract registry from the bus using the bus'
getExtension()
method. - Create an instance of your contract resolver.
- Register your contract resolver with the registry using the registry's
register()
method.
Example 29.4, “Registering a Contract Resolver” shows the code for registering a contract resolver with the default registry.
Example 29.4. Registering a Contract Resolver
The code in Example 29.4, “Registering a Contract Resolver” does the following:
Registering a contract resolver using configuration
You can also implement a contract resolver so that it can be added to a client through configuration. The contract resolver is implemented in such a way that when the runtime reads the configuration and instantiates the resolver, the resolver registers itself. Because the runtime handles the initialization, you can decide at runtime if a client needs to use the contract resolver.
To implement a contract resolver so that it can be added to a client through configuration do the following:
- Add an
init()
method to your contract resolver implementation. - Add logic to your
init()
method that registers the contract resolver with the contract resolver registry as shown in Example 29.4, “Registering a Contract Resolver”. - Decorate the
init()
method with the@PostConstruct
annotation.
Example 29.5, “Service Contract Resolver that can be Registered Using Configuration” shows a contract resolver implementation that can be added to a client using configuration.
Example 29.5. Service Contract Resolver that can be Registered Using Configuration
import javax.annotation.PostConstruct; import javax.annotation.Resource; import javax.xml.namespace.QName; import org.apache.cxf.Bus; import org.apache.cxf.BusFactory; public class UddiResolver implements ServiceContractResolver { private Bus bus; ... @PostConstruct public void init() { BusFactory bf=BusFactory.newInstance(); Bus bus=bf.createBus(); if (null != bus) { ServiceContractResolverRegistry resolverRegistry = bus.getExtension(ServiceContractResolverRegistry.class); if (resolverRegistry != null) { resolverRegistry.register(this); } } } public URI getContractLocation(QName serviceName) { ... } }
To register the contract resolver with a client you need to add a
bean
element to the client's configuration. The bean
element's class
attribute is the name of the class implementing the contract resolver.
Example 29.6, “Bean Configuring a Contract Resolver” shows a bean for adding a configuration resolver implemented by the
org.apache.cxf.demos.myContractResolver
class.
Example 29.6. Bean Configuring a Contract Resolver
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> ... <bean id="myResolver" class="org.apache.cxf.demos.myContractResolver" /> ... </beans>
Contract resolution order
When a new proxy is created, the runtime uses the contract registry resolver to locate the remote service's WSDL contract. The contract resolver registry calls each contract resolver's
getContractLocation()
method in the order in which the resolvers were registered. It returns the first URI returned from one of the registered contract resolvers.
If you registered a contract resolver that attempted to resolve the WSDL contract at a well known shared file system, it would be the only contract resolver used. However, if you subsequently registered a contract resolver that resolved WSDL locations using a UDDI registry, the registry could use both resolvers to locate a service's WSDL contract. The registry would first attempt to locate the contract using the shared file system contract resolver. If that contract resolver failed, the registry would then attempt to locate it using the UDDI contract resolver.