KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > eventaudit > SMTPEventAuditManager


1 package org.enhydra.shark.eventaudit;
2
3 import java.util.List JavaDoc;
4 import java.util.Properties JavaDoc;
5
6 import javax.mail.*;
7 import javax.mail.internet.InternetAddress JavaDoc;
8 import javax.mail.internet.MimeMessage JavaDoc;
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 /**
19  * This persistent manager send an email to every person that has to
20  * accept a new task DODSPersistentManager is the default persistent
21  * manager of Enhydra-Shark.
22  * <p>
23  * In addition to original functionality (coded against beta 2 by
24  * Mathias), there is now new configuration procedure, and separation
25  * from other components - persistence manager and tool agents.
26  *
27  * @version 1.1
28  * @author Mathias Holst
29  * @since 9. August 2004
30  */

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 JavaDoc _PARAM_PREFIX = "SMTPEventAuditManager";
42
43    private static final String JavaDoc _PARAM_OTHER_MGR = ".OtherClassName";
44
45    private static final String JavaDoc _PARAM_OTHER_MGR_DEFAULT = "org.enhydra.shark.eventaudit.DODSEventAuditManager";
46
47    private static final String JavaDoc _PARAM_DEBUG = ".Debug";
48
49    private static final String JavaDoc _PARAM_ENABLE = ".Enable";
50
51    private static final String JavaDoc _PARAM_SERVER_ADDR = ".Server";
52
53    private static final String JavaDoc _PARAM_SERVER_PORT = ".Port";
54
55    private static final String JavaDoc _PARAM_AUTH = ".Auth";
56
57    protected static final String JavaDoc _PARAM_PASSWD = ".Passwd";
58
59    private static final String JavaDoc _PARAM_SOURCE = ".Source";
60
61    private static final String JavaDoc _PARAM_MESSAGE_TEMPLATE = ".Message";
62
63    private static final String JavaDoc _PARAM_SUBJECT_TEMPLATE = ".Subject";
64
65    private String JavaDoc _subject;
66
67    private String JavaDoc _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 JavaDoc 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    /**
94     * Description of the Method
95     *
96     * @param text_msg Description of the Parameter
97     * @param subject Description of the Parameter
98     * @param destination_address Description of the Parameter
99     * @throws RootException
100     */

101    private void sendEmail(String JavaDoc text_msg,
102                           String JavaDoc subject,
103                           String JavaDoc destination_address) throws RootException {
104       // Get system properties
105
try {
106          // FIXME: can we create this props object only once
107
final Properties JavaDoc props = new Properties JavaDoc();
108          // Setup mail server
109
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          // User name
114
final String JavaDoc auth = cus.getProperty(_PARAM_PREFIX + _PARAM_AUTH);
115          javax.mail.Session JavaDoc 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             // Get session
122
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          // Define message
133
final MimeMessage JavaDoc message = new MimeMessage JavaDoc(session);
134          message.setFrom(new InternetAddress JavaDoc(cus.getProperty(_PARAM_PREFIX
135                                                              + _PARAM_SOURCE)));
136          message.addRecipient(Message.RecipientType.TO,
137                               new InternetAddress JavaDoc(destination_address));
138          message.setSubject(subject);
139          message.setContent(text_msg, "text/plain");
140          // Send message
141
Transport.send(message);
142       } catch (Exception JavaDoc e) {
143          if (DEBUG) {
144             e.printStackTrace();
145          }
146          throw new EventAuditException(e);
147       }
148    }
149
150    /**
151     * Description of the Method
152     *
153     * @param assea Description of the Parameter
154     * @param ti Description of the Parameter
155     * @exception PersistenceException Description of the Exception
156     */

157    public void persist(AssignmentEventAuditPersistenceInterface assea,
158                        SharkTransaction ti) throws EventAuditException {
159       other.persist(assea, ti);
160       // senhd email to person that has to accpet new task
161
if (enable_email) {
162          try {
163             // get user's login name
164
final String JavaDoc login = assea.getNewResourceUsername();
165             // get user's real name
166
final String JavaDoc person = Shark.getInstance()
167                .getAdminInterface()
168                .getUserGroupAdministration()
169                .getUserRealName(login);
170             // get user's email-address
171
final String JavaDoc email = Shark.getInstance()
172                .getAdminInterface()
173                .getUserGroupAdministration()
174                .getUserEMailAddress(login);
175             // define subject of email
176
final String JavaDoc subject = parse(_subject,
177                                          person,
178                                          assea.getActivityName(),
179                                          assea.getProcessName(),
180                                          assea.getProcessDefinitionId());
181             final String JavaDoc msg = parse(_message,
182                                      person,
183                                      assea.getActivityName(),
184                                      assea.getProcessName(),
185                                      assea.getProcessDefinitionId());
186             // finally send email
187
sendEmail(msg, subject, email);
188          } catch (Exception JavaDoc e) {
189             System.err.println(e);
190             if (DEBUG) {
191                e.printStackTrace();
192             }
193          }
194       }
195    }
196
197    private String JavaDoc parse(String JavaDoc template,
198                         String JavaDoc person,
199                         String JavaDoc activity,
200                         String JavaDoc process,
201                         String JavaDoc definition) {
202       String JavaDoc 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 JavaDoc restoreProcessHistory(String JavaDoc procId, SharkTransaction ti) throws EventAuditException {
254       return other.restoreProcessHistory(procId, ti);
255    }
256
257    public List JavaDoc restoreActivityHistory(String JavaDoc procId,
258                                       String JavaDoc 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 JavaDoc getNextId(String JavaDoc idName) throws EventAuditException {
300       return other.getNextId(idName);
301    }
302 }
303
304
Popular Tags