001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.desktop.admin.mbeans.tasks;
006:
007: import java.util.Map;
008: import java.util.Set;
009: import java.util.Iterator;
010: import java.util.Locale;
011: import java.util.ResourceBundle;
012: import java.util.PropertyResourceBundle;
013: import java.util.StringTokenizer;
014: import java.text.MessageFormat;
015:
016: import java.io.InputStream;
017: import java.io.Reader;
018: import java.io.ByteArrayInputStream;
019: import java.io.BufferedReader;
020: import java.io.IOException;
021:
022: import javax.xml.parsers.*;
023: import org.xml.sax.*;
024: import org.xml.sax.helpers.*;
025: import org.w3c.dom.*;
026:
027: import com.sun.portal.desktop.context.DPContext;
028: import com.sun.portal.desktop.dp.DPRoot;
029: import com.sun.portal.desktop.dp.DPNode;
030: import com.sun.portal.desktop.dp.DPChannel;
031: import com.sun.portal.desktop.dp.DPPropertyHolder;
032: import com.sun.portal.desktop.dp.DPContainerChannel;
033: import com.sun.portal.desktop.dp.DPProvider;
034: import com.sun.portal.desktop.dp.DPCollection;
035: import com.sun.portal.desktop.dp.DPAvailable;
036: import com.sun.portal.desktop.dp.DPSelected;
037: import com.sun.portal.desktop.dp.DPProperties;
038: import com.sun.portal.desktop.dp.DPProperty;
039: import com.sun.portal.desktop.dp.DPReferenceList;
040: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
041: import com.sun.portal.desktop.dp.xml.XMLDPErrorHandler;
042: import com.sun.portal.desktop.dp.xml.XMLDPEntityResolver;
043: import com.sun.portal.desktop.dp.xml.XMLDPRoot;
044: import com.sun.portal.desktop.dp.xml.XMLDPCollection;
045: import com.sun.portal.desktop.dp.xml.XMLDPReferenceList;
046: import com.sun.portal.desktop.dp.xml.XMLDPChannel;
047: import com.sun.portal.desktop.dp.xml.XMLDPContainerChannel;
048: import com.sun.portal.desktop.dp.xml.XMLDPProvider;
049:
050: public class DPAUtil {
051:
052: public static final String RESOURCE_BASE = "desktopDPA";
053: public static ResourceBundle rb = PropertyResourceBundle.getBundle(
054: RESOURCE_BASE, Locale.getDefault());
055:
056: public static final int CONTEXT_LINE_NUM = 5;
057:
058: public static void setLocale(java.util.Locale locale) {
059: // load resource bundle based on locale
060: rb = PropertyResourceBundle.getBundle(RESOURCE_BASE, locale);
061: }
062:
063: /**
064: * given a string representation of the locale (i.e. en_US)
065: * create a Locale obj.
066: */
067: public static Locale getLocale(String stringformat) {
068:
069: if (stringformat == null)
070: return Locale.getDefault();
071: StringTokenizer tk = new StringTokenizer(stringformat, "_");
072: String lang = "";
073: String country = "";
074: String variant = "";
075: if (tk.hasMoreTokens())
076: lang = tk.nextToken();
077: if (tk.hasMoreTokens())
078: country = tk.nextToken();
079: if (tk.hasMoreTokens())
080: variant = tk.nextToken();
081: return new java.util.Locale(lang, country, variant);
082:
083: }
084:
085: public static String getLocalizedString(String key) {
086: return rb.getString(key);
087: }
088:
089: public static String getLocalizedString(String key, Object[] objs) {
090:
091: if (objs != null && objs.length > 0) {
092: MessageFormat mf = new MessageFormat("");
093: mf.setLocale(rb.getLocale());
094: mf.applyPattern(rb.getString(key));
095: return mf.format(objs);
096: } else {
097: return rb.getString(key);
098: }
099: }
100:
101: public static void debug(String key) {
102:
103: debug(key, null);
104: }
105:
106: public static void debug(String key, Object[] tokens) {
107:
108: String msg = getLocalizedString(key, tokens);
109: Object[] toks = { msg };
110: System.out.println(getLocalizedString("msgDebug", toks));
111: }
112:
113: public static void warning(String key) {
114:
115: warning(key, null);
116: }
117:
118: public static void warning(String key, Object[] tokens) {
119:
120: String msg = getLocalizedString(key, tokens);
121: Object[] toks = { msg };
122: System.out.println(getLocalizedString("msgWarning", toks));
123: }
124:
125: /**
126: * Note that this method does NOT do DTD validation.
127: * However, if there is no doctype, then an exception is thrown.
128: */
129: public static Element getElement(DPContext adc,
130: InputStream xmlByteStream) throws DPAException {
131:
132: DocumentBuilderFactory dbf = DocumentBuilderFactory
133: .newInstance();
134:
135: DocumentBuilder db = null;
136: try {
137: dbf.setValidating(false);
138: db = dbf.newDocumentBuilder();
139: db.setErrorHandler(new XMLDPErrorHandler(adc));
140: db.setEntityResolver(new XMLDPEntityResolver());
141: } catch (Exception ex) {
142: throw new DPAException("errorDocumentBuilder", ex);
143: }
144:
145: Document d = null;
146: try {
147: xmlByteStream.reset();
148: InputSource is = new InputSource(xmlByteStream);
149:
150: d = db.parse(is);
151: } catch (SAXParseException spe) {
152: Object[] tokens = { Integer.toString(spe.getLineNumber()) };
153: throw new DPAException("errorXMLParseLine", spe, tokens);
154: } catch (Exception e) {
155: throw new DPAException("errorXMLParse", e);
156: }
157:
158: if (d.getDoctype() == null) {
159: throw new DPAException("errorMissingDocType");
160: }
161:
162: return d.getDocumentElement();
163: }
164:
165: /**
166: * combines content of newDpp into oldDpp. note that this one
167: * does not take merging into consideration.
168: */
169: public static void combineProperties(DPCollection oldDpp,
170: DPCollection newDpp) throws DPAException {
171:
172: combineCollections(oldDpp, newDpp);
173:
174: //
175: // reset merge attributes
176: //
177: XMLDPCollection xmlNewDpp = (XMLDPCollection) newDpp;
178: Element e = xmlNewDpp.getElement();
179: oldDpp.setMergeType(newDpp.getMergeType());
180: oldDpp.setLock(xmlNewDpp.isLockedElement(e));
181: oldDpp.setPropagate(xmlNewDpp.isPropagateElement(e));
182: oldDpp.setAdvanced(xmlNewDpp.isAdvancedElement(e));
183: }
184:
185: /**
186: * combines content of newDpl into oldDpl. note that this one
187: * does not take merging into consideration.
188: */
189: public static void combineLists(DPReferenceList oldDpl,
190: DPReferenceList newDpl) throws DPAException {
191:
192: combineCollections(oldDpl, newDpl);
193:
194: //
195: // reset merge attributes
196: //
197: XMLDPReferenceList xmlNewDpl = (XMLDPReferenceList) newDpl;
198: Element e = xmlNewDpl.getElement();
199: oldDpl.setMergeType(newDpl.getMergeType());
200: oldDpl.setLock(xmlNewDpl.isLockedElement(e));
201: oldDpl.setAdvanced(xmlNewDpl.isAdvancedElement(e));
202: }
203:
204: /**
205: * combines content of newDpc into oldDpc. note that this one
206: * does not take merging into consideration.
207: */
208: private static void combineCollections(DPCollection oldDpc,
209: DPCollection newDpc) throws DPAException {
210:
211: XMLDPCollection xnewDpc = (XMLDPCollection) newDpc;
212: Set names = xnewDpc.getNamesFromThis();
213:
214: for (Iterator i = names.iterator(); i.hasNext();) {
215: String name = (String) i.next();
216: DPProperty dpProperty = xnewDpc.getFromThis(name);
217: XMLDPCollection xoldDpc = (XMLDPCollection) oldDpc;
218: DPProperty oldDpProperty = xoldDpc.getFromThis(name);
219:
220: if (oldDpProperty == null) {
221: //
222: // add if not found
223: //
224: oldDpc.add(dpProperty);
225:
226: } else {
227:
228: if (dpProperty.getType() == oldDpProperty.getType()) {
229: if (dpProperty instanceof DPCollection) {
230: //
231: // call recursive combineCollections()
232: // if collection type is detected
233: //
234: DPCollection collection = (DPCollection) dpProperty;
235: DPCollection oldCollection = (DPCollection) oldDpProperty;
236: combineCollections(oldCollection, collection);
237: } else {
238: //
239: // replace existing property if non-collection type
240: //
241: oldDpc.remove(name);
242: oldDpc.add(dpProperty);
243: }
244: } else {
245: //
246: // raise error if types do not match
247: //
248: Object[] tokens = { name };
249: throw new DPAException("errorCombineConflict",
250: tokens);
251: }
252: }
253: }
254: }
255:
256: /**
257: * combines contents of newDpc into oldDpc. note that it
258: * does not take merging into consideration.
259: */
260: public static void combineChannels(DPChannel oldDpc,
261: DPChannel newDpc) throws DPAException {
262:
263: //
264: // raise error if types do not match
265: //
266: if (oldDpc.getType() != newDpc.getType()) {
267: Object[] tokens = { oldDpc.getName() };
268: throw new DPAException("errorCombineConflict", tokens);
269: }
270:
271: //
272: // combine properties
273: //
274: XMLDPChannel xoldDpc = (XMLDPChannel) oldDpc;
275: XMLDPChannel xnewDpc = (XMLDPChannel) newDpc;
276: DPProperties oprops = xoldDpc.getPropertiesFromThis();
277: DPProperties nprops = xnewDpc.getPropertiesFromThis();
278: combineProperties(oprops, nprops);
279:
280: if (oldDpc instanceof DPContainerChannel) {
281: //
282: // combine available/selected list
283: //
284: XMLDPContainerChannel xoldDpcc = (XMLDPContainerChannel) oldDpc;
285: XMLDPContainerChannel xnewDpcc = (XMLDPContainerChannel) newDpc;
286: DPAvailable odpa = xoldDpcc.getAvailableFromThis();
287: DPSelected odps = xoldDpcc.getSelectedFromThis();
288: DPAvailable ndpa = xnewDpcc.getAvailableFromThis();
289: DPSelected ndps = xnewDpcc.getSelectedFromThis();
290: combineLists(odpa, ndpa);
291: combineLists(odps, ndps);
292:
293: //
294: // container may contain child container(s)/channel(s)
295: //
296: Set names = xnewDpc.getChannelNamesFromThis();
297: for (Iterator i = names.iterator(); i.hasNext();) {
298: String name = (String) i.next();
299: DPChannel nch = xnewDpc.getChannelFromThis(name);
300: DPChannel och = null;
301: if ((och = xoldDpc.getChannelFromThis(name)) != null) {
302: //
303: // if found, recursively call combineChannels
304: //
305: combineChannels(och, nch);
306: } else {
307: //
308: // if not found, add
309: //
310: oldDpc.addChannel(nch);
311: }
312: }
313: }
314:
315: //
316: // reset merge attributes
317: //
318: XMLDPChannel xmlNewDpc = (XMLDPChannel) newDpc;
319: Element e = xmlNewDpc.getElement();
320: oldDpc.setProviderName(newDpc.getProviderName());
321: oldDpc.setMergeType(newDpc.getMergeType());
322: oldDpc.setLock(xmlNewDpc.isLockedElement(e));
323: oldDpc.setAdvanced(xmlNewDpc.isAdvancedElement(e));
324: }
325:
326: /**
327: * combines contents of newProv into oldProv. note that it
328: * does not take merging into consideration.
329: */
330: public static void combineProviders(DPProvider oldDpp,
331: DPProvider newDpp) throws DPAException {
332:
333: //
334: // combine properties
335: //
336: XMLDPProvider xoldDpp = (XMLDPProvider) oldDpp;
337: XMLDPProvider xnewDpp = (XMLDPProvider) newDpp;
338: DPProperties oprops = xoldDpp.getPropertiesFromThis();
339: DPProperties nprops = xnewDpp.getPropertiesFromThis();
340: combineProperties(oprops, nprops);
341:
342: //
343: // reset merge attributes
344: //
345: XMLDPProvider xmlNewDpp = (XMLDPProvider) newDpp;
346: Element e = xmlNewDpp.getElement();
347: oldDpp.setClassName(newDpp.getClassName());
348: oldDpp.setProviderVersion(newDpp.getProviderVersion());
349: oldDpp.setMergeType(newDpp.getMergeType());
350: oldDpp.setLock(xmlNewDpp.isLockedElement(e));
351: oldDpp.setAdvanced(xmlNewDpp.isAdvancedElement(e));
352: }
353:
354: /**
355: * combines contents of newDpc into oldDpc. note that it
356: * does not take merging into consideration.
357: */
358: public static void combineRoots(DPRoot oldDpr, DPRoot newDpr)
359: throws DPAException {
360:
361: //
362: // combine properties
363: //
364: XMLDPRoot xoldDpr = (XMLDPRoot) oldDpr;
365: XMLDPRoot xnewDpr = (XMLDPRoot) newDpr;
366: DPProperties oprops = xoldDpr.getPropertiesFromThis();
367: DPProperties nprops = xnewDpr.getPropertiesFromThis();
368: combineProperties(oprops, nprops);
369:
370: //
371: // combine channels
372: //
373: Set channels = xnewDpr.getChannelNamesFromThis();
374: for (Iterator i = channels.iterator(); i.hasNext();) {
375: String ch = (String) i.next();
376: DPChannel odpc = xoldDpr.getChannelFromThis(ch);
377: DPChannel ndpc = xnewDpr.getChannelFromThis(ch);
378: if (odpc != null) {
379: combineChannels(odpc, ndpc);
380: } else {
381: oldDpr.addChannel(ndpc);
382: }
383: }
384:
385: //
386: // combine providers
387: //
388: Set providers = xnewDpr.getProviderNamesFromThis();
389: for (Iterator i = providers.iterator(); i.hasNext();) {
390: String ch = (String) i.next();
391: DPProvider odpc = xoldDpr.getProviderFromThis(ch);
392: DPProvider ndpc = xnewDpr.getProviderFromThis(ch);
393: if (odpc != null) {
394: combineProviders(odpc, ndpc);
395: } else {
396: oldDpr.addProvider(ndpc);
397: }
398: }
399:
400: //
401: // reset merge attributes
402: //
403: XMLDPRoot xmlNewDpr = (XMLDPRoot) newDpr;
404: Element e = xmlNewDpr.getElement();
405: oldDpr.setVersion(newDpr.getVersion());
406: oldDpr.setMergeType(newDpr.getMergeType());
407: oldDpr.setLock(xmlNewDpr.isLockedElement(e));
408: oldDpr.setAdvanced(xmlNewDpr.isAdvancedElement(e));
409: // Note: setPriority may throw exception that's being
410: // igored from the caller (CreatePAR). Please don't add
411: // any line below this line.
412: oldDpr.setPriority(newDpr.getPriority());
413: }
414:
415: public static String getLines(Reader reader, int linenum) {
416: StringBuffer buf = new StringBuffer();
417: String line = null;
418: BufferedReader br = new BufferedReader(reader);
419:
420: try {
421: int i = 1;
422: int beg = linenum - CONTEXT_LINE_NUM;
423: int end = linenum + CONTEXT_LINE_NUM;
424: buf.append("\n");
425: while ((line = br.readLine()) != null) {
426: if (i == linenum) {
427: buf.append("(*)").append(line).append("\n");
428: } else if (i >= beg && i <= end) {
429: buf.append(" ").append(line).append("\n");
430: }
431: ++i;
432: }
433: buf.append("\n");
434: } catch (IOException ioe) {
435: return "";
436: }
437: return buf.toString();
438: }
439: }
|