1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.axis2.deployment.util;
21
22 import org.apache.axiom.om.OMAbstractFactory;
23 import org.apache.axiom.om.OMElement;
24 import org.apache.axiom.om.OMFactory;
25 import org.apache.axiom.soap.SOAP11Constants;
26 import org.apache.axiom.soap.SOAP12Constants;
27 import org.apache.axis2.AxisFault;
28 import org.apache.axis2.classloader.JarFileClassLoader;
29 import org.apache.axis2.Constants;
30 import org.apache.axis2.context.ConfigurationContext;
31 import org.apache.axis2.deployment.DeploymentClassLoader;
32 import org.apache.axis2.deployment.DeploymentConstants;
33 import org.apache.axis2.deployment.DeploymentException;
34 import org.apache.axis2.deployment.repository.util.ArchiveReader;
35 import org.apache.axis2.deployment.repository.util.DeploymentFileData;
36 import org.apache.axis2.description;
37 import org.apache.axis2.description.java2wsdl.DefaultSchemaGenerator;
38 import org.apache.axis2.description.java2wsdl.DocLitBareSchemaGenerator;
39 import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
40 import org.apache.axis2.description.java2wsdl.SchemaGenerator;
41 import org.apache.axis2.engine.AxisConfiguration;
42 import org.apache.axis2.engine.Handler;
43 import org.apache.axis2.engine.MessageReceiver;
44 import org.apache.axis2.util.JavaUtils;
45 import org.apache.axis2.util.Loader;
46 import org.apache.axis2.util.PolicyUtil;
47 import org.apache.axis2.wsdl.WSDLConstants;
48 import org.apache.axis2.wsdl.WSDLUtil;
49 import org.apache.commons.logging.Log;
50 import org.apache.commons.logging.LogFactory;
51 import org.apache.neethi.PolicyComponent;
52 import org.apache.ws.commons.schema.utils.NamespaceMap;
53
54 import javax.jws.WebMethod;
55 import javax.jws.WebService;
56 import javax.xml.namespace.QName;
57 import javax.xml.stream.FactoryConfigurationError;
58 import javax.xml.stream.XMLStreamException;
59 import java.io;
60 import java.lang.reflect.InvocationTargetException;
61 import java.lang.reflect.Method;
62 import java.lang.reflect.Modifier;
63 import java.net.MalformedURLException;
64 import java.net.URL;
65 import java.net.URLClassLoader;
66 import java.net.URLDecoder;
67 import java.security.AccessController;
68 import java.security.PrivilegedAction;
69 import java.security.PrivilegedActionException;
70 import java.security.PrivilegedExceptionAction;
71 import java.util;
72 import java.util.zip.ZipEntry;
73 import java.util.zip.ZipInputStream;
74
75 public class Utils {
76
77 public static String defaultEncoding = new OutputStreamWriter(System.out).getEncoding();
78
79 private static Log log = LogFactory.getLog(Utils.class);
80
81 public static void addFlowHandlers(Flow flow, ClassLoader clsLoader)
82 throws AxisFault {
83 int count = flow.getHandlerCount();
84
85 for (int j = 0; j < count; j++) {
86 HandlerDescription handlermd = flow.getHandler(j);
87 Handler handler;
88
89 final Class handlerClass = getHandlerClass(
90 handlermd.getClassName(), clsLoader);
91
92 try {
93 handler = (Handler)org.apache.axis2.java.security.AccessController
94 .doPrivileged(new PrivilegedExceptionAction() {
95 public Object run() throws InstantiationException,
96 IllegalAccessException {
97 return handlerClass.newInstance();
98 }
99 });
100 handler.init(handlermd);
101 handlermd.setHandler(handler);
102 } catch (PrivilegedActionException e) {
103 throw AxisFault.makeFault(e);
104 }
105 }
106 }
107
108 public static boolean loadHandler(ClassLoader loader1,
109 HandlerDescription desc) throws DeploymentException {
110 String handlername = desc.getClassName();
111 Handler handler;
112 try {
113 final Class handlerClass = Loader.loadClass(loader1, handlername);
114 Package aPackage = (Package)org.apache.axis2.java.security.AccessController
115 .doPrivileged(new PrivilegedAction() {
116 public Object run() {
117 return handlerClass.getPackage();
118 }
119 });
120 if (aPackage != null
121 && aPackage.getName().equals("org.apache.axis2.engine")) {
122 String name = handlerClass.getName();
123 log.warn("Dispatcher " + name + " is now deprecated.");
124 if (name.indexOf("InstanceDispatcher") != -1) {
125 log.warn("Please remove the entry for "
126 + handlerClass.getName() + "from axis2.xml");
127 } else {
128 log.warn(
129 "Please edit axis2.xml and replace with the same class in org.apache.axis2.dispatchers package");
130 }
131 }
132 handler = (Handler)org.apache.axis2.java.security.AccessController
133 .doPrivileged(new PrivilegedExceptionAction() {
134 public Object run() throws InstantiationException,
135 IllegalAccessException {
136 return handlerClass.newInstance();
137 }
138 });
139 handler.init(desc);
140 desc.setHandler(handler);
141 } catch (ClassNotFoundException e) {
142 if (handlername.indexOf("jaxws") > 0) {
143 log.warn("[JAXWS] - unable to load " + handlername);
144 return false;
145 }
146 throw new DeploymentException(e);
147 } catch (Exception e) {
148 throw new DeploymentException(e);
149 }
150 return true;
151 }
152
153 public static URL[] getURLsForAllJars(URL url, File tmpDir) {
154 FileInputStream fin = null;
155 InputStream in = null;
156 ZipInputStream zin = null;
157 try {
158 ArrayList array = new ArrayList();
159 in = url.openStream();
160 String fileName = url.getFile();
161 int index = fileName.lastIndexOf('/');
162 if (index != -1) {
163 fileName = fileName.substring(index + 1);
164 }
165 final File f = createTempFile(fileName, in, tmpDir);
166
167 fin = (FileInputStream)org.apache.axis2.java.security.AccessController
168 .doPrivileged(new PrivilegedExceptionAction() {
169 public Object run() throws FileNotFoundException {
170 return new FileInputStream(f);
171 }
172 });
173 array.add(f.toURL());
174 zin = new ZipInputStream(fin);
175
176 ZipEntry entry;
177 String entryName;
178 while ((entry = zin.getNextEntry()) != null) {
179 entryName = entry.getName();
180 /**
181 * id the entry name start with /lib and end with .jar then
182 * those entry name will be added to the arraylist
183 */
184 if ((entryName != null)
185 && entryName.toLowerCase().startsWith("lib/")
186 && entryName.toLowerCase().endsWith(".jar")) {
187 String suffix = entryName.substring(4);
188 File f2 = createTempFile(suffix, zin, tmpDir);
189 array.add(f2.toURL());
190 }
191 }
192 return (URL[])array.toArray(new URL[array.size()]);
193 } catch (Exception e) {
194 throw new RuntimeException(e);
195 } finally {
196 if (fin != null) {
197 try {
198 fin.close();
199 } catch (IOException e) {
200 //
201 }
202 }
203 if (in != null) {
204 try {
205 in.close();
206 } catch (IOException e) {
207 //
208 }
209 }
210 if (zin != null) {
211 try {
212 zin.close();
213 } catch (IOException e) {
214 //
215 }
216 }
217 }
218 }
219
220 public static File createTempFile(final String suffix, InputStream in,
221 final File tmpDir) throws IOException {
222 byte data[] = new byte[2048];
223 int count;
224 File f = TempFileManager.createTempFile("axis2", suffix);
225
226 // if (tmpDir == null) {
227 // String directory = (String)org.apache.axis2.java.security.AccessController
228 // .doPrivileged(new PrivilegedAction() {
229 // public Object run() {
230 // return System.getProperty("java.io.tmpdir");
231 // }
232 // });
233 // final File tempFile = new File(directory, "_axis2");
234 // Boolean exists = (Boolean)org.apache.axis2.java.security.AccessController
235 // .doPrivileged(new PrivilegedAction() {
236 // public Object run() {
237 // return tempFile.exists();
238 // }
239 // });
240 // if (!exists) {
241 // Boolean mkdirs = (Boolean)org.apache.axis2.java.security.AccessController
242 // .doPrivileged(new PrivilegedAction() {
243 // public Object run() {
244 // return tempFile.mkdirs();
245 // }
246 // });
247 // if (!mkdirs) {
248 // throw new IOException("Unable to create the directory");
249 // }
250 // }
251 // try {
252 // f = (File)org.apache.axis2.java.security.AccessController
253 // .doPrivileged(new PrivilegedExceptionAction() {
254 // public Object run() throws IOException {
255 // return File.createTempFile("axis2", suffix,
256 // tempFile);
257 // }
258 // });
259 // f.deleteOnExit();
260 // } catch (PrivilegedActionException e) {
261 // throw (IOException)e.getException();
262 // }
263 // } else {
264 // try {
265 // f = (File)org.apache.axis2.java.security.AccessController
266 // .doPrivileged(new PrivilegedExceptionAction() {
267 // public Object run() throws IOException {
268 // return File.createTempFile("axis2", suffix,
269 // tmpDir);
270 // }
271 // });
272 // f.deleteOnExit();
273 // } catch (PrivilegedActionException e) {
274 // throw (IOException)e.getException();
275 // }
276 // }
277 // if (log.isDebugEnabled()) {
278 // log.debug("Created temporary file : " + f.getAbsolutePath());// $NON-SEC-4
279 // }
280 // final File f2 = f;
281 // org.apache.axis2.java.security.AccessController
282 // .doPrivileged(new PrivilegedAction() {
283 // public Object run() {
284 // f2.deleteOnExit();
285 // return null;
286 // }
287 // });
288 FileOutputStream out;
289 final File f2 = f;
290 try {
291 out = (FileOutputStream)org.apache.axis2.java.security.AccessController
292 .doPrivileged(new PrivilegedExceptionAction() {
293 public Object run() throws FileNotFoundException {
294 return new FileOutputStream(f2);
295 }
296 });
297 } catch (PrivilegedActionException e) {
298 throw (FileNotFoundException)e.getException();
299 }
300 while ((count = in.read(data, 0, 2048)) != -1) {
301 out.write(data, 0, count);
302 }
303 out.close();
304 return f;
305 }
306
307 public static ClassLoader getClassLoader(ClassLoader parent, String path)
308 throws DeploymentException {
309 return getClassLoader(parent, new File(path));
310 }
311
312 /**
313 * Get a ClassLoader which contains a classpath of a) the passed directory and b) any jar files
314 * inside the "lib/" or "Lib/" subdirectory of the passed directory.
315 *
316 * @param parent parent ClassLoader which will be the parent of the result of this method
317 * @param file a File which must be a directory for this to be useful
318 * @return a new ClassLoader pointing to both the passed dir and jar files under lib/
319 * @throws DeploymentException if problems occur
320 */
321 public static ClassLoader getClassLoader(final ClassLoader parent, File file)
322 throws DeploymentException {
323 URLClassLoader classLoader;
324
325 if (file == null)
326 return null; // Shouldn't this just return the parent?
327
328 try {
329 ArrayList urls = new ArrayList();
330 urls.add(file.toURL());
331
332 // lower case directory name
333 File libfiles = new File(file, "lib");
334 if (!addFiles(urls, libfiles)) {
335 // upper case directory name
336 libfiles = new File(file, "Lib");
337 addFiles(urls, libfiles);
338 }
339
340 final URL urllist[] = new URL[urls.size()];
341 for (int i = 0; i < urls.size(); i++) {
342 urllist[i] = (URL)urls.get(i);
343 }
344 classLoader = (URLClassLoader)AccessController
345 .doPrivileged(new PrivilegedAction() {
346 public Object run() {
347 if (useJarFileClassLoader()) {
348 return new JarFileClassLoader(urllist, parent);
349 } else {
350 return new URLClassLoader(urllist, parent);
351 }
352 }
353 });
354 return classLoader;
355 } catch (MalformedURLException e) {
356 throw new DeploymentException(e);
357 }
358 }
359
360 private static boolean useJarFileClassLoader() {
361 // The JarFileClassLoader was created to address a locking problem seen only on Windows platforms.
362 // It carries with it a slight performance penalty that needs to be addressed. Rather than make
363 // *nix OSes carry this burden we'll engage the JarFileClassLoader for Windows or if the user
364 // specifically requests it.
365 boolean useJarFileClassLoader;
366 if (System.getProperty("org.apache.axis2.classloader.JarFileClassLoader") == null) {
367 useJarFileClassLoader = System.getProperty("os.name").startsWith("Windows");
368 } else {
369 useJarFileClassLoader = Boolean.getBoolean("org.apache.axis2.classloader.JarFileClassLoader");
370 }
371 return useJarFileClassLoader;
372 }
373
374 private static boolean addFiles(ArrayList urls, final File libfiles)
375 throws MalformedURLException {
376 Boolean exists = (Boolean)org.apache.axis2.java.security.AccessController
377 .doPrivileged(new PrivilegedAction() {
378 public Object run() {
379 return libfiles.exists();
380 }
381 });
382 if (exists) {
383 urls.add(libfiles.toURL());
384 File jarfiles[] = (File[])org.apache.axis2.java.security.AccessController
385 .doPrivileged(new PrivilegedAction() {
386 public Object run() {
387 return libfiles.listFiles();
388 }
389 });
390 int i = 0;
391 while (i < jarfiles.length) {
392 File jarfile = jarfiles[i];
393 if (jarfile.getName().endsWith(".jar")) {
394 urls.add(jarfile.toURL());
395 }
396 i++;
397 }
398 }
399 return exists;
400 }
401
402 private static Class getHandlerClass(String className, ClassLoader loader1)
403 throws AxisFault {
404 Class handlerClass;
405
406 try {
407 handlerClass = Loader.loadClass(loader1, className);
408 } catch (ClassNotFoundException e) {
409 throw AxisFault.makeFault(e);
410 }
411
412 return handlerClass;
413 }
414
415 /**
416 * This guy will create a AxisService using java reflection
417 *
418 * @param axisService the target AxisService
419 * @param axisConfig the in-scope AxisConfiguration
420 * @param excludeOperations a List of Strings (or null), each containing a method to exclude
421 * @param nonRpcMethods a List of Strings (or null), each containing a non-rpc method name
422 * @throws Exception if a problem occurs
423 */
424 public static void fillAxisService(final AxisService axisService,
425 AxisConfiguration axisConfig, ArrayList excludeOperations,
426 ArrayList nonRpcMethods) throws Exception {
427 String serviceClass;
428 Parameter implInfoParam = axisService
429 .getParameter(Constants.SERVICE_CLASS);
430 ClassLoader serviceClassLoader = axisService.getClassLoader();
431
432 if (implInfoParam != null) {
433 serviceClass = (String)implInfoParam.getValue();
434 } else {
435 // if Service_Class is null, every AbstractMR will look for
436 // ServiceObjectSupplier. This is user specific and may contain
437 // other looks.
438 implInfoParam = axisService
439 .getParameter(Constants.SERVICE_OBJECT_SUPPLIER);
440 if (implInfoParam != null) {
441 String className = ((String)implInfoParam.getValue()).trim();
442 final Class serviceObjectMaker = Loader.loadClass(
443 serviceClassLoader, className);
444 if (serviceObjectMaker.getModifiers() != Modifier.PUBLIC) {
445 throw new AxisFault("Service class " + className
446 + " must have public as access Modifier");
447 }
448
449 // Find static getServiceObject() method, call it if there
450 final Method method = (Method)org.apache.axis2.java.security.AccessController
451 .doPrivileged(new PrivilegedExceptionAction() {
452 public Object run() throws NoSuchMethodException {
453 return serviceObjectMaker.getMethod(
454 "getServiceObject",
455 AxisService.class);
456 }
457 });
458 Object obj = null;
459 if (method != null) {
460 obj = org.apache.axis2.java.security.AccessController
461 .doPrivileged(new PrivilegedExceptionAction() {
462 public Object run()
463 throws InstantiationException,
464 IllegalAccessException,
465 InvocationTargetException {
466 return method.invoke(serviceObjectMaker.newInstance(),
467 axisService);
468 }
469 });
470 }
471 if (obj == null) {
472 log.warn("ServiceObjectSupplier implmentation Object could not be found");
473 throw new DeploymentException(
474 "ServiceClass or ServiceObjectSupplier implmentation Object could not be found");
475 }
476 serviceClass = obj.getClass().getName();
477 } else {
478 return;
479 }
480 }
481 // adding name spaces
482 NamespaceMap map = new NamespaceMap();
483 map.put(Java2WSDLConstants.AXIS2_NAMESPACE_PREFIX,
484 Java2WSDLConstants.AXIS2_XSD);
485 map.put(Java2WSDLConstants.DEFAULT_SCHEMA_NAMESPACE_PREFIX,
486 Java2WSDLConstants.URI_2001_SCHEMA_XSD);
487 axisService.setNamespaceMap(map);
488 SchemaGenerator schemaGenerator;
489 Parameter generateBare = axisService
490 .getParameter(Java2WSDLConstants.DOC_LIT_BARE_PARAMETER);
491 if (generateBare != null && "true".equals(generateBare.getValue())) {
492 schemaGenerator = new DocLitBareSchemaGenerator(serviceClassLoader,
493 serviceClass.trim(),
494 axisService.getSchemaTargetNamespace(),
495 axisService
496 .getSchemaTargetNamespacePrefix(),
497 axisService);
498 } else {
499 schemaGenerator = new DefaultSchemaGenerator(serviceClassLoader,
500 serviceClass.trim(),
501 axisService.getSchemaTargetNamespace(),
502 axisService
503 .getSchemaTargetNamespacePrefix(),
504 axisService);
505 }
506 schemaGenerator.setExcludeMethods(excludeOperations);
507 schemaGenerator.setNonRpcMethods(nonRpcMethods);
508 if (!axisService.isElementFormDefault()) {
509 schemaGenerator
510 .setElementFormDefault(Java2WSDLConstants.FORM_DEFAULT_UNQUALIFIED);
511 }
512 // package to namespace map
513 schemaGenerator.setPkg2nsmap(axisService.getP2nMap());
514 Collection schemas = schemaGenerator.generateSchema();
515 axisService.addSchema(schemas);
516 axisService.setSchemaTargetNamespace(schemaGenerator
517 .getSchemaTargetNameSpace());
518 axisService.setTypeTable(schemaGenerator.getTypeTable());
519 if (Java2WSDLConstants.DEFAULT_TARGET_NAMESPACE.equals(axisService
520 .getTargetNamespace())) {
521 axisService
522 .setTargetNamespace(schemaGenerator.getTargetNamespace());
523 }
524
525 Method[] method = schemaGenerator.getMethods();
526 PhasesInfo pinfo = axisConfig.getPhasesInfo();
527
528 for (Method jmethod : method) {
529 String opName = jmethod.getName();
530 AxisOperation operation = axisService
531 .getOperation(new QName(opName));
532 // if the operation there in services.xml then try to set it schema
533 // element name
534 if (operation == null) {
535 operation = axisService.getOperation(new QName(
536 jmethod.getName()));
537 }
538 MessageReceiver mr =
539 axisService.getMessageReceiver(operation.getMessageExchangePattern());
540 if (mr == null) {
541 mr = axisConfig.getMessageReceiver(operation.getMessageExchangePattern());
542 }
543 if (operation.getMessageReceiver() == null) {
544 operation.setMessageReceiver(mr);
545 }
546 pinfo.setOperationPhases(operation);
547 axisService.addOperation(operation);
548 if (operation.getSoapAction() == null) {
549 operation.setSoapAction("urn:" + opName);
550 }
551 }
552 }
553
554 public static AxisOperation getAxisOperationForJmethod(Method method)
555 throws AxisFault {
556 AxisOperation operation;
557 if ("void".equals(method.getReturnType().getName())) {
558 if (method.getExceptionTypes().length > 0) {
559 operation = AxisOperationFactory
560 .getAxisOperation(WSDLConstants.MEP_CONSTANT_ROBUST_IN_ONLY);
561 } else {
562 operation = AxisOperationFactory
563 .getAxisOperation(WSDLConstants.MEP_CONSTANT_IN_ONLY);
564 }
565 } else {
566 operation = AxisOperationFactory
567 .getAxisOperation(WSDLConstants.MEP_CONSTANT_IN_OUT);
568 }
569 String opName = method.getName();
570 operation.setName(new QName(opName));
571 WebMethod methodAnnon = method.getAnnotation(WebMethod.class);
572 if (methodAnnon != null) {
573 String action = methodAnnon.action();
574 if (action != null && !"".equals(action)) {
575 operation.setSoapAction(action);
576 }
577 }
578 return operation;
579 }
580
581 public static OMElement getParameter(String name, String value,
582 boolean locked) {
583 OMFactory fac = OMAbstractFactory.getOMFactory();
584 OMElement parameter = fac.createOMElement("parameter", null);
585 parameter.addAttribute("name", name, null);
586 parameter.addAttribute("locked", Boolean.toString(locked), null);
587 parameter.setText(value);
588 return parameter;
589 }
590
591 /**
592 * Modules can contain services in some cases. This method will deploy all the services
593 * for a given AxisModule into the current AxisConfiguration.
594 * <p>
595 * The code looks for an "aars/" directory inside the module (either .mar or exploded),
596 * and an "aars.list" file inside that to figure out which services to deploy. Note that all
597 * services deployed this way will have access to the Module's classes.
598 * </p>
599 *
600 * @param module the AxisModule to search for services
601 * @param configCtx ConfigurationContext in which to deploy
602 */
603
604 public static void deployModuleServices(AxisModule module,
605 ConfigurationContext configCtx) throws AxisFault {
606 try {
607 AxisConfiguration axisConfig = configCtx.getAxisConfiguration();
608 ArchiveReader archiveReader = new ArchiveReader();
609 PhasesInfo phasesInfo = axisConfig.getPhasesInfo();
610 final ClassLoader moduleClassLoader = module.getModuleClassLoader();
611 ArrayList services = new ArrayList();
612 final InputStream in = (InputStream)org.apache.axis2.java.security.AccessController
613 .doPrivileged(new PrivilegedAction() {
614 public Object run() {
615 return moduleClassLoader
616 .getResourceAsStream("aars/aars.list");
617 }
618 });
619 if (in != null) {
620 BufferedReader input;
621 try {
622 input = new BufferedReader(
623 (InputStreamReader)org.apache.axis2.java.security.AccessController
624 .doPrivileged(new PrivilegedAction() {
625 public Object run() {
626 return new InputStreamReader(in);
627 }
628 }));
629 String line;
630 while ((line = input.readLine()) != null) {
631 line = line.trim();
632 if (line.length() > 0 && line.charAt(0) != '#') {
633 services.add(line);
634 }
635 }
636 input.close();
637 } catch (IOException ex) {
638 ex.printStackTrace();
639 }
640 }
641 if (services.size() > 0) {
642 for (Object service1 : services) {
643 final String servicename = (String)service1;
644 if (servicename == null || "".equals(servicename)) {
645 continue;
646 }
647 InputStream fin = (InputStream)org.apache.axis2.java.security.AccessController
648 .doPrivileged(new PrivilegedAction() {
649 public Object run() {
650 return moduleClassLoader
651 .getResourceAsStream("aars/"
652 + servicename);
653 }
654 });
655 if (fin == null) {
656 throw new AxisFault("No service archive found : "
657 + servicename);
658 }
659 File inputFile = Utils
660 .createTempFile(
661 servicename,
662 fin,
663 (File)axisConfig
664 .getParameterValue(
665 Constants.Configuration.ARTIFACTS_TEMP_DIR));
666 DeploymentFileData filedata = new DeploymentFileData(
667 inputFile);
668
669 filedata
670 .setClassLoader(
671 false,
672 moduleClassLoader,
673 (File)axisConfig
674 .getParameterValue(
675 Constants.Configuration.ARTIFACTS_TEMP_DIR));
676 HashMap wsdlservice = archiveReader.processWSDLs(filedata);
677 if (wsdlservice != null && wsdlservice.size() > 0) {
678 Iterator servicesitr = wsdlservice.values().iterator();
679 while (servicesitr.hasNext()) {
680 AxisService service = (AxisService)servicesitr
681 .next();
682 Iterator operations = service.getOperations();
683 while (operations.hasNext()) {
684 AxisOperation axisOperation = (AxisOperation)operations
685 .next();
686 phasesInfo.setOperationPhases(axisOperation);
687 }
688 }
689 }
690 AxisServiceGroup serviceGroup = new AxisServiceGroup(
691 axisConfig);
692 serviceGroup.setServiceGroupClassLoader(filedata
693 .getClassLoader());
694 ArrayList serviceList = archiveReader.processServiceGroup(
695 filedata.getAbsolutePath(), filedata, serviceGroup,
696 false, wsdlservice, configCtx);
697 for (Object aServiceList : serviceList) {
698 AxisService axisService = (AxisService)aServiceList;
699 Parameter moduleService = new Parameter();
700 moduleService.setValue("true");
701 moduleService.setName(AxisModule.MODULE_SERVICE);
702 axisService.addParameter(moduleService);
703 serviceGroup.addService(axisService);
704 }
705 axisConfig.addServiceGroup(serviceGroup);
706 fin.close();
707 }
708 }
709 } catch (IOException e) {
710 throw AxisFault.makeFault(e);
711 }
712 }
713
714 /**
715 * Normalize a uri containing ../ and ./ paths.
716 *
717 * @param uri The uri path to normalize
718 * @return The normalized uri
719 */
720 public static String normalize(String uri) {
721 if ("".equals(uri)) {
722 return uri;
723 }
724 int leadingSlashes;
725 for (leadingSlashes = 0; leadingSlashes < uri.length()
726 && uri.charAt(leadingSlashes) == '/'; ++leadingSlashes) {
727 // FIXME: this block is empty!!
728 }
729 boolean isDir = (uri.charAt(uri.length() - 1) == '/');
730 StringTokenizer st = new StringTokenizer(uri, "/");
731 LinkedList clean = new LinkedList();
732 while (st.hasMoreTokens()) {
733 String token = st.nextToken();
734 if ("..".equals(token)) {
735 if (!clean.isEmpty() && !"..".equals(clean.getLast())) {
736 clean.removeLast();
737 if (!st.hasMoreTokens()) {
738 isDir = true;
739 }
740 } else {
741 clean.add("..");
742 }
743 } else if (!".".equals(token) && !"".equals(token)) {
744 clean.add(token);
745 }
746 }
747 StringBuffer sb = new StringBuffer();
748 while (leadingSlashes-- > 0) {
749 sb.append('/');
750 }
751 for (Iterator it = clean.iterator(); it.hasNext();) {
752 sb.append(it.next());
753 if (it.hasNext()) {
754 sb.append('/');
755 }
756 }
757 if (isDir && sb.length() > 0 && sb.charAt(sb.length() - 1) != '/') {
758 sb.append('/');
759 }
760 return sb.toString();
761 }
762
763 public static String getPath(String parent, String childPath) {
764 Stack parentStack = new Stack();
765 Stack childStack = new Stack();
766 if (parent != null) {
767 String[] values = parent.split("/");
768 if (values.length > 0) {
769 for (String value : values) {
770 parentStack.push(value);
771 }
772 }
773 }
774 String[] values = childPath.split("/");
775 if (values.length > 0) {
776 for (String value : values) {
777 childStack.push(value);
778 }
779 }
780 String filepath = "";
781 while (!childStack.isEmpty()) {
782 String value = (String)childStack.pop();
783 if ("..".equals(value)) {
784 parentStack.pop();
785 } else if (!"".equals(value)) {
786 if ("".equals(filepath)) {
787 filepath = value;
788 } else {
789 filepath = value + "/" + filepath;
790 }
791 }
792 }
793 while (!parentStack.isEmpty()) {
794 String value = (String)parentStack.pop();
795 if (!"".equals(value)) {
796 filepath = value + "/" + filepath;
797 }
798 }
799 return filepath;
800 }
801
802 /**
803 * Get names of all *.jar files inside the lib/ directory of a given jar URL
804 *
805 * @param url base URL of a JAR to search
806 * @return a List containing file names (Strings) of all files matching "[lL]ib/*.jar"
807 */
808 public static List findLibJars(URL url) {
809 ArrayList embedded_jars = new ArrayList();
810 try {
811 ZipInputStream zin = new ZipInputStream(url.openStream());
812 ZipEntry entry;
813 String entryName;
814 while ((entry = zin.getNextEntry()) != null) {
815 entryName = entry.getName();
816 /**
817 * if the entry name start with /lib and ends with .jar add it
818 * to the the arraylist
819 */
820 if (entryName != null
821 && (entryName.startsWith("lib/") || entryName
822 .startsWith("Lib/"))
823 && entryName.endsWith(".jar")) {
824 embedded_jars.add(entryName);
825 }
826 }
827 zin.close();
828 } catch (Exception e) {
829 throw new RuntimeException(e);
830 }
831 return embedded_jars;
832 }
833
834 /**
835 * Add the Axis2 lifecycle / session methods to a pre-existing list of names that will be
836 * excluded when generating schemas.
837 *
838 * @param excludeList an ArrayList containing method names - we'll add ours to this.
839 */
840 public static void addExcludeMethods(ArrayList excludeList) {
841 excludeList.add("init");
842 excludeList.add("setOperationContext");
843 excludeList.add("startUp");
844 excludeList.add("destroy");
845 excludeList.add("shutDown");
846 }
847
848 public static DeploymentClassLoader createClassLoader(File serviceFile)
849 throws MalformedURLException {
850 ClassLoader contextClassLoader =
851 (ClassLoader)org.apache.axis2.java.security.AccessController
852 .doPrivileged(new PrivilegedAction() {
853 public Object run() {
854 return Thread.currentThread().getContextClassLoader();
855 }
856 });
857 return createDeploymentClassLoader(new URL[]{serviceFile.toURL()},
858 contextClassLoader, new ArrayList());
859 }
860
861 public static ClassLoader createClassLoader(ArrayList urls,
862 ClassLoader serviceClassLoader,
863 boolean extractJars,
864 File tmpDir) {
865 URL url = (URL)urls.get(0);
866 if (extractJars) {
867 try {
868 URL[] urls1 = Utils.getURLsForAllJars(url, tmpDir);
869 urls.remove(0);
870 urls.addAll(0, Arrays.asList(urls1));
871 URL[] urls2 = (URL[])urls.toArray(new URL[urls.size()]);
872 return createDeploymentClassLoader(urls2, serviceClassLoader,
873 null);
874 } catch (Exception e) {
875 log
876 .warn("Exception extracting jars into temporary directory : "
877 + e.getMessage()
878 + " : switching to alternate class loading mechanism");
879 log.debug(e.getMessage(), e);
880 }
881 }
882 List embedded_jars = Utils.findLibJars(url);
883 URL[] urls2 = (URL[])urls.toArray(new URL[urls.size()]);
884 return createDeploymentClassLoader(urls2, serviceClassLoader,
885 embedded_jars);
886 }
887
888 public static File toFile(URL url) throws UnsupportedEncodingException {
889 String path = URLDecoder.decode(url.getPath(), defaultEncoding);
890 return new File(path.replace('/', File.separatorChar).replace('|', ':'));
891 }
892
893 public static ClassLoader createClassLoader(URL[] urls,
894 ClassLoader serviceClassLoader,
895 boolean extractJars,
896 File tmpDir) {
897 if (extractJars) {
898 try {
899 URL[] urls1 = Utils.getURLsForAllJars(urls[0], tmpDir);
900 return createDeploymentClassLoader(urls1, serviceClassLoader,
901 null);
902 } catch (Exception e) {
903 log
904 .warn("Exception extracting jars into temporary directory : "
905 + e.getMessage()
906 + " : switching to alternate class loading mechanism");
907 log.debug(e.getMessage(), e);
908 }
909 }
910 List embedded_jars = Utils.findLibJars(urls[0]);
911 return createDeploymentClassLoader(urls, serviceClassLoader,
912 embedded_jars);
913 }
914
915 private static DeploymentClassLoader createDeploymentClassLoader(
916 final URL[] urls, final ClassLoader serviceClassLoader,
917 final List embeddedJars) {
918 return (DeploymentClassLoader)AccessController
919 .doPrivileged(new PrivilegedAction() {
920 public Object run() {
921 return new DeploymentClassLoader(urls, embeddedJars,
922 serviceClassLoader);
923 }
924 });
925 }
926
927 /**
928 * This method is to process bean exclude parameter and the XML format of that would be
929 * <parameter name="beanPropertyRules"> <bean class="full qualified class name"
930 * excludeProperties="name,age"/>+ </parameter>
931 *
932 * @param service , AxisService object
933 */
934 public static void processBeanPropertyExclude(AxisService service) {
935 Parameter excludeBeanProperty = service
936 .getParameter("beanPropertyRules");
937 if (excludeBeanProperty != null) {
938 OMElement parameterElement = excludeBeanProperty
939 .getParameterElement();
940 Iterator bneasItr = parameterElement.getChildrenWithName(new QName(
941 "bean"));
942 ExcludeInfo excludeInfo = new ExcludeInfo();
943 while (bneasItr.hasNext()) {
944 OMElement bean = (OMElement)bneasItr.next();
945 String clazz = bean.getAttributeValue(new QName(
946 DeploymentConstants.TAG_CLASS_NAME));
947 String excludePropertees = bean.getAttributeValue(new QName(
948 DeploymentConstants.TAG_EXCLUDE_PROPERTIES));
949 String includeProperties = bean.getAttributeValue(new QName(
950 DeploymentConstants.TAG_INCLUDE_PROPERTIES));
951 excludeInfo.putBeanInfo(clazz, new BeanExcludeInfo(
952 excludePropertees, includeProperties));
953 }
954 service.setExcludeInfo(excludeInfo);
955 }
956 }
957
958 public static String getShortFileName(String filename) {
959 File file = new File(filename);
960 return file.getName();
961 }
962
963 /**
964 * The util method to prepare the JSR 181 annotated service name from given annotation or for
965 * defaults JSR 181 specifies that the in javax.jws.WebService the parameter serviceName
966 * contains the wsdl:service name to mapp. If its not available then the default will be Simple
967 * name of the class + "Service"
968 *
969 * @param serviceClass the service Class
970 * @param serviceAnnotation a WebService annotation, or null
971 * @return String version of the ServiceName according to the JSR 181 spec
972 */
973 public static String getAnnotatedServiceName(Class serviceClass, WebService serviceAnnotation) {
974 String serviceName = "";
975 if (serviceAnnotation != null && serviceAnnotation.serviceName() != null) {
976 serviceName = serviceAnnotation.serviceName();
977 }
978 if (serviceName.equals("")) {
979 serviceName = serviceClass.getName();
980 int firstChar = serviceName.lastIndexOf('.') + 1;
981 if (firstChar > 0) {
982 serviceName = serviceName.substring(firstChar);
983 }
984 serviceName += "Service";
985 }
986 return serviceName;
987 }
988
989 public static void addEndpointsToService(AxisService axisService)
990 throws AxisFault {
991
992 String serviceName = axisService.getName();
993 Iterator transportInValues = null;
994
995 if (axisService.isEnableAllTransports()) {
996 AxisConfiguration axisConfiguration = axisService
997 .getAxisConfiguration();
998 if (axisConfiguration != null) {
999 ArrayList transports = new ArrayList();
1000 for (Object o : axisConfiguration.getTransportsIn().values()) {
1001 TransportInDescription transportInDescription = (TransportInDescription)o;
1002 transports.add(transportInDescription.getName());
1003 }
1004 transportInValues = transports.iterator();
1005 }
1006 } else {
1007 transportInValues = axisService.getExposedTransports().iterator();
1008 }
1009
1010 HashMap bindingCache = new HashMap();
1011
1012 if (transportInValues != null) {
1013 for (; transportInValues.hasNext();) {
1014 String transportName = (String)transportInValues.next();
1015 String protocol = transportName.substring(0, 1).toUpperCase()
1016 + transportName.substring(1, transportName.length())
1017 .toLowerCase();
1018
1019 //TODO do we use this method , we need to disable Http, SOAP11,SOAP12
1020 // Bindings according to parameters if we are using this
1021 /*
1022 * populates soap11 endpoint
1023 */
1024 String soap11EndpointName = serviceName + protocol
1025 + "Soap11Endpoint";
1026
1027 AxisEndpoint httpSoap11Endpoint = new AxisEndpoint();
1028 httpSoap11Endpoint.setName(soap11EndpointName);
1029 httpSoap11Endpoint.setParent(axisService);
1030 httpSoap11Endpoint.setTransportInDescription(transportName);
1031 populateSoap11Endpoint(axisService, httpSoap11Endpoint,
1032 bindingCache);
1033 axisService.addEndpoint(httpSoap11Endpoint.getName(),
1034 httpSoap11Endpoint);
1035 // setting soap11 endpoint as the default endpoint
1036 axisService.setEndpointName(soap11EndpointName);
1037
1038 /*
1039 * generating Soap12 endpoint
1040 */
1041 String soap12EndpointName = serviceName + protocol
1042 + "Soap12Endpoint";
1043 AxisEndpoint httpSoap12Endpoint = new AxisEndpoint();
1044 httpSoap12Endpoint.setName(soap12EndpointName);
1045 httpSoap12Endpoint.setParent(axisService);
1046 httpSoap12Endpoint.setTransportInDescription(transportName);
1047 populateSoap12Endpoint(axisService, httpSoap12Endpoint,
1048 bindingCache);
1049 axisService.addEndpoint(httpSoap12Endpoint.getName(),
1050 httpSoap12Endpoint);
1051
1052 /*
1053 * generating Http endpoint
1054 */
1055 if ("http".equals(transportName)) {
1056 String httpEndpointName = serviceName + protocol
1057 + "Endpoint";
1058 AxisEndpoint httpEndpoint = new AxisEndpoint();
1059 httpEndpoint.setName(httpEndpointName);
1060 httpEndpoint.setParent(axisService);
1061 httpEndpoint.setTransportInDescription(transportName);
1062 populateHttpEndpoint(axisService, httpEndpoint, bindingCache);
1063 axisService.addEndpoint(httpEndpoint.getName(),
1064 httpEndpoint);
1065 }
1066 }
1067 }
1068 }
1069
1070 public static void addEndpointsToService(AxisService axisService,
1071 AxisConfiguration axisConfiguration) throws AxisFault {
1072
1073 String serviceName = axisService.getName();
1074 Iterator transportInValues = null;
1075
1076 if (axisConfiguration != null) {
1077 ArrayList transports = new ArrayList();
1078 for (Object o : axisConfiguration.getTransportsIn().values()) {
1079 TransportInDescription transportInDescription = (TransportInDescription)o;
1080 transports.add(transportInDescription.getName());
1081 }
1082 transportInValues = transports.iterator();
1083 }
1084
1085 HashMap bindingCache = new HashMap();
1086 if (transportInValues != null) {
1087 for (; transportInValues.hasNext();) {
1088 String transportName = (String)transportInValues.next();
1089 String protocol = transportName.substring(0, 1).toUpperCase()
1090 + transportName.substring(1, transportName.length())
1091 .toLowerCase();
1092
1093 // axis2.xml indicated no HTTP binding?
1094 boolean disableREST = false;
1095 Parameter disableRESTParameter = axisService
1096 .getParameter(org.apache.axis2.Constants.Configuration.DISABLE_REST);
1097 if (disableRESTParameter != null
1098 && JavaUtils.isTrueExplicitly(disableRESTParameter.getValue())) {
1099 disableREST = true;
1100 }
1101
1102 boolean disableSOAP11 = false;
1103 Parameter disableSOAP11Parameter = axisService
1104 .getParameter(org.apache.axis2.Constants.Configuration.DISABLE_SOAP11);
1105 if (disableSOAP11Parameter != null
1106 && JavaUtils.isTrueExplicitly(disableSOAP11Parameter.getValue())) {
1107 disableSOAP11 = true;
1108 }
1109
1110 boolean disableSOAP12 = false;
1111 Parameter disableSOAP12Parameter = axisService
1112 .getParameter(org.apache.axis2.Constants.Configuration.DISABLE_SOAP12);
1113 if (disableSOAP12Parameter != null
1114 && JavaUtils
1115 .isTrueExplicitly(disableSOAP12Parameter.getValue())) {
1116 disableSOAP12 = true;
1117 }
1118
1119 /*
1120 * populates soap11 endpoint
1121 */
1122 if (!disableSOAP11) {
1123 String soap11EndpointName = serviceName + protocol
1124 + "Soap11Endpoint";
1125
1126 AxisEndpoint httpSoap11Endpoint = new AxisEndpoint();
1127 httpSoap11Endpoint.setName(soap11EndpointName);
1128 httpSoap11Endpoint.setParent(axisService);
1129 httpSoap11Endpoint.setTransportInDescription(transportName);
1130 populateSoap11Endpoint(axisService, httpSoap11Endpoint,
1131 bindingCache);
1132 axisService.addEndpoint(httpSoap11Endpoint.getName(),
1133 httpSoap11Endpoint);
1134 // setting soap11 endpoint as the default endpoint
1135 axisService.setEndpointName(soap11EndpointName);
1136 }
1137
1138 /*
1139 * generating Soap12 endpoint
1140 */
1141 if (!disableSOAP12) {
1142 String soap12EndpointName = serviceName + protocol
1143 + "Soap12Endpoint";
1144 AxisEndpoint httpSoap12Endpoint = new AxisEndpoint();
1145 httpSoap12Endpoint.setName(soap12EndpointName);
1146 httpSoap12Endpoint.setParent(axisService);
1147 httpSoap12Endpoint.setTransportInDescription(transportName);
1148 populateSoap12Endpoint(axisService, httpSoap12Endpoint,
1149 bindingCache);
1150 axisService.addEndpoint(httpSoap12Endpoint.getName(),
1151 httpSoap12Endpoint);
1152 }
1153
1154 /*
1155 * generating Http endpoint
1156 */
1157 if (("http".equals(transportName)
1158 || "https".equals(transportName)) && !disableREST) {
1159 String httpEndpointName = serviceName + protocol
1160 + "Endpoint";
1161 AxisEndpoint httpEndpoint = new AxisEndpoint();
1162 httpEndpoint.setName(httpEndpointName);
1163 httpEndpoint.setParent(axisService);
1164 httpEndpoint.setTransportInDescription(transportName);
1165 populateHttpEndpoint(axisService, httpEndpoint, bindingCache);
1166 axisService.addEndpoint(httpEndpoint.getName(),
1167 httpEndpoint);
1168 }
1169 }
1170 }
1171 }
1172
1173 public static void addSoap11Endpoint(AxisService axisService, URL url)
1174 throws Exception {
1175 String protocol = url.getProtocol();
1176 protocol = protocol.substring(0, 1).toUpperCase()
1177 + protocol.substring(1, protocol.length()).toLowerCase();
1178
1179 String serviceName = axisService.getName();
1180 String soap11EndpointName = serviceName + protocol + "Soap11Endpoint";
1181
1182 AxisEndpoint httpSoap11Endpoint = new AxisEndpoint();
1183 httpSoap11Endpoint.setName(soap11EndpointName);
1184 httpSoap11Endpoint.setParent(axisService);
1185 httpSoap11Endpoint.setEndpointURL(url.toString());
1186 httpSoap11Endpoint.setTransportInDescription(url.getProtocol());
1187
1188 populateSoap11Endpoint(axisService, httpSoap11Endpoint, null);
1189 axisService.addEndpoint(httpSoap11Endpoint.getName(),
1190 httpSoap11Endpoint);
1191 // setting soap11 endpoint as the default endpoint
1192 axisService.setEndpointName(soap11EndpointName);
1193 }
1194
1195 public static void addSoap12Endpoint(AxisService axisService, URL url)
1196 throws Exception {
1197 String protocol = url.getProtocol();
1198 protocol = protocol.substring(0, 1).toUpperCase()
1199 + protocol.substring(1, protocol.length()).toLowerCase();
1200
1201 String serviceName = axisService.getName();
1202 String soap12EndpointName = serviceName + protocol + "Soap12Endpoint";
1203
1204 AxisEndpoint httpSoap12Endpoint = new AxisEndpoint();
1205 httpSoap12Endpoint.setName(soap12EndpointName);
1206 httpSoap12Endpoint.setParent(axisService);
1207 httpSoap12Endpoint.setEndpointURL(url.toString());
1208 httpSoap12Endpoint.setTransportInDescription(url.getProtocol());
1209
1210 populateSoap12Endpoint(axisService, httpSoap12Endpoint, null);
1211 axisService.addEndpoint(httpSoap12Endpoint.getName(),
1212 httpSoap12Endpoint);
1213 }
1214
1215 public static void addHttpEndpoint(AxisService axisService, URL url) {
1216 String serviceName = axisService.getName();
1217 String protocol = url.getProtocol();
1218 protocol = protocol.substring(0, 1).toUpperCase()
1219 + protocol.substring(1, protocol.length()).toLowerCase();
1220
1221 String httpEndpointName = serviceName + protocol + "Endpoint";
1222 AxisEndpoint httpEndpoint = new AxisEndpoint();
1223 httpEndpoint.setName(httpEndpointName);
1224 httpEndpoint.setParent(axisService);
1225 httpEndpoint.setEndpointURL(url.toString());
1226 httpEndpoint.setTransportInDescription(url.getProtocol());
1227 populateHttpEndpoint(axisService, httpEndpoint, null);
1228 axisService.addEndpoint(httpEndpoint.getName(), httpEndpoint);
1229 }
1230
1231 public static void processPolicyAttachments(Iterator attachmentElements,
1232 AxisService service) throws XMLStreamException,
1233 FactoryConfigurationError {
1234 OMElement attachmentElement;
1235 HashMap attachmentsMap = new HashMap();
1236
1237 for (; attachmentElements.hasNext();) {
1238 attachmentElement = (OMElement)attachmentElements.next();
1239 OMElement appliesToElem = attachmentElement
1240 .getFirstChildWithName(new QName(
1241 DeploymentConstants.POLICY_NS_URI,
1242 DeploymentConstants.TAG_APPLIES_TO));
1243 ArrayList policyComponents = new ArrayList();
1244
1245 // process <wsp:Policy> elements ..
1246 for (Iterator policyElements = attachmentElement
1247 .getChildrenWithName(new QName(
1248 DeploymentConstants.POLICY_NS_URI,
1249 DeploymentConstants.TAG_POLICY)); policyElements
1250 .hasNext();) {
1251 PolicyComponent policy = PolicyUtil
1252 .getPolicyFromOMElement((OMElement)policyElements
1253 .next());
1254 policyComponents.add(policy);
1255 }
1256
1257 // process <wsp:PolicyReference> elements ..
1258 for (Iterator policyRefElements = attachmentElement
1259 .getChildrenWithName(new QName(
1260 DeploymentConstants.POLICY_NS_URI,
1261 DeploymentConstants.TAG_POLICY_REF)); policyRefElements
1262 .hasNext();) {
1263 PolicyComponent policyRef = PolicyUtil
1264 .getPolicyReferenceFromOMElement((OMElement)policyRefElements
1265 .next());
1266 policyComponents.add(policyRef);
1267 }
1268
1269 for (Iterator policySubjects = appliesToElem
1270 .getChildrenWithName(new QName("policy-subject")); policySubjects
1271 .hasNext();) {
1272 OMElement policySubject = (OMElement)policySubjects.next();
1273 String identifier = policySubject.getAttributeValue(new QName(
1274 "identifier"));
1275
1276 ArrayList values = (ArrayList)attachmentsMap.get(identifier);
1277 if (values == null) {
1278 values = new ArrayList();
1279 attachmentsMap.put(identifier, values);
1280 }
1281 values.addAll(policyComponents);
1282 }
1283 }
1284
1285 for (Object o : attachmentsMap.keySet()) {
1286 String identifier = (String)o;
1287 if (identifier.startsWith("binding:soap")) {
1288 processSoapAttachments(identifier, (List)attachmentsMap
1289 .get(identifier), service);
1290 }
1291 }
1292 }
1293
1294 private static void populateSoap11Endpoint(AxisService axisService,
1295 AxisEndpoint axisEndpoint, HashMap bindingCache) {
1296 String serviceName = axisService.getName();
1297 String name = serviceName + "Soap11Binding";
1298
1299 QName bindingName = new QName(name);
1300
1301 AxisBinding axisBinding = (bindingCache != null) ? (AxisBinding)bindingCache
1302 .get(name)
1303 : null;
1304 if (axisBinding == null) {
1305 axisBinding = new AxisBinding();
1306 axisBinding.setName(bindingName);
1307
1308 axisBinding.setType(Java2WSDLConstants.TRANSPORT_URI);
1309 axisBinding.setProperty(WSDLConstants.WSDL_1_1_STYLE,
1310 WSDLConstants.STYLE_DOC);
1311
1312 axisBinding.setProperty(WSDL2Constants.ATTR_WSOAP_VERSION,
1313 SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
1314
1315 for (Iterator iterator = axisService.getChildren(); iterator
1316 .hasNext();) {
1317 AxisOperation operation = (AxisOperation)iterator.next();
1318 AxisBindingOperation axisBindingOperation = new AxisBindingOperation();
1319
1320 axisBindingOperation.setName(operation.getName());
1321 axisBindingOperation.setAxisOperation(operation);
1322
1323 String soapAction = operation.getSoapAction();
1324 if (soapAction != null) {
1325 axisBindingOperation.setProperty(
1326 WSDL2Constants.ATTR_WSOAP_ACTION, soapAction);
1327 }
1328 axisBinding.addChild(axisBindingOperation.getName(),
1329 axisBindingOperation);
1330 populateBindingOperation(axisBinding,
1331 axisBindingOperation);
1332 }
1333 if (bindingCache != null) {
1334 bindingCache.put(name, axisBinding);
1335 }
1336 }
1337 axisBinding.setParent(axisEndpoint);
1338 axisEndpoint.setBinding(axisBinding);
1339 }
1340
1341 private static void populateSoap12Endpoint(AxisService axisService,
1342 AxisEndpoint axisEndpoint, HashMap bindingCache) {
1343 String serviceName = axisService.getName();
1344 String name = serviceName + "Soap12Binding";
1345
1346 QName bindingName = new QName(name);
1347
1348 AxisBinding axisBinding = (bindingCache != null) ? (AxisBinding)bindingCache
1349 .get(name)
1350 : null;
1351 if (axisBinding == null) {
1352 axisBinding = new AxisBinding();
1353 axisBinding.setName(bindingName);
1354
1355 axisBinding.setType(Java2WSDLConstants.TRANSPORT_URI);
1356 axisBinding.setProperty(WSDLConstants.WSDL_1_1_STYLE,
1357 WSDLConstants.STYLE_DOC);
1358
1359 axisBinding.setProperty(WSDL2Constants.ATTR_WSOAP_VERSION,
1360 SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
1361
1362 for (Iterator iterator = axisService.getChildren(); iterator
1363 .hasNext();) {
1364 AxisOperation operation = (AxisOperation)iterator.next();
1365 AxisBindingOperation axisBindingOperation = new AxisBindingOperation();
1366
1367 axisBindingOperation.setName(operation.getName());
1368 axisBindingOperation.setAxisOperation(operation);
1369
1370 String soapAction = operation.getSoapAction();
1371 if (soapAction != null) {
1372 axisBindingOperation.setProperty(
1373 WSDL2Constants.ATTR_WSOAP_ACTION, soapAction);
1374 }
1375 axisBinding.addChild(axisBindingOperation.getName(),
1376 axisBindingOperation);
1377
1378 populateBindingOperation(axisBinding,
1379 axisBindingOperation);
1380 }
1381 if (bindingCache != null) {
1382 bindingCache.put(name, axisBinding);
1383 }
1384 }
1385 axisBinding.setParent(axisEndpoint);
1386 axisEndpoint.setBinding(axisBinding);
1387 }
1388
1389 private static void populateHttpEndpoint(AxisService axisService,
1390 AxisEndpoint axisEndpoint, HashMap bindingCache) {
1391 String serviceName = axisService.getName();
1392 String name = serviceName + "HttpBinding";
1393
1394 QName bindingName = new QName(name);
1395
1396 AxisBinding axisBinding = (bindingCache != null) ? (AxisBinding)bindingCache
1397 .get(name)
1398 : null;
1399
1400 if (axisBinding == null) {
1401 axisBinding = new AxisBinding();
1402 axisBinding.setName(bindingName);
1403
1404 axisBinding.setType(WSDL2Constants.URI_WSDL2_HTTP);
1405 axisBinding.setProperty(WSDL2Constants.ATTR_WHTTP_METHOD, "POST");
1406
1407 for (Iterator iterator = axisService.getChildren(); iterator
1408 .hasNext();) {
1409 AxisOperation operation = (AxisOperation)iterator.next();
1410 AxisBindingOperation axisBindingOperation = new AxisBindingOperation();
1411
1412 QName operationQName = operation.getName();
1413 axisBindingOperation.setName(operationQName);
1414 axisBindingOperation.setAxisOperation(operation);
1415 String httpLocation = operationQName.getLocalPart();
1416 axisBindingOperation.setProperty(WSDL2Constants.ATTR_WHTTP_LOCATION, httpLocation);
1417 axisBinding.addChild(axisBindingOperation.getName(),
1418 axisBindingOperation);
1419
1420 populateBindingOperation(axisBinding,
1421 axisBindingOperation);
1422 }
1423 if (bindingCache != null) {
1424 bindingCache.put(name, axisBinding);
1425 }
1426 }
1427 axisBinding.setParent(axisEndpoint);
1428 axisEndpoint.setBinding(axisBinding);
1429 }
1430
1431 private static void populateBindingOperation(AxisBinding axisBinding,
1432 AxisBindingOperation axisBindingOperation) {
1433
1434 AxisOperation axisOperation = axisBindingOperation.getAxisOperation();
1435
1436 if (WSDLUtil.isInputPresentForMEP(axisOperation
1437 .getMessageExchangePattern())) {
1438 AxisMessage axisInMessage = axisOperation
1439 .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
1440 AxisBindingMessage axisBindingInMessage = new AxisBindingMessage();
1441
1442 axisBindingInMessage.setName(axisInMessage.getName());
1443 axisBindingInMessage.setDirection(axisInMessage.getDirection());
1444 axisBindingInMessage.setAxisMessage(axisInMessage);
1445
1446 axisBindingInMessage.setParent(axisBindingOperation);
1447 axisBindingOperation.addChild(WSDLConstants.MESSAGE_LABEL_IN_VALUE,
1448 axisBindingInMessage);
1449 }
1450
1451 if (WSDLUtil.isOutputPresentForMEP(axisOperation
1452 .getMessageExchangePattern())) {
1453 AxisMessage axisOutMessage = axisOperation
1454 .getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
1455 AxisBindingMessage axisBindingOutMessage = new AxisBindingMessage();
1456
1457 axisBindingOutMessage.setName(axisOutMessage.getName());
1458 axisBindingOutMessage.setDirection(axisOutMessage.getDirection());
1459 axisBindingOutMessage.setAxisMessage(axisOutMessage);
1460
1461 axisBindingOutMessage.setParent(axisBindingOperation);
1462 axisBindingOperation.addChild(
1463 WSDLConstants.MESSAGE_LABEL_OUT_VALUE,
1464 axisBindingOutMessage);
1465 }
1466
1467 ArrayList faultMessagesList = axisOperation.getFaultMessages();
1468 for (Object aFaultMessagesList : faultMessagesList) {
1469 AxisMessage axisFaultMessage = (AxisMessage)aFaultMessagesList;
1470 AxisBindingMessage axisBindingFaultMessage = new AxisBindingMessage();
1471 axisBindingFaultMessage.setName(axisFaultMessage.getName());
1472 axisBindingFaultMessage.setFault(true);
1473 axisBindingFaultMessage.setAxisMessage(axisFaultMessage);
1474 axisBindingFaultMessage.setParent(axisBindingOperation);
1475 axisBindingOperation.addFault(axisBindingFaultMessage);
1476 axisBinding.addFault(axisBindingFaultMessage);
1477 }
1478
1479 axisBindingOperation.setAxisOperation(axisOperation);
1480 axisBindingOperation.setParent(axisBinding);
1481 }
1482
1483 private static void processSoapAttachments(String identifier,
1484 List policyComponents, AxisService service) {
1485 Map map = service.getEndpoints();
1486 String soapVersion =
1487 (identifier.indexOf("soap12") > -1) ? SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI
1488 : SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI;
1489
1490 for (Object o : map.values()) {
1491 AxisEndpoint axisEndpoint = (AxisEndpoint)o;
1492 AxisBinding axisBinding = axisEndpoint.getBinding();
1493 String wsoap = (String)axisBinding
1494 .getProperty(WSDL2Constants.ATTR_WSOAP_VERSION);
1495 if (soapVersion.equals(wsoap)) {
1496 String[] identifiers = identifier.split("/");
1497 int key = identifiers.length;
1498 if (key == 1) {
1499 axisBinding.getPolicySubject().attachPolicyComponents(
1500 policyComponents);
1501 } else if (key == 2 || key == 3) {
1502 String opName = identifiers[1];
1503 opName = opName.substring(opName.indexOf(":") + 1, opName
1504 .length());
1505 AxisBindingOperation bindingOperation = null;
1506 boolean found = false;
1507 for (Iterator i = axisBinding.getChildren(); i.hasNext();) {
1508 bindingOperation = (AxisBindingOperation)i.next();
1509 if (opName.equals(bindingOperation.getName()
1510 .getLocalPart())) {
1511 found = true;
1512 break;
1513 }
1514 }
1515 if (!found) {
1516 throw new IllegalArgumentException(
1517 "Invalid binding operation " + opName);
1518 }
1519
1520 if (key == 2) {
1521 bindingOperation.getPolicySubject()
1522 .attachPolicyComponents(policyComponents);
1523 } else {
1524 if ("in".equals(identifiers[2])) {
1525 AxisBindingMessage bindingInMessage =
1526 (AxisBindingMessage)bindingOperation
1527 .getChild(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
1528 bindingInMessage.getPolicySubject()
1529 .attachPolicyComponents(policyComponents);
1530
1531 } else if ("out".equals(identifiers[2])) {
1532 AxisBindingMessage bindingOutMessage =
1533 (AxisBindingMessage)bindingOperation
1534 .getChild(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
1535 bindingOutMessage.getPolicySubject()
1536 .attachPolicyComponents(policyComponents);
1537 } else {
1538 // FIXME faults
1539 }
1540 }
1541 }
1542 break;
1543 }
1544 }
1545 }
1546
1547 public static boolean isSoap11Binding(AxisBinding binding) {
1548 String type = binding.getType();
1549 if (Java2WSDLConstants.TRANSPORT_URI.equals(type)
1550 || WSDL2Constants.URI_WSDL2_SOAP.equals(type)) {
1551 String v = (String)binding
1552 .getProperty(WSDL2Constants.ATTR_WSOAP_VERSION);
1553 if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(v)) {
1554 return true;
1555 }
1556 }
1557 return false;
1558 }
1559
1560 public static boolean isSoap12Binding(AxisBinding binding) {
1561 String type = binding.getType();
1562 if (Java2WSDLConstants.TRANSPORT_URI.equals(type)
1563 || WSDL2Constants.URI_WSDL2_SOAP.equals(type)) {
1564 String v = (String)binding
1565 .getProperty(WSDL2Constants.ATTR_WSOAP_VERSION);
1566 if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(v)) {
1567 return true;
1568 }
1569 }
1570 return false;
1571 }
1572
1573 public static boolean isHttpBinding(AxisBinding binding) {
1574 String type = binding.getType();
1575 return WSDL2Constants.URI_WSDL2_HTTP.equals(type);
1576 }
1577
1578 public static AxisBinding getSoap11Binding(AxisService service) {
1579 for (Object o : service.getEndpoints().values()) {
1580 AxisEndpoint endpoint = (AxisEndpoint)o;
1581 AxisBinding binding = endpoint.getBinding();
1582
1583 if (isSoap11Binding(binding)) {
1584 return binding;
1585 }
1586 }
1587 return null;
1588 }
1589
1590 public static AxisBinding getSoap12Binding(AxisService service) {
1591 for (Object o : service.getEndpoints().values()) {
1592 AxisEndpoint endpoint = (AxisEndpoint)o;
1593 AxisBinding binding = endpoint.getBinding();
1594
1595 if (isSoap12Binding(binding)) {
1596 return binding;
1597 }
1598 }
1599 return null;
1600 }
1601
1602 public static AxisBinding getHttpBinding(AxisService service) {
1603 for (Object o : service.getEndpoints().values()) {
1604 AxisEndpoint endpoint = (AxisEndpoint)o;
1605 AxisBinding binding = endpoint.getBinding();
1606
1607 if (isHttpBinding(binding)) {
1608 return binding;
1609 }
1610 }
1611 return null;
1612 }
1613
1614 public static AxisBindingOperation getBindingOperation(AxisBinding binding,
1615 AxisOperation operation) {
1616 QName opName = operation.getName();
1617 for (Iterator bindingOps = binding.getChildren(); bindingOps.hasNext();) {
1618 AxisBindingOperation bindingOp = (AxisBindingOperation)bindingOps.next();
1619 if (opName.equals(bindingOp.getName())) {
1620 return bindingOp;
1621 }
1622 }
1623 return null;
1624 }
1625
1626 public static AxisBindingMessage getBindingMessage(AxisBindingOperation bindingOperation,
1627 AxisMessage message) {
1628 String msgName = message.getName();
1629 for (Iterator bindingMessages = bindingOperation.getChildren();
1630 bindingMessages.hasNext();) {
1631 AxisBindingMessage bindingMessage = (AxisBindingMessage)bindingMessages.next();
1632 if (msgName.equals(bindingMessage.getName())) {
1633 return bindingMessage;
1634 }
1635 }
1636 return null;
1637 }
1638 }