001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036:
037: package com.sun.portal.community.notification.impl;
038:
039: import java.util.Set;
040: import java.util.HashSet;
041: import java.util.Iterator;
042: import java.util.Properties;
043: import java.util.logging.Logger;
044: import java.util.logging.Level;
045:
046: import javax.servlet.http.HttpServletRequest;
047:
048: import com.sun.portal.log.common.PortalLogger;
049:
050: import com.sun.portal.community.impl.CommunityProperties;
051:
052: import com.sun.portal.community.notification.NotificationManager;
053: import com.sun.portal.community.notification.NotificationException;
054: import com.sun.portal.community.notification.Notification;
055: import com.sun.portal.community.notification.Notifier;
056: import com.sun.portal.community.notification.NotificationType;
057: import com.sun.portal.community.notification.NotificationTypes;
058:
059: public class PropertiesNotificationManagerImpl implements
060: NotificationManager {
061:
062: private static NotificationTypes _types = new NotificationTypes();
063: private static Set _notifiers = new HashSet();
064:
065: private static Logger logger = PortalLogger
066: .getLogger(PropertiesNotificationManagerImpl.class);
067:
068: public void init() throws NotificationException {
069:
070: CommunityProperties cProps = CommunityProperties.getInstance();
071:
072: //
073: // get active notification type(s)
074: //
075: Set types = cProps.getNotificationTypes();
076: if (types != null && types.size() > 0) {
077: for (Iterator i = types.iterator(); i.hasNext();) {
078: String typeStr = (String) i.next();
079: NotificationType type = NotificationType
080: .valueOf(typeStr);
081: _types.add(type);
082: }
083: }
084:
085: //
086: // get available notifier(s)
087: //
088: Set notifiers = cProps.getNotifierClasses();
089: if (notifiers != null && notifiers.size() > 0) {
090: for (Iterator i = notifiers.iterator(); i.hasNext();) {
091: String notifierClass = (String) i.next();
092: Notifier notifier = loadNotifier(notifierClass);
093: notifier.init();
094: _notifiers.add(notifier);
095: }
096: }
097:
098: if (logger.isLoggable(Level.INFO)) {
099: logger.log(Level.INFO, "PSCPM_CSPCNI00000", _notifiers);
100: logger.log(Level.INFO, "PSCPM_CSPCNI00005", _types);
101: }
102: }
103:
104: public void send(HttpServletRequest request,
105: Notification notification) throws NotificationException {
106:
107: if (_types.contains(notification.getType())) {
108: for (Iterator i = _notifiers.iterator(); i.hasNext();) {
109: Notifier notifier = (Notifier) i.next();
110: notifier.send(request, notification);
111: }
112: }
113: }
114:
115: private Notifier loadNotifier(String notifierClassName)
116: throws NotificationException {
117:
118: Notifier notifier = null;
119: try {
120: notifier = (Notifier) Class.forName(notifierClassName)
121: .newInstance();
122: } catch (ClassNotFoundException cnfe) {
123: throw new NotificationException(
124: "PropertiesNotificationManagerImpl.init(): Failed to load Notifier="
125: + notifierClassName, cnfe);
126: } catch (InstantiationException ie) {
127: throw new NotificationException(
128: "PropertiesNotificationManagerImpl.init(): Failed to load Notifier="
129: + notifierClassName, ie);
130: } catch (IllegalAccessException iae) {
131: throw new NotificationException(
132: "PropertiesNotificationManagerImpl.init(): Failed to load Notifier="
133: + notifierClassName, iae);
134: }
135: return notifier;
136: }
137: }
|