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