001: /**
002: * Copyright 2003 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.wsrp.consumer.cli;
013:
014: import java.util.Map;
015: import java.util.HashMap;
016: import java.util.Set;
017: import java.util.Iterator;
018: import java.util.logging.Level;
019: import java.util.logging.Logger;
020:
021: import java.io.StringReader;
022:
023: import org.xml.sax.SAXParseException;
024:
025: import com.iplanet.am.util.Debug;
026:
027: import com.sun.portal.desktop.encode.Encoder;
028:
029: import com.sun.portal.desktop.dp.DPRoot;
030: import com.sun.portal.desktop.dp.DPChannel;
031: import com.sun.portal.desktop.dp.DPError;
032: import com.sun.portal.desktop.dp.xml.XMLDPFactory;
033: import com.sun.portal.desktop.dp.xml.XMLDPRoot;
034:
035: import com.sun.portal.desktop.context.DSAMEAdminDPContext;
036:
037: import com.sun.portal.wsrp.common.stubs.PortletDescription;
038:
039: import com.sun.portal.wsrp.consumer.common.WSRPProviderConstants;
040: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
041:
042: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntityManager;
043: import com.sun.portal.wsrp.consumer.producermanager.ProducerEntity;
044: import com.sun.portal.log.common.PortalLogger;
045:
046: class PEADeploy implements WSRPProviderConstants {
047:
048: StringBuffer chnames = new StringBuffer();
049: private static Logger logger = PortalLogger
050: .getLogger(PEADeploy.class);
051:
052: public String process(ProducerEntityManager pem,
053: DSAMEAdminDPContext dadc, String dn, String pid,
054: String rid, String chname, boolean cont, boolean verbose,
055: boolean dryrun, Debug debug) throws PEAException {
056:
057: //
058: // sanity check: rid depends on pid
059: // chname depends on rid
060: //
061: if (pid == null && rid != null) {
062: throw new PEAException("errorNoPIDWithRID");
063: }
064: if (rid == null && chname != null) {
065: throw new PEAException("errorNoRIDWithCh");
066: }
067:
068: if (verbose) {
069: if (rid != null) {
070:
071: if (logger.isLoggable(Level.FINEST)) {
072: Object[] tokens = { dn, pid, rid };
073: logger
074: .log(Level.FINEST, "PSWS_CSPWCCL0007",
075: tokens);
076: }
077: } else {
078: if (logger.isLoggable(Level.FINEST)) {
079: Object[] tokens = { dn };
080: logger
081: .log(Level.FINEST, "PSWS_CSPWCCL0008",
082: tokens);
083: }
084: }
085: }
086:
087: //
088: // get dp document
089: //
090: if (logger.isLoggable(Level.FINE)) {
091: Object[] tokens = { dn };
092: logger.log(Level.FINE, "PSWS_CSPWCCL0009", tokens);
093: }
094:
095: String doc = null;
096: try {
097: doc = dadc.getDPDocumentByDN(dn);
098: } catch (Throwable ex) {
099: Object[] tokens = { dn };
100: throw new PEAException("errorRetrieveDP", ex, tokens);
101: }
102:
103: //
104: // build merged dproot
105: //
106: DPRoot dpr = null;
107: XMLDPFactory dpf = XMLDPFactory.getInstance();
108: try {
109: if (doc != null && doc.length() > 0) {
110: dpr = dpf.createRoot(dadc, doc);
111: } else {
112: //
113: // empty dp doc created
114: //
115: dpr = dpf.createRoot(dadc);
116: }
117: //
118: // add mergers
119: //
120: Set names = dadc.getDPDocumentNames(dn);
121: dpr = dpf.addMergers(dadc, dpr, names, new HashMap());
122: } catch (Throwable ex) {
123: Object[] tokens = { dn };
124: throw new PEAException("errorCreateDPRoot", ex, tokens);
125: }
126:
127: //
128: // deploy wsrp portlet channel(s)
129: //
130: // 1. when pid and rid are specified, deploy only that portlet.
131: // 2. when pid alone is specified, deploy all portlets offered by
132: // the producer
133: // 3. when neither is specified, deploy all portlets offered by
134: // all producers that are currently found under the consumer
135: //
136: if (pid != null) {
137: ProducerEntity pe = null;
138: try {
139: pe = pem.getProducerEntity(pid);
140: } catch (WSRPConsumerException wce) {
141: Object[] tokens = { dn, pid };
142: throw new PEAException("errorGetPE", wce, tokens);
143: }
144: if (rid != null) {
145: //
146: // make sure rid is a valid one
147: //
148: PortletDescription[] pds = pe.getServiceDescription()
149: .getOfferedPortlets();
150: boolean found = false;
151: int i = 0;
152: for (; i < pds.length; i++) {
153: if (pds[i].getPortletHandle().equals(rid)) {
154: found = true;
155: break;
156: }
157: }
158: if (!found) {
159: Object[] tokens = { dn, pid, rid };
160: throw new PEAException("errorInvalidRID", tokens);
161: }
162: deployPortlet(dpr, dadc, dn, pds[i], pe, rid, chname,
163: verbose);
164: } else {
165: deployAllPortlets(dpr, dadc, dn, pe, cont, verbose,
166: debug);
167: }
168: } else {
169: Set pidSet = null;
170: try {
171: pidSet = pem.getProducerEntityIds();
172: } catch (WSRPConsumerException wce) {
173: Object[] tokens = { dn };
174: throw new PEAException("errorGetPEs", wce, tokens);
175: }
176: if (pidSet.size() > 0) {
177: for (Iterator i = pidSet.iterator(); i.hasNext();) {
178: ProducerEntity pe = null;
179: try {
180: pe = pem.getProducerEntity((String) i.next());
181: } catch (WSRPConsumerException wce) {
182: Object[] tokens = { dn, pid };
183: throw new PEAException("errorGetPE", wce,
184: tokens);
185: }
186: deployAllPortlets(dpr, dadc, dn, pe, cont, verbose,
187: debug);
188: }
189: }
190: }
191:
192: //
193: // write out dp doc
194: //
195: writeDP(dpr, dadc, dn, verbose, dryrun);
196:
197: //
198: // return channel name list
199: //
200: return chnames.toString();
201: }
202:
203: protected void deployAllPortlets(DPRoot dpr,
204: DSAMEAdminDPContext dadc, String dn, ProducerEntity pe,
205: boolean cont, boolean verbose, Debug debug)
206: throws PEAException {
207:
208: String pid = pe.getId();
209: PortletDescription[] pds = pe.getServiceDescription()
210: .getOfferedPortlets();
211:
212: for (int i = 0; i < pds.length; i++) {
213: String rid = pds[i].getPortletHandle();
214: if (cont) {
215: //
216: // catch any throwable if continous mode is on
217: //
218: try {
219: deployPortlet(dpr, dadc, dn, pds[i], pe, rid, null,
220: verbose);
221: } catch (Throwable th) {
222: if (logger.isLoggable(Level.SEVERE)) {
223: Object[] tokens = { dn, pid, rid };
224: logger.log(Level.SEVERE, "PSWS_CSPWCCL0010",
225: tokens);
226: logger
227: .log(Level.SEVERE, "PSWS_CSPWCCL0011",
228: th);
229: }
230: }
231: } else {
232: deployPortlet(dpr, dadc, dn, pds[i], pe, rid, null,
233: verbose);
234: }
235: }
236: }
237:
238: public DPRoot deployPortlet(DPRoot dpr, DSAMEAdminDPContext dadc,
239: String dn, PortletDescription pd, ProducerEntity pe,
240: String rid, String chname, boolean verbose)
241: throws PEAException {
242:
243: String pid = pe.getId();
244:
245: if (pd.getUsesMethodGet().booleanValue()) {
246: Object[] tokens = { dn, pid, rid };
247: throw new PEAException("errorMethodGetOn", tokens);
248: }
249:
250: if (logger.isLoggable(Level.FINEST)) {
251: Object[] tokens = { dn, pid, rid };
252: logger.log(Level.FINEST, "PSWS_CSPWCCL0012", tokens);
253: }
254:
255: //
256: // in case channel name is unspecified, create an unique name
257: //
258: if (chname == null) {
259: String pname = pe.getName();
260: //
261: // escape offending characters
262: //
263: String pnameEsc = Encoder.FORMNAME_ENCODER.encode(pname);
264: String ridEsc = Encoder.FORMNAME_ENCODER.encode(rid);
265: chname = pnameEsc + "_" + ridEsc;
266: }
267:
268: if (logger.isLoggable(Level.FINEST)) {
269: Object[] tokens = { dn, chname };
270: logger.log(Level.FINEST, "PSWS_CSPWCCL0013", tokens);
271: }
272:
273: boolean dupFound = false;
274: XMLDPRoot xdpr = (XMLDPRoot) dpr;
275: try {
276: dupFound = (xdpr.getChannelFromThis(chname) != null);
277: if (!dupFound) {
278: dupFound = (xdpr.getProviderFromThis(chname) != null);
279: }
280: } catch (Throwable ex) {
281: Object[] tokens = { dn, chname };
282: throw new PEAException("errorCheckDup", ex, tokens);
283: }
284:
285: if (dupFound) {
286: Object[] tokens = { dn, chname };
287: throw new PEAException("errorDupFound", tokens);
288: }
289:
290: DPChannel ch = null;
291: try {
292: XMLDPFactory dpf = XMLDPFactory.getInstance();
293: ch = dpf.createChannel(dadc, dpr, xdpr.getDocument(),
294: chname, WSRP_PROVIDER);
295: ch = dpr.addChannel(ch);
296: ch.getProperties().setString(CONSUMER_ID, dn);
297: ch.getProperties().setString(PRODUCER_ENTITY_ID, pid);
298: ch.getProperties().setString(PORTLET_ID, rid);
299: //
300: // TBD: portletHandle == rid ?
301: //
302: ch.getProperties().setString(PORTLET_HANDLE, rid);
303: } catch (Throwable ex) {
304: Object[] tokens = { dn, chname };
305: throw new PEAException("errorAddCh", ex, tokens);
306: }
307:
308: //
309: // record channel name
310: //
311: if (ch != null) {
312: chnames.append(ch.getName()).append("\n");
313: }
314:
315: return dpr;
316: }
317:
318: protected void writeDP(DPRoot dpr, DSAMEAdminDPContext dadc,
319: String dn, boolean verbose, boolean dryrun)
320: throws PEAException {
321:
322: //
323: // check for invalid XML
324: //
325: String dpDoc = null;
326: try {
327: //
328: // set dirty to false before writing out.
329: // not doing this will cause "dirty=true" attribute to
330: // be added which will then cause validation error.
331: //
332: if (dpr.isDirty()) {
333: dpr.setDirty(false);
334: }
335: dpDoc = dpr.toString();
336: XMLDPFactory dpf = XMLDPFactory.getInstance();
337: dpf.createRoot(dadc, dpDoc);
338: } catch (DPError de) {
339: Throwable wrapped = de.getCause();
340: if (wrapped != null && wrapped instanceof SAXParseException) {
341: SAXParseException spe = (SAXParseException) wrapped;
342: int linenum = spe.getLineNumber();
343: Object[] tokens = { PEAUtil.getLines(new StringReader(
344: dpDoc), linenum) };
345: throw new PEAException("errorInvalidXMLText", de,
346: tokens);
347: }
348: } catch (Throwable ex) {
349: throw new PEAException("errorInvalidXML", ex);
350: }
351:
352: //
353: // write out changes only if dryrun is off
354: //
355: if (!dryrun) {
356: if (logger.isLoggable(Level.FINEST)) {
357: Object[] tokens = { dn };
358: logger.log(Level.FINEST, "", tokens);
359: }
360:
361: try {
362: StringBuffer sb = new StringBuffer(256);
363: dpr.toXML(sb, 0);
364: dadc.storeDPDocumentByDN(dn, sb.toString());
365: } catch (Throwable ex) {
366: Object[] tokens = { dn };
367: throw new PEAException("ErrorStoreDP", ex);
368: }
369: }
370: }
371: }
|