001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * DeployDecriptorReader.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.engine.sequencing.util;
012:
013: import com.sun.jbi.engine.sequencing.SequencingEngineContext;
014: import com.sun.jbi.engine.sequencing.SequencingEngineResources;
015:
016: import org.w3c.dom.Document;
017: import org.w3c.dom.Element;
018: import org.w3c.dom.NamedNodeMap;
019: import org.w3c.dom.Node;
020: import org.w3c.dom.NodeList;
021:
022: import java.util.StringTokenizer;
023: import java.util.logging.Logger;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.parsers.DocumentBuilderFactory;
027:
028: /**
029: * This class reads and loads the SU jbi.xml.
030: *
031: * @author Sun Microsystems, Inc.
032: */
033: public class DeployDescriptorReader extends UtilBase implements
034: SequencingEngineResources {
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: * Endpoint List.
052: */
053: private EndpointInfo[] mEPList;
054:
055: /**
056: * Number of consumers.
057: */
058: private int mNoOfConsumers;
059:
060: /**
061: * Number of providers.
062: */
063: private int mNoOfProviders;
064:
065: /**
066: * The total number of end points in the config file
067: */
068: private int mTotalEndpoints = 0;
069:
070: /**
071: * Creates a new ConfigReader object.
072: */
073: public DeployDescriptorReader() {
074: mLog = SequencingEngineContext.getInstance().getLogger();
075: mTranslator = new StringTranslator();
076: setValid(true);
077: }
078:
079: /**
080: * Returns the number of consumer endpoints.
081: *
082: * @return number of consumers.
083: */
084: public int getConsumerCount() {
085: return mNoOfConsumers;
086: }
087:
088: /**
089: * Sets the endpoint.
090: *
091: * @param node node.
092: * @param ep endpoint info.
093: */
094: public void setEndpoint(Node node, EndpointInfo ep) {
095: NamedNodeMap map = node.getAttributes();
096:
097: try {
098: String epname = map.getNamedItem("endpoint-name")
099: .getNodeValue();
100: String sername = map.getNamedItem("service-name")
101: .getNodeValue();
102: String intername = map.getNamedItem("interface-name")
103: .getNodeValue();
104: mLog.info("LOOK OUT" + epname + " " + sername + " "
105: + intername);
106: ep.setServiceName(new QName(getNamespace(sername),
107: getLocalName(sername)));
108: ep.setInterfaceName(new QName(getNamespace(intername),
109: getLocalName(intername)));
110: ep.setEndpointName(epname);
111: } catch (Exception e) {
112: mLog.severe("Failed loading DD");
113: e.printStackTrace();
114: setError(getError()
115: + mTranslator.getString(SEQ_LOAD_DD_FAILED) + "\n"
116: + e.getMessage());
117: setValid(false);
118: }
119: }
120:
121: /**
122: * Checks if the endpoint is present in the jbi.xml.
123: *
124: * @param sername service name.
125: * @param intername interface name.
126: * @param epname endpoint name.
127: *
128: * @return true if endpoint is present.
129: */
130: public boolean isPresent(QName sername, QName intername,
131: String epname) {
132:
133: for (int ep = 0; ep < mEPList.length; ep++) {
134:
135: if ((mEPList[ep].getServiceName().toString().equals(sername
136: .toString()))
137: && (mEPList[ep].getInterfaceName().toString()
138: .equals(intername.toString()))
139: && (mEPList[ep].getEndpointName().equals(epname))) {
140: return true;
141: }
142: }
143:
144: return false;
145: }
146:
147: /**
148: * Returns number of provider endpoints
149: *
150: * @return number of providers.
151: */
152: public int getProviderCount() {
153: return mNoOfProviders;
154: }
155:
156: /**
157: * Initializes the config file and loads services.
158: *
159: * @param doc Name of the config file.
160: */
161: public void init(Document doc) {
162: try {
163: mDoc = doc;
164: mDoc.getDocumentElement().normalize();
165: load();
166: } catch (Exception genException) {
167: mLog.severe(mTranslator.getString(SEQ_LOAD_DD_FAILED));
168: genException.printStackTrace();
169: setException(genException);
170: setError(getError()
171: + mTranslator.getString(SEQ_LOAD_DD_FAILED) + "\n"
172: + genException.getMessage());
173: setValid(false);
174: }
175: }
176:
177: /**
178: * Loads data.
179: */
180: public void load() {
181: try {
182: NodeList providers = mDoc.getElementsByTagName("provides");
183: mNoOfProviders = providers.getLength();
184:
185: NodeList consumers = mDoc.getElementsByTagName("consumes");
186: mNoOfConsumers = consumers.getLength();
187: mEPList = new EndpointInfo[mNoOfConsumers + mNoOfProviders];
188:
189: for (int i = 0; i < mNoOfProviders; i++) {
190: Node node = providers.item(i);
191: EndpointInfo sb = new EndpointInfo();
192: setEndpoint(node, sb);
193: sb.setProvider();
194:
195: if (!isValid()) {
196: setError(mTranslator.getString(SEQ_LOAD_DD_FAILED)
197: + "\n" + getError());
198:
199: return;
200: }
201:
202: mEPList[i] = sb;
203: }
204:
205: for (int i = 0; i < mNoOfConsumers; i++) {
206: Node node = consumers.item(i);
207: EndpointInfo sb = new EndpointInfo();
208: setEndpoint(node, sb);
209:
210: if (!isValid()) {
211: setError(mTranslator.getString(SEQ_LOAD_DD_FAILED)
212: + "\n" + getError());
213:
214: return;
215: }
216:
217: mEPList[i + mNoOfProviders] = sb;
218: }
219: } catch (Exception e) {
220: e.printStackTrace();
221: setError(getError()
222: + mTranslator.getString(SEQ_LOAD_DD_FAILED) + "\n"
223: + e.getMessage());
224: setValid(false);
225: }
226: }
227:
228: /**
229: * Gets the local name from the quname.
230: *
231: * @param qname Qualified name of service.
232: *
233: * @return String local name
234: */
235: private String getLocalName(String qname) {
236: StringTokenizer tok = new StringTokenizer(qname, ":");
237:
238: try {
239: if (tok.countTokens() == 1) {
240: return qname;
241: }
242:
243: tok.nextToken();
244:
245: return tok.nextToken();
246: } catch (Exception e) {
247: return "";
248: }
249: }
250:
251: /**
252: * Gets the namespace from the qname.
253: *
254: * @param qname Qname of service
255: *
256: * @return namespace namespace of service
257: */
258: private String getNamespace(String qname) {
259: StringTokenizer tok = new StringTokenizer(qname, ":");
260: String prefix = null;
261:
262: try {
263: if (tok.countTokens() == 1) {
264: return "";
265: }
266:
267: prefix = tok.nextToken();
268:
269: NamedNodeMap map = mDoc.getDocumentElement()
270: .getAttributes();
271:
272: for (int j = 0; j < map.getLength(); j++) {
273: Node n = map.item(j);
274:
275: if (n.getLocalName().trim().equals(prefix.trim())) {
276: return n.getNodeValue();
277: }
278: }
279: } catch (Exception e) {
280: ;
281: }
282:
283: return "";
284: }
285:
286: /**
287: * Class which holds the endpoint information.
288: *
289: * @author Sun Microsystems, Inc.
290: */
291: public class EndpointInfo {
292: /**
293: * Interface name.
294: */
295: QName interfacename;
296:
297: /**
298: * Service name.
299: */
300: QName servicename;
301:
302: /**
303: * Endpoint name.
304: */
305: String endpointname;
306:
307: /**
308: * Provider endpoint.
309: */
310: boolean provider = false;
311:
312: /**
313: * Sets the endpoint name.
314: *
315: * @param epname endpoint name.
316: */
317: public void setEndpointName(String epname) {
318: endpointname = epname;
319: }
320:
321: /**
322: * Returns the endpoint name.
323: *
324: * @return endpoint name.
325: */
326: public String getEndpointName() {
327: return endpointname;
328: }
329:
330: /**
331: * Sets the interface name.
332: *
333: * @param intername interface name.
334: */
335: public void setInterfaceName(QName intername) {
336: interfacename = intername;
337: }
338:
339: /**
340: * Returns the interface name.
341: *
342: * @return interface name.
343: */
344: public QName getInterfaceName() {
345: return interfacename;
346: }
347:
348: /**
349: * Sets the endpoint as provider.
350: */
351: public void setProvider() {
352: provider = true;
353: }
354:
355: /**
356: * Returns true if the endpoint is provider.
357: *
358: * @return true if provider endpoint.
359: */
360: public boolean getProvider() {
361: return provider;
362: }
363:
364: /**
365: * Sets the service name.
366: *
367: * @param sername service name.
368: */
369: public void setServiceName(QName sername) {
370: servicename = sername;
371: }
372:
373: /**
374: * Returns the service name.
375: *
376: * @return the service name.
377: */
378: public QName getServiceName() {
379: return servicename;
380: }
381:
382: }
383: }
|