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: * @(#)DeployDescriptorReader.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.file.util;
030:
031: import com.sun.jbi.binding.file.FileBindingContext;
032: import com.sun.jbi.binding.file.FileBindingResources;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.NamedNodeMap;
037: import org.w3c.dom.Node;
038: import org.w3c.dom.NodeList;
039:
040: import java.util.StringTokenizer;
041: import java.util.logging.Logger;
042: import java.util.List;
043: import java.util.ArrayList;
044:
045: import javax.xml.namespace.QName;
046: import javax.xml.parsers.DocumentBuilderFactory;
047:
048: /**
049: * This class reads the SU jbi.xml and loads up the values.
050: *
051: * @author Sun Microsystems, Inc.
052: */
053: public class DeployDescriptorReader extends UtilBase implements
054: FileBindingResources {
055: /**
056: * Document object of the XML config file.
057: */
058: private Document mDoc;
059:
060: /**
061: * Logger Object
062: */
063: private Logger mLog;
064:
065: /**
066: * i18n
067: */
068: private StringTranslator mTranslator;
069:
070: /**
071: * List of endpoints in the SU jbi.xml.
072: */
073: private EndpointInfo[] mEPList;
074:
075: /**
076: * Number of consumers.
077: */
078: private int mNoOfConsumers;
079:
080: /**
081: * Number of providers.
082: */
083: private int mNoOfProviders;
084:
085: /**
086: * The total number of end points in the config file
087: */
088: private int mTotalEndpoints = 0;
089:
090: /**
091: * Type of deployment, WSDL, XML or WSDL11
092: */
093: private String mType;
094:
095: /**
096: * Creates a new ConfigReader object.
097: */
098: public DeployDescriptorReader() {
099: mLog = FileBindingContext.getInstance().getLogger();
100: mTranslator = new StringTranslator();
101: setValid(true);
102: }
103:
104: public List getKeyList() {
105: ArrayList list = new ArrayList();
106: try {
107: for (int ep = 0; ep < mEPList.length; ep++) {
108: list.add(mEPList[ep].getKey());
109: }
110: } catch (Exception e) {
111: ;
112: }
113: return list;
114: }
115:
116: /**
117: * Returns the number of consumer endpoints.
118: *
119: * @return consumer endpoint count.
120: */
121: public int getConsumerCount() {
122: return mNoOfConsumers;
123: }
124:
125: /**
126: * Sets the endpoitn attributes.
127: *
128: * @param node provider/consumer node.
129: * @param ep endpoint information.
130: */
131: public void setEndpoint(Node node, EndpointInfo ep) {
132: NamedNodeMap map = node.getAttributes();
133:
134: try {
135: String epname = map.getNamedItem("endpoint-name")
136: .getNodeValue();
137: String sername = map.getNamedItem("service-name")
138: .getNodeValue();
139: String intername = map.getNamedItem("interface-name")
140: .getNodeValue();
141: ep.setServiceName(new QName(getNamespace(sername),
142: getLocalName(sername)));
143: ep.setInterfaceName(new QName(getNamespace(intername),
144: getLocalName(intername)));
145: ep.setEndpointName(epname);
146: } catch (Exception e) {
147: e.printStackTrace();
148: setError(getError()
149: + mTranslator.getString(FBC_LOAD_DD_FAILED) + "\n"
150: + e.getMessage());
151: setValid(false);
152: }
153: }
154:
155: /**
156: * Sets the type of artifact.
157: */
158: private void setType(String type) {
159: mType = type;
160: }
161:
162: /**
163: * Gets the type of artifact
164: */
165:
166: public String getType() {
167: return mType;
168: }
169:
170: private void setArtifacts() {
171: NodeList namelist = mDoc.getElementsByTagNameNS("*",
172: "artifactstype");
173:
174: if (namelist == null) {
175: /* This means the tag is not present. default type is WSDL20
176: */
177: setType("WSDL20");
178: return;
179: }
180:
181: Element name = (Element) namelist.item(0);
182: String sValue = null;
183:
184: try {
185: sValue = ((Node) (name.getChildNodes().item(0)))
186: .getNodeValue().trim();
187: } catch (NullPointerException ne) {
188: setType("WSDL20");
189: return;
190: }
191:
192: setType(sValue);
193: }
194:
195: /**
196: * Verifies if the endpoint in the artifacts XML file is present in
197: * the deployment descriptor.
198: *
199: * @param sername service name.
200: * @param intername interface name.
201: * @param epname endpoint name.
202: * @param role role.
203: *
204: * @return true if present.
205: */
206: public boolean isPresent(QName sername, QName intername,
207: String epname, int role) {
208:
209: for (int ep = 0; ep < mEPList.length; ep++) {
210:
211: if ((mEPList[ep].getServiceName().toString().equals(sername
212: .toString()))
213: && (mEPList[ep].getInterfaceName().toString()
214: .equals(intername.toString()))
215: && (mEPList[ep].getEndpointName().equals(epname))
216: && (mEPList[ep].getStyle() == role)) {
217: return true;
218: }
219: }
220:
221: return false;
222: }
223:
224: /**
225: * Returns the number of provider endpoints.
226: *
227: * @return provider endpoint count.
228: */
229: public int getProviderCount() {
230: return mNoOfProviders;
231: }
232:
233: /**
234: * Initializes the config file and loads services.
235: *
236: * @param doc Name of the config file.
237: */
238: public void init(Document doc) {
239: try {
240: mDoc = doc;
241: mDoc.getDocumentElement().normalize();
242: load();
243: } catch (Exception genException) {
244: mLog.severe(mTranslator.getString(FBC_LOAD_DD_FAILED));
245: genException.printStackTrace();
246: setException(genException);
247: setError(getError()
248: + mTranslator.getString(FBC_LOAD_DD_FAILED) + "\n"
249: + genException.getMessage());
250: setValid(false);
251: }
252: }
253:
254: /**
255: * Loads the data.
256: */
257: public void load() {
258: try {
259: NodeList providers = mDoc.getElementsByTagName("provides");
260: mNoOfProviders = providers.getLength();
261:
262: NodeList consumers = mDoc.getElementsByTagName("consumes");
263: mNoOfConsumers = consumers.getLength();
264: mEPList = new EndpointInfo[mNoOfConsumers + mNoOfProviders];
265: setArtifacts();
266: for (int i = 0; i < mNoOfProviders; i++) {
267: Node node = providers.item(i);
268: EndpointInfo sb = new EndpointInfo();
269: setEndpoint(node, sb);
270: sb.setProvider();
271:
272: if (!isValid()) {
273: setError(mTranslator.getString(FBC_LOAD_DD_FAILED)
274: + "\n" + getError());
275:
276: return;
277: }
278:
279: mEPList[i] = sb;
280: }
281:
282: for (int i = 0; i < mNoOfConsumers; i++) {
283: Node node = consumers.item(i);
284: EndpointInfo sb = new EndpointInfo();
285: setEndpoint(node, sb);
286:
287: if (!isValid()) {
288: setError(mTranslator.getString(FBC_LOAD_DD_FAILED)
289: + "\n" + getError());
290:
291: return;
292: }
293:
294: mEPList[i + mNoOfProviders] = sb;
295: }
296: } catch (Exception e) {
297: mLog.severe("Failed loading DD");
298: e.printStackTrace();
299: setError(getError()
300: + mTranslator.getString(FBC_LOAD_DD_FAILED) + "\n"
301: + e.getMessage());
302: setValid(false);
303: }
304: }
305:
306: /**
307: * Gets the local name from the quname.
308: *
309: * @param qname Qualified name of service.
310: *
311: * @return String local name
312: */
313: private String getLocalName(String qname) {
314: StringTokenizer tok = new StringTokenizer(qname, ":");
315:
316: try {
317: if (tok.countTokens() == 1) {
318: return qname;
319: }
320:
321: tok.nextToken();
322:
323: return tok.nextToken();
324: } catch (Exception e) {
325: return "";
326: }
327: }
328:
329: /**
330: * Gets the namespace from the qname.
331: *
332: * @param qname Qname of service
333: *
334: * @return namespace namespace of service
335: */
336: private String getNamespace(String qname) {
337: StringTokenizer tok = new StringTokenizer(qname, ":");
338: String prefix = null;
339:
340: try {
341: if (tok.countTokens() == 1) {
342: return "";
343: }
344:
345: prefix = tok.nextToken();
346:
347: NamedNodeMap map = mDoc.getDocumentElement()
348: .getAttributes();
349:
350: for (int j = 0; j < map.getLength(); j++) {
351: Node n = map.item(j);
352:
353: if (n.getLocalName().trim().equals(prefix.trim())) {
354: return n.getNodeValue();
355: }
356: }
357: } catch (Exception e) {
358: ;
359: }
360:
361: return "";
362: }
363:
364: /**
365: * Class which holds the endpoint information.
366: *
367: * @author Sun Microsystems, Inc.
368: */
369: public class EndpointInfo {
370: /**
371: * Interface name.
372: */
373: QName interfacename;
374:
375: /**
376: * Service name.
377: */
378: QName servicename;
379:
380: /**
381: * Endpoint name.
382: */
383: String endpointname;
384:
385: /**
386: * Provider endpoint.
387: */
388: boolean provider = false;
389:
390: /**
391: * Sets the endpoint name.
392: *
393: * @param epname endpoint name.
394: */
395: public void setEndpointName(String epname) {
396: endpointname = epname;
397: }
398:
399: /**
400: * Returns the endpoint name.
401: *
402: * @return endpoint name.
403: */
404: public String getEndpointName() {
405: return endpointname;
406: }
407:
408: /**
409: * Sets the interface name.
410: *
411: * @param intername interface name.
412: */
413: public void setInterfaceName(QName intername) {
414: interfacename = intername;
415: }
416:
417: /**
418: * Returns the interface name.
419: *
420: * @return interface name.
421: */
422: public QName getInterfaceName() {
423: return interfacename;
424: }
425:
426: /**
427: * Sets the endpoint as provider.
428: */
429: public void setProvider() {
430: provider = true;
431: }
432:
433: /**
434: * Returns true if the endpoint is provider.
435: *
436: * @return true if provider endpoint.
437: */
438: public boolean getProvider() {
439: return provider;
440: }
441:
442: /**
443: * Sets the service name.
444: *
445: * @param sername service name.
446: */
447: public void setServiceName(QName sername) {
448: servicename = sername;
449: }
450:
451: /**
452: * Returns the service name.
453: *
454: * @return the service name.
455: */
456: public QName getServiceName() {
457: return servicename;
458: }
459:
460: /**
461: * Returns the style of endpoint.
462: *
463: * @return provider or consumer.
464: */
465: public int getStyle() {
466: if (getProvider()) {
467: return ConfigData.PROVIDER;
468: } else {
469: return ConfigData.CONSUMER;
470: }
471: }
472:
473: /**
474: * Returns a unique key combination
475: *
476: * @return string
477: */
478: public String getKey() {
479: if (getStyle() == ConfigData.PROVIDER) {
480: return servicename.toString()
481: + interfacename.toString() + endpointname
482: + ConfigData.PROVIDER_STRING;
483: } else {
484: return servicename.toString()
485: + interfacename.toString() + endpointname
486: + ConfigData.CONSUMER_STRING;
487: }
488: }
489: }
490: }
|