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.binding.file.util;
030:
031: import com.sun.jbi.binding.file.FileBindingResources;
032: import com.sun.jbi.binding.file.FileBindingContext;
033: import com.sun.jbi.wsdl2.Binding;
034: import com.sun.jbi.wsdl2.Description;
035: import com.sun.jbi.wsdl2.Endpoint;
036: import com.sun.jbi.wsdl2.WsdlFactory;
037:
038: import org.w3c.dom.DocumentFragment;
039: import org.w3c.dom.NamedNodeMap;
040: import org.w3c.dom.Node;
041:
042: import java.util.logging.Logger;
043:
044: /**
045: * This class is uses to validate the configuration file supplied during
046: * deployment conforms to the schema.
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public final class WSDLFileValidator extends UtilBase implements
051: FileBindingResources {
052: /**
053: * Description object
054: */
055: private static Description sDefinition;
056:
057: /**
058: * Inbound / Outbound
059: */
060: private String mCurValue = "";
061:
062: /**
063: * Name of the file to parse.
064: */
065: private String mFileName;
066:
067: /**
068: * Translator for i18n messages.
069: */
070: private StringTranslator mTranslator;
071:
072: /**
073: * Creates a new WSDLFileValidator object.
074: *
075: * @param wsdlfile schema file name
076: */
077: public WSDLFileValidator(String wsdlfile) {
078: mFileName = wsdlfile;
079: mTranslator = new StringTranslator();
080: }
081:
082: /**
083: * Returns the document object obtained as a result of parsing.
084: *
085: * @return document object
086: */
087: public Description getDocument() {
088: return sDefinition;
089: }
090:
091: /**
092: * This method has to be invoked to check the validity of the input
093: * document.
094: */
095: public void validate() {
096: parse();
097:
098: if (isValid()) {
099: validateEndpoints();
100: }
101: }
102:
103: /**
104: * Checks if a binging is file binding
105: *
106: * @param df Document fragment
107: *
108: * @return true if file binding
109: */
110: private boolean isFileBinding(DocumentFragment df) {
111: NamedNodeMap n = df.getFirstChild().getAttributes();
112:
113: for (int g = 0; g < n.getLength(); g++) {
114: if (n.item(g).getLocalName().equals(
115: ConfigData.WSDL_BINDING_TYPE)) {
116: if (n.item(g).getNodeValue().equals(
117: ConfigData.FILENAMESPACE)) {
118: return true;
119: }
120: }
121: }
122:
123: return false;
124: }
125:
126: /**
127: * Checks the Endpoint.
128: *
129: * @param ep endpoint
130: */
131: private void checkEndpoint(Endpoint ep) {
132: DocumentFragment docfrag = ep.toXmlDocumentFragment();
133:
134: Node epnode = docfrag.getFirstChild();
135: NamedNodeMap epattributes = epnode.getAttributes();
136: checkRequired(epattributes, ConfigData.ENDPOINT_TYPE);
137:
138: if (mCurValue.trim().equalsIgnoreCase(
139: ConfigData.CONSUMER_STRING)) {
140: checkRequired(epattributes, ConfigData.WSDL_INPUT_LOCATION);
141: checkRequired(epattributes,
142: ConfigData.WSDL_PROCESSED_LOCATION);
143: }
144:
145: checkRequired(epattributes, ConfigData.WSDL_OUTPUT_LOCATION);
146:
147: if (!isValid()) {
148: setError("\n\tEndpoint : " + ep.getName() + getError());
149: }
150: }
151:
152: /**
153: * Checks the required attributes.
154: *
155: * @param map Node map
156: * @param attr attributes that has to be checked.
157: */
158: private void checkRequired(NamedNodeMap map, String attr) {
159: Node attrnode = map.getNamedItemNS(ConfigData.FILENAMESPACE,
160: attr);
161:
162: if ((attrnode == null)) {
163: setValid(false);
164: setError(getError() + "\n\t" + "Required attribute \""
165: + attr + "\" not found");
166:
167: return;
168: }
169:
170: try {
171: mCurValue = attrnode.getNodeValue().trim();
172:
173: if (mCurValue.equals("")) {
174: setValid(false);
175: setError(getError() + "\n\t" + "Required attribute \""
176: + attr
177: + "\" has to have a value, cannot be null");
178: }
179: } catch (Exception e) {
180: setValid(false);
181: setError(getError() + "\n\t"
182: + "Cannot get attribute value of " + attr);
183: setException(e);
184: }
185: }
186:
187: /**
188: * Parses the input XML file.
189: */
190: private void parse() {
191: try {
192: com.sun.jbi.component.ComponentContext ctx = (com.sun.jbi.component.ComponentContext) FileBindingContext
193: .getInstance().getContext();
194: WsdlFactory wsdlFactory = ctx.getWsdlFactory();
195: com.sun.jbi.wsdl2.WsdlReader wsdlRdr = wsdlFactory
196: .newWsdlReader();
197: sDefinition = wsdlRdr.readDescription(mFileName);
198: } catch (Exception e) {
199: setError("WSDL file " + mFileName + " parse error, reason "
200: + e.getMessage());
201: setException(e);
202: setValid(false);
203: e.printStackTrace();
204: }
205: }
206:
207: /**
208: * Validates the endpoints.
209: */
210: private void validateEndpoints() {
211: int services = sDefinition.getServicesLength();
212: boolean found = false;
213:
214: for (int i = 0; i < services; i++) {
215: try {
216: com.sun.jbi.wsdl2.Service ser = sDefinition
217: .getService(i);
218: int endpoints = ser.getEndpointsLength();
219:
220: for (int k = 0; k < endpoints; k++) {
221: Endpoint ep = ser.getEndpoint(k);
222: Binding b = ep.getBinding();
223:
224: if (!isFileBinding(b.toXmlDocumentFragment())) {
225: continue;
226: }
227:
228: found = true;
229: checkEndpoint(ep);
230:
231: if (!isValid()) {
232: setError("\n\tBinding " + " : " + b.getName()
233: + getError());
234:
235: return;
236: }
237: }
238: } catch (Exception e) {
239: setError(mTranslator.getString(FBC_WSDL_ENDPOINT_ERROR,
240: e.getMessage()));
241: setException(e);
242: setValid(false);
243: e.printStackTrace();
244:
245: return;
246: }
247: }
248:
249: if (!found) {
250: setError(getError()
251: + mTranslator.getString(FBC_WSDL_NO_ENDPOINTS));
252: setValid(false);
253: }
254: }
255: }
|