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