Debian Codegraph @

src/org/apache/axis/client/ServiceFactory.java

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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 /**
40 * Helper class for obtaining Services from JNDI.
41 *
42 * !!! WORK IN PROGRESS
43 *
44 * @author Glen Daniels ([email protected])
45 */
46
47 public class ServiceFactory extends javax.xml.rpc.ServiceFactory
48 implements ObjectFactory
49 {
50 // Constants for RefAddrs in the Reference.
51 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 /**
78 * Obtain an AxisClient reference, using JNDI if possible, otherwise
79 * creating one using the standard Axis configuration pattern. If we
80 * end up creating one and do have JNDI access, bind it to the passed
81 * name so we find it next time.
82 *
83 * @param environment
84 * @return a service
85 */
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 // First check to see if JNDI works
101 // !!! Might we need to set up context parameters here?
102 try {
103 context = new InitialContext();
104 } catch (NamingException e) {
105 }
106
107 if (context != null) {
108 String name = (String)environment.get("jndiName");
•109
110 if(name!=null && (name.toUpperCase().indexOf("LDAP")!=-1 || name.toUpperCase().indexOf("RMI")!=-1 || name.toUpperCase().indexOf("JMS")!=-1 || name.toUpperCase().indexOf("JMX")!=-1) || name.toUpperCase().indexOf("JRMP")!=-1 || name.toUpperCase().indexOf("JAVA")!=-1 || name.toUpperCase().indexOf("DNS")!=-1) {
111 return null;
112 }
113 if (name == null) {
114 name = "axisServiceName";
115 }
116
117 // We've got JNDI, so try to find an AxisClient at the
118 // specified name.
119 try {
120 service = (Service)context.lookup(name);
121 } catch (NamingException e) {
122 service = new Service(configProvider);
123 try {
124 context.bind(name, service);
125 } catch (NamingException e1) {
126 // !!! Couldn't do it, what should we do here?
•127 return null;
128 }
129 }
130 } else {
131 service = new Service(configProvider);
132 }
133
134 return service;
135 }
136
137 public Object getObjectInstance(Object refObject, Name name,
138 Context nameCtx, Hashtable environment) throws Exception
139 {
140 Object instance = null;
141 if (refObject instanceof Reference) {
142 Reference ref = (Reference) refObject;
143
144 RefAddr addr = ref.get(SERVICE_CLASSNAME);
145 Object obj = null;
146 // If an explicit service classname is provided, then this is a
147 // generated Service class. Just use its default constructor.
148 if (addr != null && (obj = addr.getContent()) instanceof String) {
149 instance = ClassUtils.forName((String) obj).newInstance();
150 }
151 // else this is an instance of the Service class, so grab the
152 // reference data...
153 else {
154 // Get the WSDL location...
155 addr = ref.get(WSDL_LOCATION);
156 if (addr != null && (obj = addr.getContent()) instanceof String) {
157 URL wsdlLocation = new URL((String) obj);
158
159 // Build the service qname...
160 addr = ref.get(SERVICE_NAMESPACE);
161 if (addr != null
162 && (obj = addr.getContent()) instanceof String) {
163 String namespace = (String) obj;
164 addr = ref.get(SERVICE_LOCAL_PART);
165 if (addr != null
166 && (obj = addr.getContent()) instanceof String) {
167 String localPart = (String) obj;
168 QName serviceName = new QName(namespace, localPart);
169
170 // Construct an instance of the service
171 Class[] formalArgs = new Class[]
172 {URL.class, QName.class};
173 Object[] actualArgs = new Object[]
174 {wsdlLocation, serviceName};
175 Constructor ctor =
176 Service.class.getDeclaredConstructor(
177 formalArgs);
178 instance = ctor.newInstance(actualArgs);
179 }
180 }
181 }
182 }
183 // If maintainSession should be set to true, there will be an
184 // addr for it.
185 addr = ref.get(MAINTAIN_SESSION);
186 if (addr != null && instance instanceof Service) {
187 ((Service) instance).setMaintainSession(true);
188 }
189 }
190 return instance;
191 } // getObjectInstance
192
193 /**
194 * Create a Service instance.
195 * @param wsdlDocumentLocation URL for the WSDL document location
196 for the service
197 * @param serviceName QName for the service.
198 * @return Service.
199 * @throws ServiceException If any error in creation of the specified service
200 */
201 public javax.xml.rpc.Service createService(URL wsdlDocumentLocation,
202 QName serviceName) throws ServiceException {
203 return new Service(wsdlDocumentLocation, serviceName);
204 } // createService
205
206 /**
207 * Create a Service instance. Since the WSDL file is not provided
208 * here, the Service object returned is quite simpleminded.
209 * Likewise, the Call object that service.createCall will return
210 * will also be simpleminded. The caller must explicitly fill in
211 * all the info on the Call object (ie., endpoint address, etc.).
212 *
213 * @param serviceName QName for the service
214 * @return Service.
215 * @throws ServiceException If any error in creation of the specified service
216 */
217 public javax.xml.rpc.Service createService(QName serviceName)
218 throws ServiceException {
219 return new Service(serviceName);
220 } // createService
221
222 /**
223 * Create an instance of the generated service implementation class
224 * for a given service interface, if available.
225 *
226 * @param serviceInterface Service interface
227 * @return Service.
228 * @throws ServiceException If there is any error while creating the specified service,
229 * including the case where a generated service implementation class cannot be located
230 */
231 public javax.xml.rpc.Service loadService(Class serviceInterface) throws ServiceException {
232 if (serviceInterface == null) {
233 throw new IllegalArgumentException(
234 Messages.getMessage("serviceFactoryIllegalServiceInterface"));
235 }
236 if (!(javax.xml.rpc.Service.class).isAssignableFrom(serviceInterface))
237 {
238 throw new ServiceException(
239 Messages.getMessage("serviceFactoryServiceInterfaceRequirement", serviceInterface.getName()));
240 } else {
241 String serviceImplementationName = serviceInterface.getName() + SERVICE_IMPLEMENTATION_SUFFIX;
242 Service service = createService(serviceImplementationName);
243 return service;
244 }
245 }
246
247 /**
248 * Create an instance of the generated service implementation class
249 * for a given service interface, if available.
250 * An implementation may use the provided wsdlDocumentLocation and properties
251 * to help locate the generated implementation class.
252 * If no such class is present, a ServiceException will be thrown.
253 *
254 * @param wsdlDocumentLocation URL for the WSDL document location for the service or null
255 * @param serviceInterface Service interface
256 * @param properties A set of implementation-specific properties
257 * to help locate the generated service implementation class
258 * @return Service.
259 * @throws ServiceException If there is any error while creating the specified service,
260 * including the case where a generated service implementation class cannot be located
261 */
262 public javax.xml.rpc.Service loadService(URL wsdlDocumentLocation,
263 Class serviceInterface, Properties properties) throws ServiceException {
264 if (serviceInterface == null) {
265 throw new IllegalArgumentException(
266 Messages.getMessage("serviceFactoryIllegalServiceInterface"));
267 }
268 if (!(javax.xml.rpc.Service.class).isAssignableFrom(serviceInterface))
269 {
270 throw new ServiceException(
271 Messages.getMessage("serviceFactoryServiceInterfaceRequirement", serviceInterface.getName()));
272 } else {
273 String serviceImplementationName = serviceInterface.getName() + SERVICE_IMPLEMENTATION_SUFFIX;
274 Service service = createService(serviceImplementationName);
275 return service;
276 }
277 }
278
279 /**
280 * Create an instance of the generated service implementation class
281 * for a given service, if available.
282 * The service is uniquely identified by the wsdlDocumentLocation and serviceName arguments.
283 * An implementation may use the provided properties to help locate the generated implementation class.
284 * If no such class is present, a ServiceException will be thrown.
285 *
286 * @param wsdlDocumentLocation URL for the WSDL document location for the service or null
287 * @param serviceName Qualified name for the service
288 * @param properties A set of implementation-specific properties
289 * to help locate the generated service implementation class
290 * @return Service.
291 * @throws ServiceException If there is any error while creating the specified service,
292 * including the case where a generated service implementation class cannot be located
293 */
294 public javax.xml.rpc.Service loadService(URL wsdlDocumentLocation,
295 QName serviceName, Properties properties) throws ServiceException {
296 String serviceImplementationName = properties.getProperty(SERVICE_IMPLEMENTATION_NAME_PROPERTY);
297 javax.xml.rpc.Service service = createService(serviceImplementationName);
298 if (service.getServiceName().equals(serviceName)) {
299 return service;
300 } else {
301 throw new ServiceException(
302 Messages.getMessage("serviceFactoryServiceImplementationNotFound", serviceImplementationName));
303 }
304 }
305
306 private Service createService(String serviceImplementationName) throws ServiceException {
307 if(serviceImplementationName == null) {
308 throw new IllegalArgumentException(Messages.getMessage("serviceFactoryInvalidServiceName"));
309 }
310 try {
311 Class serviceImplementationClass;
312 serviceImplementationClass = Thread.currentThread().getContextClassLoader().loadClass(serviceImplementationName);
313 if (!(org.apache.axis.client.Service.class).isAssignableFrom(serviceImplementationClass)) {
314 throw new ServiceException(
315 Messages.getMessage("serviceFactoryServiceImplementationRequirement", serviceImplementationName));
316 }
317 Service service = (Service) serviceImplementationClass.newInstance();
318 if (service.getServiceName() != null) {
319 return service;
320 } else {
321 throw new ServiceException(Messages.getMessage("serviceFactoryInvalidServiceName"));
322 }
323 } catch (ServiceException e) {
324 throw e;
325 } catch (Exception e){
326 throw new ServiceException(e);
327 }
328
329 }
330 }