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