| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
package net.admin4j.ui.filters; |
| 15 | |
|
| 16 | |
import java.io.IOException; |
| 17 | |
import java.util.ArrayList; |
| 18 | |
import java.util.List; |
| 19 | |
import java.util.StringTokenizer; |
| 20 | |
|
| 21 | |
import javax.servlet.FilterChain; |
| 22 | |
import javax.servlet.FilterConfig; |
| 23 | |
import javax.servlet.ServletException; |
| 24 | |
import javax.servlet.ServletRequest; |
| 25 | |
import javax.servlet.ServletResponse; |
| 26 | |
|
| 27 | |
import net.admin4j.config.Admin4JConfiguration; |
| 28 | |
import net.admin4j.monitor.ConcurrentUsageDetector; |
| 29 | |
import net.admin4j.util.Daemon; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | 9 | public class ConcurrentUsageFilter extends BaseNotificationFilter { |
| 41 | |
|
| 42 | |
@SuppressWarnings("unused") |
| 43 | 9 | private Daemon concurrentUsageDaemon = null; |
| 44 | |
private ConcurrentUsageDetector concurrentUsageDetector; |
| 45 | |
|
| 46 | |
public void destroy() { |
| 47 | |
|
| 48 | |
|
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public void doFilter(ServletRequest request, ServletResponse response, |
| 52 | |
FilterChain chain) throws IOException, ServletException { |
| 53 | 6 | concurrentUsageDetector.increment(); |
| 54 | 6 | try {chain.doFilter(request, response);} |
| 55 | |
finally { |
| 56 | 6 | concurrentUsageDetector.decrement(); |
| 57 | 6 | } |
| 58 | |
|
| 59 | 6 | } |
| 60 | |
|
| 61 | |
public void init(FilterConfig config) throws ServletException { |
| 62 | 6 | super.init(config); |
| 63 | |
|
| 64 | 6 | long sleepIntervalMillis = ConcurrentUsageDetector.DEFAULT_SLEEP_INTERVAL; |
| 65 | 6 | Integer[] thresholdLevels = ConcurrentUsageDetector.DEFAULT_ALERT_LEVELS; |
| 66 | |
|
| 67 | 6 | String sleepIntervalMillisStr = config.getInitParameter("sleep.interval.millis"); |
| 68 | 6 | if (sleepIntervalMillisStr != null) { |
| 69 | |
try { |
| 70 | 3 | sleepIntervalMillis = Long.parseLong(sleepIntervalMillisStr); |
| 71 | 0 | } catch (Exception e) { |
| 72 | 0 | sleepIntervalMillis = ConcurrentUsageDetector.DEFAULT_SLEEP_INTERVAL; |
| 73 | 0 | this.logger.error("Invalid sleep.interval.millis parameter for ConcurrentUsageFilter. Default used. sleep.interval.millis="+ sleepIntervalMillisStr, |
| 74 | |
e); |
| 75 | 3 | } |
| 76 | |
} |
| 77 | 3 | else if ( Admin4JConfiguration.getConcurrentUsageSleepIntervalMillis() != null ) { |
| 78 | 0 | sleepIntervalMillis = Admin4JConfiguration.getConcurrentUsageSleepIntervalMillis(); |
| 79 | |
} |
| 80 | |
|
| 81 | 6 | String alertLevelsStr = config.getInitParameter("alert.levels"); |
| 82 | 6 | if (alertLevelsStr != null) { |
| 83 | |
try { |
| 84 | 3 | StringTokenizer token = new StringTokenizer(alertLevelsStr, ","); |
| 85 | 3 | List<Integer> list = new ArrayList<Integer>(); |
| 86 | 6 | while (token.hasMoreTokens()) { |
| 87 | 3 | list.add(Integer.valueOf(token.nextToken().trim())); |
| 88 | |
} |
| 89 | 3 | thresholdLevels = list.toArray(new Integer[0]); |
| 90 | |
} |
| 91 | 0 | catch (Exception e) { |
| 92 | 0 | thresholdLevels = ConcurrentUsageDetector.DEFAULT_ALERT_LEVELS; |
| 93 | 0 | this.logger.error("Invalid alert.levels parameter for ConcurrentUsageFilter. Default used. alert.levels="+ alertLevelsStr, |
| 94 | |
e); |
| 95 | 3 | } |
| 96 | |
|
| 97 | |
} |
| 98 | 3 | else if ( Admin4JConfiguration.getConcurrentUsageAlertLevels() != null ) { |
| 99 | 0 | thresholdLevels = Admin4JConfiguration.getConcurrentUsageAlertLevels(); |
| 100 | |
} |
| 101 | |
|
| 102 | 6 | concurrentUsageDetector = new ConcurrentUsageDetector(this.notifier, thresholdLevels); |
| 103 | 6 | concurrentUsageDaemon = new Daemon(concurrentUsageDetector, "Admin4j Concurrent Usage Detector", sleepIntervalMillis); |
| 104 | |
|
| 105 | 6 | } |
| 106 | |
|
| 107 | |
} |