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.logging.Level;
010: import java.util.logging.Logger;
011:
012: import com.sun.portal.desktop.context.AdminDPContext;
013: import com.sun.portal.desktop.context.DPContext;
014:
015: import com.sun.portal.desktop.dp.DPRoot;
016: import com.sun.portal.desktop.dp.DPNode;
017: import com.sun.portal.desktop.dp.DPProvider;
018: import com.sun.portal.desktop.dp.DPPropertyHolder;
019: import com.sun.portal.desktop.dp.DPChannel;
020: import com.sun.portal.desktop.dp.DPContainerChannel;
021: import com.sun.portal.desktop.dp.DPProperties;
022: import com.sun.portal.desktop.dp.DPProperty;
023: import com.sun.portal.desktop.dp.DPReferenceList;
024:
025: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
026: import com.sun.portal.desktop.dp.xml.XMLDPRoot;
027: import com.sun.portal.desktop.dp.xml.XMLDPNode;
028: import com.sun.portal.desktop.dp.xml.XMLDPChannel;
029: import com.sun.portal.desktop.dp.xml.XMLDPPropertyHolder;
030: import com.sun.portal.desktop.dp.xml.XMLDPContainerChannel;
031: import com.sun.portal.desktop.dp.xml.XMLDPProperties;
032: import com.sun.portal.desktop.dp.xml.XMLDPReferenceList;
033: import com.sun.portal.log.common.PortalLogger;
034:
035: import java.util.logging.Level;
036: import java.util.logging.Logger;
037:
038: class DPARemove {
039:
040: private static Logger logger = PortalLogger
041: .getLogger(DPARemove.class);
042: // XML DP Factory
043: XMLDPFactory dpf = XMLDPFactory.getInstance();
044: boolean verbose = false;
045:
046: // types
047: private static final String TYPE_ROOT = "root";
048: private static final String TYPE_CHANNEL = "channel";
049: private static final String TYPE_PROVIDER = "provider";
050: private static final String TYPE_PROPERTY = "property";
051: private static final String TYPE_AVAILABLE = "available";
052: private static final String TYPE_SELECTED = "selected";
053:
054: public void process(AdminDPContext adc, String dn, boolean global,
055: String parent, String name, String type, boolean verbose,
056: boolean dryrun, Logger logger) throws DPAException {
057:
058: this .verbose = verbose;
059:
060: if (type.toLowerCase().equals(TYPE_ROOT)) {
061: try {
062: if (!global) {
063: adc.removeDPDocument(dn);
064: } else {
065: adc.removeGlobalDPDocument();
066: }
067: return;
068: } catch (Throwable ex) {
069: Object[] tokens = { global ? DPAUtil
070: .getLocalizedString("msgGlobal") : dn };
071: throw new DPAException("errorRemoveDP", ex, tokens);
072: }
073: }
074:
075: DPRoot dpr = null;
076: String doc = null;
077: try {
078: if (!global) {
079: doc = adc.getDPDocument(dn);
080: } else {
081: doc = adc.getGlobalDPDocument();
082: }
083: } catch (Throwable ex) {
084: Object[] tokens = { global ? DPAUtil
085: .getLocalizedString("msgGlobal") : dn };
086: throw new DPAException("errorRetrieveDP", ex, tokens);
087: }
088: try {
089: dpr = dpf.createRoot(adc, doc);
090: } catch (Throwable ex) {
091: Object[] tokens = { global ? DPAUtil
092: .getLocalizedString("msgGlobal") : dn };
093: throw new DPAException("errorCreateDPRoot", ex, tokens);
094: }
095:
096: if (type.toLowerCase().equals(TYPE_CHANNEL)) {
097: if (name == null) {
098: throw new DPAException("errorChannelNameUnspecified");
099: }
100: dpr = doRemoveChannel(dpr, parent, name);
101:
102: } else if (type.toLowerCase().equals(TYPE_PROVIDER)) {
103: if (name == null) {
104: throw new DPAException("errorProviderNameUnspecified");
105: }
106: if (parent != null) {
107: Object[] tokens = { parent };
108: throw new DPAException("errorProviderCannotHaveParent",
109: tokens);
110: }
111: dpr = doRemoveProvider(dpr, name);
112:
113: } else if (type.toLowerCase().equals(TYPE_PROPERTY)) {
114: if (name == null) {
115: throw new DPAException("errorPropertyNameUnspecified");
116: }
117: dpr = doRemoveProperty(dpr, parent, name);
118:
119: } else if (type.toLowerCase().equals(TYPE_AVAILABLE)
120: || type.toLowerCase().equals(TYPE_SELECTED)) {
121: if (name == null) {
122: throw new DPAException("errorReferenceValueUnspecified");
123: }
124: if (parent == null) {
125: throw new DPAException("errorParentUnspecified");
126: }
127: dpr = doRemoveReference(dpr, parent, name, type);
128:
129: } else {
130: Object[] tokens = { type };
131: throw new DPAException("errorInvalidType", tokens);
132: }
133:
134: //
135: // FIXME(susu): we don't check for invalid XML here.
136: // can't think of any scenario where removing dp obj
137: // will result in invalid XML.
138:
139: // write out changed dp only if dryrun is off
140: if (!dryrun) {
141: if (verbose) {
142: logger.log(Level.FINEST, "PSDT_CSPDDC0047");
143: }
144: try {
145: //
146: // set dirty to false before writing out.
147: // not doing this will cause "dirty=true" attribute to
148: // be added which will then cause validation error.
149: //
150: if (dpr.isDirty()) {
151: dpr.setDirty(false);
152: }
153: StringBuffer sb = new StringBuffer(256);
154: dpr.toXML(sb, 0);
155: if (!global) {
156: adc.storeDPDocument(dn, sb.toString());
157: } else {
158: adc.storeGlobalDPDocument(sb.toString());
159: }
160: } catch (Throwable ex) {
161: throw new DPAException("errorStoreDP", ex);
162: }
163: }
164:
165: }
166:
167: private DPRoot doRemoveChannel(DPRoot dpr, String parent,
168: String name) throws DPAException {
169:
170: if (verbose) {
171: Object[] tokens = {
172: name,
173: (parent == null ? DPAUtil
174: .getLocalizedString("msgRoot") : parent) };
175: logger.log(Level.FINEST, "PSDT_CSPDDC0048", tokens);
176: }
177:
178: DPNode node = null;
179: try {
180: if (parent == null) {
181: node = dpr;
182: } else {
183: if (verbose) {
184: Object[] tokens = { parent };
185: logger.log(Level.FINEST, "PSDT_CSPDDC0049", tokens);
186: }
187: XMLDPRoot xdpr = (XMLDPRoot) dpr;
188: node = xdpr.getChannelFromThis(parent);
189: }
190: } catch (Throwable ex) {
191: Object[] tokens = { parent == null ? DPAUtil
192: .getLocalizedString("msgRoot") : parent };
193: throw new DPAException("errorFindParent", ex, tokens);
194: }
195:
196: if (verbose) {
197: Object[] tokens = { parent == null ? DPAUtil
198: .getLocalizedString("msgRoot") : parent };
199: logger.log(Level.FINEST, "PSDT_CSPDDC0050", tokens);
200: }
201: DPChannel ch = null;
202: try {
203: XMLDPNode xnode = (XMLDPNode) node;
204: ch = xnode.getChannelFromThis(name);
205: } catch (Throwable ex) {
206: Object[] tokens = {
207: (parent == null ? DPAUtil
208: .getLocalizedString("msgRoot") : parent),
209: name };
210: throw new DPAException("errorLookupChannel", ex, tokens);
211: }
212:
213: if (ch == null) {
214: Object[] tokens = {
215: (parent == null ? DPAUtil
216: .getLocalizedString("msgRoot") : parent),
217: name };
218: throw new DPAException("errorFindChannel", tokens);
219: }
220:
221: try {
222: String leafName = null;
223: int i = name.lastIndexOf(ch.CHANNEL_NAME_SEPARATOR);
224: if (i != -1) {
225: leafName = name.substring(i + 1);
226: } else {
227: leafName = name;
228: }
229: (((XMLDPChannel) ch).getParentNodeFromThis())
230: .removeChannel(leafName);
231: } catch (Throwable ex) {
232: Object[] tokens = {
233: (parent == null ? DPAUtil
234: .getLocalizedString("msgRoot") : parent),
235: name };
236: throw new DPAException("errorRemoveChannel", ex, tokens);
237: }
238: return dpr;
239: }
240:
241: private DPRoot doRemoveProvider(DPRoot dpr, String name)
242: throws DPAException {
243:
244: if (verbose) {
245: Object[] tokens = { name };
246: logger.log(Level.FINEST, "PSDT_CSPDDC0051", tokens);
247: }
248:
249: if (verbose) {
250: logger.log(Level.FINEST, "PSDT_CSPDDC0052");
251: }
252: boolean exists = false;
253: try {
254: XMLDPRoot xdpr = (XMLDPRoot) dpr;
255: exists = (xdpr.getProviderFromThis(name) != null);
256: } catch (Throwable ex) {
257: Object[] tokens = { name };
258: throw new DPAException("errorLookupProvider", ex, tokens);
259: }
260: if (!exists) {
261: Object[] tokens = { name };
262: throw new DPAException("errorFindProvider", tokens);
263: }
264:
265: try {
266: dpr.removeProvider(name);
267: } catch (Throwable ex) {
268: Object[] tokens = { name };
269: throw new DPAException("errorRemoveProvider", ex, tokens);
270: }
271:
272: return dpr;
273: }
274:
275: private DPRoot doRemoveProperty(DPRoot dpr, String parent,
276: String name) throws DPAException {
277:
278: if (verbose) {
279: Object[] tokens = {
280: name,
281: (parent == null ? DPAUtil
282: .getLocalizedString("msgRoot") : parent) };
283: logger.log(Level.FINEST, "PSDT_CSPDDC0053", tokens);
284: }
285:
286: DPPropertyHolder ph = null;
287: try {
288: if (parent == null) {
289: ph = dpr;
290: } else {
291: if (verbose) {
292: Object[] tokens = { parent };
293: logger.log(Level.FINEST, "PSDT_CSPDDC0054", tokens);
294: }
295: XMLDPRoot xdpr = (XMLDPRoot) dpr;
296: ph = xdpr.getChannelFromThis(parent);
297: if (ph == null) {
298: ph = xdpr.getProviderFromThis(parent);
299: }
300: }
301: } catch (Throwable ex) {
302: Object[] tokens = { parent == null ? DPAUtil
303: .getLocalizedString("msgRoot") : parent };
304: throw new DPAException("errorFindParent", ex, tokens);
305: }
306:
307: if (ph == null) {
308: Object[] tokens = { parent == null ? DPAUtil
309: .getLocalizedString("msgRoot") : parent };
310: throw new DPAException("errorFindParent", tokens);
311: }
312:
313: if (verbose) {
314: Object[] tokens = { parent == null ? DPAUtil
315: .getLocalizedString("msgRoot") : parent };
316: logger.log(Level.FINEST, "PSDT_CSPDDC0055", tokens);
317: }
318: DPProperties props = null;
319: boolean exists = false;
320: try {
321: XMLDPPropertyHolder xph = (XMLDPPropertyHolder) ph;
322: props = xph.getPropertiesFromThis();
323: XMLDPProperties xprops = (XMLDPProperties) props;
324: exists = (xprops.getFromThis(name) != null);
325: } catch (Throwable ex) {
326: Object[] tokens = {
327: (parent == null ? DPAUtil
328: .getLocalizedString("msgRoot") : parent),
329: name };
330: throw new DPAException("errorLookupProperty", ex, tokens);
331: }
332:
333: if (!exists) {
334: Object[] tokens = {
335: (parent == null ? DPAUtil
336: .getLocalizedString("msgRoot") : parent),
337: name };
338: throw new DPAException("errorFindProperty", tokens);
339: }
340:
341: try {
342: props.remove(name);
343: } catch (Throwable ex) {
344: Object[] tokens = {
345: (parent == null ? DPAUtil
346: .getLocalizedString("msgRoot") : parent),
347: name };
348: throw new DPAException("errorRemoveProperty", ex, tokens);
349: }
350:
351: return dpr;
352: }
353:
354: private DPRoot doRemoveReference(DPRoot dpr, String parent,
355: String value, String type) throws DPAException {
356:
357: if (verbose) {
358: Object[] tokens = { type, value, parent };
359: logger.log(Level.FINEST, "PSDT_CSPDDC0056", tokens);
360: }
361:
362: DPNode node = null;
363: try {
364: if (verbose) {
365: Object[] tokens = { parent };
366: logger.log(Level.FINEST, "PSDT_CSPDDC0057", tokens);
367: }
368: XMLDPRoot xdpr = (XMLDPRoot) dpr;
369: node = xdpr.getChannelFromThis(parent);
370: } catch (Throwable ex) {
371: Object[] tokens = { parent };
372: throw new DPAException("errorFindParent", ex, tokens);
373: }
374:
375: if (node == null) {
376: Object[] tokens = { parent };
377: throw new DPAException("errorFindParent", tokens);
378: }
379:
380: if (!(node instanceof DPContainerChannel)) {
381: Object[] tokens = { parent };
382: throw new DPAException("errorParentNotCotainer", tokens);
383: }
384:
385: if (verbose) {
386: Object[] tokens = { type, parent };
387: logger.log(Level.FINEST, "PSDT_CSPDDC0058", tokens);
388: }
389:
390: DPContainerChannel cc = (DPContainerChannel) node;
391: DPReferenceList rl = null;
392: boolean exists = false;
393: try {
394: XMLDPContainerChannel xcc = (XMLDPContainerChannel) cc;
395: if (type.toLowerCase().equals(TYPE_AVAILABLE)) {
396: rl = xcc.getAvailableFromThis();
397: } else if (type.toLowerCase().equals(TYPE_SELECTED)) {
398: rl = xcc.getSelectedFromThis();
399: }
400: XMLDPReferenceList xrl = (XMLDPReferenceList) rl;
401: exists = (xrl.getFromThis(value) != null);
402: } catch (Throwable ex) {
403: Object[] tokens = { type, parent };
404: throw new DPAException("errorlookupDPList", ex, tokens);
405: }
406:
407: if (!exists) {
408: Object[] tokens = { type, parent, value };
409: throw new DPAException("errorFindReference", tokens);
410: }
411:
412: try {
413: rl.remove(value);
414: } catch (Throwable ex) {
415: Object[] tokens = { type, parent, value };
416: throw new DPAException("errorRemoveReference", ex, tokens);
417: }
418:
419: return dpr;
420: }
421:
422: }
|