1 package org.enhydra.shark.eventaudit; 2 3 import java.util.List ; 4 import java.util.Properties ; 5 6 import javax.mail.*; 7 import javax.mail.internet.InternetAddress ; 8 import javax.mail.internet.MimeMessage ; 9 10 import org.enhydra.shark.Shark; 11 import org.enhydra.shark.api.RootException; 12 import org.enhydra.shark.api.SharkTransaction; 13 import org.enhydra.shark.api.internal.eventaudit.*; 14 import org.enhydra.shark.api.internal.instancepersistence.PersistenceException; 15 import org.enhydra.shark.api.internal.working.CallbackUtilities; 16 import org.enhydra.shark.utilities.MiscUtilities; 17 18 31 public class SMTPEventAuditManager implements EventAuditManagerInterface { 32 33 private boolean DEBUG; 34 35 private boolean enable_email; 36 37 private CallbackUtilities cus; 38 39 private EventAuditManagerInterface other; 40 41 private static final String _PARAM_PREFIX = "SMTPEventAuditManager"; 42 43 private static final String _PARAM_OTHER_MGR = ".OtherClassName"; 44 45 private static final String _PARAM_OTHER_MGR_DEFAULT = "org.enhydra.shark.eventaudit.DODSEventAuditManager"; 46 47 private static final String _PARAM_DEBUG = ".Debug"; 48 49 private static final String _PARAM_ENABLE = ".Enable"; 50 51 private static final String _PARAM_SERVER_ADDR = ".Server"; 52 53 private static final String _PARAM_SERVER_PORT = ".Port"; 54 55 private static final String _PARAM_AUTH = ".Auth"; 56 57 protected static final String _PARAM_PASSWD = ".Passwd"; 58 59 private static final String _PARAM_SOURCE = ".Source"; 60 61 private static final String _PARAM_MESSAGE_TEMPLATE = ".Message"; 62 63 private static final String _PARAM_SUBJECT_TEMPLATE = ".Subject"; 64 65 private String _subject; 66 67 private String _message; 68 69 public void configure(CallbackUtilities cus) throws RootException { 70 try { 71 other = (EventAuditManagerInterface) Class.forName(cus.getProperty(_PARAM_PREFIX 72 + _PARAM_OTHER_MGR, 73 _PARAM_OTHER_MGR_DEFAULT)) 74 .newInstance(); 75 other.configure(cus); 76 } catch (Throwable t) {} 77 DEBUG = Boolean.valueOf(cus.getProperty(_PARAM_PREFIX + _PARAM_DEBUG, 78 "false")).booleanValue(); 79 enable_email = Boolean.valueOf(cus.getProperty(_PARAM_PREFIX 80 + _PARAM_ENABLE, "false")) 81 .booleanValue(); 82 _subject = cus.getProperty(_PARAM_PREFIX + _PARAM_SUBJECT_TEMPLATE, 83 "New task: {activity}"); 84 _message = cus.getProperty(_PARAM_PREFIX + _PARAM_MESSAGE_TEMPLATE, 85 "Dear {person},\n\nyou have a new task!\n\n" 86 + "name: {activity}\n" 87 + "workflow: {process}\n" 88 + "workflow id: {definition}"); 89 this.cus = cus; 90 if (DEBUG) System.err.println(_PARAM_PREFIX + " configured"); 91 } 92 93 101 private void sendEmail(String text_msg, 102 String subject, 103 String destination_address) throws RootException { 104 try { 106 final Properties props = new Properties (); 108 props.put("mail.smtp.host", cus.getProperty(_PARAM_PREFIX 110 + _PARAM_SERVER_ADDR)); 111 props.put("mail.smtp.port", cus.getProperty(_PARAM_PREFIX 112 + _PARAM_SERVER_PORT)); 113 final String auth = cus.getProperty(_PARAM_PREFIX + _PARAM_AUTH); 115 javax.mail.Session session; 116 if (auth.equals("")) { 117 session = Session.getInstance(props); 118 } else { 119 props.put("mail.smtp.user", auth); 120 props.put("mail.smtp.auth", "true"); 121 session = Session.getInstance(props, 123 new Authenticator() { 124 public PasswordAuthentication getPasswordAuthentication() { 125 return new PasswordAuthentication(auth, 126 cus.getProperty(_PARAM_PREFIX 127 + _PARAM_PASSWD)); 128 } 129 }); 130 131 } 132 final MimeMessage message = new MimeMessage (session); 134 message.setFrom(new InternetAddress (cus.getProperty(_PARAM_PREFIX 135 + _PARAM_SOURCE))); 136 message.addRecipient(Message.RecipientType.TO, 137 new InternetAddress (destination_address)); 138 message.setSubject(subject); 139 message.setContent(text_msg, "text/plain"); 140 Transport.send(message); 142 } catch (Exception e) { 143 if (DEBUG) { 144 e.printStackTrace(); 145 } 146 throw new EventAuditException(e); 147 } 148 } 149 150 157 public void persist(AssignmentEventAuditPersistenceInterface assea, 158 SharkTransaction ti) throws EventAuditException { 159 other.persist(assea, ti); 160 if (enable_email) { 162 try { 163 final String login = assea.getNewResourceUsername(); 165 final String person = Shark.getInstance() 167 .getAdminInterface() 168 .getUserGroupAdministration() 169 .getUserRealName(login); 170 final String email = Shark.getInstance() 172 .getAdminInterface() 173 .getUserGroupAdministration() 174 .getUserEMailAddress(login); 175 final String subject = parse(_subject, 177 person, 178 assea.getActivityName(), 179 assea.getProcessName(), 180 assea.getProcessDefinitionId()); 181 final String msg = parse(_message, 182 person, 183 assea.getActivityName(), 184 assea.getProcessName(), 185 assea.getProcessDefinitionId()); 186 sendEmail(msg, subject, email); 188 } catch (Exception e) { 189 System.err.println(e); 190 if (DEBUG) { 191 e.printStackTrace(); 192 } 193 } 194 } 195 } 196 197 private String parse(String template, 198 String person, 199 String activity, 200 String process, 201 String definition) { 202 String ret = template; 203 if (-1 != template.indexOf("{person}")) { 204 ret = MiscUtilities.replaceAll(ret, "{person}", person); 205 } 206 if (-1 != template.indexOf("{activity}")) { 207 ret = MiscUtilities.replaceAll(ret, "{activity}", activity); 208 } 209 if (-1 != template.indexOf("{process}")) { 210 ret = MiscUtilities.replaceAll(ret, "{process}", process); 211 } 212 if (-1 != template.indexOf("{definition}")) { 213 ret = MiscUtilities.replaceAll(ret, "{definition}", definition); 214 } 215 return ret; 216 } 217 218 public void persist(CreateProcessEventAuditPersistenceInterface cpea, 219 SharkTransaction ti) throws EventAuditException { 220 other.persist(cpea, ti); 221 } 222 223 public void persist(DataEventAuditPersistenceInterface dea, 224 SharkTransaction ti) throws EventAuditException { 225 other.persist(dea, ti); 226 } 227 228 public void persist(StateEventAuditPersistenceInterface sea, 229 SharkTransaction ti) throws EventAuditException { 230 other.persist(sea, ti); 231 } 232 233 public boolean restore(AssignmentEventAuditPersistenceInterface assea, 234 SharkTransaction ti) throws EventAuditException { 235 return other.restore(assea, ti); 236 } 237 238 public boolean restore(CreateProcessEventAuditPersistenceInterface cpea, 239 SharkTransaction ti) throws EventAuditException { 240 return other.restore(cpea, ti); 241 } 242 243 public boolean restore(DataEventAuditPersistenceInterface dea, 244 SharkTransaction ti) throws EventAuditException { 245 return other.restore(dea, ti); 246 } 247 248 public boolean restore(StateEventAuditPersistenceInterface sea, 249 SharkTransaction ti) throws EventAuditException { 250 return other.restore(sea, ti); 251 } 252 253 public List restoreProcessHistory(String procId, SharkTransaction ti) throws EventAuditException { 254 return other.restoreProcessHistory(procId, ti); 255 } 256 257 public List restoreActivityHistory(String procId, 258 String actId, 259 SharkTransaction ti) throws EventAuditException { 260 return other.restoreActivityHistory(procId, actId, ti); 261 } 262 263 public void delete(AssignmentEventAuditPersistenceInterface assea, 264 SharkTransaction ti) throws EventAuditException { 265 other.delete(assea, ti); 266 } 267 268 public void delete(CreateProcessEventAuditPersistenceInterface cpea, 269 SharkTransaction ti) throws EventAuditException { 270 other.delete(cpea, ti); 271 } 272 273 public void delete(DataEventAuditPersistenceInterface dea, 274 SharkTransaction ti) throws EventAuditException { 275 other.delete(dea, ti); 276 } 277 278 public void delete(StateEventAuditPersistenceInterface sea, 279 SharkTransaction ti) throws EventAuditException { 280 other.delete(sea, ti); 281 } 282 283 public AssignmentEventAuditPersistenceInterface createAssignmentEventAudit() { 284 return other.createAssignmentEventAudit(); 285 } 286 287 public CreateProcessEventAuditPersistenceInterface createCreateProcessEventAudit() { 288 return other.createCreateProcessEventAudit(); 289 } 290 291 public DataEventAuditPersistenceInterface createDataEventAudit() { 292 return other.createDataEventAudit(); 293 } 294 295 public StateEventAuditPersistenceInterface createStateEventAudit() { 296 return other.createStateEventAudit(); 297 } 298 299 public String getNextId(String idName) throws EventAuditException { 300 return other.getNextId(idName); 301 } 302 } 303 304 | Popular Tags |