Ce contenu n'est pas disponible dans la langue sélectionnée.
29.4. Using a contract resolver
Overview Copier lienLien copié sur presse-papiers!
ServiceContractResolver interface. You also need to register your custom resolver with the bus.
Implementing the contract resolver Copier lienLien copié sur presse-papiers!
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);
}
Registering the contract resolver programmatically Copier lienLien copié sur presse-papiers!
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.
- 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
BusFactory bf=BusFactory.newInstance(); 1
Bus bus=bf.createBus();
ServiceContractResolverRegistry registry = bus.getExtension(ServiceContractResolverRegistry); 2
JarServiceContractResolver resolver = new JarServiceContractResolver(); 3
registry.register(resolver); 4
Registering a contract resolver using configuration Copier lienLien copié sur presse-papiers!
- 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@PostConstructannotation.
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)
{
...
}
}
bean element to the client's configuration. The bean element's class attribute is the name of the class implementing the contract resolver.
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 Copier lienLien copié sur presse-papiers!
getContractLocation() method in the order in which the resolvers were registered. It returns the first URI returned from one of the registered contract resolvers.