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.xslt.util;
030:
031: import com.sun.jbi.engine.xslt.TEResources;
032: import com.sun.jbi.engine.xslt.TransformationEngineContext;
033:
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.NodeList;
038:
039: import java.io.File;
040:
041: import java.util.logging.Logger;
042:
043: import javax.jbi.component.ComponentContext;
044:
045: /**
046: * DOCUMENT ME!
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public class ServiceListReader implements TEResources {
051: /**
052: * DOCUMENT ME!
053: */
054: private ComponentContext mContext = null;
055:
056: /**
057: * DOCUMENT ME!
058: */
059: private File mServiceListFile;
060:
061: /**
062: *
063: */
064: private Logger mLogger = null;
065:
066: /**
067: * DOCUMENT ME!
068: */
069: private String mDeployId = null;
070:
071: /**
072: * DOCUMENT ME!
073: */
074: private String mError;
075:
076: /**
077: *
078: */
079: private String mListFile;
080:
081: /**
082: * DOCUMENT ME!
083: */
084: private String mSchemaFile;
085:
086: /**
087: * DOCUMENT ME!
088: */
089: private String mServiceListName;
090:
091: /**
092: *
093: */
094: private StringTranslator mTranslator = null;
095:
096: /**
097: * DOCUMENT ME!
098: */
099: private ConfigBean[] mListofService = null;
100:
101: /**
102: * DOCUMENT ME!
103: */
104: private int mServiceCount = 0;
105:
106: /**
107: * Creates a new ServiceListReader object.
108: *
109: * @param suPath Service Unit path!
110: * @param jbiContext Component context
111: */
112: public ServiceListReader(String suPath, ComponentContext jbiContext) {
113: mTranslator = new StringTranslator("com.sun.jbi.engine.xslt",
114: this .getClass().getClassLoader());
115: mDeployId = suPath;
116: mContext = jbiContext;
117:
118: mListFile = suPath + File.separatorChar
119: + ConfigData.CONFIG_FILE_NAME;
120: mSchemaFile = jbiContext.getInstallRoot() + File.separatorChar
121: + ConfigData.SCHEMA_FILE_NAME;
122:
123: mLogger = TransformationEngineContext.getInstance().getLogger(
124: "");
125: }
126:
127: /**
128: * DOCUMENT ME!
129: *
130: * @return DOCUMENT ME!
131: */
132: public String getError() {
133: return mError;
134: }
135:
136: /**
137: * DOCUMENT ME!
138: *
139: * @return DOCUMENT ME!
140: */
141: public String getListName() {
142: return mServiceListName;
143: }
144:
145: /**
146: * DOCUMENT ME!
147: *
148: * @return DOCUMENT ME!
149: */
150: public int getServiceCount() {
151: return mServiceCount;
152: }
153:
154: /**
155: * DOCUMENT ME!
156: *
157: * @return DOCUMENT ME!
158: */
159: public ConfigBean[] getServices() {
160: return mListofService;
161: }
162:
163: /**
164: * DOCUMENT ME!
165: *
166: * @return DOCUMENT ME!
167: */
168: public boolean check() {
169: if ((mDeployId == null) || (mDeployId.equals(""))) {
170: setError(mTranslator
171: .getString(TEResources.INVALID_DEPLOY_ID)
172: + mDeployId);
173:
174: return false;
175: }
176:
177: String root = mContext.getInstallRoot();
178:
179: mServiceListFile = new File(mListFile);
180:
181: if (!mServiceListFile.exists()) {
182: setError(mTranslator
183: .getString(TEResources.INVALID_SERVICE_LOCATION)
184: + mServiceListFile.getAbsolutePath());
185:
186: return false;
187: }
188:
189: return true;
190: }
191:
192: /**
193: * DOCUMENT ME!
194: */
195: public void loadServices() {
196: if (!check()) {
197: setError(mTranslator.getString(TEResources.CHECK_FAILED)
198: + getError());
199:
200: return;
201: }
202:
203: ServiceListValidator slv = new ServiceListValidator(
204: mSchemaFile, mServiceListFile.getAbsolutePath());
205:
206: // slv.validate();
207: Document listDoc = slv.parse();
208:
209: // code for extracting namespace
210: //Element elem = listDoc.getDocumentElement();
211: //String namespace = elem.getAttribute(ConfigData.SERVICE_NAMESPACE);
212:
213: if (listDoc == null) {
214: setError(slv.getError());
215:
216: return;
217: }
218:
219: setListName(listDoc);
220:
221: NodeList list = listDoc
222: .getElementsByTagName(ConfigData.SERVICE);
223: mServiceCount = list.getLength();
224: mLogger.info(mTranslator.getString(TEResources.TOTAL_SERVICE)
225: + mServiceCount);
226: mListofService = new ConfigBean[mServiceCount];
227:
228: try {
229: for (int i = 0; i < mServiceCount; i++) {
230: Node node = list.item(i);
231: ConfigBean sb = new ConfigBean();
232:
233: if (node.getNodeType() == Node.ELEMENT_NODE) {
234: /*if (namespace != null)
235: {
236: sb.setValue(ConfigData.SERVICE_NAMESPACE, namespace);
237: }*/
238:
239: //For each <Service> tag, there should be only one
240: //<service-name> tag
241: Element servEle = (Element) node;
242: NodeList servNodeList = servEle
243: .getElementsByTagName(ConfigData.SERVICE_NAME);
244:
245: //There should be one and only one <service-name> element
246: //under each <service> tag.
247: Node sNode = servNodeList.item(0);
248:
249: if (sNode != null) {
250: setByTagName(sNode, sb,
251: ConfigData.SERVICE_NAMESPACE);
252: setByTagName(sNode, sb,
253: ConfigData.SERVICENAME_LOCALPART);
254: } else {
255: mLogger
256: .severe("Some problem getting service-name element. Check.");
257: }
258:
259: //setByTagName(node, sb, ConfigData.NAME);
260: //setByTagName(node, sb, ConfigData.SERVICE_NAMESPACE);
261: setByTagName(node, sb, ConfigData.ENDPOINT);
262: setByTagName(node, sb,
263: ConfigData.SERVICE_DESCRIPTION);
264: setByTagName(node, sb, ConfigData.CREATE_CACHE);
265: setByTagName(node, sb, ConfigData.SERVICEID);
266: setByTagName(node, sb, ConfigData.COLUMN_HEADERS);
267: setByTagName(node, sb, ConfigData.COLUMN_HEADER);
268: setByTagName(node, sb, ConfigData.COLUMN_SEPARATOR);
269: setByTagName(node, sb,
270: ConfigData.FIRST_ROW_COL_HEADERS);
271: }
272:
273: mListofService[i] = sb;
274: mLogger.finer(mTranslator
275: .getString(TEResources.BEAN_UPLOADED)
276: + mListofService[i]);
277: }
278: } catch (Exception ee) {
279: setError(ee.getMessage());
280: ee.printStackTrace();
281: }
282: }
283:
284: /**
285: * Utility method for setting the Bean object from the XML Nodes.
286: *
287: * @param node The node which needs to be
288: * @param sb The end point bean object which has to be updated
289: * @param tagName the tag name which needs to be read.
290: */
291: private void setByTagName(Node node, ConfigBean sb, String tagName) {
292: Element ele = (Element) node;
293: NodeList namelist = ele.getElementsByTagName(tagName);
294:
295: if (namelist != null) {
296: for (int i = 0; i < namelist.getLength(); i++) {
297: Element name = (Element) namelist.item(i);
298: String sValue = null;
299:
300: try {
301: sValue = ((Node) (name.getChildNodes().item(0)))
302: .getNodeValue().trim();
303: } catch (NullPointerException ne) {
304: //ne.printStackTrace();
305: return;
306: }
307:
308: mLogger.finer("Setting " + tagName + " to value = "
309: + sValue + " *********");
310: sb.setValue(tagName, sValue);
311: }
312: }
313: }
314:
315: /**
316: * DOCUMENT ME!
317: *
318: * @param error DOCUMENT ME!
319: */
320: private void setError(String error) {
321: mError = error;
322: }
323:
324: /**
325: * DOCUMENT ME!
326: *
327: * @param doc NOT YET DOCUMENTED
328: */
329: private void setListName(Document doc) {
330: NodeList nl = doc.getElementsByTagName(ConfigData.SERVICE);
331: Node serviceListName = nl.item(0);
332:
333: try {
334: mServiceListName = serviceListName.getFirstChild()
335: .getNodeValue();
336: } catch (Exception e) {
337: e.printStackTrace();
338: mServiceListName = "";
339: }
340: }
341: }
|