001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)WSDL11FileValidator.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.file.util;
030:
031: import com.sun.jbi.binding.file.FileBindingResources;
032:
033: import org.w3c.dom.DocumentFragment;
034: import org.w3c.dom.NamedNodeMap;
035: import org.w3c.dom.Node;
036: import org.w3c.dom.Element;
037:
038: import java.util.logging.Logger;
039:
040: import javax.wsdl.Binding;
041: import javax.wsdl.Definition;
042: import javax.wsdl.OperationType;
043: import javax.wsdl.Port;
044: import javax.wsdl.PortType;
045: import javax.wsdl.extensions.UnknownExtensibilityElement;
046: import javax.wsdl.factory.WSDLFactory;
047: import javax.wsdl.xml.WSDLReader;
048:
049: import java.util.Collection;
050: import java.util.Map;
051: import java.util.Iterator;
052: import java.util.LinkedList;
053: import java.util.List;
054:
055: import javax.xml.namespace.QName;
056:
057: /**
058: * This class is uses to validate the configuration file supplied during
059: * deployment conforms to the schema.
060: *
061: * @author Sun Microsystems, Inc.
062: */
063: public final class WSDL11FileValidator extends UtilBase implements
064: FileBindingResources {
065: /**
066: * Definitions object
067: */
068: private static Definition sDefinition;
069:
070: /**
071: * Inbound / Outbound
072: */
073: private String mCurValue = "";
074:
075: /**
076: * Name of the file to parse.
077: */
078: private String mFileName;
079:
080: /**
081: * Translator for i18n messages.
082: */
083: private StringTranslator mTranslator;
084:
085: /**
086: * Creates a new WSDL11FileValidator object.
087: *
088: * @param wsdlfile schema file name
089: */
090: public WSDL11FileValidator(String wsdlfile) {
091: mFileName = wsdlfile;
092: mTranslator = new StringTranslator();
093: }
094:
095: /**
096: * Returns the document object obtained as a result of parsing.
097: *
098: * @return document object
099: */
100: public Definition getDocument() {
101: return sDefinition;
102: }
103:
104: /**
105: * This method has to be invoked to check the validity of the input
106: * document.
107: */
108: public void validate() {
109: parse();
110:
111: if (isValid()) {
112: validateEndpoints();
113: }
114: }
115:
116: /**
117: * Checks if a binging is file binding
118: *
119: * @param df Document fragment
120: *
121: * @return true if file binding
122: */
123: private boolean isFileBinding(Binding aBinding) {
124: java.util.List extList = aBinding.getExtensibilityElements();
125: List boper = aBinding.getBindingOperations();
126: if (extList != null) {
127: for (int i = 0; i < extList.size(); i++) {
128: if (extList.get(i) instanceof javax.wsdl.extensions.UnknownExtensibilityElement) {
129: UnknownExtensibilityElement sb = (UnknownExtensibilityElement) extList
130: .get(i);
131:
132: if ((sb.getElementType().getNamespaceURI()
133: .equals(ConfigData.FILENAMESPACE))
134: && (sb.getElementType().getLocalPart()
135: .equals("binding"))) {
136: return true;
137: }
138: }
139: }
140: }
141: return false;
142: }
143:
144: /**
145: * Checks the Endpoint.
146: *
147: * @param ep endpoint
148: */
149: private void checkEndpoint(Port ep) {
150: List extList = ep.getExtensibilityElements();
151:
152: if (extList != null) {
153: for (int i = 0; i < extList.size(); i++) {
154: if (extList.get(i) instanceof javax.wsdl.extensions.UnknownExtensibilityElement) {
155: UnknownExtensibilityElement sb = (UnknownExtensibilityElement) extList
156: .get(i);
157:
158: if (sb.getElementType().getNamespaceURI().equals(
159: ConfigData.FILENAMESPACE)
160: && sb.getElementType().getLocalPart()
161: .equals("artifacts")) {
162: Element epnode = sb.getElement();
163: NamedNodeMap epattributes = epnode
164: .getAttributes();
165: checkRequired(epattributes,
166: ConfigData.ENDPOINT_TYPE);
167: if (mCurValue.trim().equalsIgnoreCase(
168: ConfigData.CONSUMER_STRING)) {
169: checkRequired(epattributes,
170: ConfigData.WSDL_INPUT_LOCATION);
171: checkRequired(epattributes,
172: ConfigData.WSDL_PROCESSED_LOCATION);
173: }
174: checkRequired(epattributes,
175: ConfigData.WSDL_OUTPUT_LOCATION);
176: }
177: }
178: }
179: if (!isValid()) {
180: setError("\n\tEndpoint : " + ep.getName() + getError());
181: }
182: }
183: }
184:
185: /**
186: * Checks the required attributes.
187: *
188: * @param map Node map
189: * @param attr attributes that has to be checked.
190: */
191: private void checkRequired(NamedNodeMap map, String attr) {
192: Node attrnode = map.getNamedItem(attr);
193:
194: if ((attrnode == null)) {
195: setValid(false);
196: setError(getError() + "\n\t" + "Required attribute \""
197: + attr + "\" not found");
198:
199: return;
200: }
201:
202: try {
203: mCurValue = attrnode.getNodeValue().trim();
204:
205: if (mCurValue.equals("")) {
206: setValid(false);
207: setError(getError() + "\n\t" + "Required attribute \""
208: + attr
209: + "\" has to have a value, cannot be null");
210: }
211: } catch (Exception e) {
212: setValid(false);
213: setError(getError() + "\n\t"
214: + "Cannot get attribute value of " + attr);
215: setException(e);
216: }
217: }
218:
219: /**
220: * Parses the input XML file.
221: */
222: private void parse() {
223: try {
224: javax.wsdl.factory.WSDLFactory mFactory = WSDLFactory
225: .newInstance();
226: javax.wsdl.xml.WSDLReader mReader = mFactory
227: .newWSDLReader();
228: sDefinition = mReader.readWSDL(null, mFileName);
229: } catch (Exception e) {
230: setError("WSDL file " + mFileName + " parse error, reason "
231: + e.getMessage());
232: setException(e);
233: setValid(false);
234: e.printStackTrace();
235: }
236: }
237:
238: /**
239: * Validates the endpoints.
240: */
241: private void validateEndpoints() {
242: boolean found = false;
243:
244: java.util.Map servicemap = sDefinition.getServices();
245: List eplist = new LinkedList();
246: Collection services = servicemap.values();
247:
248: Iterator iter = services.iterator();
249:
250: while (iter.hasNext()) {
251: try {
252: javax.wsdl.Service service = (javax.wsdl.Service) iter
253: .next();
254:
255: Map endpointsmap = service.getPorts();
256: Collection endpoints = endpointsmap.values();
257:
258: Iterator epiter = endpoints.iterator();
259:
260: while (epiter.hasNext()) {
261: javax.wsdl.Port ep = (javax.wsdl.Port) epiter
262: .next();
263:
264: javax.wsdl.Binding b = ep.getBinding();
265:
266: if (!isFileBinding(b)) {
267: continue;
268: }
269:
270: found = true;
271: checkEndpoint(ep);
272:
273: if (!isValid()) {
274: setError("\n\tBinding " + " : " + b.getQName()
275: + getError());
276:
277: return;
278: }
279: }
280: } catch (Exception e) {
281: setError(mTranslator.getString(FBC_WSDL_ENDPOINT_ERROR,
282: e.getMessage()));
283: setException(e);
284: setValid(false);
285: e.printStackTrace();
286:
287: return;
288: }
289: }
290:
291: if (!found) {
292: setError(getError()
293: + mTranslator.getString(FBC_WSDL_NO_ENDPOINTS));
294: setValid(false);
295: }
296: }
297: }
|