001: package com.sun.portal.subscriptions.profiler;
002:
003: import com.sun.portal.subscriptions.profiler.cli.ProfilerCmd;
004:
005: import java.net.InetAddress;
006: import java.util.Properties;
007: import java.util.Date;
008: import java.util.logging.Level;
009: import java.util.logging.LogRecord;
010: import java.util.logging.Logger;
011:
012: import javax.mail.*;
013: import javax.mail.internet.*;
014:
015: import com.sun.portal.search.soif.*;
016: import com.sun.portal.log.common.PortalLogger;
017: import com.iplanet.sso.SSOException;
018: import com.iplanet.am.sdk.AMStoreConnection;
019: import com.iplanet.am.sdk.AMUser;
020: import com.iplanet.am.sdk.AMException;
021: import com.sun.portal.admin.common.util.AdminUtil;
022:
023: public class NotificationWorker implements Runnable {
024: private AMStoreConnection dsameCon;
025: private UserQueue uq;
026: private NotificationQueue nq;
027: private Properties profilerProps;
028: private Properties notifierProps;
029: private static Logger logger = PortalLogger
030: .getLogger(NotificationWorker.class);
031:
032: public NotificationWorker(AMStoreConnection c, UserQueue uq,
033: NotificationQueue nq, Properties pp, Properties np) {
034: this .dsameCon = c;
035: this .uq = uq;
036: this .nq = nq;
037: this .profilerProps = pp;
038: this .notifierProps = np;
039: }
040:
041: /*
042: * algo is :
043: * - get the notification
044: * - deliver notification
045: * - update user profile
046: *
047: * - if error, put back the user back in user queue
048: */
049: public void run() {
050: Notification n = null;
051: try {
052: while ((n = (Notification) nq.get()) != null) {
053: StringBuffer content = (StringBuffer) n.getContent();
054: AMUser u = n.getUser();
055: String uEmail = null;
056: try {
057: uEmail = (String) u.getStringAttribute("mail");
058: } catch (AMException ame) {
059: } catch (SSOException ssoe) {
060: }
061: if (uEmail != null) {
062: switch (n.getMedium()) {
063: case Notification.MEDIUM_EMAIL:
064: try {
065: Session session = Session.getInstance(
066: notifierProps, null);
067: Date msgDate = new Date();
068: Message msg = new MimeMessage(session);
069: try {
070: msg
071: .setFrom(new InternetAddress(
072: notifierProps
073: .getProperty(
074: "profilerFrom",
075: "profiler@portal.biz")));
076: } catch (AddressException ae) {
077: if (getLogger()
078: .isLoggable(Level.SEVERE)) {
079: String[] param = {
080: "profilerForm",
081: notifierProps
082: .getProperty(
083: "profilerFrom",
084: "profiler@portal.biz") };
085: getLogger().log(Level.SEVERE,
086: "PSSS_CSPP0010", param);
087: }
088: }
089: msg.setRecipients(Message.RecipientType.TO,
090: InternetAddress
091: .parse(uEmail, false));
092: msg.setSubject("Profiler output ("
093: + msgDate + ")");
094: msg
095: .setHeader("X-Mailer",
096: "portal Profiler");
097: msg.setSentDate(msgDate);
098: MimeMultipart multipart = new MimeMultipart();
099: BodyPart msgBodyHTMLPart = new MimeBodyPart();
100: msgBodyHTMLPart.setContent(n.getContent()
101: .toString(),
102: "text/html; charset=UTF-8");
103: multipart.addBodyPart(msgBodyHTMLPart);
104: msg.setContent(multipart);
105: msg.setHeader("Content-Transfer-Encoding",
106: "8bit");
107: Transport.send(msg);
108: String portalId = profilerProps
109: .getProperty("portalId");
110: if (portalId == null
111: || portalId
112: .equals(AdminUtil.UPGRADED_PORTAL)) {
113: // Upgraded portal's have a subscriptions service which has a "%PORTALID% empty
114: portalId = "";
115: }
116: try {
117: u.setStringAttribute("sunPortal"
118: + portalId + "ProfilerLastRun",
119: (new Date()).toString());
120: u.store();
121: } catch (AMException ame) {
122: if (getLogger()
123: .isLoggable(Level.SEVERE)) {
124: LogRecord rec = new LogRecord(
125: Level.SEVERE,
126: "PSSS_CSPP0011");
127: String[] param = { u.getDN() };
128: rec.setThrown(ame);
129: rec.setParameters(param);
130: rec.setLoggerName(getLogger()
131: .getName());
132: getLogger().log(rec);
133: }
134: } catch (SSOException ssoe) {
135: if (getLogger()
136: .isLoggable(Level.SEVERE)) {
137: LogRecord rec = new LogRecord(
138: Level.SEVERE,
139: "PSSS_CSPP0011");
140: String[] param = { u.getDN() };
141: rec.setThrown(ssoe);
142: rec.setParameters(param);
143: rec.setLoggerName(getLogger()
144: .getName());
145: getLogger().log(rec);
146: }
147: }
148: } catch (AddressException ae) {
149: if (getLogger().isLoggable(Level.SEVERE)) {
150: String[] param = { u.getDN(), uEmail };
151: getLogger().log(Level.SEVERE,
152: "PSSS_CSPP0010", param);
153: }
154: } catch (MessagingException me) {
155: if (getLogger().isLoggable(Level.SEVERE)) {
156: LogRecord rec = new LogRecord(
157: Level.SEVERE, "PSSS_CSPP0011");
158: String[] param = { u.getDN() };
159: rec.setThrown(me);
160: rec.setParameters(param);
161: rec
162: .setLoggerName(getLogger()
163: .getName());
164: getLogger().log(rec);
165: }
166: }
167: break;
168: case Notification.MEDIUM_IM:
169: //TODO
170: break;
171: default:
172: if (getLogger().isLoggable(Level.SEVERE))
173: getLogger().log(Level.SEVERE,
174: "PSSS_CSPP0008",
175: Thread.currentThread().toString());
176: break;
177: }
178: ;
179: } else {
180: if (getLogger().isLoggable(Level.FINE))
181: getLogger().log(Level.FINE, "PSSS_CSPP0013",
182: Thread.currentThread().toString());
183: }
184: }
185: } catch (Exception e) {
186: if (getLogger().isLoggable(Level.SEVERE))
187: getLogger().log(Level.SEVERE, "PSSS_CSPP0009", e);
188: }
189: }
190:
191: public Logger getLogger() {
192: return logger;
193: }
194:
195: public void setLogger(Logger logger) {
196: this.logger = logger;
197: }
198: }
|