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: * @(#)WSDLFileValidator.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.engine.xslt.util;
030:
031: import com.sun.jbi.wsdl2.Binding;
032: import com.sun.jbi.wsdl2.BindingOperation;
033: import com.sun.jbi.wsdl2.Description;
034: import com.sun.jbi.wsdl2.Endpoint;
035: import com.sun.jbi.wsdl2.Interface;
036: import com.sun.jbi.wsdl2.InterfaceOperation;
037: import com.sun.jbi.wsdl2.WsdlFactory;
038:
039: import org.w3c.dom.DocumentFragment;
040: import org.w3c.dom.NamedNodeMap;
041: import org.w3c.dom.Node;
042:
043: import java.io.File;
044:
045: import java.util.logging.Logger;
046:
047: import javax.xml.namespace.QName;
048:
049: import com.sun.jbi.engine.xslt.TransformationEngineContext;
050:
051: /**
052: * This class is uses to validate the configuration file supplied during
053: * deployment conforms to the schema.
054: *
055: * @author Sun Microsystems, Inc.
056: */
057: public final class WSDLFileValidator extends UtilBase {
058: /**
059: * Description object
060: */
061: private static Description sDefinition;
062:
063: /**
064: * Logger object.
065: */
066: private Logger mLog;
067:
068: /**
069: *
070: */
071: private String mCurValue = "";
072:
073: /**
074: * Name of the file to parse.
075: */
076: private String mFileName;
077:
078: /**
079: * Translator for i18n messages.
080: */
081: private StringTranslator mStringTranslator;
082:
083: /**
084: * Creates a new WSDLFileValidator object.
085: *
086: * @param supath Service Unit path
087: * @param wsdlFileName WSDL 2.0 deployment file name
088: */
089: public WSDLFileValidator(String supath, String wsdlFileName) {
090: mFileName = wsdlFileName;
091: //mFileName = supath + File.separatorChar + ConfigData.WSDL_FILE_NAME;
092: //mFileName = supath + File.separatorChar + wsdlFileName;
093:
094: mLog = TransformationEngineContext.getInstance().getLogger("");
095: mStringTranslator = new StringTranslator(
096: "com.sun.jbi.engine.xslt", this .getClass()
097: .getClassLoader());
098:
099: mLog.finer("WSDL 20 depl file name: " + mFileName);
100: }
101:
102: /**
103: * Returns the document object obtained as a result of parsing.
104: *
105: * @return document object
106: */
107: public Description getDocument() {
108: return sDefinition;
109: }
110:
111: /**
112: * DOCUMENT ME!
113: */
114: /**
115: * This method has to be invoked to check the validity of the input
116: * document.
117: */
118: public void validate() {
119: parse();
120:
121: if (isValid()) {
122: validateEndpoints();
123: }
124:
125: //mLog.info(mStringTranslator.getString(CONFIG_FILE_CHECK) + isValid());
126: mLog.info("Checking WSDL file: " + isValid());
127: }
128:
129: /**
130: * DOCUMENT ME!
131: *
132: * @param df NOT YET DOCUMENTED
133: *
134: * @return NOT YET DOCUMENTED
135: */
136: private boolean isTEBinding(DocumentFragment df) {
137: NamedNodeMap n = df.getFirstChild().getAttributes();
138:
139: for (int g = 0; g < n.getLength(); g++) {
140: if (n.item(g).getLocalName().equals("type")) {
141: if (n.item(g).getNodeValue().equals(
142: ConfigData.SERVICE_ENGINE_TYPE)) {
143: mLog.info("Found TE Endpoint.");
144:
145: return true;
146: }
147: }
148: }
149:
150: return false;
151: }
152:
153: /**
154: * DOCUMENT ME!
155: *
156: * @param ep NOT YET DOCUMENTED
157: */
158: private void checkEndpoint(Endpoint ep) {
159: DocumentFragment docfrag = ep.toXmlDocumentFragment();
160:
161: Node epnode = docfrag.getFirstChild();
162: NamedNodeMap epattributes = epnode.getAttributes();
163: int direction = findDirection(ep);
164:
165: if (direction == 0) {
166: mLog.info("Inbound");
167: }
168:
169: if (!isValid()) {
170: setError("\n\tEndpoint : " + ep.getName() + getError());
171: }
172: }
173:
174: /**
175: * DOCUMENT ME!
176: *
177: * @param map NOT YET DOCUMENTED
178: * @param attr NOT YET DOCUMENTED
179: */
180: private void checkRequired(NamedNodeMap map, String attr) {
181: Node attrnode = map.getNamedItemNS(
182: ConfigData.SERVICE_ENGINE_TYPE, attr);
183:
184: if ((attrnode == null)) {
185: setValid(false);
186: setError(getError() + "\n\t" + "Required attribute \""
187: + attr + "\" not found");
188:
189: return;
190: }
191:
192: try {
193: mCurValue = attrnode.getNodeValue().trim();
194:
195: if (mCurValue.equals("")) {
196: setValid(false);
197: setError(getError() + "\n\t" + "Required attribute \""
198: + attr
199: + "\" has to have a value, cannot be null");
200: }
201: } catch (Exception e) {
202: setValid(false);
203: setError(getError() + "\n\t"
204: + "Cannot get attribute value of " + attr);
205: setException(e);
206: }
207: }
208:
209: /**
210: * DOCUMENT ME!
211: *
212: * @param ep NOT YET DOCUMENTED
213: *
214: * @return NOT YET DOCUMENTED
215: */
216: private int findDirection(Endpoint ep) {
217: Binding b = ep.getBinding();
218: Interface bindingif = b.getInterface();
219:
220: for (int oper = 0; oper < b.getOperationsLength(); oper++) {
221: BindingOperation boper = b.getOperation(oper);
222: QName bindingopername = boper.getInterfaceOperation();
223: int ifoperlength = bindingif.getOperationsLength();
224:
225: for (int j = 0; j < ifoperlength; j++) {
226: InterfaceOperation op = bindingif.getOperation(j);
227: String pat = op.getPattern();
228:
229: if (pat.trim().equals(ConfigData.IN_OUT)
230: || pat.trim().equals(ConfigData.IN_ONLY)
231: || pat.trim().equals(ConfigData.ROBUST_IN_ONLY)
232: || pat.trim()
233: .equals(ConfigData.IN_OPTIONAL_OUT)) {
234: return 0;
235: } else {
236: return 1;
237: }
238: }
239: }
240:
241: return -1;
242: }
243:
244: /**
245: * Parses the input WSDL file.
246: */
247: private void parse() {
248: try {
249: com.sun.jbi.component.ComponentContext ctx = (com.sun.jbi.component.ComponentContext) TransformationEngineContext
250: .getInstance().getContext();
251: WsdlFactory wsdlFactory = ctx.getWsdlFactory();
252: com.sun.jbi.wsdl2.WsdlReader wsdlRdr = wsdlFactory
253: .newWsdlReader();
254: sDefinition = wsdlRdr.readDescription(mFileName);
255: } catch (Exception e) {
256: setError("WSDL file " + mFileName + " parse error, reason "
257: + e.getMessage());
258: setException(e);
259: setValid(false);
260: e.printStackTrace();
261: }
262: }
263:
264: /**
265: *
266: */
267: private void validateEndpoints() {
268: int services = sDefinition.getServicesLength();
269: boolean found = false;
270:
271: for (int i = 0; i < services; i++) {
272: try {
273: com.sun.jbi.wsdl2.Service ser = sDefinition
274: .getService(i);
275:
276: //mLog.info("Service s = "+ser.toXmlString());
277: int endpoints = ser.getEndpointsLength();
278:
279: for (int k = 0; k < endpoints; k++) {
280: Endpoint ep = ser.getEndpoint(k);
281:
282: //mLog.info("Endpoint = "+ep.toXmlString());
283: Binding b = ep.getBinding();
284:
285: //mLog.info("Binding b= "+b.toXmlString());
286: if (b == null) {
287: //mLog.info("NULL Document Fragment. Binding.");
288: mLog.info("NULL Binding Document Fragment. ");
289:
290: continue;
291: }
292:
293: if (!isTEBinding(b.toXmlDocumentFragment())) {
294: continue;
295: }
296:
297: found = true;
298:
299: //checkEndpoint(ep);
300: if (!isValid()) {
301: setError("\n\tBinding " + " : " + b.getName()
302: + getError());
303:
304: return;
305: }
306: }
307: } catch (Exception e) {
308: setError("Error occurred during endpoint validation "
309: + e.getMessage());
310: setException(e);
311: setValid(false);
312:
313: return;
314: }
315: }
316:
317: if (!found) {
318: setError(getError()
319: + "None of endpoints can be supported by TE");
320: setValid(false);
321: }
322: }
323: }
|