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