001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * ServicelistReader.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license term
009: */
010: package com.sun.jbi.engine.sequencing.servicelist;
011:
012: import com.sun.jbi.engine.sequencing.SequencingEngineContext;
013: import com.sun.jbi.engine.sequencing.SequencingEngineResources;
014: import com.sun.jbi.engine.sequencing.util.ConfigData;
015: import com.sun.jbi.engine.sequencing.util.StringTranslator;
016: import com.sun.jbi.engine.sequencing.util.UtilBase;
017:
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import org.w3c.dom.NamedNodeMap;
021: import org.w3c.dom.Node;
022: import org.w3c.dom.NodeList;
023:
024: import java.io.File;
025:
026: import java.util.StringTokenizer;
027: import java.util.logging.Logger;
028:
029: /**
030: * Class ServicelistReader.
031: *
032: * @author Sun Microsystems, Inc.
033: */
034: public class ServicelistReader extends UtilBase implements
035: SequencingEngineResources {
036: /**
037: * Document object.
038: */
039: private Document mDoc;
040:
041: /**
042: * Logger object.
043: */
044: private Logger mLog;
045:
046: /**
047: * List of services.
048: */
049: private ServicelistBean mListofService = null;
050:
051: /**
052: * Interface name.
053: */
054: private String mListInterfaceName;
055:
056: /**
057: * Interface namespace.
058: */
059: private String mListInterfaceNameSpace;
060:
061: /**
062: * List desc
063: */
064: private String mServicelistDesc;
065:
066: /**
067: * Endpoint name for this service.
068: */
069: private String mServicelistEndpoint;
070:
071: /**
072: * Servicelist MEP
073: */
074: private String mServicelistMep;
075:
076: /**
077: * List name.
078: */
079: private String mServicelistName;
080:
081: /**
082: * Namespace of this service.
083: */
084: private String mServicelistNameSpace;
085:
086: /**
087: * List opereation.
088: */
089: private String mServicelistOper;
090:
091: /**
092: * Namespace of the operation in this service.
093: */
094: private String mServicelistOperNamespace;
095:
096: /**
097: * Translator object for internationalization.
098: */
099: private StringTranslator mTranslator;
100:
101: /**
102: * Service count.
103: */
104: private int mServiceCount = 0;
105:
106: /**
107: * Creates a new ServicelistReader object.
108: */
109: public ServicelistReader() {
110: mLog = SequencingEngineContext.getInstance().getLogger();
111: mTranslator = new StringTranslator();
112: setValid(true);
113: }
114:
115: /**
116: * Gets the service count.
117: *
118: * @return number of services.
119: */
120: public int getServiceCount() {
121: return mServiceCount;
122: }
123:
124: /**
125: * Gets the list bean.
126: *
127: * @return list bean.
128: */
129: public ServicelistBean getServicelistBean() {
130: return mListofService;
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: loadServices();
143: } catch (Exception genException) {
144: mLog.severe(mTranslator.getString(SEQ_CONFIGDATA_ERROR));
145: mLog.severe(genException.getMessage());
146: setException(genException);
147: setError(getError() + "\n\t"
148: + mTranslator.getString(SEQ_CONFIGDATA_ERROR)
149: + "\n" + genException.getMessage());
150: setValid(false);
151: }
152: }
153:
154: /**
155: * Loads the services.
156: */
157: public void loadServices() {
158: mListofService = new ServicelistBean();
159: setListAttributes(mDoc);
160: mListofService.setServicename(mServicelistName);
161: mListofService.setNamespace(mServicelistNameSpace);
162: mListofService.setInterfaceNamespace(mListInterfaceNameSpace);
163: mListofService.setInterfaceName(mListInterfaceName);
164: mListofService.setListdescription(mServicelistDesc);
165: mListofService.setOperationNamespace(mServicelistOperNamespace);
166: mListofService.setOperation(mServicelistOper);
167: mListofService.setMep(mServicelistMep);
168: mListofService.setEndpointName(mServicelistEndpoint);
169:
170: NodeList list = mDoc.getElementsByTagName(ConfigData.SERVICE);
171: mServiceCount = list.getLength();
172:
173: try {
174: for (int i = 0; i < mServiceCount; i++) {
175: Node node = list.item(i);
176: ServiceBean sb = new ServiceBean();
177:
178: if (node.getNodeType() == Node.ELEMENT_NODE) {
179: setService(node, sb);
180: setInterface(node, sb);
181: setOperation(node, sb);
182: setByTagName(node, sb, ConfigData.SERVICE_ENDPOINT);
183: setByTagName(node, sb,
184: ConfigData.SERVICE_DESCRIPTION);
185: setByTagName(node, sb, ConfigData.TIMEOUT);
186: setByTagName(node, sb, ConfigData.SERVICEID);
187: setByTagName(node, sb, ConfigData.SERVICE_MEP);
188: }
189:
190: mListofService.addService(sb);
191: }
192: } catch (Exception ee) {
193: setError(getError() + ee.getMessage());
194: setException(ee);
195: ee.printStackTrace();
196: setValid(false);
197: }
198: }
199:
200: /**
201: * Utility method for setting the Bean object from the XML Nodes.
202: *
203: * @param node The node which needs to be
204: * @param sb The end point bean object which has to be updated
205: * @param tagName the tag name which needs to be read.
206: */
207: private void setByTagName(Node node, ServiceBean sb, String tagName) {
208: Element ele = (Element) node;
209: NodeList namelist = ele.getElementsByTagName(tagName);
210: Element name = (Element) namelist.item(0);
211: String sValue = null;
212:
213: try {
214: sValue = ((Node) (name.getChildNodes().item(0)))
215: .getNodeValue().trim();
216: } catch (NullPointerException ne) {
217: return;
218: }
219:
220: if (tagName.equalsIgnoreCase(ConfigData.SERVICE_NAMESPACE)) {
221: sb.setNamespace(sValue);
222: } else if (tagName
223: .equalsIgnoreCase(ConfigData.SERVICE_LOCALNAME)) {
224: sb.setName(sValue);
225: }
226:
227: else if (tagName.equalsIgnoreCase(ConfigData.SERVICE_ENDPOINT)) {
228: sb.setEndpointName(sValue);
229: } else if (tagName
230: .equalsIgnoreCase(ConfigData.SERVICE_DESCRIPTION)) {
231: sb.setDescription(sValue);
232: } else if (tagName
233: .equalsIgnoreCase(ConfigData.OPERATION_NAMESPACE)) {
234: sb.setOperationNamespace(sValue);
235: } else if (tagName.equalsIgnoreCase(ConfigData.OPERATION)) {
236: sb.setOperation(sValue);
237: } else if (tagName.equalsIgnoreCase(ConfigData.SERVICE_MEP)) {
238: sb.setMep(sValue);
239: } else if (tagName.equalsIgnoreCase(ConfigData.TIMEOUT)) {
240: long timeout = 0;
241:
242: try {
243: timeout = Integer.parseInt(sValue);
244: sb.setTimeout(timeout);
245: } catch (NumberFormatException ne) {
246: mLog.warning(mTranslator.getString(
247: SEQ_CONFIGDATA_ERROR, sb.getName()));
248: }
249: } else if (tagName.equalsIgnoreCase(ConfigData.SERVICEID)) {
250: sb.setServiceid(sValue);
251: }
252: }
253:
254: /**
255: * Sets interface.
256: *
257: * @param node node
258: * @param sb bean.
259: */
260: private void setInterface(Node node, ServiceBean sb) {
261: Element ele = (Element) node;
262: NodeList namelist = ele
263: .getElementsByTagName(ConfigData.INTERFACE);
264:
265: try {
266: Node nd = (Node) namelist.item(0);
267: String namespace = getValue(nd, ConfigData.NAMESPACE_URI);
268: String name = getValue(nd, ConfigData.LOCAL_PART);
269: sb.setInterfaceNamespace(namespace);
270: sb.setInterfaceName(name);
271: } catch (Exception e) {
272: mLog.severe(mTranslator.getString(SEQ_LOAD_CONFIG_FAILED));
273: }
274: }
275:
276: /**
277: * Sets the list atttributes.
278: *
279: * @param doc Document.
280: */
281: private void setListAttributes(Document doc) {
282: NodeList nl = doc
283: .getElementsByTagName(ConfigData.SERVICELISTATTR);
284: Node node = nl.item(0);
285: setListService(node);
286: setListInterface(node);
287: setListOperation(node);
288: mServicelistDesc = getValue(node,
289: ConfigData.SERVICELIST_DESCRIPTION);
290: mServicelistMep = getValue(node, ConfigData.LIST_MEP);
291: mServicelistEndpoint = getValue(node, ConfigData.LIST_ENDPOINT);
292: }
293:
294: /**
295: * Sets the service list interface.
296: *
297: * @param nd node.
298: */
299: private void setListInterface(Node nd) {
300: Element ele = (Element) nd;
301: NodeList namelist = ele
302: .getElementsByTagName(ConfigData.LIST_INTERFACE);
303:
304: try {
305: Node node = (Node) namelist.item(0);
306: mListInterfaceNameSpace = getValue(node,
307: ConfigData.NAMESPACE_URI);
308: mListInterfaceName = getValue(node, ConfigData.LOCAL_PART);
309: } catch (Exception e) {
310: mLog.severe(mTranslator.getString(SEQ_LOAD_CONFIG_FAILED));
311: }
312: }
313:
314: /**
315: * Sets the list operation.
316: *
317: * @param nd node.
318: */
319: private void setListOperation(Node nd) {
320: Element ele = (Element) nd;
321: NodeList namelist = ele
322: .getElementsByTagName(ConfigData.LIST_OPERATION);
323:
324: try {
325: Node node = (Node) namelist.item(0);
326: mServicelistOperNamespace = getValue(node,
327: ConfigData.NAMESPACE_URI);
328: mServicelistOper = getValue(node, ConfigData.LOCAL_PART);
329: } catch (Exception e) {
330: mLog.severe(mTranslator.getString(SEQ_LOAD_CONFIG_FAILED));
331: }
332: }
333:
334: /**
335: * Sets the service list service.
336: *
337: * @param nd node.
338: */
339: private void setListService(Node nd) {
340: Element ele = (Element) nd;
341: NodeList namelist = ele
342: .getElementsByTagName(ConfigData.LIST_SERVICE);
343:
344: try {
345: Node node = (Node) namelist.item(0);
346: mServicelistNameSpace = getValue(node,
347: ConfigData.NAMESPACE_URI);
348: mServicelistName = getValue(node, ConfigData.LOCAL_PART);
349: } catch (Exception e) {
350: mLog.severe(mTranslator.getString(SEQ_LOAD_CONFIG_FAILED));
351: }
352: }
353:
354: /**
355: * Gets the local name from the quname.
356: *
357: * @param qname Qualified name of service.
358: *
359: * @return String local name
360: */
361: private String getLocalName(String qname) {
362: StringTokenizer tok = new StringTokenizer(qname, ":");
363:
364: try {
365: if (tok.countTokens() == 1) {
366: return qname;
367: }
368:
369: tok.nextToken();
370:
371: return tok.nextToken();
372: } catch (Exception e) {
373: return "";
374: }
375: }
376:
377: /**
378: * Gets the namespace from the qname.
379: *
380: * @param qname Qname of service
381: *
382: * @return namespace namespace of service
383: */
384: private String getNamespace(String qname) {
385: StringTokenizer tok = new StringTokenizer(qname, ":");
386: String prefix = null;
387:
388: try {
389: if (tok.countTokens() == 1) {
390: return "";
391: }
392:
393: prefix = tok.nextToken();
394:
395: NamedNodeMap map = mDoc.getDocumentElement()
396: .getAttributes();
397:
398: for (int j = 0; j < map.getLength(); j++) {
399: Node n = map.item(j);
400:
401: if (n.getLocalName().trim().equals(prefix.trim())) {
402: return n.getNodeValue();
403: }
404: }
405: } catch (Exception e) {
406: ;
407: }
408:
409: return "";
410: }
411:
412: /**
413: * Sets the operation.
414: *
415: * @param node node.
416: * @param sb service bean.
417: */
418: private void setOperation(Node node, ServiceBean sb) {
419: Element ele = (Element) node;
420: NodeList namelist = ele
421: .getElementsByTagName(ConfigData.OPERATION);
422:
423: try {
424: Node nd = (Node) namelist.item(0);
425: String namespace = getValue(nd, ConfigData.NAMESPACE_URI);
426: String name = getValue(nd, ConfigData.LOCAL_PART);
427: sb.setOperationNamespace(namespace);
428: sb.setOperation(name);
429: } catch (Exception e) {
430: mLog.severe(mTranslator.getString(SEQ_LOAD_CONFIG_FAILED));
431: }
432: }
433:
434: /**
435: * Sets service.
436: *
437: * @param node node
438: * @param sb bean.
439: */
440: private void setService(Node node, ServiceBean sb) {
441: Element ele = (Element) node;
442: NodeList namelist = ele
443: .getElementsByTagName(ConfigData.SERVICE_NAME);
444:
445: try {
446: Node nd = (Node) namelist.item(0);
447: String namespace = getValue(nd, ConfigData.NAMESPACE_URI);
448: String name = getValue(nd, ConfigData.LOCAL_PART);
449: sb.setNamespace(namespace);
450: sb.setName(name);
451: } catch (Exception e) {
452: mLog.severe(mTranslator.getString(SEQ_LOAD_CONFIG_FAILED));
453: }
454: }
455:
456: /**
457: * Gets the value of the atribute.
458: *
459: * @param n node
460: * @param name name of the node.
461: *
462: * @return value of node.
463: */
464: private String getValue(Node n, String name) {
465: String s = null;
466:
467: try {
468: Element ele = (Element) n;
469: NodeList list = ele.getElementsByTagName(name);
470: Element found = (Element) list.item(0);
471: s = (String) found.getFirstChild().getNodeValue();
472: } catch (Exception e) {
473: e.printStackTrace();
474: }
475:
476: return s;
477: }
478: }
|