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.jms.deploy;
030:
031: import com.sun.jbi.StringTranslator;
032: import com.sun.jbi.binding.jms.JMSBindingContext;
033: import com.sun.jbi.binding.jms.JMSBindingResources;
034: import com.sun.jbi.binding.jms.JMSConstants;
035: import com.sun.jbi.binding.jms.config.ConfigConstants;
036: import com.sun.jbi.binding.jms.util.UtilBase;
037:
038: import com.sun.jbi.wsdl2.Binding;
039: import com.sun.jbi.wsdl2.Description;
040: import com.sun.jbi.wsdl2.Endpoint;
041: import com.sun.jbi.wsdl2.WsdlFactory;
042:
043: import com.sun.jbi.wsdl2.Interface;
044: import com.sun.jbi.wsdl2.InterfaceOperation;
045: import com.sun.jbi.wsdl2.BindingOperation;
046:
047: import org.w3c.dom.DocumentFragment;
048: import org.w3c.dom.NamedNodeMap;
049: import org.w3c.dom.Node;
050:
051: import java.util.logging.Logger;
052:
053: import javax.xml.namespace.QName;
054:
055: /**
056: * This class is uses to validate the configuration file supplied during
057: * deployment conforms to the schema.
058: *
059: * @author Sun Microsystems Inc.
060: */
061: public final class WSDLFileValidator extends
062: com.sun.jbi.binding.jms.util.UtilBase implements
063: JMSBindingResources {
064: /**
065: * Description object.
066: */
067: private static Description sDefinition;
068:
069: /**
070: * Logger object.
071: */
072: private Logger mLogger;
073: /**
074: * Current value.
075: */
076: private String mCurValue = "";
077:
078: /**
079: * Name of the file to parse.
080: */
081: private String mFileName;
082:
083: /**
084: * Translator for i18n messages.
085: */
086: private StringTranslator mStringTranslator;
087:
088: /**
089: * Creates a new WSDLFileValidator object.
090: *
091: * @param wsdlfile schema file name.
092: */
093: public WSDLFileValidator(String wsdlfile) {
094: mFileName = wsdlfile;
095: mLogger = JMSBindingContext.getInstance().getLogger();
096: mStringTranslator = JMSBindingContext.getInstance()
097: .getStringTranslator();
098: }
099:
100: /**
101: * Returns the document object obtained as a result of parsing.
102: *
103: * @return document object.
104: */
105: public Description getDocument() {
106: return sDefinition;
107: }
108:
109: /**
110: * This method has to be invoked to check the validity of the input
111: * document.
112: */
113: public void validate() {
114: parse();
115:
116: if (isValid()) {
117: validateEndpoints();
118: }
119:
120: mLogger.info(mStringTranslator.getString(JMS_CONFIG_FILE_CHECK)
121: + isValid());
122: }
123:
124: /**
125: * Checks if the binding is a JMS binding.
126: *
127: * @param df document fragment.
128: *
129: * @return true if JMS binding.
130: */
131: private boolean isJMSBinding(DocumentFragment df) {
132: NamedNodeMap n = df.getFirstChild().getAttributes();
133:
134: for (int g = 0; g < n.getLength(); g++) {
135: if (n.item(g).getLocalName().equals("type")) {
136: if (n.item(g).getNodeValue().equals(
137: ConfigConstants.JMS_NAMESPACE)) {
138: return true;
139: }
140: }
141: }
142:
143: return false;
144: }
145:
146: /**
147: * Gets the role.
148: *
149: * @param map map object.
150: *
151: * @return role.
152: */
153: private int getRole(NamedNodeMap map) {
154: Node attrnode = map.getNamedItemNS(
155: ConfigConstants.JMS_NAMESPACE,
156: ConfigConstants.ENDPOINT_TYPE);
157:
158: if ((attrnode == null)) {
159: setError(mStringTranslator.getString(
160: JMS_REQUIRED_ATTR_ERROR,
161: ConfigConstants.ENDPOINT_TYPE));
162:
163: return -1;
164: }
165:
166: try {
167: mCurValue = attrnode.getNodeValue().trim();
168:
169: if (mCurValue.equals("")) {
170: setError(mStringTranslator.getString(
171: JMS_REQUIRED_ATTR_ERROR,
172: ConfigConstants.ENDPOINT_TYPE));
173:
174: return -1;
175: }
176: } catch (Exception e) {
177: setError(mStringTranslator.getString(
178: JMS_REQUIRED_ATTR_ERROR,
179: ConfigConstants.ENDPOINT_TYPE));
180: setException(e);
181: }
182:
183: if (mCurValue.trim().equalsIgnoreCase(
184: ConfigConstants.CONSUMER_STRING)) {
185: return ConfigConstants.CONSUMER;
186: } else if (mCurValue.trim().equalsIgnoreCase(
187: ConfigConstants.PROVIDER_STRING)) {
188: return ConfigConstants.PROVIDER;
189: } else {
190: return -1;
191: }
192: }
193:
194: /**
195: * Checks the endpoint for valid information.
196: *
197: * @param ep endpoint.
198: */
199: private void checkEndpoint(Endpoint ep) {
200: DocumentFragment docfrag = ep.toXmlDocumentFragment();
201:
202: Node epnode = docfrag.getFirstChild();
203: NamedNodeMap epattributes = epnode.getAttributes();
204: checkRequired(epattributes, ConfigConstants.DESTINATION_NAME);
205: checkRequired(epattributes, ConfigConstants.DESTINATION_STYLE);
206: checkRequired(epattributes, ConfigConstants.CONNECTION_FACTORY);
207:
208: // checkRequired(epattributes, ConfigConstants.ENDPOINT_TYPE);
209: int role = getRole(epattributes);
210:
211: if (role == -1) {
212: setError(mStringTranslator.getString(JMS_INVALID_ROLE));
213: } else if (role == ConfigConstants.PROVIDER) {
214: checkRequired(epattributes, ConfigConstants.TIME_TO_LIVE);
215: } else if (role == ConfigConstants.CONSUMER) {
216: mLogger.fine("Role is consumer");
217: }
218:
219: if (!isValid()) {
220: setError("\n\tEndpoint : " + ep.getName());
221: }
222: }
223:
224: /**
225: * Checks the madatory information.
226: *
227: * @param map map object.
228: * @param attr attribute to be checked.
229: */
230: private void checkRequired(NamedNodeMap map, String attr) {
231: Node attrnode = map.getNamedItemNS(
232: ConfigConstants.JMS_NAMESPACE, attr);
233:
234: if ((attrnode == null)) {
235:
236: setError(mStringTranslator.getString(
237: JMS_REQUIRED_ATTR_ERROR, attr));
238:
239: return;
240: }
241:
242: try {
243: mCurValue = attrnode.getNodeValue().trim();
244:
245: if (mCurValue.equals("")) {
246: setError(mStringTranslator.getString(
247: JMS_REQUIRED_ATTR_ERROR, attr));
248: }
249: } catch (Exception e) {
250: setError(mStringTranslator.getString(
251: JMS_REQUIRED_ATTR_ERROR, attr));
252: setException(e);
253: }
254: }
255:
256: /**
257: * Finds if endpoint is provider or consumer.
258: *
259: * @param ep endpoint.
260: *
261: * @return provider or consumer.
262: */
263: private int findDirection(Endpoint ep) {
264: Binding b = ep.getBinding();
265: Interface bindingif = b.getInterface();
266:
267: for (int oper = 0; oper < b.getOperationsLength(); oper++) {
268: BindingOperation boper = b.getOperation(oper);
269: QName bindingopername = boper.getInterfaceOperation();
270: int ifoperlength = bindingif.getOperationsLength();
271:
272: for (int j = 0; j < ifoperlength; j++) {
273: InterfaceOperation op = bindingif.getOperation(j);
274: String pat = op.getPattern();
275:
276: if (pat.trim().equals(ConfigConstants.IN_OUT)
277: || pat.trim().equals(ConfigConstants.IN_ONLY)
278: || pat.trim().equals(
279: ConfigConstants.ROBUST_IN_ONLY)
280: || pat.trim().equals(
281: ConfigConstants.IN_OPTIONAL_OUT)) {
282: return 1;
283: } else if (pat.trim().equals(ConfigConstants.OUT_IN)
284: || pat.trim().equals(ConfigConstants.OUT_ONLY)
285: || pat.trim().equals(
286: ConfigConstants.ROBUST_OUT_ONLY)
287: || pat.trim().equals(
288: ConfigConstants.OUT_OPTIONAL_IN)) {
289: return 0;
290: }
291: }
292: }
293:
294: return -1;
295: }
296:
297: /**
298: * Parses the input XML file.
299: */
300: private void parse() {
301: try {
302: com.sun.jbi.component.ComponentContext ctx = (com.sun.jbi.component.ComponentContext) JMSBindingContext
303: .getInstance().getContext();
304: WsdlFactory wsdlFactory = ctx.getWsdlFactory();
305: com.sun.jbi.wsdl2.WsdlReader wsdlRdr = wsdlFactory
306: .newWsdlReader();
307: sDefinition = wsdlRdr.readDescription(mFileName);
308: } catch (Exception e) {
309: setError("WSDL file " + mFileName + " parse error, reason "
310: + e.getMessage());
311: setException(e);
312: e.printStackTrace();
313: }
314: }
315:
316: /**
317: * Validates the endpoints.
318: */
319: private void validateEndpoints() {
320: int services = sDefinition.getServicesLength();
321: boolean found = false;
322:
323: for (int i = 0; i < services; i++) {
324: try {
325: com.sun.jbi.wsdl2.Service ser = sDefinition
326: .getService(i);
327: int endpoints = ser.getEndpointsLength();
328:
329: for (int k = 0; k < endpoints; k++) {
330: Endpoint ep = ser.getEndpoint(k);
331: Binding b = ep.getBinding();
332:
333: if ((b == null)
334: || (!isJMSBinding(b.toXmlDocumentFragment()))) {
335: continue;
336: }
337:
338: found = true;
339: checkEndpoint(ep);
340:
341: if (!isValid()) {
342: setError("\n\tBinding " + " : " + b.getName());
343:
344: return;
345: }
346: }
347: } catch (Exception e) {
348: setError("Error occurred during endpoint validation "
349: + e.getMessage());
350: setException(e);
351: e.printStackTrace();
352:
353: return;
354: }
355: }
356:
357: if (!found) {
358: setError(mStringTranslator.getString(JMS_NO_ENDPOINTS));
359: }
360: }
361: }
|