private Object getClientServiceObject(ServiceReferenceDescriptor desc)
throws NamingException {
Class serviceInterfaceClass = null;
Object returnObj = null;
WsUtil wsUtil = new WsUtil();
try {
WSContainerResolver.set(desc);
ClassLoader cl = Thread.currentThread().getContextClassLoader();
serviceInterfaceClass = cl.loadClass(desc.getServiceInterface());
resolvePortComponentLinks(desc);
Service serviceDelegate = null;
javax.xml.ws.Service jaxwsDelegate = null;
Object injValue = null;
if( desc.hasGeneratedServiceInterface() || desc.hasWsdlFile() ) {
String serviceImplName = desc.getServiceImplClassName();
if(serviceImplName != null) {
Class serviceImplClass = cl.loadClass(serviceImplName);
serviceDelegate = (Service) serviceImplClass.newInstance();
} else {
// The target is probably a post JAXRPC-1.1- based service;
// If Service Interface class is set, check if it is indeed a subclass of Service
// initiateInstance should not be called if the user has given javax.xml.ws.Service itself
// as the interface through DD
if(javax.xml.ws.Service.class.isAssignableFrom(serviceInterfaceClass) &&
!javax.xml.ws.Service.class.equals(serviceInterfaceClass)) {
// OK - the interface class is indeed the generated service class; get an instance
injValue = initiateInstance(serviceInterfaceClass, desc);
} else {
// First try failed; Try to get the Service class type from injected field name
// and from there try to get an instance of the service class
// I assume the at all inejction target are expecting the SAME service
// interface, therefore I take the first one.
if (desc.isInjectable()) {
InjectionTarget target = desc.getInjectionTargets().iterator().next();
Class serviceType = null;
if (target.isFieldInjectable()) {
java.lang.reflect.Field f = target.getField();
if(f == null) {
String fName = target.getFieldName();
Class targetClass = cl.loadClass(target.getClassName());
try {
f = targetClass.getDeclaredField(target.getFieldName());
} catch(java.lang.NoSuchFieldException nsfe) {}// ignoring exception
}
serviceType = f.getType();
}
if (target.isMethodInjectable()) {
Method m = target.getMethod();
if(m == null) {
String mName = target.getMethodName();
Class targetClass = cl.loadClass(target.getClassName());
try {
m = targetClass.getDeclaredMethod(target.getMethodName());
} catch(java.lang.NoSuchMethodException nsfe) {}// ignoring exception
}
if (m.getParameterTypes().length==1) {
serviceType = m.getParameterTypes()[0];
}
}
if (serviceType!=null){
Class loadedSvcClass = cl.loadClass(serviceType.getCanonicalName());
injValue = initiateInstance(loadedSvcClass, desc);
}
}
}
// Unable to get hold of generated service class -> try the Service.create avenue to get a Service
if(injValue == null) {
// Here create the service with WSDL (overridden wsdl if wsdl-override is present)
// so that JAXWS runtime uses this wsdl @ runtime
javax.xml.ws.Service svc =
javax.xml.ws.Service.create((new WsUtil()).privilegedGetServiceRefWsdl(desc),
desc.getServiceName());
jaxwsDelegate = new JAXWSServiceDelegate(desc, svc, cl);
}
}
if( desc.hasHandlers() ) {
// We need the service's ports to configure the
// handler chain (since service-ref handler chain can
// optionally specify handler-port association)
// so create a configured service and call getPorts
Service configuredService =
wsUtil.createConfiguredService(desc);
Iterator ports = configuredService.getPorts();
wsUtil.configureHandlerChain
(desc, serviceDelegate, ports, cl);
}
// check if this is a post 1.1 web service
if(javax.xml.ws.Service.class.isAssignableFrom(serviceInterfaceClass)) {
// This is a JAXWS based webservice client;
// process handlers and mtom setting
// moved test for handlers into wsUtil, in case
// we have to add system handler
javax.xml.ws.Service service =
(injValue != null ?
(javax.xml.ws.Service) injValue : jaxwsDelegate);
if (service != null) {
// Now configure client side handlers
wsUtil.configureJAXWSClientHandlers(service, desc);
}
// the requested resource is not the service but one of its port.
if (injValue!=null && desc.getInjectionTargetType()!=null) {
Class requestedPortType = service.getClass().getClassLoader().loadClass(desc.getInjectionTargetType());
injValue = service.getPort(requestedPortType);