Commit d412e446e49e5fd7b54fc6e4bed010db485898cd

  • avatar
  • rmorgan <rmorgan @572de364-d518-0410…afb9c36ce0ec.>
  • Fri Dec 14 21:47:54 GMT 2007
  • Tree SHA1: 1dc0a87
  • Parent SHA1: 23e6751 ([HHQ-1370] Add custom property support to SNMPDetector for server types (merged from trunk))
  • raw diff | raw patch
Fix email filter so that restarts of the server don't cause filtered alerts to never fire.
[HHQ-1392] [merge from trunk]
src/org/hyperic/hq/bizapp/server/action/email/EmailFilter.java
(99 / 114)
  
3030import java.util.Iterator;
3131import java.util.Map;
3232import java.util.Properties;
33import java.util.Date;
3334
3435import javax.mail.Message;
3536import javax.mail.MessagingException;
4545
4646import org.apache.commons.logging.Log;
4747import org.apache.commons.logging.LogFactory;
48import org.hibernate.ObjectNotFoundException;
4948import org.hyperic.hq.appdef.server.session.PlatformManagerEJBImpl;
5049import org.hyperic.hq.appdef.shared.AppdefEntityConstants;
5150import org.hyperic.hq.appdef.shared.AppdefEntityID;
5353import org.hyperic.hq.appdef.shared.PlatformManagerLocal;
5454import org.hyperic.hq.appdef.shared.PlatformNotFoundException;
5555import org.hyperic.hq.authz.server.session.AuthzSubjectManagerEJBImpl;
56import org.hyperic.hq.authz.shared.AuthzSubjectManagerLocal;
5756import org.hyperic.hq.authz.shared.AuthzSubjectValue;
5857import org.hyperic.hq.authz.shared.PermissionException;
5958import org.hyperic.hq.common.SystemException;
6666import org.quartz.JobDetail;
6767import org.quartz.SchedulerException;
6868import org.quartz.SimpleTrigger;
69import org.quartz.Trigger;
6970
7071public class EmailFilter {
71 protected Log log = LogFactory.getLog(EmailFilter.class);
72 private Log _log = LogFactory.getLog(EmailFilter.class);
73
7274 private static final String JOB_GROUP = "EmailFilterGroup";
73 private static final IntHashMap alertBuffer = new IntHashMap();
75 private static final IntHashMap _alertBuffer = new IntHashMap();
76 private static final Object SCHEDULER_LOCK = new Object();
7477
75 private PlatformManagerLocal pltMan;
76 private ServerConfigManagerLocal configMan;
77 private AuthzSubjectValue overlord;
78 private SchedulerLocal scheduler;
79
8078 public EmailFilter() {}
8179
82 private boolean init() {
83 if (pltMan != null && overlord != null)
84 return true;
85
80 public String getAppdefEntityName(AppdefEntityID appEnt) {
81 AuthzSubjectValue overlord =
82 AuthzSubjectManagerEJBImpl.getOne().getOverlord();
8683 try {
87 pltMan = PlatformManagerEJBImpl.getOne();
88 AuthzSubjectManagerLocal authzSubjectManager =
89 AuthzSubjectManagerEJBImpl.getOne();
90 overlord = authzSubjectManager.getOverlord();
91 return true;
92 } catch (ObjectNotFoundException e) {
93 // Then we'll keep those values null
84 AppdefEntityValue entVal =
85 new AppdefEntityValue(appEnt, overlord);
86 return entVal.getName();
87 } catch (AppdefEntityNotFoundException e) {
88 _log.error("Entity ID invalid: " + e);
89 } catch (PermissionException e) {
90 // Should never happen, because we are overlord
91 _log.error("Overlord not allowed to lookup resource: " + e);
9492 }
95 return false;
96 }
9793
98 public String getAppdefEntityName(AppdefEntityID appEnt) {
99 if (init()) {
100 try {
101 AppdefEntityValue entVal =
102 new AppdefEntityValue(appEnt, overlord);
103 return entVal.getName();
104 } catch (AppdefEntityNotFoundException e) {
105 log.error("Entity ID invalid: " + e);
106 } catch (PermissionException e) {
107 // Should never happen, because we are overlord
108 log.error("Overlord not allowed to lookup resource: " + e);
109 }
110 }
11194 return appEnt.toString();
11295 }
11396
11497 private void replaceAppdefEntityHolders(AppdefEntityID appEnt,
11598 String[] strs) {
116 if (init()) {
117 try {
118 AppdefEntityValue entVal =
119 new AppdefEntityValue(appEnt, overlord);
120 String name = entVal.getName();
121 String desc = entVal.getDescription();
122
123 if (desc == null) {
124 desc = "";
125 }
126
127 for (int i = 0; i < strs.length; i++) {
128 strs[i] = strs[i].replaceAll(EmailAction.RES_NAME_HOLDER,
129 name);
130 strs[i] = strs[i].replaceAll(EmailAction.RES_DESC_HOLDER,
131 desc);
132 }
133 } catch (AppdefEntityNotFoundException e) {
134 log.error("Entity ID invalid", e);
135 } catch (PermissionException e) {
136 // Should never happen, because we are overlord
137 log.error("Overlord not allowed to lookup resource", e);
99 AuthzSubjectValue overlord =
100 AuthzSubjectManagerEJBImpl.getOne().getOverlord();
101
102 try {
103 AppdefEntityValue entVal =
104 new AppdefEntityValue(appEnt, overlord);
105 String name = entVal.getName();
106 String desc = entVal.getDescription();
107
108 if (desc == null) {
109 desc = "";
138110 }
111
112 for (int i = 0; i < strs.length; i++) {
113 strs[i] = strs[i].replaceAll(EmailAction.RES_NAME_HOLDER,
114 name);
115 strs[i] = strs[i].replaceAll(EmailAction.RES_DESC_HOLDER,
116 desc);
117 }
118 } catch (AppdefEntityNotFoundException e) {
119 _log.error("Entity ID invalid", e);
120 } catch (PermissionException e) {
121 // Should never happen, because we are overlord
122 _log.error("Overlord not allowed to lookup resource", e);
139123 }
140124 }
141125
140140 body = replStrs[1];
141141
142142 // See if alert needs to be filtered
143 if (filter && init()) {
143 if (filter) {
144 PlatformManagerLocal pltMan =
145 PlatformManagerEJBImpl.getOne();
144146 try {
145147 // Now let's look up the platform ID
146148 Integer platId;
163163 }
164164
165165 filter = false;
166 boolean scheduleJob = false;
167166
168167 // Let's see if we are adding or sending
169168 if (platId != null) {
170 synchronized (alertBuffer) {
171 if (alertBuffer.containsKey(platId.intValue())) {
169 synchronized (_alertBuffer) {
170 if (_alertBuffer.containsKey(platId.intValue())) {
172171 // Queue it up
173 Map cache = (Map)alertBuffer.get(platId.intValue());
172 Map cache = (Map) _alertBuffer.get(platId.intValue());
174173
175174 if (cache == null) {
176175 // Make sure we check again in 5 minutes
177176 cache = new Hashtable();
178 alertBuffer.put(platId.intValue(), cache);
179 scheduleJob = true;
177 _alertBuffer.put(platId.intValue(), cache);
180178 }
181179
182180 for (int i = 0; i < addresses.length; i++) {
194194
195195 filter = true;
196196 } else {
197 // Add new job to send filtered e-mail in 5 minutes
198 scheduleJob = true;
199
200197 // Add a new queue
201 alertBuffer.put(platId.intValue(), new Hashtable());
198 _alertBuffer.put(platId.intValue(), new Hashtable());
202199 }
203200 }
204201 }
205
206 if (scheduleJob) {
207 try {
208 scheduleJob(platId);
209 } catch (SchedulerException e) {
210 // Job probably already exists
211 log.error("Unable to reschedule job " + platId, e);
212 }
202
203 try {
204 scheduleJob(platId);
205 } catch (SchedulerException e) {
206 // Job probably already exists
207 _log.error("Unable to reschedule job " + platId, e);
213208 }
214209
215210 if (filter)
216211 return;
217212 } catch (PlatformNotFoundException e) {
218 log.error("Entity ID invalid: " + e);
213 _log.error("Entity ID invalid: " + e);
219214 }
220215 }
221216
218218 }
219219
220220 private InternetAddress getFromAddress() {
221 ServerConfigManagerLocal configMan =
222 ServerConfigManagerEJBImpl.getOne();
221223 try {
222 if (this.configMan == null) {
223 this.configMan = ServerConfigManagerEJBImpl.getOne();
224 }
225
226 Properties props = this.configMan.getConfig();
224 Properties props = configMan.getConfig();
227225 String from = props.getProperty(HQConstants.EmailSender);
228226 if (from != null) {
229227 return new InternetAddress(from);
230228 }
231229 } catch (ConfigPropertyException e) {
232 log.error("ConfigPropertyException fetch FROM address", e);
230 _log.error("ConfigPropertyException fetch FROM address", e);
233231 } catch (AddressException e) {
234 log.error("Bad FROM address", e);
232 _log.error("Bad FROM address", e);
235233 }
236234 return null;
237235 }
259259
260260 m.setSubject(subject);
261261
262 if (log.isDebugEnabled()) {
263 log.debug("Sending Alert Email: " + body);
264 log.debug("Sending HTML Alert Email: " + htmlBody);
262 if (_log.isDebugEnabled()) {
263 _log.debug("Sending Alert Email: " + body);
264 _log.debug("Sending HTML Alert Email: " + htmlBody);
265265 }
266266
267267 // Send to each recipient individually (for D.B. SMS)
278278 Transport.send(m);
279279 }
280280 } catch (MessagingException e) {
281 log.error("Error sending email: " + subject);
282 log.debug("Messaging Error sending email", e);
281 _log.error("Error sending email: " + subject);
282 _log.debug("Messaging Error sending email", e);
283283 }
284284 }
285285
286286 void sendFiltered(int platId) {
287287 Hashtable cache;
288288
289 synchronized (alertBuffer) {
290 if (!alertBuffer.containsKey(platId))
289 synchronized (_alertBuffer) {
290 if (!_alertBuffer.containsKey(platId))
291291 return;
292292
293 cache = (Hashtable) alertBuffer.remove(platId);
293 cache = (Hashtable) _alertBuffer.remove(platId);
294294
295295 if (cache == null || cache.size() == 0)
296296 return;
297297
298298 // Insert key again so that we continue filtering
299 alertBuffer.put(platId, null);
299 _alertBuffer.put(platId, null);
300300 }
301301
302302 AppdefEntityID platEntId = AppdefEntityID.newPlatformID(platId);
323323
324324 private void scheduleJob(Integer platId) throws SchedulerException {
325325 // Create new job name with the appId
326 String name = EmailFilterJob.class.getName() + platId;
327
328 // Create job detail
329 JobDetail jd = new JobDetail(name + "Job", JOB_GROUP,
330 EmailFilterJob.class);
331
332 String appIdStr = platId.toString();
333
334 jd.getJobDataMap().put(EmailFilterJob.APP_ID, appIdStr);
335
336 // Create trigger
337 GregorianCalendar next = new GregorianCalendar();
338 next.add(GregorianCalendar.MINUTE, 5);
339 SimpleTrigger t = new SimpleTrigger(name + "Trigger", JOB_GROUP,
340 next.getTime());
341
342 // Now schedule with EJB
343 if (scheduler == null) {
344 scheduler = SchedulerEJBImpl.getOne();
326 String name = EmailFilterJob.class.getName() + platId + "Job";
327
328 SchedulerLocal scheduler = SchedulerEJBImpl.getOne();
329
330 synchronized (SCHEDULER_LOCK) {
331
332 Trigger[] triggers = scheduler.getTriggersOfJob(name, JOB_GROUP);
333 if (triggers.length == 0) {
334 JobDetail jobDetail = new JobDetail(name, JOB_GROUP,
335 EmailFilterJob.class);
336
337 String appIdStr = platId.toString();
338
339 jobDetail.getJobDataMap().put(EmailFilterJob.APP_ID, appIdStr);
340
341 // XXX: Make this time configurable?
342 GregorianCalendar next = new GregorianCalendar();
343 next.add(GregorianCalendar.MINUTE, 5);
344 SimpleTrigger t = new SimpleTrigger(name + "Trigger", JOB_GROUP,
345 next.getTime());
346
347 Date nextfire = scheduler.scheduleJob(jobDetail, t);
348 _log.debug("Will queue alerts for platform " +
349 platId + " until " + nextfire);
350 } else {
351 // Already scheduled, there will only be a single trigger.
352 _log.debug("Already queing alerts for platform " +
353 platId + ", will fire at " +
354 triggers[0].getNextFireTime());
355 }
345356 }
346
347 scheduler.scheduleJob(jd, t);
348357 }
349358}

Comments

Add a new comment:

Login or create an account to post a comment

Add your comment