001: /**
002: * Copyright 2002 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 iPlanet
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.app.calendarcommon.calendar.provisioning;
013:
014: import com.iplanet.sso.SSOException;
015: import com.iplanet.sso.SSOToken;
016: import com.sun.comm.jdapi.DABusinessOrganization;
017: import com.sun.comm.jdapi.DAConnection;
018: import com.sun.comm.jdapi.DAConstants;
019: import com.sun.comm.jdapi.DASearchConstraint;
020: import com.sun.comm.jdapi.DASearchResults;
021: import com.sun.comm.jdapi.DAUser;
022: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtils;
023: import com.sun.portal.app.calendarcommon.calendar.SharedCalendarUtilsFactory;
024: import com.sun.portal.app.calendarcommon.common.SharedServicesException;
025: import com.sun.portal.community.CommunityFactory;
026: import com.sun.portal.portlet.service.provision.PortletProvisionPreferencesException;
027: import java.util.logging.Logger;
028: import com.sun.portal.log.common.PortalLogger;
029:
030: import com.sun.portal.portlet.service.provision.GenericPortletProvisionListener;
031: import com.sun.portal.portlet.service.provision.PortletProvisionEvent;
032: import com.sun.portal.portlet.service.provision.PortletProvisionException;
033: import com.sun.portal.portlet.service.provision.PortletProvisionPreferences;
034: import com.sun.portal.portlet.service.provision.PortletProvisionRoles;
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.Map;
038: import java.util.Properties;
039: import java.util.Set;
040: import java.util.logging.Level;
041: import javax.portlet.PortletContext;
042: import javax.servlet.http.HttpServletRequest;
043:
044: /**
045: *
046: *
047: * @author Nigel Jacobs
048: * @created May 18, 2005
049: *
050: */
051: public class SharedCalendarPortletProvisionListener extends
052: GenericPortletProvisionListener {
053:
054: static final String listenerName = "SharedCalendarPortletProvisionListener";
055: private static Logger logger = PortalLogger
056: .getLogger(SharedCalendarPortletProvisionListener.class);
057:
058: /**
059: * Implements PortletProvisionListener I/F.
060: */
061:
062: /**
063: * Method called when a provisioning event happens.
064: * @param event provisioning event
065: * @exception com.sun.portal.portlet.service.provision.PortletProvisionException thrown when there is an application error.
066: */
067: public void handleProvisionEvent(PortletProvisionEvent event)
068: throws PortletProvisionException {
069: if (event.getType().equals(
070: PortletProvisionEvent.EVENT_TYPE_CREATED)) {
071: provision(event);
072: } else if (event.getType().equals(
073: PortletProvisionEvent.EVENT_TYPE_DESTROYED)) {
074: unprovision(event);
075: } else {
076: // nothing
077: }
078: }
079:
080: public void provision(PortletProvisionEvent portletProvisionEvent)
081: throws PortletProvisionException {
082:
083: String communityID = portletProvisionEvent
084: .getCommunityPrincipalId();
085: // get the preferences object for MEMBER
086: PortletProvisionPreferences preferences = portletProvisionEvent
087: .getPortletProvisionPreferences(PortletProvisionEvent.PREFERENCES_MEMBER);
088: HttpServletRequest servletRequest = portletProvisionEvent
089: .getHttpServletRequest();
090: PortletContext portletContext = getPortletContext();
091:
092: if (logger.isLoggable(Level.FINE))
093: logger.fine("PROVISION: SHARED CAL PROVISIONING event="
094: + portletProvisionEvent);
095:
096: try {
097:
098: // TODO: Debugging - Remove
099: try {
100: //print default preferences
101: Set names = preferences.getNames();
102: Iterator namesIter = names.iterator();
103: while (namesIter.hasNext()) {
104: String name = (String) namesIter.next();
105: logger.fine("pref name:" + name + " value:"
106: + preferences.getValue(name));
107: }
108: } catch (PortletProvisionPreferencesException e) {
109: logger.log(Level.WARNING, "PrefsPrintError e="
110: + e.getMessage(), e);
111: throw new PortletProvisionException(listenerName,
112: "Error printing prefs", e);
113: }
114:
115: // TODO: Debugging - Remove
116: if (logger.isLoggable(Level.FINE)) {
117: logger.fine("CTY = "
118: + portletProvisionEvent
119: .getCommunityPrincipalId());
120: logger.fine("Portlet Name = "
121: + portletProvisionEvent.getPortletName());
122: logger.fine("Portlet Instance Name = "
123: + portletProvisionEvent
124: .getPortletInstanceName());
125: SSOToken token = portletProvisionEvent.getSSOToken();
126: if (token != null) {
127: try {
128: logger.fine("SSOToken user = "
129: + token.getPrincipal().getName());
130: } catch (SSOException e) {
131: throw new PortletProvisionException(
132: listenerName, "SSO token exception", e);
133: }
134: }
135: }
136:
137: // retrieve the the cty ID from the event, and persist it in the portlet prefs of the channel
138: try {
139: preferences
140: .setValue(CommunityFactory.COMMUNITY_ID_PREF,
141: communityID);
142: } catch (PortletProvisionPreferencesException e) {
143: throw new PortletProvisionException(
144: listenerName,
145: "Error setting community id on portlet prov prefs",
146: e);
147: }
148:
149: // retrieve config name from portlet (servlet) context, and persist it in the portlet prefs
150:
151: Object configName = portletContext
152: .getInitParameter(SharedCalendarUtils.SHARED_CAL_CONFIG_ATTR);
153: if ((configName == null)
154: || (!(configName instanceof String))) {
155: throw new PortletProvisionException(listenerName,
156: "Shared calendar config name param in portlet context is null or not a String");
157: }
158: preferences.setValue(
159: SharedCalendarUtils.SHARED_CAL_CONFIG_ATTR,
160: (String) configName);
161:
162: SharedCalendarUtils calUtils = SharedCalendarUtilsFactory
163: .getSharedCalendarUtils(portletContext);
164:
165: // create the cal proxy user for the community
166: try {
167: Properties configProps = getConfigProperties(calUtils,
168: preferences, servletRequest);
169: calUtils.getDAHandler(communityID, configProps)
170: .createCalProxyUser();
171: } catch (Exception e) {
172: throw new PortletProvisionException(listenerName,
173: "Couldn't create proxy user for cty: "
174: + communityID, e);
175: }
176:
177: // create the cal proxy calendar
178: try {
179: calUtils.getProxyCalendarStore(preferences,
180: servletRequest, communityID);
181: } catch (SharedServicesException e) {
182: throw new PortletProvisionException(listenerName,
183: "Can't create proxy calendar for community: "
184: + communityID, e);
185: }
186:
187: } catch (Exception ex) {
188: throw new PortletProvisionException(
189: "Provision failed for community: " + communityID,
190: ex);
191: }
192:
193: }
194:
195: /**
196: * Implementation of PortletProvisionListener I/F.
197: */
198: public void unprovision(PortletProvisionEvent portletProvisionEvent)
199: throws PortletProvisionException {
200:
201: String communityID = portletProvisionEvent
202: .getCommunityPrincipalId();
203: // get the preferences object for MEMBER
204: PortletProvisionPreferences preferences = portletProvisionEvent
205: .getPortletProvisionPreferences(PortletProvisionEvent.PREFERENCES_MEMBER);
206: HttpServletRequest servletRequest = portletProvisionEvent
207: .getHttpServletRequest();
208:
209: if (logger.isLoggable(Level.FINE))
210: logger.fine("UNPROVISION: SHARED CAL PROVISIONING: cty="
211: + communityID);
212:
213: try {
214:
215: Object o = getPortletContext().getAttribute(
216: SharedCalendarUtils.SHARED_CAL_UTILS_CONTEXT_ATTR);
217: if ((o == null) || !(o instanceof SharedCalendarUtils)) {
218: logger.log(Level.WARNING,
219: "SharedCalendarUtils on servlet context is null or of wrong type: "
220: + o);
221: return;
222: //throw new PortletProvisionException("SharedCalendarUtils on servlet context is null or of wrong type: " + o);
223: }
224: SharedCalendarUtils calUtils = (SharedCalendarUtils) o;
225:
226: // If autoDelete, delete the proxy cal user and calendar
227: Properties configProps = getConfigProperties(calUtils,
228: preferences, servletRequest);
229: String autoDelete = configProps
230: .getProperty(SharedCalendarUtils.CONFIG_PROP_autoDeleteProxyUserCal);
231: if ((autoDelete == null) || // default = true
232: autoDelete.equalsIgnoreCase("true")) {
233:
234: try {
235: calUtils.deleteProxyCalendarStore(communityID,
236: configProps);
237: calUtils.getDAHandler(communityID, configProps)
238: .deleteCalProxyUser();
239: } catch (Exception e) {
240: throw new PortletProvisionException(listenerName,
241: "Couldn't delete proxy user for cty: "
242: + communityID, e);
243: }
244:
245: }
246:
247: } catch (Exception ex) {
248: throw new PortletProvisionException(
249: "Unprovision failed for community: " + communityID,
250: ex);
251: }
252:
253: }
254:
255: /**
256: * Get or create a config cache on Portlet/ServletContext, indexed by cty id
257: */
258: private Map getConfigCache() {
259: PortletContext context = getPortletContext();
260: Map configs = (Map) context
261: .getAttribute(SharedCalendarUtils.CONFIG_CACHE_CONTEXT_ATTR);
262: if (configs == null) {
263: configs = new HashMap();
264: context.setAttribute(
265: SharedCalendarUtils.CONFIG_CACHE_CONTEXT_ATTR,
266: configs);
267: }
268: return configs;
269: }
270:
271: /**
272: * Delete the proxy calendar user for the community. Use JCAPI to connect to DA, using the params defined
273: * in the SSO adapter config with provided config name.
274: */
275: private void deleteCalProxyUser(
276: PortletProvisionPreferences preferences,
277: HttpServletRequest servletRequest,
278: SharedCalendarUtils calUtils, String communityID)
279: throws PortletProvisionException {
280:
281: String proxyUser = SharedCalendarUtils.CTY_CAL_PREFIX
282: + communityID;
283:
284: try {
285:
286: Properties configProps = getConfigProperties(calUtils,
287: preferences, servletRequest);
288:
289: DAConnection con = DAConnection
290: .authenticate(
291: configProps
292: .getProperty(SharedCalendarUtils.CONFIG_PROP_daHost),
293: Integer
294: .parseInt(configProps
295: .getProperty(SharedCalendarUtils.CONFIG_PROP_daPort)),
296: configProps
297: .getProperty(SharedCalendarUtils.CONFIG_PROP_daProxyUid),
298: configProps
299: .getProperty(SharedCalendarUtils.CONFIG_PROP_daProxyPassword),
300: false);
301:
302: // Get all business orgs, and select the one that corresponds to the configured domain
303: DABusinessOrganization[] orgs = con
304: .getBusinessOrganization();
305: DABusinessOrganization targetOrg = null;
306: String domain = configProps
307: .getProperty(SharedCalendarUtils.CONFIG_PROP_daDomain);
308: for (int i = 0; i < orgs.length; i++) {
309: DABusinessOrganization org = orgs[i];
310: logger.finer("DAORG: " + org.getName());
311: if (org.getName().equals(domain)) {
312: targetOrg = org;
313: }
314: }
315:
316: if (targetOrg == null) {
317: throw new PortletProvisionException(listenerName,
318: "Couldn't find a matching org for cty: "
319: + communityID + ", domain: " + domain);
320: }
321:
322: Map filter = new HashMap();
323: filter.put(DAConstants.UID, proxyUser);
324: DASearchResults rslts = targetOrg
325: .searchUsers(new DASearchConstraint(targetOrg
326: .getDN(), DASearchConstraint.SUBTREE_SCOPE,
327: filter));
328: if (rslts.hasMoreElements()) {
329: DAUser daUser = (DAUser) rslts.nextElement();
330: targetOrg.deleteUser(daUser);
331: if (logger.isLoggable(Level.FINE)) {
332: logger.fine("Deleted Proxy User: " + proxyUser);
333: }
334: } else {
335: logger.info("Proxy user: " + proxyUser
336: + " already deleted");
337: }
338:
339: } catch (Exception e) {
340: throw new PortletProvisionException(listenerName,
341: "Couldn't delete proxy user for cty: "
342: + communityID, e);
343: }
344:
345: }
346:
347: private Properties getConfigProperties(
348: SharedCalendarUtils calUtils,
349: PortletProvisionPreferences preferences,
350: HttpServletRequest request)
351: throws PortletProvisionPreferencesException,
352: SharedServicesException {
353: Map prefsMap = new HashMap();
354: Iterator names = preferences.getNames().iterator();
355: while (names.hasNext()) {
356: String name = (String) names.next();
357: prefsMap.put(name, preferences.getValue(name));
358: }
359: return calUtils.getConfigProperties(prefsMap, request);
360: }
361:
362: }
|