1 18 19 package cowsultants.itracker.ejb.beans.message; 20 21 import java.io.*; 22 import java.rmi.*; 23 import java.util.*; 24 import javax.ejb.*; 25 import javax.jms.*; 26 import javax.naming.*; 27 import javax.mail.*; 28 import javax.mail.internet.*; 29 import javax.rmi.*; 30 31 import cowsultants.itracker.ejb.util.*; 32 import cowsultants.itracker.ejb.client.util.*; 33 import cowsultants.itracker.ejb.client.models.*; 34 import cowsultants.itracker.ejb.client.interfaces.*; 35 import cowsultants.itracker.ejb.client.resources.*; 36 37 public class NotificationMessageBean implements MessageDrivenBean, MessageListener { 38 public static final String DEFAULT_CONNECTION_FACTORY = "jms/QueueConnectionFactory"; 39 public static final String DEFAULT_QUEUE_NAME = "jms/ITrackerNotificationQueue"; 40 41 public static final String DEFAULT_FROM_ADDRESS = "itracker@localhost"; 42 public static final String DEFAULT_REPLYTO_ADDRESS = "itracker@localhost"; 43 public static final String DEFAULT_SMTP_HOST = "localhost"; 44 45 MessageDrivenContext ejbContext; 46 Context jndiContext; 47 IssueHandlerHome ihHome; 48 49 String fromAddress = ""; 50 String replyToAddress = ""; 51 String smtpHost = ""; 52 53 public void setMessageDrivenContext(MessageDrivenContext mdc) { 54 ejbContext = mdc; 55 56 try { 57 jndiContext = new InitialContext(); 58 59 Object ihRef = jndiContext.lookup("java:comp/env/" + IssueHandler.JNDI_NAME); 60 ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class); 61 62 Object scRef = jndiContext.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME); 63 SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class); 64 SystemConfiguration sc = scHome.create(); 65 66 fromAddress = sc.getProperty("notification_from_address", DEFAULT_FROM_ADDRESS); 67 replyToAddress = sc.getProperty("notification_replyto_address", DEFAULT_REPLYTO_ADDRESS); 68 smtpHost = sc.getProperty("notification_smtp_host", DEFAULT_SMTP_HOST); 69 if(Logger.isLoggingDebug()) { 70 Logger.logDebug("Notification Init: From Address set to: " + fromAddress); 71 Logger.logDebug("Notification Init: ReplyTo Address set to: " + replyToAddress); 72 Logger.logDebug("Notification Init: SMTP server set to: " + smtpHost); 73 } 74 } catch(CreateException ce) { 75 throw new EJBException(ce); 76 } catch(NamingException ne) { 77 throw new EJBException(ne); 78 } 79 } 80 81 public void onMessage(javax.jms.Message message) { 82 try { 83 MapMessage notificationMsg = (MapMessage) message; 84 85 int type = notificationMsg.getInt("type"); 86 String url = notificationMsg.getString("baseURL"); 87 88 if(type == NotificationUtilities.TYPE_SELF_REGISTER) { 89 handleSelfRegistrationNotification(notificationMsg, url); 90 } else { 91 handleIssueNotification(notificationMsg, type, url); 92 } 93 } catch(Exception e) { 94 throw new EJBException(e); 95 } 96 } 97 98 private void handleSelfRegistrationNotification(MapMessage notificationMsg, String url) { 99 try { 100 String toAddress = (String ) notificationMsg.getObject("toAddress"); 101 102 if(toAddress != null && ! "".equals(toAddress)) { 103 String subject = ITrackerResources.getString("itracker.email.selfreg.subject"); 104 String msgText = ITrackerResources.getString("itracker.email.selfreg.body", ITrackerResources.getDefaultLocale(), 105 new Object [] {(String ) notificationMsg.getObject("login"), url + "/login.jsp"}); 106 EmailHandler.sendEmail(toAddress, subject, msgText); 107 } 108 } catch(Exception e) { 109 throw new EJBException(e); 110 } 111 } 112 113 private void handleIssueNotification(MapMessage notificationMsg, int type, String url) { 114 try { 115 HashSet addresses = null; 116 NotificationModel[] notifications; 117 Integer issueId = (Integer ) notificationMsg.getObject("issueId"); 118 119 if(issueId != null) { 120 byte[] addressesBytes = (byte[]) notificationMsg.getObject("addresses"); 121 if(addressesBytes != null) { 122 try { 123 ByteArrayInputStream bais = new ByteArrayInputStream(addressesBytes); 124 ObjectInputStream ois = new ObjectInputStream(bais); 125 addresses = (HashSet) ois.readObject(); 126 } catch(Exception e) { 127 Logger.logDebug("Unable to read address list for notification: " + e.getMessage()); 128 } 129 } 130 Integer lastModifiedDays = (Integer ) notificationMsg.getObject("lastModifiedDays"); 131 if(lastModifiedDays == null || lastModifiedDays.intValue() < 0) { 132 lastModifiedDays = new Integer (cowsultants.itracker.web.scheduler.tasks.ReminderNotification.DEFAULT_ISSUE_AGE); 133 } 134 135 IssueHandler ih = ihHome.create(); 136 if(addresses == null) { 137 addresses = new HashSet(); 138 notifications = ih.getIssueNotifications(issueId); 139 for(int i = 0; i < notifications.length; i++) { 140 if(notifications[i].getUserEmail() != null && notifications[i].getUserEmail().indexOf('@') >= 0) { 141 addresses.add(notifications[i].getUserEmail()); 142 } 143 } 144 } 145 146 IssueModel issue = ih.getIssue(issueId); 147 IssueActivityModel[] activity = ih.getIssueActivity(issueId, false); 148 IssueHistoryModel history = ih.getLastIssueHistory(issueId); 149 ComponentModel[] components = ih.getIssueComponents(issueId); 150 VersionModel[] versions = ih.getIssueVersions(issueId); 151 if(addresses.size() > 0) { 152 String subject = ""; 153 if(type == NotificationUtilities.TYPE_CREATED) { 154 subject = ITrackerResources.getString("itracker.email.issue.subject.created", 155 new Object [] {issue.getId(), issue.getProjectName(), lastModifiedDays}); 156 } else if(type == NotificationUtilities.TYPE_ASSIGNED) { 157 subject = ITrackerResources.getString("itracker.email.issue.subject.assigned", 158 new Object [] {issue.getId(), issue.getProjectName(), lastModifiedDays}); 159 } else if(type == NotificationUtilities.TYPE_CLOSED) { 160 subject = ITrackerResources.getString("itracker.email.issue.subject.closed", 161 new Object [] {issue.getId(), issue.getProjectName(), lastModifiedDays}); 162 } else if(type == NotificationUtilities.TYPE_ISSUE_REMINDER) { 163 subject = ITrackerResources.getString("itracker.email.issue.subject.reminder", 164 new Object [] {issue.getId(), issue.getProjectName(), lastModifiedDays}); 165 } else { 166 subject = ITrackerResources.getString("itracker.email.issue.subject.updated", 167 new Object [] {issue.getId(), issue.getProjectName(), lastModifiedDays}); 168 } 169 170 171 String activityString = ""; 172 String componentString = ""; 173 String versionString = ""; 174 for(int i = 0; i < activity.length; i++) { 175 activityString += IssueUtilities.getActivityName(activity[i].getType()) + ": " + activity[i].getDescription() + "\n"; 176 } 177 for(int i = 0; i < components.length; i++) { 178 componentString += (i != 0 ? ", " : "") + components[i].getName(); 179 } 180 for(int i = 0; i < versions.length; i++) { 181 versionString += (i != 0 ? ", " : "") + versions[i].getNumber(); 182 } 183 184 String msgText = ""; 185 if(type == NotificationUtilities.TYPE_ISSUE_REMINDER) { 186 msgText = ITrackerResources.getString("itracker.email.issue.body.reminder", 187 new Object [] {url + "/view_issue.jsp?id=" + issue.getId(), 188 issue.getProjectName(), 189 issue.getDescription(), 190 IssueUtilities.getStatusName(issue.getStatus()), 191 IssueUtilities.getSeverityName(issue.getSeverity()), 192 (issue.getOwnerFirstName() != null ? issue.getOwnerFirstName() : "") + " " + (issue.getOwnerLastName() != null ? issue.getOwnerLastName() : ""), 193 componentString, 194 (history == null ? "" : history.getUserFirstName() + " " + history.getUserLastName()), 195 (history == null ? "" : HTMLUtilities.removeMarkup(history.getDescription())), 196 lastModifiedDays, 197 activityString 198 }); 199 } else { 200 msgText = ITrackerResources.getString("itracker.email.issue.body.standard", 201 new Object [] {url + "/view_issue.jsp?id=" + issue.getId(), 202 issue.getProjectName(), 203 issue.getDescription(), 204 IssueUtilities.getStatusName(issue.getStatus()), 205 issue.getResolution(), 206 IssueUtilities.getSeverityName(issue.getSeverity()), 207 (issue.getOwnerFirstName() != null ? issue.getOwnerFirstName() : "") + " " + (issue.getOwnerLastName() != null ? issue.getOwnerLastName() : ""), 208 componentString, 209 (history == null ? "" : history.getUserFirstName() + " " + history.getUserLastName()), 210 (history == null ? "" : HTMLUtilities.removeMarkup(history.getDescription())), 211 activityString 212 }); 213 } 214 EmailHandler.sendEmail(addresses, subject, msgText); 215 ih.updateIssueActivityNotification(issue.getId(), true); 216 } 217 } 218 } catch(Exception e) { 219 throw new EJBException(e); 220 } 221 } 222 223 public void ejbCreate() {} 224 225 public void ejbRemove() { 226 try { 227 jndiContext.close(); 228 ejbContext = null; 229 } catch(NamingException ne) { 230 } 231 } 232 } | Popular Tags |