001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.deployment.repository.util;
021:
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.OMNamespace;
024: import org.apache.axis2.AxisFault;
025: import org.apache.axis2.context.ConfigurationContext;
026: import org.apache.axis2.deployment.DeploymentConstants;
027: import org.apache.axis2.deployment.DeploymentErrorMsgs;
028: import org.apache.axis2.deployment.DeploymentException;
029: import org.apache.axis2.deployment.DescriptionBuilder;
030: import org.apache.axis2.deployment.ModuleBuilder;
031: import org.apache.axis2.deployment.ServiceBuilder;
032: import org.apache.axis2.deployment.ServiceGroupBuilder;
033: import org.apache.axis2.deployment.resolver.AARBasedWSDLLocator;
034: import org.apache.axis2.deployment.resolver.AARFileBasedURIResolver;
035: import org.apache.axis2.deployment.resolver.WarBasedWSDLLocator;
036: import org.apache.axis2.deployment.resolver.WarFileBasedURIResolver;
037: import org.apache.axis2.description.*;
038: import org.apache.axis2.engine.AxisConfiguration;
039: import org.apache.axis2.i18n.Messages;
040: import org.apache.axis2.namespace.Constants;
041: import org.apache.axis2.util.XMLUtils;
042: import org.apache.axis2.util.Utils;
043: import org.apache.commons.logging.Log;
044: import org.apache.commons.logging.LogFactory;
045:
046: import javax.xml.stream.XMLStreamException;
047: import java.io.ByteArrayInputStream;
048: import java.io.ByteArrayOutputStream;
049: import java.io.File;
050: import java.io.FileInputStream;
051: import java.io.FileNotFoundException;
052: import java.io.IOException;
053: import java.io.InputStream;
054: import java.util.ArrayList;
055: import java.util.HashMap;
056: import java.util.List;
057: import java.util.zip.ZipEntry;
058: import java.util.zip.ZipInputStream;
059:
060: public class ArchiveReader implements DeploymentConstants {
061: private static final Log log = LogFactory
062: .getLog(ArchiveReader.class);
063:
064: public ArrayList buildServiceGroup(InputStream zin,
065: DeploymentFileData currentFile,
066: AxisServiceGroup axisServiceGroup, HashMap wsdlServices,
067: ConfigurationContext configCtx) throws XMLStreamException,
068: AxisFault {
069:
070: DescriptionBuilder builder = new DescriptionBuilder(zin,
071: configCtx);
072: OMElement rootElement = builder.buildOM();
073: String elementName = rootElement.getLocalName();
074:
075: if (TAG_SERVICE.equals(elementName)) {
076: AxisService axisService = null;
077: String serviceName = DescriptionBuilder
078: .getShortFileName(currentFile.getName());
079: if (serviceName != null) {
080: axisService = (AxisService) wsdlServices
081: .get(serviceName);
082: }
083: if (axisService == null) {
084: axisService = (AxisService) wsdlServices
085: .get(DescriptionBuilder
086: .getShortFileName(currentFile.getName()));
087: }
088: if (axisService == null) {
089: axisService = new AxisService(serviceName);
090: } else {
091: axisService.setWsdlFound(true);
092: axisService.setCustomWsdl(true);
093: }
094:
095: axisService.setParent(axisServiceGroup);
096: axisService.setClassLoader(currentFile.getClassLoader());
097:
098: ServiceBuilder serviceBuilder = new ServiceBuilder(
099: configCtx, axisService);
100: serviceBuilder.setWsdlServiceMap(wsdlServices);
101: AxisService service = serviceBuilder
102: .populateService(rootElement);
103:
104: ArrayList serviceList = new ArrayList();
105: serviceList.add(service);
106: return serviceList;
107: } else if (TAG_SERVICE_GROUP.equals(elementName)) {
108: ServiceGroupBuilder groupBuilder = new ServiceGroupBuilder(
109: rootElement, wsdlServices, configCtx);
110: return groupBuilder.populateServiceGroup(axisServiceGroup);
111: }
112: throw new AxisFault("Invalid services.xml found");
113: }
114:
115: /**
116: * Extracts Service XML files and builds the service groups.
117: *
118: * @param filename
119: * @param axisServiceGroup
120: * @param extractService
121: * @param wsdls
122: * @param configCtx
123: * @return Returns ArrayList.
124: * @throws DeploymentException
125: */
126: public ArrayList processServiceGroup(String filename,
127: DeploymentFileData currentFile,
128: AxisServiceGroup axisServiceGroup, boolean extractService,
129: HashMap wsdls, ConfigurationContext configCtx)
130: throws AxisFault {
131: // get attribute values
132: if (!extractService) {
133: ZipInputStream zin = null;
134: FileInputStream fin = null;
135: try {
136: fin = new FileInputStream(filename);
137: zin = new ZipInputStream(fin);
138: ZipEntry entry;
139: while ((entry = zin.getNextEntry()) != null) {
140: if (entry.getName().equalsIgnoreCase(SERVICES_XML)) {
141: axisServiceGroup
142: .setServiceGroupName(DescriptionBuilder
143: .getShortFileName(currentFile
144: .getName()));
145: return buildServiceGroup(zin, currentFile,
146: axisServiceGroup, wsdls, configCtx);
147: }
148: }
149: throw new DeploymentException(Messages.getMessage(
150: DeploymentErrorMsgs.SERVICE_XML_NOT_FOUND,
151: filename));
152: } catch (Exception e) {
153: throw new DeploymentException(e);
154: } finally {
155: if (zin != null) {
156: try {
157: zin.close();
158: } catch (IOException e) {
159: log.info(Messages
160: .getMessage("errorininputstreamclose"));
161: }
162: }
163: if (fin != null) {
164: try {
165: fin.close();
166: } catch (IOException e) {
167: log.info(Messages
168: .getMessage("errorininputstreamclose"));
169: }
170: }
171: }
172: } else {
173: File file = new File(filename, SERVICES_XML);
174: if (!file.exists()) {
175: // try for meta-inf
176: file = new File(filename, SERVICES_XML.toLowerCase());
177: }
178: if (file.exists()) {
179: InputStream in = null;
180: try {
181: in = new FileInputStream(file);
182: axisServiceGroup.setServiceGroupName(currentFile
183: .getName());
184: return buildServiceGroup(in, currentFile,
185: axisServiceGroup, wsdls, configCtx);
186: } catch (FileNotFoundException e) {
187: throw new DeploymentException(Messages.getMessage(
188: DeploymentErrorMsgs.FILE_NOT_FOUND, e
189: .getMessage()));
190: } catch (XMLStreamException e) {
191: throw new DeploymentException(Messages.getMessage(
192: DeploymentErrorMsgs.XML_STREAM_EXCEPTION, e
193: .getMessage()));
194: } finally {
195: if (in != null) {
196: try {
197: in.close();
198: } catch (IOException e) {
199: log
200: .info(Messages
201: .getMessage("errorininputstreamclose"));
202: }
203: }
204: }
205: } else {
206: throw new DeploymentException(
207: Messages
208: .getMessage(DeploymentErrorMsgs.SERVICE_XML_NOT_FOUND));
209: }
210: }
211: }
212:
213: /**
214: * Creats AxisService.
215: *
216: * @param in
217: * @return Returns AxisService.
218: * @throws DeploymentException
219: */
220: private List processWSDLFile(
221: WSDLToAxisServiceBuilder axisServiceBuilder,
222: File serviceArchiveFile, boolean isArchive, InputStream in,
223: String baseURI) throws DeploymentException {
224: try {
225:
226: if (serviceArchiveFile != null && isArchive) {
227: axisServiceBuilder
228: .setCustomResolver(new AARFileBasedURIResolver(
229: serviceArchiveFile));
230: if (axisServiceBuilder instanceof WSDL11ToAllAxisServicesBuilder) {
231:
232: ((WSDL11ToAllAxisServicesBuilder) axisServiceBuilder)
233: .setCustomWSLD4JResolver(new AARBasedWSDLLocator(
234: baseURI, serviceArchiveFile, in));
235: } else if (axisServiceBuilder instanceof WSDL20ToAllAxisServicesBuilder) {
236: // trying to use the jar scheme as the base URI. I think this can be used to handle
237: // wsdl 1.1 as well without using a custom URI resolver. Need to look at it later.
238: axisServiceBuilder.setBaseUri("jar:file://"
239: + serviceArchiveFile.toURI() + "!/"
240: + baseURI);
241: }
242: } else {
243: if (serviceArchiveFile != null) {
244: axisServiceBuilder.setBaseUri(serviceArchiveFile
245: .getParentFile().toURI().toString());
246: }
247: }
248: if (axisServiceBuilder instanceof WSDL11ToAllAxisServicesBuilder) {
249: return ((WSDL11ToAllAxisServicesBuilder) axisServiceBuilder)
250: .populateAllServices();
251: } else if (axisServiceBuilder instanceof WSDL20ToAllAxisServicesBuilder) {
252: return ((WSDL20ToAllAxisServicesBuilder) axisServiceBuilder)
253: .populateAllServices();
254: }
255: } catch (AxisFault axisFault) {
256: log.info("Trouble processing wsdl file :"
257: + axisFault.getMessage());
258: if (log.isDebugEnabled()) {
259: log.debug(axisFault);
260: }
261: }
262: return null;
263: }
264:
265: /**
266: * Creates service objects from wsdl file inside a service archive file.
267: *
268: * @param file <code>ArchiveFileData</code>
269: * @throws DeploymentException <code>DeploymentException</code>
270: */
271: public HashMap processWSDLs(DeploymentFileData file)
272: throws DeploymentException {
273: File serviceFile = file.getFile();
274: // to store service come from wsdl files
275: HashMap servicesMap = new HashMap();
276: boolean isDirectory = serviceFile.isDirectory();
277: if (isDirectory) {
278: try {
279: File metaInfFolder = new File(serviceFile, META_INF);
280:
281: if (!metaInfFolder.exists()) {
282: metaInfFolder = new File(serviceFile, META_INF
283: .toLowerCase());
284: if (!metaInfFolder.exists()) {
285: throw new DeploymentException(
286: Messages
287: .getMessage(
288: DeploymentErrorMsgs.META_INF_MISSING,
289: serviceFile.getName()));
290: }
291: }
292:
293: processFilesInFolder(metaInfFolder, servicesMap);
294:
295: } catch (FileNotFoundException e) {
296: throw new DeploymentException(e);
297: } catch (IOException e) {
298: throw new DeploymentException(e);
299: } catch (XMLStreamException e) {
300: throw new DeploymentException(e);
301: }
302: } else {
303: ZipInputStream zin;
304: FileInputStream fin;
305: try {
306: fin = new FileInputStream(serviceFile);
307: zin = new ZipInputStream(fin);
308:
309: //TODO Check whether this WSDL is empty
310:
311: ZipEntry entry;
312: byte[] buf = new byte[1024];
313: int read;
314: ByteArrayOutputStream out;
315: while ((entry = zin.getNextEntry()) != null) {
316: String entryName = entry.getName().toLowerCase();
317: if (entryName.startsWith(META_INF.toLowerCase())
318: && entryName.endsWith(SUFFIX_WSDL)) {
319: out = new ByteArrayOutputStream();
320:
321: // we do not want to generate the services for the
322: // imported wsdl of one file.
323: if ((entryName.indexOf("/") != entryName
324: .lastIndexOf("/"))
325: || (entryName.indexOf("wsdl_") != -1)) {
326: //only care abt the toplevel wsdl
327: continue;
328: }
329:
330: while ((read = zin.read(buf)) > 0) {
331: out.write(buf, 0, read);
332: }
333:
334: ByteArrayInputStream in = new ByteArrayInputStream(
335: out.toByteArray());
336:
337: // now the question is which version of WSDL file this archive contains.
338: // lets check the namespace of the root element and decide. But since we are
339: // using axiom (dude, you are becoming handy here :)), we will not build the
340: // whole thing.
341: OMNamespace documentElementNS = ((OMElement) XMLUtils
342: .toOM(in)).getNamespace();
343: if (documentElementNS != null) {
344: WSDLToAxisServiceBuilder wsdlToAxisServiceBuilder;
345: if (WSDL2Constants.WSDL_NAMESPACE
346: .equals(documentElementNS
347: .getNamespaceURI())) {
348: // we have a WSDL 2.0 document here.
349: wsdlToAxisServiceBuilder = new WSDL20ToAllAxisServicesBuilder(
350: new ByteArrayInputStream(out
351: .toByteArray()));
352: wsdlToAxisServiceBuilder
353: .setBaseUri(entryName);
354: } else if (Constants.NS_URI_WSDL11
355: .equals(documentElementNS
356: .getNamespaceURI())) {
357: wsdlToAxisServiceBuilder = new WSDL11ToAllAxisServicesBuilder(
358: new ByteArrayInputStream(out
359: .toByteArray()));
360: } else {
361: throw new DeploymentException(Messages
362: .getMessage("invalidWSDLFound"));
363: }
364: List services = processWSDLFile(
365: wsdlToAxisServiceBuilder,
366: serviceFile, true,
367: new ByteArrayInputStream(out
368: .toByteArray()), entry
369: .getName());
370: if (services != null) {
371: for (int i = 0; i < services.size(); i++) {
372: AxisService axisService = (AxisService) services
373: .get(i);
374: if (axisService != null) {
375: servicesMap
376: .put(axisService
377: .getName(),
378: axisService);
379: }
380: }
381: }
382: }
383: }
384: }
385: try {
386: zin.close();
387: } catch (IOException e) {
388: log.info(e);
389: }
390: try {
391: fin.close();
392: } catch (IOException e) {
393: log.info(e);
394: }
395: } catch (FileNotFoundException e) {
396: throw new DeploymentException(e);
397: } catch (IOException e) {
398: throw new DeploymentException(e);
399: } catch (XMLStreamException e) {
400: throw new DeploymentException(e);
401: }
402: }
403: return servicesMap;
404: }
405:
406: public List getAxisServiceFromWsdl(InputStream in,
407: ClassLoader loader, String wsdlUrl) throws Exception {
408: // ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
409:
410: // now the question is which version of WSDL file this archive contains.
411: // lets check the namespace of the root element and decide. But since we are
412: // using axiom (dude, you are becoming handy here :)), we will not build the
413: // whole thing.
414: OMElement element = (OMElement) XMLUtils.toOM(in);
415: OMNamespace documentElementNS = element.getNamespace();
416: if (documentElementNS != null) {
417: WSDL11ToAllAxisServicesBuilder wsdlToAxisServiceBuilder;
418: ByteArrayOutputStream out = new ByteArrayOutputStream();
419: element.serialize(out);
420: if (Constants.NS_URI_WSDL11.equals(documentElementNS
421: .getNamespaceURI())) {
422: wsdlToAxisServiceBuilder = new WSDL11ToAllAxisServicesBuilder(
423: new ByteArrayInputStream(out.toByteArray()));
424: wsdlToAxisServiceBuilder
425: .setCustomWSLD4JResolver(new WarBasedWSDLLocator(
426: wsdlUrl, loader,
427: new ByteArrayInputStream(out
428: .toByteArray())));
429: wsdlToAxisServiceBuilder
430: .setCustomResolver(new WarFileBasedURIResolver(
431: loader));
432: return wsdlToAxisServiceBuilder.populateAllServices();
433: } else {
434: throw new DeploymentException(Messages
435: .getMessage("invalidWSDLFound"));
436: }
437: }
438: return null;
439: }
440:
441: public void processFilesInFolder(File folder, HashMap servicesMap)
442: throws FileNotFoundException, XMLStreamException,
443: DeploymentException {
444: File files[] = folder.listFiles();
445: for (int i = 0; i < files.length; i++) {
446: File file1 = files[i];
447: if (file1.getName().toLowerCase().endsWith(SUFFIX_WSDL)) {
448: InputStream in = new FileInputStream(file1);
449: FileInputStream in2;
450:
451: // now the question is which version of WSDL file this archive contains.
452: // lets check the namespace of the root element and decide. But since we are
453: // using axiom (dude, you are becoming handy here :)), we will not build the
454: // whole thing.
455: OMNamespace documentElementNS = ((OMElement) XMLUtils
456: .toOM(in)).getNamespace();
457: if (documentElementNS != null) {
458: WSDLToAxisServiceBuilder wsdlToAxisServiceBuilder;
459: if (WSDL2Constants.WSDL_NAMESPACE
460: .equals(documentElementNS.getNamespaceURI())) {
461: // we have a WSDL 2.0 document here.
462: in2 = new FileInputStream(file1);
463: wsdlToAxisServiceBuilder = new WSDL20ToAllAxisServicesBuilder(
464: in2);
465: } else if (Constants.NS_URI_WSDL11
466: .equals(documentElementNS.getNamespaceURI())) {
467: in2 = new FileInputStream(file1);
468: wsdlToAxisServiceBuilder = new WSDL11ToAllAxisServicesBuilder(
469: in2);
470: } else {
471: throw new DeploymentException(Messages
472: .getMessage("invalidWSDLFound"));
473: }
474:
475: FileInputStream in3 = new FileInputStream(file1);
476: List services = processWSDLFile(
477: wsdlToAxisServiceBuilder, file1, false,
478: in2, file1.toURI().toString());
479:
480: if (services != null) {
481: for (int j = 0; j < services.size(); j++) {
482: AxisService axisService = (AxisService) services
483: .get(j);
484: if (axisService != null) {
485: servicesMap.put(axisService.getName(),
486: axisService);
487: }
488: }
489: }
490: try {
491: in2.close();
492: in3.close();
493: } catch (IOException e) {
494: log.info(e);
495: }
496: }
497: try {
498: in.close();
499: } catch (IOException e) {
500: log.info(e);
501: }
502: }
503: }
504: }
505:
506: public void readModuleArchive(DeploymentFileData deploymentFile,
507: AxisModule module, boolean explodedDir,
508: AxisConfiguration axisConfig) throws DeploymentException {
509:
510: // get attribute values
511: boolean moduleXMLFound = false;
512: String shortFileName = DescriptionBuilder
513: .getShortFileName(deploymentFile.getName());
514: if (!explodedDir) {
515: ZipInputStream zin;
516: FileInputStream fin;
517: try {
518: fin = new FileInputStream(deploymentFile
519: .getAbsolutePath());
520: zin = new ZipInputStream(fin);
521: ZipEntry entry;
522: while ((entry = zin.getNextEntry()) != null) {
523: if (entry.getName().equalsIgnoreCase(MODULE_XML)) {
524: moduleXMLFound = true;
525: ModuleBuilder builder = new ModuleBuilder(zin,
526: module, axisConfig);
527: // setting module name
528: module.setName(Utils
529: .getModuleName(shortFileName));
530: module.setVersion(Utils
531: .getModuleVersion(shortFileName));
532: builder.populateModule();
533: break;
534: }
535: }
536: zin.close();
537: fin.close();
538: if (!moduleXMLFound) {
539: throw new DeploymentException(Messages.getMessage(
540: DeploymentErrorMsgs.MODULE_XML_MISSING,
541: deploymentFile.getAbsolutePath()));
542: }
543: } catch (Exception e) {
544: throw new DeploymentException(e);
545: }
546: } else {
547: File file = new File(deploymentFile.getAbsolutePath(),
548: MODULE_XML);
549:
550: if (file.exists()
551: || (file = new File(deploymentFile
552: .getAbsolutePath(), MODULE_XML
553: .toLowerCase())).exists()) {
554: InputStream in = null;
555: try {
556: in = new FileInputStream(file);
557: ModuleBuilder builder = new ModuleBuilder(in,
558: module, axisConfig);
559: // setting module name
560: module.setName(Utils.getModuleName(shortFileName));
561: module.setVersion(Utils
562: .getModuleVersion(shortFileName));
563: builder.populateModule();
564: } catch (FileNotFoundException e) {
565: throw new DeploymentException(Messages.getMessage(
566: DeploymentErrorMsgs.FILE_NOT_FOUND, e
567: .getMessage()));
568: } finally {
569: if (in != null) {
570: try {
571: in.close();
572: } catch (IOException e) {
573: log
574: .info(Messages
575: .getMessage("errorininputstreamclose"));
576: }
577: }
578: }
579: } else {
580: throw new DeploymentException(Messages.getMessage(
581: DeploymentErrorMsgs.MODULE_XML_MISSING,
582: deploymentFile.getAbsolutePath()));
583: }
584: }
585: }
586: }
|