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