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: * @(#)ServiceAssemblyInfo.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.ui.common;
030:
031: import com.sun.jbi.ui.common.ServiceAssemblyDD.ServiceUnitDD;
032: import java.io.PrintWriter;
033: import java.io.Reader;
034: import java.io.StringReader;
035: import java.io.StringWriter;
036: import java.util.ArrayList;
037: import java.util.Collections;
038: import java.util.Comparator;
039: import java.util.Iterator;
040: import java.util.List;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043: import org.w3c.dom.NodeList;
044:
045: /** This class reads/writes the service assembly info list xml.
046: * @author Sun Microsystems, Inc.
047: */
048: public class ServiceAssemblyInfo {
049: /** namespace */
050: public static final String XMLNS = "http://java.sun.com/xml/ns/jbi/service-assembly-info-list";
051:
052: /**
053: * state
054: */
055: public static final String STARTED_STATE = "Started";
056: /**
057: * state
058: */
059: public static final String STOPPED_STATE = "Stopped";
060: /**
061: * state
062: */
063: public static final String SHUTDOWN_STATE = "Shutdown";
064: /**
065: * state
066: */
067: public static final String UNKNOWN_STATE = "Unknown";
068: /**
069: * state
070: */
071: public static final String ANY_STATE = "any";
072: /**
073: * state
074: */
075: public static final String ANY_FW_STATE = "any";
076:
077: /**
078: * Holds value of property name.
079: */
080: private String mName;
081: /**
082: * Holds value of property name.
083: */
084: private List mSUInfoList;
085:
086: /**
087: * Holds value of property description.
088: */
089: private String mDescription;
090:
091: /**
092: * Holds value of property state.
093: */
094: private String mState;
095:
096: /** Creates a new instance of JBIComponentInfo */
097: public ServiceAssemblyInfo() {
098: this ("", "", UNKNOWN_STATE);
099: }
100:
101: /**
102: * Creates a new instance of JBIComponentInfo
103: * @param state state
104: * @param name name
105: * @param description description
106: */
107: public ServiceAssemblyInfo(String name, String description,
108: String state) {
109: this .mName = name;
110: this .mDescription = description;
111: this .mState = state;
112: this .mSUInfoList = new ArrayList();
113: }
114:
115: /**
116: * Getter for property aliasName.
117: * @return Value of property aliasName.
118: */
119: public String getName() {
120: return this .mName;
121: }
122:
123: /**
124: * Setter for property aliasName.
125: * @param name New value of property aliasName.
126: */
127: public void setName(String name) {
128: this .mName = name;
129: }
130:
131: /**
132: * Getter for property description.
133: * @return Value of property description.
134: */
135: public String getDescription() {
136: return this .mDescription;
137: }
138:
139: /**
140: * Setter for property description.
141: * @param description New value of property description.
142: */
143: public void setDescription(String description) {
144: this .mDescription = description;
145: }
146:
147: /**
148: * Getter for property state.
149: * @return Value of property state.
150: */
151: public String getState() {
152: return this .mState;
153: }
154:
155: /**
156: * Setter for property state.
157: * @param state state
158: */
159: public void setState(String state) {
160: this .mState = state;
161: }
162:
163: /**
164: * Getter for property description.
165: * @return Value of property description.
166: */
167: public List getServiceUnitInfoList() {
168: return this .mSUInfoList;
169: }
170:
171: /** string value
172: * @return string value
173: */
174: public String toString() {
175: return "Name = " + this .getName() + "\nDescription = "
176: + this .getDescription() + "\nState = "
177: + this .getState() + "\nService Units = "
178: + this .getServiceUnitInfoList().size();
179:
180: }
181:
182: /** xml text
183: * @return xml text
184: */
185: public String toXmlString() {
186: List suInfoList = getServiceUnitInfoList();
187: String suInfoText = ServiceUnitInfo.writeAsXmlText(suInfoList);
188:
189: return "<service-assembly-info \n" + "name=\"" + this .getName()
190: + "\" \n" + "state=\"" + this .getState() + "\" >\n"
191: + "<description>" + this .getDescription()
192: + "</description> \n" + suInfoText + "\n"
193: + "</service-assembly-info>";
194: }
195:
196: /**
197: * return component info object
198: * @param saInfoEl xml element
199: * @return ServiceAssemblyInfo.
200: */
201: public static ServiceAssemblyInfo createServiceAssemblyInfo(
202: Element saInfoEl) {
203:
204: ServiceAssemblyInfo info = new ServiceAssemblyInfo();
205:
206: String name = saInfoEl.getAttribute("name");
207: String state = saInfoEl.getAttribute("state");
208:
209: info.setName(name);
210: info.setState(state);
211:
212: String desc = null;
213: Element descEl = DOMUtil.UTIL.getChildElement(saInfoEl,
214: "description");
215: if (descEl != null) {
216: desc = DOMUtil.UTIL.getTextData(descEl);
217: }
218: info.setDescription(desc);
219:
220: // construct the su info list
221:
222: NodeList suInfoElList = DOMUtil.UTIL.getChildElements(saInfoEl,
223: "service-unit-info");
224:
225: List list = ServiceUnitInfo
226: .createServiceUnitInfoList(suInfoElList);
227:
228: info.getServiceUnitInfoList().addAll(list);
229:
230: return info;
231: }
232:
233: /**
234: * creates list of objects
235: * @return list of objects
236: * @param saInfoListEl xml element.
237: */
238: public static List createServiceAssemblyInfoList(
239: Element saInfoListEl) {
240: ArrayList saInfoList = new ArrayList();
241:
242: // construct the compInfo list
243: if (saInfoListEl == null) {
244: // System.out.println("No Root Element <compInfoList>");
245: return saInfoList; // return empty list
246: }
247:
248: String version = saInfoListEl.getAttribute("version");
249:
250: NodeList saInfoNodeList = DOMUtil.UTIL.getChildElements(
251: saInfoListEl, "service-assembly-info");
252: int size = saInfoNodeList.getLength();
253: // System.out.println("compInfo found in XML Document : " + size);
254: for (int i = 0; i < size; ++i) {
255: Element saInfoEl = (Element) saInfoNodeList.item(i);
256: if (saInfoEl != null) {
257: ServiceAssemblyInfo saInfo = createServiceAssemblyInfo(saInfoEl);
258: saInfoList.add(saInfo);
259: }
260: }
261:
262: ServiceAssemblyInfo.sort(saInfoList);
263: return saInfoList;
264: }
265:
266: /** creates list of objects
267: * @param xmlReader xml text reader
268: * @return list of objects
269: */
270: public static List readFromXmlTextWithProlog(Reader xmlReader) {
271: ArrayList saInfoList = new ArrayList();
272: Document xmlDoc = null;
273:
274: try {
275: xmlDoc = DOMUtil.UTIL.buildDOMDocument(xmlReader);
276: } catch (Exception ex) {
277: Util.logDebug(ex);
278: }
279:
280: if (xmlDoc == null) {
281: return saInfoList; // return empty list
282: }
283:
284: // construct the compInfo list
285: Element saInfoListEl = DOMUtil.UTIL.getElement(xmlDoc,
286: "service-assembly-info-list");
287: if (saInfoListEl == null) {
288: // System.out.println("No Root Element <compInfoList>");
289: return saInfoList; // return empty list
290: }
291:
292: return createServiceAssemblyInfoList(saInfoListEl);
293: }
294:
295: /** creates list of objects
296: * @param xmlText xml text
297: * @return list of objects
298: */
299: public static List readFromXmlTextWithProlog(String xmlText) {
300: return readFromXmlTextWithProlog(new StringReader(xmlText));
301: }
302:
303: /**
304: * write to xml text
305: * @return xml text
306: * @param saInfoList list.
307: */
308: public static String writeAsXmlText(List saInfoList) {
309: ServiceAssemblyInfo.sort(saInfoList);
310:
311: StringWriter stringWriter = new StringWriter();
312: PrintWriter writer = new PrintWriter(stringWriter, true);
313: // begine xml
314: writer.println("<service-assembly-info-list "
315: + " version=\"1.0\" " + " xmlns=\""
316: + ServiceAssemblyInfo.XMLNS + "\" " + " >");
317: int size = saInfoList.size();
318: for (int i = 0; i < size; ++i) {
319: ServiceAssemblyInfo info = (ServiceAssemblyInfo) saInfoList
320: .get(i);
321: writer.println(info.toXmlString());
322: }
323: // end xml
324: writer.println("</service-assembly-info-list>");
325:
326: try {
327: writer.close();
328: stringWriter.close();
329: } catch (Exception ex) {
330: // ignore as it will never happen for strings
331: }
332: return stringWriter.toString();
333: }
334:
335: /**
336: * write to xml text
337: * @return xml text
338: * @param saInfoList list.
339: */
340: public static String writeAsXmlTextWithProlog(List saInfoList) {
341: StringBuffer buffer = new StringBuffer();
342: // begine xml
343: buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
344: buffer.append("\n");
345: buffer.append(writeAsXmlText(saInfoList));
346: buffer.append("\n");
347: // end xml
348: return buffer.toString();
349: }
350:
351: /**
352: * creates sa info
353: * @return ServiceAssemblyINfo
354: * @param saDD sa descriptor.
355: */
356: public static ServiceAssemblyInfo createFromServiceAssemblyDD(
357: ServiceAssemblyDD saDD) {
358:
359: ServiceAssemblyInfo saInfo = new ServiceAssemblyInfo();
360:
361: saInfo.setName(saDD.getName());
362: saInfo.setDescription(saDD.getDescription());
363:
364: // init su info.
365: List suDDList = saDD.getServiceUnitDDList();
366: List suInfoList = new ArrayList();
367: for (Iterator itr = suDDList.iterator(); itr.hasNext();) {
368: ServiceUnitInfo suInfo = ServiceUnitInfo
369: .createFromServiceUnitDD((ServiceUnitDD) itr.next());
370: suInfoList.add(suInfo);
371: }
372: saInfo.getServiceUnitInfoList().addAll(suInfoList);
373:
374: return saInfo;
375: }
376:
377: /**
378: * create sa info
379: * @param jbiXmlReader reader
380: * @return sa info object
381: */
382: public static ServiceAssemblyInfo createFromServiceAssemblyDD(
383: Reader jbiXmlReader) {
384: try {
385: ServiceAssemblyDD dd = (ServiceAssemblyDD) ServiceAssemblyDD
386: .createJBIDescriptor(jbiXmlReader);
387: if (dd != null) {
388: return createFromServiceAssemblyDD(dd);
389: }
390: } catch (Exception ex) {
391: Util.logDebug(ex);
392: }
393: return null;
394: }
395:
396: /**
397: * sorts the list.
398: * @param saInfoList list.
399: */
400: public static void sort(List saInfoList) {
401: try {
402: Collections.sort(saInfoList, new Comparator() {
403: public int compare(Object o1, Object o2) {
404: return ((ServiceAssemblyInfo) o1).getName()
405: .compareTo(
406: ((ServiceAssemblyInfo) o2)
407: .getName());
408: }
409: });
410: } catch (ClassCastException ccEx) {
411: // log and
412: // do nothing.
413: } catch (UnsupportedOperationException unsupEx) {
414: // log and
415: // do nothing.
416: }
417: }
418:
419: }
|