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.file.util;
030:
031: import com.sun.jbi.binding.file.EndpointBean;
032: import com.sun.jbi.binding.file.FileBindingContext;
033: import com.sun.jbi.binding.file.FileBindingResources;
034:
035: import org.w3c.dom.Document;
036: import org.w3c.dom.Element;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039:
040: import java.util.logging.Logger;
041:
042: import javax.xml.parsers.DocumentBuilderFactory;
043:
044: /**
045: * Reads the configuration XML file and loads the data into the EndpointBean
046: * objects. The parsing and reading information may not be state of the art
047: * but serves the purpose of getting the data from XML. Simple.
048: *
049: * @author Sun Microsystems, Inc.
050: */
051: public class ConfigReader extends UtilBase implements
052: FileBindingResources {
053: /**
054: * Document object of the XML config file.
055: */
056: private Document mDoc;
057:
058: /**
059: * Logger Object
060: */
061: private Logger mLog;
062:
063: /**
064: * i18n.
065: */
066: private StringTranslator mTranslator;
067:
068: /**
069: * The list of endpoints as configured in the config file. This list
070: * contains all the encpoints and their attibutes.
071: */
072: private EndpointBean[] mEndpointInfoList = null;
073:
074: /**
075: * The total number of end points in the config file.
076: */
077: private int mTotalEndpoints = 0;
078:
079: /**
080: * Creates a new ConfigReader object.
081: */
082: public ConfigReader() {
083: mLog = FileBindingContext.getInstance().getLogger();
084: mTranslator = new StringTranslator();
085: setValid(true);
086: }
087:
088: /**
089: * Returns the Bean object corresponding to the endpoint.
090: *
091: * @param endpoint endpoint name.
092: *
093: * @return endpoint Bean object.
094: */
095: public EndpointBean getBean(String endpoint) {
096: /*Search for the bean corresponding to the service and endpoint name
097: */
098: for (int j = 0; j < mEndpointInfoList.length; j++) {
099: String tmp = mEndpointInfoList[j].getUniqueName();
100:
101: if (tmp.trim().equals(endpoint)) {
102: return mEndpointInfoList[j];
103: }
104: }
105:
106: return null;
107: }
108:
109: /**
110: * Gets the endpoint list corresponding to a config file.
111: *
112: * @return End point Bean list
113: */
114: public EndpointBean[] getEndpoint() {
115: return mEndpointInfoList;
116: }
117:
118: /**
119: * Returns the total number of endopoints in the config file.
120: *
121: * @return int number of endpoints.
122: */
123: public int getEndpointCount() {
124: return mTotalEndpoints;
125: }
126:
127: /**
128: * Initializes the config file and loads services.
129: *
130: * @param doc Name of the config file.
131: */
132: public void init(Document doc) {
133: try {
134: mDoc = doc;
135: mDoc.getDocumentElement().normalize();
136: loadServicesList();
137: } catch (Exception genException) {
138: mLog.severe(mTranslator.getString(FBC_LOAD_CONFIG_FAILED));
139: genException.printStackTrace();
140: setException(genException);
141: setError(getError()
142: + mTranslator.getString(FBC_LOAD_CONFIG_FAILED)
143: + "\n" + genException.getMessage());
144: setValid(false);
145: }
146: }
147:
148: /**
149: * Utility method for setting the Bean object from the XML Nodes.
150: *
151: * @param node The node which needs to be
152: * @param sb The end point bean object which has to be updated
153: * @param tagName the tag name which needs to be read.
154: */
155: private void setEndpointBeanByTagName(Node node, EndpointBean sb,
156: String tagName) {
157: Element ele = (Element) node;
158: NodeList namelist = ele.getElementsByTagName(tagName);
159:
160: if (namelist == null) {
161: /* This means the tag is not present
162: */
163: return;
164: }
165:
166: Element name = (Element) namelist.item(0);
167: String sValue = null;
168:
169: try {
170: sValue = ((Node) (name.getChildNodes().item(0)))
171: .getNodeValue().trim();
172: } catch (NullPointerException ne) {
173: sb.setValue(tagName, sValue);
174:
175: return;
176: }
177:
178: sb.setValue(tagName, sValue);
179: }
180:
181: /**
182: * Sets the interface.
183: *
184: * @param nd Node
185: * @param eb Endpoint Bean.
186: */
187: private void setInterface(Node nd, EndpointBean eb) {
188: Element ele = (Element) nd;
189: NodeList namelist = ele
190: .getElementsByTagName(ConfigData.INTERFACE);
191:
192: if (namelist == null) {
193: /* This means the tag is not present
194: */
195: return;
196: }
197:
198: Node node = (Node) namelist.item(0);
199:
200: try {
201: eb.setValue(ConfigData.INTERFACE_NAMESPACE, getValue(node,
202: ConfigData.NAMESPACE_URI));
203: eb.setValue(ConfigData.INTERFACE_LOCALNAME, getValue(node,
204: ConfigData.LOCAL_PART));
205: } catch (Exception e) {
206: mLog.severe(mTranslator.getString(FBC_LOAD_CONFIG_FAILED));
207: }
208: }
209:
210: /**
211: * Returns the operation name corresponding to the endpoint node.
212: *
213: * @param nd Node
214: *
215: * @return operation name
216: */
217: private String getOperationName(Node nd) {
218: Element ele = (Element) nd;
219: NodeList namelist = ele.getElementsByTagName(ConfigData.NAME);
220:
221: Node node = (Node) namelist.item(0);
222:
223: return getValue(node, ConfigData.LOCAL_PART);
224: }
225:
226: /**
227: * Returns the operation namespace for the operation node.
228: *
229: * @param nd node to be parsed.
230: *
231: * @return namespace of operation.
232: */
233: private String getOperationNamespace(Node nd) {
234: Element ele = (Element) nd;
235: NodeList namelist = ele.getElementsByTagName(ConfigData.NAME);
236: Node node = (Node) namelist.item(0);
237:
238: return getValue(node, ConfigData.NAMESPACE_URI);
239: }
240:
241: /**
242: * Util method to get all operations loaded into Beans.
243: *
244: * @param node Endpoint node that has to be parsed.
245: * @param eb endpoint bean object where info is to be loaded.
246: *
247: * @throws Exception exception.
248: */
249: private void setOperations(Node node, EndpointBean eb)
250: throws Exception {
251: try {
252: Element ele = (Element) node;
253: NodeList list = ele
254: .getElementsByTagName(ConfigData.OPERATION);
255:
256: if (list.getLength() == 0) {
257: setError(mTranslator.getString(FBC_NO_OPERATIONS, eb
258: .getUniqueName()));
259:
260: return;
261: }
262:
263: for (int i = 0; i < list.getLength(); i++) {
264: Node nd = (Node) list.item(i);
265: String namespace = null;
266: String name = null;
267: String mep = null;
268: String input = null;
269: String output = null;
270: String ext = null;
271: String prefix = null;
272: namespace = getOperationNamespace(nd);
273: name = getOperationName(nd);
274: mep = getValue(nd, ConfigData.MEP);
275: input = getValue(nd, ConfigData.INPUT_MESSAGE_TYPE);
276: output = getValue(nd, ConfigData.OUTPUT_MESSAGE_TYPE);
277: prefix = getValue(nd, ConfigData.OUTPUTPREFIX);
278: ext = getValue(nd, ConfigData.OUTPUTEXTENSION);
279:
280: if ((namespace == null)
281: || (namespace.trim().equals(""))) {
282: setError(mTranslator
283: .getString(FBC_OPERATION_NAMESPACE_NULL));
284: }
285:
286: if ((name == null) || (name.trim().equals(""))) {
287: setError(mTranslator
288: .getString(FBC_OPERATION_NAME_NULL));
289: }
290:
291: if ((mep == null) || (mep.trim().equals(""))) {
292: setError(mTranslator.getString(FBC_MEP_NULL));
293: }
294:
295: if ((input == null) || (input.trim().equals(""))) {
296: input = ConfigData.DEFAULT_MESSAGE_TYPE;
297: }
298:
299: if ((output == null) || (output.trim().equals(""))) {
300: output = ConfigData.DEFAULT_MESSAGE_TYPE;
301: }
302:
303: if (!isValid()) {
304: return;
305: }
306:
307: eb.addOperation(namespace, name, mep, input, output,
308: ext, prefix);
309: }
310: } catch (Exception e) {
311: throw e;
312: }
313: }
314:
315: /**
316: * Sets the role of the endpoint.
317: *
318: * @param eb Endpoint Bean.
319: */
320: private void setRole(EndpointBean eb) {
321: String role = (String) eb.getValue(ConfigData.ENDPOINT_TYPE);
322:
323: if (role == null) {
324: eb.setRole(ConfigData.CONSUMER);
325: } else {
326: if (role.trim()
327: .equalsIgnoreCase(ConfigData.PROVIDER_STRING)) {
328: eb.setRole(ConfigData.PROVIDER);
329: } else {
330: eb.setRole(ConfigData.CONSUMER);
331: }
332: }
333: }
334:
335: /**
336: * Sets the service.
337: *
338: * @param nd Node
339: * @param eb Endpoint Bean.
340: */
341: private void setService(Node nd, EndpointBean eb) {
342: Element ele = (Element) nd;
343: NodeList namelist = ele
344: .getElementsByTagName(ConfigData.SERVICE);
345:
346: if (namelist == null) {
347: /* This means the tag is not present
348: */
349: return;
350: }
351:
352: Node node = (Node) namelist.item(0);
353:
354: try {
355: eb.setValue(ConfigData.SERVICE_NAMESPACE, getValue(node,
356: ConfigData.NAMESPACE_URI));
357: eb.setValue(ConfigData.SERVICE_LOCALNAME, getValue(node,
358: ConfigData.LOCAL_PART));
359: } catch (Exception e) {
360: mLog.severe(mTranslator.getString(FBC_LOAD_CONFIG_FAILED));
361: }
362: }
363:
364: /**
365: * Util method to extract test node value from an element.
366: *
367: * @param n node.
368: * @param name name of the element from which info is extracted.
369: *
370: * @return Value.
371: */
372: private String getValue(Node n, String name) {
373: String s = null;
374:
375: try {
376: Element ele = (Element) n;
377: NodeList list = ele.getElementsByTagName(name);
378: Element found = (Element) list.item(0);
379: s = (String) found.getFirstChild().getNodeValue();
380: } catch (Exception e) {
381: e.printStackTrace();
382: }
383:
384: return s;
385: }
386:
387: /**
388: * Checks the required atributes.
389: *
390: * @param eb Bean object
391: * @param counter index of endpoint in the list.
392: */
393: private void checkRequired(EndpointBean eb, int counter) {
394: if (eb.getValue(ConfigData.SERVICE_NAMESPACE).trim().equals("")) {
395: setError(getError() + "\n" + counter + " "
396: + mTranslator.getString(FBC_INVALID_SERVICE, ""));
397: setValid(false);
398: }
399:
400: if (eb.getValue(ConfigData.SERVICE_LOCALNAME).trim().equals("")) {
401: setError(getError() + "\n" + counter + " "
402: + mTranslator.getString(FBC_INVALID_SERVICE, ""));
403: setValid(false);
404: }
405:
406: if (eb.getValue(ConfigData.ENDPOINTNAME).trim().equals("")) {
407: setError(getError()
408: + "\n"
409: + counter
410: + " "
411: + mTranslator.getString(FBC_INVALID_ENDPOINT, eb
412: .getValue(ConfigData.SERVICE_LOCALNAME)));
413: setValid(false);
414: }
415:
416: if (eb.getValue(ConfigData.INPUTDIR).trim().equals("")) {
417: setError(getError()
418: + "\n"
419: + counter
420: + " "
421: + mTranslator.getString(FBC_INVALID_INPUTFOLDER, eb
422: .getValue(ConfigData.ENDPOINTNAME)));
423: setValid(false);
424: }
425:
426: if (eb.getValue(ConfigData.OUTPUTDIR).trim().equals("")) {
427: setError(getError()
428: + "\n"
429: + counter
430: + " "
431: + mTranslator.getString(FBC_INVALID_OUTPUTFOLDER,
432: eb.getValue(ConfigData.ENDPOINTNAME)));
433: setValid(false);
434: }
435:
436: if (eb.getValue(ConfigData.PROCESSEDDIR).trim().equals("")) {
437: setError(getError()
438: + "\n"
439: + counter
440: + " "
441: + mTranslator.getString(
442: FBC_INVALID_PROCESSEDFOLDER, eb
443: .getValue(ConfigData.ENDPOINTNAME)));
444: setValid(false);
445: }
446: }
447:
448: /**
449: * Parses the config files and loads them into bean objects.
450: */
451: private void loadServicesList() {
452: NodeList list = mDoc.getElementsByTagName(ConfigData.ENDPOINT);
453: mTotalEndpoints = list.getLength();
454: mEndpointInfoList = new EndpointBean[mTotalEndpoints];
455:
456: try {
457: for (int i = 0; i < mTotalEndpoints; i++) {
458: Node node = list.item(i);
459: EndpointBean sb = new EndpointBean();
460:
461: if (node.getNodeType() == Node.ELEMENT_NODE) {
462: setService(node, sb);
463: setInterface(node, sb);
464: setEndpointBeanByTagName(node, sb,
465: ConfigData.ENDPOINTNAME);
466: setEndpointBeanByTagName(node, sb,
467: ConfigData.ENDPOINT_TYPE);
468: setEndpointBeanByTagName(node, sb,
469: ConfigData.INPUTDIR);
470: setEndpointBeanByTagName(node, sb,
471: ConfigData.OUTPUTDIR);
472: setEndpointBeanByTagName(node, sb,
473: ConfigData.PROCESSEDDIR);
474: setEndpointBeanByTagName(node, sb,
475: ConfigData.INPUTPATTERN);
476: setOperations(node, sb);
477: }
478:
479: setRole(sb);
480: sb.setDeploymentType("XML");
481: checkRequired(sb, i);
482:
483: if (!isValid()) {
484: setError(mTranslator
485: .getString(FBC_LOAD_CONFIG_FAILED)
486: + "\n" + getError());
487:
488: return;
489: }
490:
491: mEndpointInfoList[i] = sb;
492: }
493: } catch (Exception ee) {
494: mLog.severe(mTranslator.getString(FBC_LOAD_CONFIG_FAILED));
495: ee.printStackTrace();
496: setException(ee);
497: setError(getError()
498: + mTranslator.getString(FBC_LOAD_CONFIG_FAILED)
499: + "\n" + ee.getMessage());
500: setValid(false);
501: }
502: }
503: }
|