001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.soap.wsdl;
030:
031: import com.caucho.log.Log;
032: import com.caucho.util.L10N;
033: import com.caucho.vfs.Vfs;
034:
035: import javax.jws.WebService;
036:
037: import javax.xml.bind.JAXBContext;
038: import javax.xml.bind.JAXBException;
039: import javax.xml.bind.Unmarshaller;
040:
041: import javax.xml.ws.WebServiceException;
042:
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.net.URL;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048:
049: /**
050: * WSDL Parser
051: */
052: public class WSDLParser {
053: private final static Logger log = Log.open(WSDLParser.class);
054: private final static L10N L = new L10N(WSDLParser.class);
055: private static JAXBContext _context = null;
056:
057: public static WSDLDefinitions parse(Class api)
058: throws WebServiceException {
059: WebService service = (WebService) api
060: .getAnnotation(WebService.class);
061:
062: if (service == null) {
063: if (log.isLoggable(Level.FINER))
064: log.finer("No @WebService found on " + api);
065:
066: return null;
067: }
068:
069: String wsdlLocation = null;
070:
071: if (!"".equals(service.wsdlLocation())) {
072: wsdlLocation = service.wsdlLocation();
073: } else if (!"".equals(service.endpointInterface())) {
074: try {
075: ClassLoader loader = api.getClassLoader();
076: Class ei = loader
077: .loadClass(service.endpointInterface());
078:
079: service = (WebService) api
080: .getAnnotation(WebService.class);
081:
082: if (service == null) {
083: if (log.isLoggable(Level.FINER))
084: log.finer("No @WebService found on " + api);
085:
086: return null;
087: }
088:
089: if (!"".equals(service.wsdlLocation()))
090: wsdlLocation = service.wsdlLocation();
091: } catch (ClassNotFoundException e) {
092: }
093: }
094:
095: if (wsdlLocation == null) {
096: if (log.isLoggable(Level.FINER))
097: log.finer("No WSDL location found on " + api);
098:
099: return null;
100: }
101:
102: return parse(wsdlLocation);
103: }
104:
105: public static WSDLDefinitions parse(URL wsdlLocation)
106: throws WebServiceException {
107: return parse(wsdlLocation.toString());
108: }
109:
110: public static WSDLDefinitions parse(String wsdlLocation)
111: throws WebServiceException {
112: InputStream is = null;
113:
114: if (log.isLoggable(Level.FINER))
115: log.finer("Getting WSDL: " + wsdlLocation);
116:
117: try {
118: is = Vfs.openRead(wsdlLocation);
119:
120: return parse(is);
121: } catch (IOException e) {
122: throw new WebServiceException(L.l(
123: "Unable to download WSDL: {0}", wsdlLocation), e);
124: } catch (JAXBException e) {
125: throw new WebServiceException(L.l(
126: "Unable to parse WSDL: {0}", wsdlLocation), e);
127: } finally {
128: try {
129: if (is != null)
130: is.close();
131: } catch (IOException e) {
132: }
133: }
134: }
135:
136: /**
137: * Starts a WSDL element.
138: */
139: public static WSDLDefinitions parse(InputStream is)
140: throws IOException, JAXBException {
141: try {
142: if (_context == null) {
143: _context = JAXBContext
144: .newInstance("com.caucho.soap.wsdl:"
145: + "com.caucho.xml.schema");
146: }
147:
148: Unmarshaller unmarshaller = _context.createUnmarshaller();
149: return (WSDLDefinitions) unmarshaller.unmarshal(is);
150: } catch (JAXBException e) {
151: throw e;
152: } catch (Exception e) {
153: throw new JAXBException(e);
154: }
155: }
156: }
|