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