Commit d412e446e49e5fd7b54fc6e4bed010db485898cd
- Diff rendering mode:
- inline
- side by side
src/org/hyperic/hq/bizapp/server/action/email/EmailFilter.java
(99 / 114)
|   | |||
| 30 | 30 | import java.util.Iterator; | |
| 31 | 31 | import java.util.Map; | |
| 32 | 32 | import java.util.Properties; | |
| 33 | import java.util.Date; | ||
| 33 | 34 | ||
| 34 | 35 | import javax.mail.Message; | |
| 35 | 36 | import javax.mail.MessagingException; | |
| … | … | ||
| 45 | 45 | ||
| 46 | 46 | import org.apache.commons.logging.Log; | |
| 47 | 47 | import org.apache.commons.logging.LogFactory; | |
| 48 | import org.hibernate.ObjectNotFoundException; | ||
| 49 | 48 | import org.hyperic.hq.appdef.server.session.PlatformManagerEJBImpl; | |
| 50 | 49 | import org.hyperic.hq.appdef.shared.AppdefEntityConstants; | |
| 51 | 50 | import org.hyperic.hq.appdef.shared.AppdefEntityID; | |
| … | … | ||
| 53 | 53 | import org.hyperic.hq.appdef.shared.PlatformManagerLocal; | |
| 54 | 54 | import org.hyperic.hq.appdef.shared.PlatformNotFoundException; | |
| 55 | 55 | import org.hyperic.hq.authz.server.session.AuthzSubjectManagerEJBImpl; | |
| 56 | import org.hyperic.hq.authz.shared.AuthzSubjectManagerLocal; | ||
| 57 | 56 | import org.hyperic.hq.authz.shared.AuthzSubjectValue; | |
| 58 | 57 | import org.hyperic.hq.authz.shared.PermissionException; | |
| 59 | 58 | import org.hyperic.hq.common.SystemException; | |
| … | … | ||
| 66 | 66 | import org.quartz.JobDetail; | |
| 67 | 67 | import org.quartz.SchedulerException; | |
| 68 | 68 | import org.quartz.SimpleTrigger; | |
| 69 | import org.quartz.Trigger; | ||
| 69 | 70 | ||
| 70 | 71 | public class EmailFilter { | |
| 71 | protected Log log = LogFactory.getLog(EmailFilter.class); | ||
| 72 | private Log _log = LogFactory.getLog(EmailFilter.class); | ||
| 73 | |||
| 72 | 74 | 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(); | ||
| 74 | 77 | ||
| 75 | private PlatformManagerLocal pltMan; | ||
| 76 | private ServerConfigManagerLocal configMan; | ||
| 77 | private AuthzSubjectValue overlord; | ||
| 78 | private SchedulerLocal scheduler; | ||
| 79 | |||
| 80 | 78 | public EmailFilter() {} | |
| 81 | 79 | ||
| 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(); | ||
| 86 | 83 | 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); | ||
| 94 | 92 | } | |
| 95 | return false; | ||
| 96 | } | ||
| 97 | 93 | ||
| 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 | } | ||
| 111 | 94 | return appEnt.toString(); | |
| 112 | 95 | } | |
| 113 | 96 | ||
| 114 | 97 | private void replaceAppdefEntityHolders(AppdefEntityID appEnt, | |
| 115 | 98 | 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 = ""; | ||
| 138 | 110 | } | |
| 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); | ||
| 139 | 123 | } | |
| 140 | 124 | } | |
| 141 | 125 | ||
| … | … | ||
| 140 | 140 | body = replStrs[1]; | |
| 141 | 141 | ||
| 142 | 142 | // See if alert needs to be filtered | |
| 143 | if (filter && init()) { | ||
| 143 | if (filter) { | ||
| 144 | PlatformManagerLocal pltMan = | ||
| 145 | PlatformManagerEJBImpl.getOne(); | ||
| 144 | 146 | try { | |
| 145 | 147 | // Now let's look up the platform ID | |
| 146 | 148 | Integer platId; | |
| … | … | ||
| 163 | 163 | } | |
| 164 | 164 | ||
| 165 | 165 | filter = false; | |
| 166 | boolean scheduleJob = false; | ||
| 167 | 166 | ||
| 168 | 167 | // Let's see if we are adding or sending | |
| 169 | 168 | if (platId != null) { | |
| 170 | synchronized (alertBuffer) { | ||
| 171 | if (alertBuffer.containsKey(platId.intValue())) { | ||
| 169 | synchronized (_alertBuffer) { | ||
| 170 | if (_alertBuffer.containsKey(platId.intValue())) { | ||
| 172 | 171 | // Queue it up | |
| 173 | Map cache = (Map)alertBuffer.get(platId.intValue()); | ||
| 172 | Map cache = (Map) _alertBuffer.get(platId.intValue()); | ||
| 174 | 173 | ||
| 175 | 174 | if (cache == null) { | |
| 176 | 175 | // Make sure we check again in 5 minutes | |
| 177 | 176 | cache = new Hashtable(); | |
| 178 | alertBuffer.put(platId.intValue(), cache); | ||
| 179 | scheduleJob = true; | ||
| 177 | _alertBuffer.put(platId.intValue(), cache); | ||
| 180 | 178 | } | |
| 181 | 179 | ||
| 182 | 180 | for (int i = 0; i < addresses.length; i++) { | |
| … | … | ||
| 194 | 194 | ||
| 195 | 195 | filter = true; | |
| 196 | 196 | } else { | |
| 197 | // Add new job to send filtered e-mail in 5 minutes | ||
| 198 | scheduleJob = true; | ||
| 199 | |||
| 200 | 197 | // Add a new queue | |
| 201 | alertBuffer.put(platId.intValue(), new Hashtable()); | ||
| 198 | _alertBuffer.put(platId.intValue(), new Hashtable()); | ||
| 202 | 199 | } | |
| 203 | 200 | } | |
| 204 | 201 | } | |
| 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); | ||
| 213 | 208 | } | |
| 214 | 209 | ||
| 215 | 210 | if (filter) | |
| 216 | 211 | return; | |
| 217 | 212 | } catch (PlatformNotFoundException e) { | |
| 218 | log.error("Entity ID invalid: " + e); | ||
| 213 | _log.error("Entity ID invalid: " + e); | ||
| 219 | 214 | } | |
| 220 | 215 | } | |
| 221 | 216 | ||
| … | … | ||
| 218 | 218 | } | |
| 219 | 219 | ||
| 220 | 220 | private InternetAddress getFromAddress() { | |
| 221 | ServerConfigManagerLocal configMan = | ||
| 222 | ServerConfigManagerEJBImpl.getOne(); | ||
| 221 | 223 | try { | |
| 222 | if (this.configMan == null) { | ||
| 223 | this.configMan = ServerConfigManagerEJBImpl.getOne(); | ||
| 224 | } | ||
| 225 | |||
| 226 | Properties props = this.configMan.getConfig(); | ||
| 224 | Properties props = configMan.getConfig(); | ||
| 227 | 225 | String from = props.getProperty(HQConstants.EmailSender); | |
| 228 | 226 | if (from != null) { | |
| 229 | 227 | return new InternetAddress(from); | |
| 230 | 228 | } | |
| 231 | 229 | } catch (ConfigPropertyException e) { | |
| 232 | log.error("ConfigPropertyException fetch FROM address", e); | ||
| 230 | _log.error("ConfigPropertyException fetch FROM address", e); | ||
| 233 | 231 | } catch (AddressException e) { | |
| 234 | log.error("Bad FROM address", e); | ||
| 232 | _log.error("Bad FROM address", e); | ||
| 235 | 233 | } | |
| 236 | 234 | return null; | |
| 237 | 235 | } | |
| … | … | ||
| 259 | 259 | ||
| 260 | 260 | m.setSubject(subject); | |
| 261 | 261 | ||
| 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); | ||
| 265 | 265 | } | |
| 266 | 266 | ||
| 267 | 267 | // Send to each recipient individually (for D.B. SMS) | |
| … | … | ||
| 278 | 278 | Transport.send(m); | |
| 279 | 279 | } | |
| 280 | 280 | } 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); | ||
| 283 | 283 | } | |
| 284 | 284 | } | |
| 285 | 285 | ||
| 286 | 286 | void sendFiltered(int platId) { | |
| 287 | 287 | Hashtable cache; | |
| 288 | 288 | ||
| 289 | synchronized (alertBuffer) { | ||
| 290 | if (!alertBuffer.containsKey(platId)) | ||
| 289 | synchronized (_alertBuffer) { | ||
| 290 | if (!_alertBuffer.containsKey(platId)) | ||
| 291 | 291 | return; | |
| 292 | 292 | ||
| 293 | cache = (Hashtable) alertBuffer.remove(platId); | ||
| 293 | cache = (Hashtable) _alertBuffer.remove(platId); | ||
| 294 | 294 | ||
| 295 | 295 | if (cache == null || cache.size() == 0) | |
| 296 | 296 | return; | |
| 297 | 297 | ||
| 298 | 298 | // Insert key again so that we continue filtering | |
| 299 | alertBuffer.put(platId, null); | ||
| 299 | _alertBuffer.put(platId, null); | ||
| 300 | 300 | } | |
| 301 | 301 | ||
| 302 | 302 | AppdefEntityID platEntId = AppdefEntityID.newPlatformID(platId); | |
| … | … | ||
| 323 | 323 | ||
| 324 | 324 | private void scheduleJob(Integer platId) throws SchedulerException { | |
| 325 | 325 | // 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 | } | ||
| 345 | 356 | } | |
| 346 | |||
| 347 | scheduler.scheduleJob(jd, t); | ||
| 348 | 357 | } | |
| 349 | 358 | } |
Comments
Add your comment
Please log in to comment



Add a new comment:
Login or create an account to post a comment