1 16 17 package org.apache.axis.client; 18 19 import org.apache.axis.EngineConfiguration; 20 import org.apache.axis.configuration.EngineConfigurationFactoryFinder; 21 import org.apache.axis.utils.ClassUtils; 22 import org.apache.axis.utils.Messages; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.naming.Name ; 27 import javax.naming.NamingException ; 28 import javax.naming.RefAddr ; 29 import javax.naming.Reference ; 30 import javax.naming.spi.ObjectFactory ; 31 import javax.xml.namespace.QName ; 32 import javax.xml.rpc.ServiceException ; 33 import java.lang.reflect.Constructor ; 34 import java.net.URL ; 35 import java.util.Hashtable ; 36 import java.util.Map ; 37 import java.util.Properties ; 38 39 46 47 public class ServiceFactory extends javax.xml.rpc.ServiceFactory 48 implements ObjectFactory 49 { 50 public static final String SERVICE_CLASSNAME = "service classname"; 52 public static final String WSDL_LOCATION = "WSDL location"; 53 public static final String MAINTAIN_SESSION = "maintain session"; 54 public static final String SERVICE_NAMESPACE = "service namespace"; 55 public static final String SERVICE_LOCAL_PART = "service local part"; 56 public static final String SERVICE_IMPLEMENTATION_NAME_PROPERTY = "serviceImplementationName"; 57 58 private static final String SERVICE_IMPLEMENTATION_SUFFIX = "Locator"; 59 60 private static EngineConfiguration _defaultEngineConfig = null; 61 62 private static ThreadLocal threadDefaultConfig = new ThreadLocal (); 63 64 public static void setThreadDefaultConfig(EngineConfiguration config) 65 { 66 threadDefaultConfig.set(config); 67 } 68 69 private static EngineConfiguration getDefaultEngineConfig() { 70 if (_defaultEngineConfig == null) { 71 _defaultEngineConfig = 72 EngineConfigurationFactoryFinder.newFactory().getClientEngineConfig(); 73 } 74 return _defaultEngineConfig; 75 } 76 77 86 public static Service getService(Map environment) 87 { 88 Service service = null; 89 InitialContext context = null; 90 91 EngineConfiguration configProvider = 92 (EngineConfiguration)environment.get(EngineConfiguration.PROPERTY_NAME); 93 94 if (configProvider == null) 95 configProvider = (EngineConfiguration)threadDefaultConfig.get(); 96 97 if (configProvider == null) 98 configProvider = getDefaultEngineConfig(); 99 100 try { 103 context = new InitialContext (); 104 } catch (NamingException e) { 105 } 106 107 if (context != null) { 108 String name = (String )environment.get("jndiName"); 109 if (name == null) { 110 name = "axisServiceName"; 111 } 112 113 try { 116 service = (Service)context.lookup(name); 117 } catch (NamingException e) { 118 service = new Service(configProvider); 119 try { 120 context.bind(name, service); 121 } catch (NamingException e1) { 122 } 124 } 125 } else { 126 service = new Service(configProvider); 127 } 128 129 return service; 130 } 131 132 public Object getObjectInstance(Object refObject, Name name, 133 Context nameCtx, Hashtable environment) throws Exception 134 { 135 Object instance = null; 136 if (refObject instanceof Reference ) { 137 Reference ref = (Reference ) refObject; 138 139 RefAddr addr = ref.get(SERVICE_CLASSNAME); 140 Object obj = null; 141 if (addr != null && (obj = addr.getContent()) instanceof String ) { 144 instance = ClassUtils.forName((String ) obj).newInstance(); 145 } 146 else { 149 addr = ref.get(WSDL_LOCATION); 151 if (addr != null && (obj = addr.getContent()) instanceof String ) { 152 URL wsdlLocation = new URL ((String ) obj); 153 154 addr = ref.get(SERVICE_NAMESPACE); 156 if (addr != null 157 && (obj = addr.getContent()) instanceof String ) { 158 String namespace = (String ) obj; 159 addr = ref.get(SERVICE_LOCAL_PART); 160 if (addr != null 161 && (obj = addr.getContent()) instanceof String ) { 162 String localPart = (String ) obj; 163 QName serviceName = new QName (namespace, localPart); 164 165 Class [] formalArgs = new Class [] 167 {URL .class, QName .class}; 168 Object [] actualArgs = new Object [] 169 {wsdlLocation, serviceName}; 170 Constructor ctor = 171 Service.class.getDeclaredConstructor( 172 formalArgs); 173 instance = ctor.newInstance(actualArgs); 174 } 175 } 176 } 177 } 178 addr = ref.get(MAINTAIN_SESSION); 181 if (addr != null && instance instanceof Service) { 182 ((Service) instance).setMaintainSession(true); 183 } 184 } 185 return instance; 186 } 188 196 public javax.xml.rpc.Service createService(URL wsdlDocumentLocation, 197 QName serviceName) throws ServiceException { 198 return new Service(wsdlDocumentLocation, serviceName); 199 } 201 212 public javax.xml.rpc.Service createService(QName serviceName) 213 throws ServiceException { 214 return new Service(serviceName); 215 } 217 226 public javax.xml.rpc.Service loadService(Class serviceInterface) throws ServiceException { 227 if (serviceInterface == null) { 228 throw new IllegalArgumentException ( 229 Messages.getMessage("serviceFactoryIllegalServiceInterface")); 230 } 231 if (!(javax.xml.rpc.Service .class).isAssignableFrom(serviceInterface)) 232 { 233 throw new ServiceException ( 234 Messages.getMessage("serviceFactoryServiceInterfaceRequirement", serviceInterface.getName())); 235 } else { 236 String serviceImplementationName = serviceInterface.getName() + SERVICE_IMPLEMENTATION_SUFFIX; 237 Service service = createService(serviceImplementationName); 238 return service; 239 } 240 } 241 242 257 public javax.xml.rpc.Service loadService(URL wsdlDocumentLocation, 258 Class serviceInterface, Properties properties) throws ServiceException { 259 if (serviceInterface == null) { 260 throw new IllegalArgumentException ( 261 Messages.getMessage("serviceFactoryIllegalServiceInterface")); 262 } 263 if (!(javax.xml.rpc.Service .class).isAssignableFrom(serviceInterface)) 264 { 265 throw new ServiceException ( 266 Messages.getMessage("serviceFactoryServiceInterfaceRequirement", serviceInterface.getName())); 267 } else { 268 String serviceImplementationName = serviceInterface.getName() + SERVICE_IMPLEMENTATION_SUFFIX; 269 Service service = createService(serviceImplementationName); 270 return service; 271 } 272 } 273 274 289 public javax.xml.rpc.Service loadService(URL wsdlDocumentLocation, 290 QName serviceName, Properties properties) throws ServiceException { 291 String serviceImplementationName = properties.getProperty(SERVICE_IMPLEMENTATION_NAME_PROPERTY); 292 javax.xml.rpc.Service service = createService(serviceImplementationName); 293 if (service.getServiceName().equals(serviceName)) { 294 return service; 295 } else { 296 throw new ServiceException ( 297 Messages.getMessage("serviceFactoryServiceImplementationNotFound", serviceImplementationName)); 298 } 299 } 300 301 private Service createService(String serviceImplementationName) throws ServiceException { 302 if(serviceImplementationName == null) { 303 throw new IllegalArgumentException (Messages.getMessage("serviceFactoryInvalidServiceName")); 304 } 305 try { 306 Class serviceImplementationClass; 307 serviceImplementationClass = Thread.currentThread().getContextClassLoader().loadClass(serviceImplementationName); 308 if (!(org.apache.axis.client.Service.class).isAssignableFrom(serviceImplementationClass)) { 309 throw new ServiceException ( 310 Messages.getMessage("serviceFactoryServiceImplementationRequirement", serviceImplementationName)); 311 } 312 Service service = (Service) serviceImplementationClass.newInstance(); 313 if (service.getServiceName() != null) { 314 return service; 315 } else { 316 throw new ServiceException (Messages.getMessage("serviceFactoryInvalidServiceName")); 317 } 318 } catch (ServiceException e) { 319 throw e; 320 } catch (Exception e){ 321 throw new ServiceException (e); 322 } 323 324 } 325 } 326 | Popular Tags |