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: * @(#)ConfigReader.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:
033: import com.sun.jbi.binding.jms.EndpointBean;
034: import com.sun.jbi.binding.jms.JMSBindingContext;
035: import com.sun.jbi.binding.jms.JMSBindingResources;
036: import com.sun.jbi.binding.jms.config.ConfigConstants;
037: import java.util.logging.Logger;
038:
039: import org.w3c.dom.Document;
040: import org.w3c.dom.Element;
041: import org.w3c.dom.Node;
042: import org.w3c.dom.NodeList;
043:
044: /**
045: * Reads the configuration file endpoints.xml and loads the data into the
046: * EndpointBean objects.
047: *
048: * @author Sun Microsystems Inc.
049: */
050: public class ConfigReader extends com.sun.jbi.binding.jms.util.UtilBase
051: implements JMSBindingResources {
052: /**
053: * Document object of the XML config file.
054: */
055: private Document mDoc;
056:
057: /**
058: * Logger Object.
059: */
060: private Logger mLogger;
061:
062: /**
063: * Namespace.
064: */
065: private String mNameSpace = "";
066:
067: /**
068: * i18n.
069: */
070: private StringTranslator mTranslator;
071:
072: /**
073: * The list of endpoints as configured in the config file. This list
074: * contains all the encpoints and their attibutes.
075: */
076: private EndpointBean[] mEndpointInfoList = null;
077:
078: /**
079: * The total number of end points in the config file.
080: */
081: private int mTotalEndpoints = 0;
082:
083: /**
084: * Creates a new ConfigReader object.
085: */
086: public ConfigReader() {
087: mLogger = JMSBindingContext.getInstance().getLogger();
088: mTranslator = JMSBindingContext.getInstance()
089: .getStringTranslator();
090:
091: setValid(true);
092: }
093:
094: /**
095: * Returns the Bean object corresponding to the endpoint.
096: *
097: * @param endpoint endpoint name.
098: *
099: * @return endpoint Bean object.
100: */
101: public EndpointBean getBean(String endpoint) {
102: /*Search for the bean corresponding to the service and endpoint name
103: */
104: for (int j = 0; j < mEndpointInfoList.length; j++) {
105: String tmp = mEndpointInfoList[j].getUniqueName();
106:
107: if (tmp.trim().equals(endpoint)) {
108: return mEndpointInfoList[j];
109: }
110: }
111:
112: return null;
113: }
114:
115: /**
116: * Gets the endpoint list corresponding to a config file.
117: *
118: * @return End point Bean list.
119: */
120: public EndpointBean[] getEndpoint() {
121: return mEndpointInfoList;
122: }
123:
124: /**
125: * Returns the total number of endpoints in the config file.
126: *
127: * @return int number of endpoints.
128: */
129: public int getEndpointCount() {
130: return mTotalEndpoints;
131: }
132:
133: /**
134: * Initializes the config file and loads services.
135: *
136: * @param doc Name of the config file.
137: */
138: public void init(Document doc) {
139: try {
140: mDoc = doc;
141: mDoc.getDocumentElement().normalize();
142: loadServicesList();
143: } catch (Exception genException) {
144: mLogger.severe(mTranslator
145: .getString(JMS_LOAD_CONFIG_FAILED));
146: genException.printStackTrace();
147: setException(genException);
148: setError(mTranslator.getString(JMS_LOAD_CONFIG_FAILED)
149: + "\n" + genException.getMessage());
150: }
151: }
152:
153: /**
154: * Sets the connection parameters.
155: *
156: * @param node params node.
157: * @param eb endpoint bean.
158: *
159: * @throws Exception exception.
160: */
161: private void setConnectionParams(Node node, EndpointBean eb)
162: throws Exception {
163: try {
164: Element ele = (Element) node;
165: NodeList list = ele
166: .getElementsByTagName(ConfigConstants.CONNECTION_PARAMS);
167:
168: if (list.getLength() == 0) {
169: setError(mTranslator
170: .getString(JMS_MISSING_CONNECTION_PARAMS));
171:
172: return;
173: }
174:
175: Node nd = (Node) list.item(0);
176: String factory = null;
177: String user = null;
178: String pass = null;
179:
180: factory = getValue(nd, ConfigConstants.CONNECTION_FACTORY);
181: user = getValue(nd, ConfigConstants.CONNECTION_USER_ID);
182: pass = getValue(nd, ConfigConstants.CONNECTION_PASSWORD);
183:
184: if ((factory == null) || (factory.trim().equals(""))) {
185: setError(mTranslator
186: .getString(JMS_INVALID_FACTORY_NAME));
187: }
188:
189: if ((user == null) || (user.trim().equals(""))) {
190: user = "";
191: }
192:
193: if ((pass == null) || (pass.trim().equals(""))
194: || (user.trim().equals(""))) {
195: pass = "";
196: }
197:
198: eb.setValue(ConfigConstants.CONNECTION_FACTORY, factory);
199: eb.setValue(ConfigConstants.CONNECTION_USER_ID, user);
200: eb.setValue(ConfigConstants.CONNECTION_PASSWORD, pass);
201: } catch (Exception e) {
202: throw e;
203: }
204: }
205:
206: /**
207: * Utility method for setting the Bean object from the XML Nodes.
208: *
209: * @param node The node which needs to be.
210: * @param sb The end point bean object which has to be updated.
211: * @param tagName the tag name which needs to be read.
212: */
213: private void setEndpointBeanByTagName(Node node, EndpointBean sb,
214: String tagName) {
215: Element ele = (Element) node;
216: NodeList namelist = ele.getElementsByTagName(tagName);
217:
218: if (namelist == null) {
219: /* This means the tag is not present
220: */
221: return;
222: }
223:
224: Element name = (Element) namelist.item(0);
225: String sValue = null;
226:
227: try {
228: sValue = ((Node) (name.getChildNodes().item(0)))
229: .getNodeValue().trim();
230: } catch (NullPointerException ne) {
231: sb.setValue(tagName, sValue);
232:
233: return;
234: }
235:
236: sb.setValue(tagName, sValue);
237: }
238:
239: /**
240: * Sets the interface name for this endpoint.
241: *
242: * @param nd Node.
243: * @param eb endpoint bean.
244: */
245: private void setInterface(Node nd, EndpointBean eb) {
246: Element ele = (Element) nd;
247: NodeList namelist = ele
248: .getElementsByTagName(ConfigConstants.INTERFACE);
249:
250: if (namelist == null) {
251: /* This means the tag is not present
252: */
253: return;
254: }
255:
256: Node node = (Node) namelist.item(0);
257:
258: try {
259: eb.setValue(ConfigConstants.INTERFACE_NAMESPACE, getValue(
260: node, ConfigConstants.NAMESPACE_URI));
261: eb.setValue(ConfigConstants.INTERFACE_LOCALNAME, getValue(
262: node, ConfigConstants.LOCAL_PART));
263: } catch (Exception e) {
264: mLogger.severe(mTranslator
265: .getString(JMS_LOAD_CONFIG_FAILED));
266: }
267: }
268:
269: /**
270: * Sets the namespace.
271: */
272: private void setNamespace() {
273: mNameSpace = mDoc.getDocumentElement().getAttribute(
274: ConfigConstants.TARGET_NAMESPACE);
275:
276: if (mNameSpace == null) {
277: mLogger.warning(mTranslator
278: .getString(JMS_CANNOT_GET_NAMESPACE));
279:
280: return;
281: }
282: }
283:
284: /**
285: * Gets the operation name.
286: *
287: * @param nd operation node.
288: *
289: * @return String operation name.
290: */
291: private String getOperationName(Node nd) {
292: Element ele = (Element) nd;
293: NodeList namelist = ele
294: .getElementsByTagName(ConfigConstants.NAME);
295:
296: Node node = (Node) namelist.item(0);
297:
298: return getValue(node, ConfigConstants.LOCAL_PART);
299: }
300:
301: /**
302: * Returns the operation namespace.
303: *
304: * @param nd Node namespace node.
305: *
306: * @return operation namespace.
307: */
308: private String getOperationNamespace(Node nd) {
309: Element ele = (Element) nd;
310: NodeList namelist = ele
311: .getElementsByTagName(ConfigConstants.NAME);
312: Node node = (Node) namelist.item(0);
313:
314: return getValue(node, ConfigConstants.NAMESPACE_URI);
315: }
316:
317: /**
318: * Sets the operations.
319: *
320: * @param node node.
321: * @param eb endpoint bean.
322: *
323: * @throws Exception exception.
324: */
325: private void setOperations(Node node, EndpointBean eb)
326: throws Exception {
327: try {
328: Element ele = (Element) node;
329: NodeList list = ele
330: .getElementsByTagName(ConfigConstants.OPERATION);
331:
332: if (list.getLength() == 0) {
333: setError(mTranslator.getString(JMS_NO_OPERATIONS, eb
334: .getUniqueName()));
335:
336: return;
337: }
338:
339: for (int i = 0; i < list.getLength(); i++) {
340: Node nd = (Node) list.item(i);
341: String namespace = null;
342: String name = null;
343: String mep = null;
344: String input = null;
345: String output = null;
346: namespace = getOperationNamespace(nd);
347: name = getOperationName(nd);
348: mep = getValue(nd, ConfigConstants.MEP);
349: input = getValue(nd, ConfigConstants.INPUT_MESSAGE_TYPE);
350: output = getValue(nd,
351: ConfigConstants.OUTPUT_MESSAGE_TYPE);
352:
353: if ((namespace == null)
354: || (namespace.trim().equals(""))) {
355: setError(mTranslator
356: .getString(JMS_OPERATION_NAMESPACE_NULL));
357: }
358:
359: if ((name == null) || (name.trim().equals(""))) {
360: setError(mTranslator
361: .getString(JMS_OPERATION_NAME_NULL));
362: }
363:
364: if ((mep == null) || (mep.trim().equals(""))) {
365: setError(mTranslator.getString(JMS_MEP_NULL));
366: }
367:
368: if ((input == null) || (input.trim().equals(""))) {
369: setError(mTranslator
370: .getString(JMS_INPUT_TYPE_NAME_NULL));
371: }
372:
373: if ((output == null) || (output.trim().equals(""))) {
374: setError(mTranslator
375: .getString(JMS_OUTPUT_TYPE_NAME_NULL));
376: }
377:
378: if (!isValid()) {
379: return;
380: }
381:
382: if (operationAllowed(eb, mep)) {
383: eb
384: .addOperation(namespace, name, mep, input,
385: output);
386: } else {
387: setWarning(mTranslator.getString(
388: JMS_INCONSISTENT_OPERATION, name));
389: }
390: }
391: } catch (Exception e) {
392: throw e;
393: }
394: }
395:
396: /**
397: * Sets the role.
398: *
399: * @param eb Endpoint bean.
400: */
401: private void setRole(EndpointBean eb) {
402: String role = (String) eb
403: .getValue(ConfigConstants.ENDPOINT_TYPE);
404:
405: if (role == null) {
406: eb.setRole(ConfigConstants.CONSUMER);
407: } else {
408: if (role.trim().equalsIgnoreCase(
409: ConfigConstants.PROVIDER_STRING)) {
410: eb.setRole(ConfigConstants.PROVIDER);
411: } else {
412: eb.setRole(ConfigConstants.CONSUMER);
413: }
414: }
415: }
416:
417: /**
418: * Sets the service.
419: *
420: * @param nd service node.
421: * @param eb endpoint bean.
422: */
423: private void setService(Node nd, EndpointBean eb) {
424: Element ele = (Element) nd;
425: NodeList namelist = ele
426: .getElementsByTagName(ConfigConstants.SERVICE);
427:
428: if (namelist == null) {
429: /* This means the tag is not present
430: */
431: return;
432: }
433:
434: Node node = (Node) namelist.item(0);
435:
436: try {
437: eb.setValue(ConfigConstants.SERVICE_NAMESPACE, getValue(
438: node, ConfigConstants.NAMESPACE_URI));
439: eb.setValue(ConfigConstants.SERVICENAME, getValue(node,
440: ConfigConstants.LOCAL_PART));
441: } catch (Exception e) {
442: mLogger.severe(mTranslator
443: .getString(JMS_LOAD_CONFIG_FAILED));
444: }
445: }
446:
447: /**
448: * Sets the style.
449: *
450: * @param eb Endpoint bean.
451: */
452: private void setStyle(EndpointBean eb) {
453: if (eb.getValue(ConfigConstants.DESTINATION_STYLE).trim()
454: .equalsIgnoreCase(ConfigConstants.QUEUE_STRING)) {
455: eb.setStyle(ConfigConstants.QUEUE);
456: } else if (eb.getValue(ConfigConstants.DESTINATION_STYLE)
457: .trim().equalsIgnoreCase(ConfigConstants.TOPIC_STRING)) {
458: eb.setStyle(ConfigConstants.TOPIC);
459: } else {
460: setError(mTranslator.getString(JMS_INVALID_STYLE));
461: }
462: }
463:
464: /**
465: * Gets the value for an element.
466: *
467: * @param n node node element.
468: * @param name string.
469: *
470: * @return value of that node.
471: */
472: private String getValue(Node n, String name) {
473: String s = null;
474:
475: try {
476: Element ele = (Element) n;
477: NodeList list = ele.getElementsByTagName(name);
478: Element found = (Element) list.item(0);
479: s = (String) found.getFirstChild().getNodeValue();
480: } catch (Exception e) {
481: ; // e.printStackTrace();
482: }
483:
484: return s;
485: }
486:
487: /**
488: * Parses the config files and loads them into bean objects.
489: */
490: private void loadServicesList() {
491: NodeList list = mDoc
492: .getElementsByTagName(ConfigConstants.ENDPOINT);
493: mTotalEndpoints = list.getLength();
494: mEndpointInfoList = new EndpointBean[mTotalEndpoints];
495:
496: try {
497: for (int i = 0; i < mTotalEndpoints; i++) {
498: Node node = list.item(i);
499: EndpointBean sb = new EndpointBean();
500: sb.setDeploymentType("XML");
501: sb.setWsdlDefinition(mDoc);
502: if (node.getNodeType() == Node.ELEMENT_NODE) {
503: setEndpointBeanByTagName(node, sb,
504: ConfigConstants.ENDPOINTNAME);
505: setEndpointBeanByTagName(node, sb,
506: ConfigConstants.DESTINATION_NAME);
507: setEndpointBeanByTagName(node, sb,
508: ConfigConstants.DESTINATION_STYLE);
509: setEndpointBeanByTagName(node, sb,
510: ConfigConstants.DURABILITY);
511: setEndpointBeanByTagName(node, sb,
512: ConfigConstants.MESSAGE_SELECTOR);
513: setEndpointBeanByTagName(node, sb,
514: ConfigConstants.ENDPOINT_TYPE);
515: setEndpointBeanByTagName(node, sb,
516: ConfigConstants.REPLY_TO);
517: setEndpointBeanByTagName(node, sb,
518: ConfigConstants.TIME_TO_LIVE);
519: }
520:
521: setRole(sb);
522: setService(node, sb);
523: setInterface(node, sb);
524: setConnectionParams(node, sb);
525: setStyle(sb);
526: setOperations(node, sb);
527: checkRequired(sb, i);
528:
529: if (!isValid()) {
530: setError(mTranslator
531: .getString(JMS_LOAD_CONFIG_FAILED));
532:
533: return;
534: }
535:
536: mEndpointInfoList[i] = sb;
537: }
538: } catch (Exception ee) {
539: mLogger.severe(mTranslator
540: .getString(JMS_LOAD_CONFIG_FAILED));
541: ee.printStackTrace();
542: setException(ee);
543: setError(mTranslator.getString(JMS_LOAD_CONFIG_FAILED)
544: + "\n" + ee.getMessage());
545: }
546: }
547:
548: /**
549: * Checks if the operation is allowed.
550: *
551: * @param eb endpoint bean.
552: * @param mep mep.
553: *
554: * @return true if operation is allowed.
555: */
556: private boolean operationAllowed(EndpointBean eb, String mep) {
557: int style = eb.getStyle();
558: boolean allowed = true;
559:
560: if (style == ConfigConstants.TOPIC) {
561: if (mep.trim().equals(ConfigConstants.IN_OUT)
562: || mep.trim().equals(
563: ConfigConstants.IN_OPTIONAL_OUT)
564: || mep.trim().equals(ConfigConstants.OUT_IN)
565: || mep.trim().equals(
566: ConfigConstants.OUT_OPTIONAL_IN)
567: || mep.trim().equals(
568: ConfigConstants.ROBUST_OUT_ONLY)) {
569: allowed = false;
570: }
571: }
572:
573: return allowed;
574: }
575:
576: /**
577: * Checks if all non null attributes are defined.
578: *
579: * @param eb endpoint bean.
580: * @param counter index of endpoint.
581: */
582:
583: private void checkRequired(EndpointBean eb, int counter) {
584: clear();
585: if (eb.getValue(ConfigConstants.SERVICE_NAMESPACE).trim()
586: .equals("")) {
587: setError("Endpoint "
588: + counter
589: + " "
590: + mTranslator.getString(JMS_SERVICE_NAMESPACE_NULL,
591: ""));
592: }
593:
594: if (eb.getValue(ConfigConstants.SERVICENAME).trim().equals("")) {
595: setError("Endpoint " + counter + " "
596: + mTranslator.getString(JMS_SERVICE_NAME_NULL, ""));
597: }
598:
599: if (eb.getValue(ConfigConstants.ENDPOINTNAME).trim().equals("")) {
600: setError("Endpoint "
601: + counter
602: + " "
603: + mTranslator.getString(JMS_ENDPOINT_NAME_NULL, eb
604: .getValue(ConfigConstants.SERVICENAME)));
605: }
606:
607: if (eb.getValue(ConfigConstants.CONNECTION_FACTORY).trim()
608: .equals("")) {
609: setError("Endpoint "
610: + counter
611: + " "
612: + mTranslator.getString(
613: JMS_CONNECTION_FACTORY_NULL,
614: eb.getValue(ConfigConstants.ENDPOINTNAME)));
615: }
616:
617: if (eb.getValue(ConfigConstants.INTERFACE_NAMESPACE).trim()
618: .equals("")) {
619: setError("Endpoint "
620: + counter
621: + " "
622: + mTranslator.getString(
623: JMS_INTERFACE_NAMESPACE_NULL,
624: eb.getValue(ConfigConstants.ENDPOINTNAME)));
625: }
626:
627: if (eb.getValue(ConfigConstants.INTERFACE_LOCALNAME).trim()
628: .equals("")) {
629: setError("Endpoint "
630: + counter
631: + " "
632: + mTranslator.getString(JMS_INTERFACE_NAME_NULL, eb
633: .getValue(ConfigConstants.ENDPOINTNAME)));
634: }
635: if (eb.getValue(ConfigConstants.DESTINATION_NAME).trim()
636: .equals("")) {
637: setError("Endpoint "
638: + counter
639: + " "
640: + mTranslator.getString(JMS_DESTINATION_NAME_NULL,
641: eb.getValue(ConfigConstants.ENDPOINTNAME)));
642: }
643: }
644: }
|