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.dp.cli;
006:
007: import java.util.Map;
008: import java.util.Set;
009: import java.util.Iterator;
010: import java.util.logging.Level;
011: import java.util.logging.Logger;
012:
013: import java.io.InputStream;
014: import java.io.StringReader;
015: import org.w3c.dom.Element;
016: import org.xml.sax.SAXParseException;
017:
018: import com.sun.portal.desktop.context.AdminDPContext;
019: import com.sun.portal.desktop.context.DPContext;
020:
021: import com.sun.portal.desktop.dp.DPError;
022: import com.sun.portal.desktop.dp.DPRoot;
023: import com.sun.portal.desktop.dp.DPNode;
024: import com.sun.portal.desktop.dp.DPProvider;
025: import com.sun.portal.desktop.dp.DPPropertyHolder;
026: import com.sun.portal.desktop.dp.DPContainerChannel;
027: import com.sun.portal.desktop.dp.DPChannel;
028: import com.sun.portal.desktop.dp.DPProperties;
029: import com.sun.portal.desktop.dp.DPProperty;
030:
031: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
032: import com.sun.portal.desktop.dp.xml.XMLDPTags;
033: import com.sun.portal.desktop.dp.xml.XMLDPRoot;
034: import com.sun.portal.desktop.dp.xml.XMLDPNode;
035: import com.sun.portal.desktop.dp.xml.XMLDPPropertyHolder;
036: import com.sun.portal.desktop.dp.xml.XMLDPProperties;
037: import com.sun.portal.log.common.PortalLogger;
038:
039: import java.util.logging.Logger;
040: import java.util.logging.Level;
041:
042: class DPAAdd {
043:
044: private static Logger logger = PortalLogger.getLogger(DPAAdd.class);
045: // XML DP Factory
046: XMLDPFactory dpf = XMLDPFactory.getInstance();
047:
048: boolean verbose = false;
049:
050: public void process(AdminDPContext adc, String dn, boolean global,
051: String parent, InputStream[] xmlByteStreams,
052: boolean verbose, boolean dryrun, Logger logger)
053: throws DPAException {
054:
055: this .verbose = verbose;
056:
057: logger.finest("PSDT_CSPDDC0004");
058:
059: // get document
060: String doc = null;
061: try {
062: if (!global) {
063: doc = adc.getDPDocument(dn);
064: } else {
065: doc = adc.getGlobalDPDocument();
066: }
067: } catch (Throwable ex) {
068: Object[] tokens = { global ? DPAUtil
069: .getLocalizedString("msgGlobal") : dn };
070: throw new DPAException("errorRetrieveDP", ex, tokens);
071: }
072:
073: // build dproot
074: DPRoot dpr = null;
075: try {
076: if (doc != null && doc.length() > 0) {
077: dpr = dpf.createRoot(adc, doc);
078: }
079: } catch (Throwable ex) {
080: Object[] tokens = { global ? DPAUtil
081: .getLocalizedString("msgGlobal") : dn };
082: throw new DPAException("errorCreateDPRoot", ex, tokens);
083: }
084:
085: // iterate over xml input
086: for (int i = 0; i < xmlByteStreams.length; i++) {
087: dpr = doAdd(adc, dn, global, parent, xmlByteStreams[i],
088: verbose, dpr);
089: }
090:
091: logger.finest("PSDT_CSPDDC0005");
092:
093: //
094: // FIXME(susu): thre may be a better way to check for
095: // invalid xml.
096: //
097:
098: // check for invalid XML
099: String dpDoc = null;
100: try {
101: //
102: // set dirty to false before writing out.
103: // not doing this will cause "dirty=true" attribute to
104: // be added which will then cause validation error.
105: //
106: if (dpr.isDirty()) {
107: dpr.setDirty(false);
108: }
109: dpDoc = dpr.toString();
110: dpf.createRoot(adc, dpDoc);
111: } catch (DPError de) {
112: Throwable wrapped = de.getCause();
113: if (wrapped != null && wrapped instanceof SAXParseException) {
114: SAXParseException spe = (SAXParseException) wrapped;
115: int linenum = spe.getLineNumber();
116: Object[] tokens = { DPAUtil.getLines(new StringReader(
117: dpDoc), linenum) };
118: throw new DPAException("errorInvalidXMLText", de,
119: tokens);
120: }
121: } catch (Throwable ex) {
122: throw new DPAException("errorInvalidXML", ex);
123: }
124:
125: // write out changed dp only if dryrun is off
126: if (!dryrun) {
127: if (verbose) {
128: logger.finest("PSDT_CSPDDC0006");
129: }
130: try {
131: StringBuffer sb = new StringBuffer(256);
132: dpr.toXML(sb, 0);
133: if (!global) {
134: adc.storeDPDocument(dn, sb.toString());
135: } else {
136: adc.storeGlobalDPDocument(sb.toString());
137: }
138: } catch (Throwable ex) {
139: throw new DPAException("errorStoreDP", ex);
140: }
141: }
142: }
143:
144: private DPRoot doAdd(AdminDPContext adc, String dn, boolean global,
145: String parent, InputStream xmlByteStream, boolean verbose,
146: DPRoot dpr) throws DPAException {
147:
148: Element element = DPAUtil.getElement(adc, xmlByteStream);
149: String tag = element.getTagName();
150:
151: if (verbose) {
152: Object[] tokens = { tag };
153: logger.log(Level.FINEST, "PSDT_CSPDDC0007", tokens);
154: }
155:
156: // adding dp
157: if (tag.equals(XMLDPTags.DISPLAYPROFILE_TAG)) {
158: if (parent != null) {
159: Object[] tokens = { parent };
160: throw new DPAException("errorCannotHaveParent", tokens);
161: }
162: dpr = doAddDP(adc, dn, global, element);
163:
164: } else {
165:
166: // cannot add dp objects to an empty base dp
167: if (dpr == null) {
168: Object[] tokens = { global ? DPAUtil
169: .getLocalizedString("msgGlobal") : dn };
170: throw new DPAException("errorCreateDPRoot", tokens);
171: }
172:
173: // add dp obj.
174: if (tag.equals(XMLDPTags.CHANNEL_TAG)
175: || tag.equals(XMLDPTags.CONTAINER_TAG)) {
176:
177: dpr = doAddChannel(adc, dpr, parent, element);
178:
179: } else if (tag.equals(XMLDPTags.PROVIDER_TAG)) {
180:
181: if (parent != null) {
182: Object[] tokens = { parent };
183: throw new DPAException(
184: "errorProviderCannotHaveParent", tokens);
185: }
186: dpr = doAddProvider(adc, dpr, element);
187:
188: } else if (tag.equals(XMLDPTags.STRING_TAG)
189: || tag.equals(XMLDPTags.BOOLEAN_TAG)
190: || tag.equals(XMLDPTags.INTEGER_TAG)
191: || tag.equals(XMLDPTags.COLLECTION_TAG)
192: || tag.equals(XMLDPTags.CONDITIONALPROPERTIES_TAG)
193: || tag.equals(XMLDPTags.LOCALE_TAG)) {
194:
195: dpr = doAddProperty(adc, dpr, parent, element);
196:
197: } else if (tag.equals(XMLDPTags.PROPERTIES_TAG)
198: || tag.equals(XMLDPTags.AVAILABLE_TAG)
199: || tag.equals(XMLDPTags.SELECTED_TAG)) {
200: Object[] tokens = { tag };
201: throw new DPAException("errorCannotAdd", tokens);
202: } else {
203: Object[] tokens = { tag };
204: throw new DPAException("errorUnsupportedTag", tokens);
205: }
206: }
207:
208: return dpr;
209: }
210:
211: private DPRoot doAddDP(AdminDPContext adc, String dn,
212: boolean global, Element element) throws DPAException {
213:
214: if (verbose) {
215: Object[] tokens = { global ? DPAUtil
216: .getLocalizedString("msgGlobal") : dn };
217: logger.log(Level.FINEST, "PSDT_CSPDDC0008", tokens);
218: }
219:
220: String doc = null;
221: try {
222: if (!global) {
223: doc = adc.getDPDocument(dn);
224: } else {
225: doc = adc.getGlobalDPDocument();
226: }
227: } catch (Throwable ex) {
228: Object[] tokens = { global ? DPAUtil
229: .getLocalizedString("msgGlobal") : dn };
230: throw new DPAException("errorRetrieveDP", ex, tokens);
231: }
232:
233: if (doc != null && doc.length() > 0) {
234: Object[] tokens = { global ? DPAUtil
235: .getLocalizedString("msgGlobal") : dn };
236: throw new DPAException("errorDupDP", tokens);
237: }
238: DPRoot dpr = null;
239: try {
240: dpr = dpf.getRoot(adc, element);
241: } catch (Throwable ex) {
242: throw new DPAException("errorInvalidXML", ex);
243: }
244:
245: return dpr;
246: }
247:
248: private DPRoot doAddChannel(DPContext adc, DPRoot dpr,
249: String parent, Element element) throws DPAException {
250:
251: if (verbose) {
252: Object[] tokens = {
253: element.getAttribute("name"),
254: (parent == null ? DPAUtil
255: .getLocalizedString("msgRoot") : parent) };
256: logger.log(Level.FINEST, "PSDT_CSPDDC0009", tokens);
257: }
258:
259: DPNode node = null;
260: try {
261: if (parent == null) {
262: node = dpr;
263: } else {
264: if (verbose) {
265: Object[] tokens = { parent };
266: logger.log(Level.FINEST, "PSDT_CSPDDC0010", tokens);
267: }
268: XMLDPRoot xdpr = (XMLDPRoot) dpr;
269: node = xdpr.getChannelFromThis(parent);
270: }
271: } catch (Throwable ex) {
272: Object[] tokens = { parent == null ? DPAUtil
273: .getLocalizedString("msgRoot") : parent };
274: throw new DPAException("errorFindParent", ex, tokens);
275: }
276:
277: if (node == null) {
278: Object[] tokens = { parent == null ? DPAUtil
279: .getLocalizedString("msgRoot") : parent };
280: throw new DPAException("errorFindParent", tokens);
281: }
282:
283: if (!(node instanceof DPContainerChannel || node instanceof DPRoot)) {
284: Object[] tokens = { parent == null ? DPAUtil
285: .getLocalizedString("msgRoot") : parent };
286: throw new DPAException("errorParentNotCotainer", tokens);
287: }
288:
289: if (verbose) {
290: logger.finest("PSDT_CSPDDC0061");
291: }
292: DPChannel ch = null;
293: try {
294: ch = dpf.getChannel(adc, dpr, element);
295: } catch (Throwable ex) {
296: throw new DPAException("errorCreateDPChannel", ex);
297: }
298:
299: if (verbose) {
300: Object[] tokens = { ch.getName() };
301: logger.log(Level.FINEST, "PSDT_CSPDDC0011", tokens);
302: }
303: boolean dupFound = false;
304: try {
305: //
306: // check to make sure channel doesn't
307: // exist in parent node and that there is no provider
308: // by the same name
309: //
310: XMLDPNode xnode = (XMLDPNode) node;
311: dupFound = (xnode.getChannelFromThis(ch.getName()) != null);
312: XMLDPRoot xdpr = (XMLDPRoot) dpr;
313: if (!dupFound) {
314: dupFound = (xdpr.getProviderFromThis(ch.getName()) != null);
315: }
316: } catch (Throwable ex) {
317: throw new DPAException("errorCheckDupName", ex);
318: }
319: if (dupFound) {
320: throw new DPAException("errorDupName");
321: }
322:
323: try {
324: node.addChannel(ch);
325: } catch (Throwable ex) {
326: Object[] tokens = {
327: (parent == null ? DPAUtil
328: .getLocalizedString("msgRoot") : parent),
329: ch.getName() };
330: throw new DPAException("errorAddChannel", ex, tokens);
331: }
332:
333: return dpr;
334: }
335:
336: private DPRoot doAddProvider(DPContext adc, DPRoot dpr,
337: Element element) throws DPAException {
338:
339: if (verbose) {
340: Object[] tokens = { element.getAttribute("name") };
341: logger.log(Level.FINEST, "PSDT_CSPDDC0012", tokens);
342: }
343:
344: if (verbose) {
345: logger.log(Level.FINEST, "PSDT_CSPDDC0013");
346: }
347: DPProvider p = null;
348: try {
349: p = dpf.getProvider(adc, dpr, element);
350: } catch (Throwable ex) {
351: throw new DPAException("errorCreateDPProvider", ex);
352: }
353:
354: if (verbose) {
355: Object[] tokens = { p.getName() };
356: logger.log(Level.FINEST, "PSDT_CSPDDC0014", tokens);
357: }
358: boolean dupFound = false;
359: try {
360: //
361: // only need to check for dupe channel
362: // name at the root level
363: //
364: XMLDPRoot xdpr = (XMLDPRoot) dpr;
365: dupFound = (xdpr.getChannelFromThis(p.getName()) != null);
366: if (!dupFound) {
367: dupFound = (xdpr.getProviderFromThis(p.getName()) != null);
368: }
369: } catch (Throwable ex) {
370: throw new DPAException("errorCheckDupName", ex);
371: }
372: if (dupFound) {
373: throw new DPAException("errorDupName");
374: }
375:
376: try {
377: dpr.addProvider(p);
378: } catch (Throwable ex) {
379: Object[] tokens = { p.getName() };
380: throw new DPAException("errorAddProvider", ex, tokens);
381: }
382:
383: return dpr;
384: }
385:
386: private DPRoot doAddProperty(DPContext adc, DPRoot dpr,
387: String parent, Element element) throws DPAException {
388:
389: if (verbose) {
390: Object[] tokens = {
391: element.getAttribute("name"),
392: (parent == null ? DPAUtil
393: .getLocalizedString("msgRoot") : parent) };
394: logger.log(Level.FINEST, "PSDT_CSPDDC0015", tokens);
395: }
396:
397: DPPropertyHolder ph = null;
398: try {
399: if (parent == null) {
400: ph = dpr;
401: } else {
402: if (verbose) {
403: Object[] tokens = { parent };
404: logger.log(Level.FINEST, "PSDT_CSPDDC0016", tokens);
405: }
406: XMLDPRoot xdpr = (XMLDPRoot) dpr;
407: ph = xdpr.getChannelFromThis(parent);
408: if (ph == null) {
409: ph = xdpr.getProviderFromThis(parent);
410: }
411: }
412: } catch (Throwable ex) {
413: Object[] tokens = { parent == null ? DPAUtil
414: .getLocalizedString("msgRoot") : parent };
415: throw new DPAException("errorFindParent", ex, tokens);
416: }
417:
418: if (ph == null) {
419: Object[] tokens = { parent == null ? DPAUtil
420: .getLocalizedString("msgRoot") : parent };
421: throw new DPAException("errorFindParent", tokens);
422: }
423:
424: if (verbose) {
425: logger.finest("PSDT_CSPDDC0060");
426: }
427: DPProperty p = null;
428: try {
429: p = dpf.getProperty(adc, dpr, element);
430: } catch (Throwable ex) {
431: throw new DPAException("errorCreateDPProperty", ex);
432: }
433:
434: if (verbose) {
435: Object[] tokens = { parent == null ? DPAUtil
436: .getLocalizedString("msgRoot") : parent };
437: logger.log(Level.FINEST, "PSDT_CSPDDC0017", tokens);
438: }
439:
440: DPProperties props = null;
441: boolean exists = false;
442: try {
443: XMLDPPropertyHolder xph = (XMLDPPropertyHolder) ph;
444: props = xph.getPropertiesFromThis();
445: XMLDPProperties xprops = (XMLDPProperties) props;
446: exists = (xprops.getFromThis(p.getName()) != null);
447: } catch (Throwable ex) {
448: Object[] tokens = {
449: (parent == null ? DPAUtil
450: .getLocalizedString("msgRoot") : parent),
451: p.getName() };
452: throw new DPAException("errorLookupProperty", ex, tokens);
453: }
454:
455: if (exists) {
456: Object[] tokens = {
457: (parent == null ? DPAUtil
458: .getLocalizedString("msgRoot") : parent),
459: p.getName() };
460: throw new DPAException("errorDupProperty", tokens);
461: }
462:
463: try {
464: props.add(p);
465: } catch (Throwable ex) {
466: Object[] tokens = {
467: (parent == null ? DPAUtil
468: .getLocalizedString("msgRoot") : parent),
469: p.getName() };
470: throw new DPAException("errorAddProperty", ex, tokens);
471: }
472:
473: return dpr;
474: }
475:
476: }
|