001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.portlet.cli;
007:
008: import java.util.List;
009: import java.util.ArrayList;
010:
011: import java.io.InputStream;
012: import java.io.StringReader;
013: import java.io.Reader;
014: import java.io.BufferedReader;
015: import java.io.IOException;
016:
017: import com.sun.portal.desktop.context.DSAMEAdminDPContext;
018: import com.sun.portal.desktop.dp.DPRoot;
019: import com.sun.portal.desktop.dp.DPProvider;
020:
021: import com.sun.portal.desktop.DesktopError;
022:
023: import com.sun.portal.desktop.dp.xml.XMLDPTags;
024: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
025: import com.sun.portal.desktop.dp.xml.XMLDPRoot;
026:
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.parsers.DocumentBuilder;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.DOMException;
032: import org.xml.sax.SAXParseException;
033:
034: /**
035: * PDDPUpdater is responsible for adding the provider
036: * entries created by PDProviderEntryGenerator class to
037: * the DP document and storing the updated DP document.
038: */
039: public class PDDPUpdater {
040:
041: private DSAMEAdminDPContext dadc = null;
042: private String dn = null;
043: private boolean verbose = false;
044: private boolean global = false;
045:
046: public static final int CONTEXT_LINE_NUM = 5;
047:
048: // XML DP Factory
049: XMLDPFactory dpf = XMLDPFactory.getInstance();
050:
051: public PDDPUpdater(DSAMEAdminDPContext dadc, String dn,
052: boolean global, boolean verbose) {
053: this .dadc = dadc;
054: this .dn = dn;
055: this .global = global;
056: this .verbose = verbose;
057: }
058:
059: public void addProviders(List providerElements)
060: throws PortletDeployerException {
061:
062: // build dproot
063: DPRoot dpr = getDPRoot();
064: dpr.setDummy(false);
065: // add dp obj.
066: for (int i = 0; i < providerElements.size(); i++) {
067: Element element = (Element) providerElements.get(i);
068: dpr = addProvider(dpr, element);
069: }
070:
071: if (verbose) {
072: PortletDeployerLocalizer.debug("dbgValidatingDP");
073: }
074:
075: // check for invalid XML
076: String dpDoc = null;
077: try {
078: //
079: // set dirty to false before writing out.
080: // not doing this will cause "dirty=true" attribute to
081: // be added which will then cause validation error.
082: //
083: dpr = setDirty(dpr);
084: dpDoc = dpr.toString();
085: dpf.createRoot(dadc, dpDoc);
086: } catch (DesktopError de) {
087: Throwable wrapped = de.getCause();
088: if (wrapped != null && wrapped instanceof SAXParseException) {
089: SAXParseException spe = (SAXParseException) wrapped;
090: int linenum = spe.getLineNumber();
091: Object[] tokens = { getLines(new StringReader(dpDoc),
092: linenum) };
093: throw new PortletDeployerException(
094: "errorInvalidXMLText", de, tokens);
095: }
096: } catch (Throwable ex) {
097: throw new PortletDeployerException("errorInvalidXML", ex);
098: }
099:
100: storeDPDocument(dpr);
101:
102: }
103:
104: public DPRoot addProvider(DPRoot dpr, Element element)
105: throws PortletDeployerException {
106: if (verbose) {
107: Object[] tokens = { element.getAttribute("name") };
108: PortletDeployerLocalizer.debug("dbgAddingProvider", tokens);
109: PortletDeployerLocalizer.debug("dbgCreatingDPProvider");
110: }
111: DPProvider p = null;
112: try {
113: p = dpf.getProvider(dadc, dpr, element);
114: } catch (Throwable ex) {
115: throw new PortletDeployerException("errorCreateDPProvider",
116: ex);
117: }
118:
119: if (verbose) {
120: Object[] tokens = { p.getName() };
121: PortletDeployerLocalizer.debug("dbgCheckDupName", tokens);
122: }
123: boolean dupFound = false;
124: try {
125: //
126: // only need to check for dupe channel
127: // name at the root level
128: //
129: XMLDPRoot xdpr = (XMLDPRoot) dpr;
130: dupFound = (xdpr.getChannelFromThis(p.getName()) != null);
131: if (!dupFound) {
132: dupFound = (xdpr.getProviderFromThis(p.getName()) != null);
133: }
134: } catch (Throwable ex) {
135: throw new PortletDeployerException("errorCheckDupName", ex);
136: }
137: if (dupFound) {
138: //Changing this part of code to log a warning message
139: //for multi-instance deployment.
140: //throw new PortletDeployerException("errorDupName");
141: Object[] tokens = { p.getName() };
142: PortletDeployerLocalizer.warning("WarningDupName", tokens);
143: }
144:
145: try {
146: dpr.addProvider(p);
147: } catch (Throwable ex) {
148: Object[] tokens = { p.getName() };
149: throw new PortletDeployerException("errorAddProvider", ex,
150: tokens);
151: }
152:
153: return dpr;
154: }
155:
156: public void removeProviders(List providers)
157: throws PortletDeployerException {
158: DPRoot dpr = getDPRoot();
159:
160: // remove providers.
161: for (int i = 0; i < providers.size(); i++) {
162: String name = (String) providers.get(i);
163: dpr = removeProvider(dpr, name);
164: }
165: dpr = setDirty(dpr);
166: storeDPDocument(dpr);
167: }
168:
169: public DPRoot removeProvider(DPRoot dpr, String name)
170: throws PortletDeployerException {
171: if (verbose) {
172: Object[] tokens = { name };
173: PortletDeployerLocalizer.debug("dbgRemovingProvider",
174: tokens);
175: }
176:
177: if (verbose) {
178: PortletDeployerLocalizer.debug("dbgCheckExistingProvider");
179: }
180: boolean exists = false;
181: try {
182: XMLDPRoot xdpr = (XMLDPRoot) dpr;
183: exists = (xdpr.getProviderFromThis(name) != null);
184: } catch (Throwable ex) {
185: Object[] tokens = { name };
186: throw new PortletDeployerException("errorLookupProvider",
187: ex, tokens);
188: }
189: if (!exists) {
190: Object[] tokens = { name };
191: //Changing this part of code to log a warning message
192: //for multi-instance deployment.
193: //throw new PortletDeployerException("errorFindProvider", tokens);
194: PortletDeployerLocalizer.warning("WarningFindProvider",
195: tokens);
196: }
197:
198: try {
199: dpr.removeProvider(name);
200: } catch (Throwable ex) {
201: Object[] tokens = { name };
202: throw new PortletDeployerException("errorRemoveProvider",
203: ex, tokens);
204: }
205:
206: return dpr;
207:
208: }
209:
210: private DPRoot getDPRoot() throws PortletDeployerException {
211: // get document
212: String doc = null;
213: try {
214: if (!global) {
215: doc = dadc.getDPDocumentByDN(dn);
216: } else {
217: doc = dadc.getGlobalDPDocument();
218: }
219: } catch (Throwable ex) {
220: Object[] tokens = { global ? PortletDeployerLocalizer
221: .getLocalizedString("msgGlobal") : dn };
222: throw new PortletDeployerException("errorRetrieveDP", ex,
223: tokens);
224: }
225:
226: // build dproot
227: DPRoot dpr = null;
228: try {
229: if (doc == null) {
230: dpr = dpf.createRoot(dadc);
231: } else {
232: dpr = dpf.createRoot(dadc, doc);
233: }
234: } catch (Throwable ex) {
235: Object[] tokens = { dn };
236: throw new PortletDeployerException("errorCreateDPRoot", ex,
237: tokens);
238: }
239: return dpr;
240:
241: }
242:
243: private void storeDPDocument(DPRoot dpr)
244: throws PortletDeployerException {
245: if (verbose) {
246: PortletDeployerLocalizer.debug("dbgWritingDP");
247: }
248: try {
249: if (!global) {
250: dadc.storeDPDocumentByDN(dn, dpr.toString());
251: } else {
252: dadc.storeGlobalDPDocument(dpr.toString());
253: }
254: } catch (Throwable ex) {
255: throw new PortletDeployerException("errorStoreDP", ex);
256: }
257: }
258:
259: private DPRoot setDirty(DPRoot dpr) throws PortletDeployerException {
260: try {
261: if (dpr.isDirty()) {
262: dpr.setDirty(false);
263: }
264: } catch (Throwable ex) {
265: throw new PortletDeployerException("errorSetDirty", ex);
266: }
267: return dpr;
268: }
269:
270: public static String getLines(Reader reader, int linenum) {
271: StringBuffer buf = new StringBuffer();
272: String line = null;
273: BufferedReader br = new BufferedReader(reader);
274:
275: try {
276: int i = 1;
277: int beg = linenum - CONTEXT_LINE_NUM;
278: int end = linenum + CONTEXT_LINE_NUM;
279: buf.append("\n");
280: while ((line = br.readLine()) != null) {
281: if (i == linenum) {
282: buf.append("(*)").append(line).append("\n");
283: } else if (i >= beg && i <= end) {
284: buf.append(" ").append(line).append("\n");
285: }
286: ++i;
287: }
288: buf.append("\n");
289: } catch (IOException ioe) {
290: return "";
291: }
292: return buf.toString();
293: }
294: }
|