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