commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1532257 - in /commons/sandbox/monitoring/trunk: src/site/markdown/ web/src/main/java/org/apache/commons/monitoring/web/registration/ web/src/main/java/org/apache/commons/monitoring/web/session/ web/src/main/resources/META-INF/ web/src/main...
Date Tue, 15 Oct 2013 09:15:57 GMT
Author: rmannibucau
Date: Tue Oct 15 09:15:56 2013
New Revision: 1532257

URL: http://svn.apache.org/r1532257
Log:
making web monitoring automatic in a servlet 3 container (but still configurable) + fixing
role name of session monitoring (to distinguish between apps

Added:
    commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/registration/
    commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/registration/WebMonitoringInitializer.java
    commons/sandbox/monitoring/trunk/web/src/main/resources/META-INF/
    commons/sandbox/monitoring/trunk/web/src/main/resources/META-INF/services/
    commons/sandbox/monitoring/trunk/web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
Modified:
    commons/sandbox/monitoring/trunk/src/site/markdown/jta.md
    commons/sandbox/monitoring/trunk/src/site/markdown/web.md
    commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/session/MonitoringSessionListener.java

Modified: commons/sandbox/monitoring/trunk/src/site/markdown/jta.md
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/site/markdown/jta.md?rev=1532257&r1=1532256&r2=1532257&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/site/markdown/jta.md (original)
+++ commons/sandbox/monitoring/trunk/src/site/markdown/jta.md Tue Oct 15 09:15:56 2013
@@ -35,6 +35,7 @@ to add `commons-monitoring-web` to your 
     </web-app>
 
 Note: you can register it manually using `org.apache.commons.monitoring.gauges.Gauge$LoaderHelper`
or `org.apache.commons.monitoring.repositories.Repository#addGauge`.
+Note 2: in a servlet 3 container it is done automatically if `org.apache.commons.monitoring.web.activated`
is true (by default)
 
 Then you need to add on the beans which can be enrolled in transactions you want to monitor
the annotation
 `org.apache.commons.monitoring.jta.JTAMonitored` (CDI beans) or the interceptor `org.apache.commons.monitoring.jta.JTAInterceptor`

Modified: commons/sandbox/monitoring/trunk/src/site/markdown/web.md
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/src/site/markdown/web.md?rev=1532257&r1=1532256&r2=1532257&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/src/site/markdown/web.md (original)
+++ commons/sandbox/monitoring/trunk/src/site/markdown/web.md Tue Oct 15 09:15:56 2013
@@ -37,14 +37,19 @@ Simply add the filter `org.apache.common
         <url-pattern>/*</url-pattern>
     </filter-mapping>
 
+Note: in a servlet 3 container you can simply configure `org.apache.commons.monitoring.web.monitored-urls`
to the
+servlet pattern you want to match. If you want to register the `MonitoringFilter` yourself
just set the
+init parameter `org.apache.commons.monitoring.web.activated` to false.
+
 ## Monitor sessions
 
 Simply add the listener `org.apache.commons.monitoring.web.servlet.MonitoringFilter`:
 
     <listener>
-      <listener-class>org.apache.commons.monitoring.web.servlet.MonitoringFilter</listener-class>
+      <listener-class>org.apache.commons.monitoring.web.session.MonitoringSessionListener</listener-class>
     </listener>
 
+Note: in a servlet 3 container and if `org.apache.commons.monitoring.web.activated` is not
set to false it is added by default.
 
 ## Accessing results
 

Added: commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/registration/WebMonitoringInitializer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/registration/WebMonitoringInitializer.java?rev=1532257&view=auto
==============================================================================
--- commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/registration/WebMonitoringInitializer.java
(added)
+++ commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/registration/WebMonitoringInitializer.java
Tue Oct 15 09:15:56 2013
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.monitoring.web.registration;
+
+import org.apache.commons.monitoring.configuration.Configuration;
+import org.apache.commons.monitoring.web.discovery.GaugeDiscoveryListener;
+import org.apache.commons.monitoring.web.servlet.MonitoringFilter;
+import org.apache.commons.monitoring.web.session.MonitoringSessionListener;
+
+import javax.servlet.DispatcherType;
+import javax.servlet.ServletContainerInitializer;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import java.util.EnumSet;
+import java.util.Set;
+
+public class WebMonitoringInitializer implements ServletContainerInitializer {
+    @Override
+    public void onStartup(final Set<Class<?>> classes, final ServletContext ctx)
throws ServletException {
+        final String activated = ctx.getInitParameter(Configuration.COMMONS_MONITORING_PREFIX
+ "web.activated");
+        if ("false".equalsIgnoreCase(activated)) {
+            return;
+        }
+
+        ctx.addListener(GaugeDiscoveryListener.class);
+        ctx.addListener(MonitoringSessionListener.class);
+
+        String monitoredUrls = ctx.getInitParameter(Configuration.COMMONS_MONITORING_PREFIX
+ "web.monitored-urls");
+        if (monitoredUrls == null) {
+            monitoredUrls = "/*";
+        }
+        ctx.addFilter("monitoring-filter", MonitoringFilter.class).addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class),
false, monitoredUrls);
+    }
+}

Modified: commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/session/MonitoringSessionListener.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/session/MonitoringSessionListener.java?rev=1532257&r1=1532256&r2=1532257&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/session/MonitoringSessionListener.java
(original)
+++ commons/sandbox/monitoring/trunk/web/src/main/java/org/apache/commons/monitoring/web/session/MonitoringSessionListener.java
Tue Oct 15 09:15:56 2013
@@ -22,19 +22,17 @@ import org.apache.commons.monitoring.cou
 import org.apache.commons.monitoring.repositories.Repository;
 import org.apache.commons.monitoring.stopwatches.StopWatch;
 
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
 import javax.servlet.http.HttpSessionEvent;
 import javax.servlet.http.HttpSessionListener;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
-public class MonitoringSessionListener implements HttpSessionListener {
+public class MonitoringSessionListener implements HttpSessionListener, ServletContextListener
{
     private final Map<String, StopWatch> watches = new ConcurrentHashMap<String,
StopWatch>();
 
-    private final Counter counter;
-
-    public MonitoringSessionListener() {
-        counter = Repository.INSTANCE.getCounter(new Counter.Key(new Role("session", Unit.UNARY),
"session"));
-    }
+    private Counter counter;
 
     @Override
     public void sessionCreated(final HttpSessionEvent httpSessionEvent) {
@@ -49,4 +47,18 @@ public class MonitoringSessionListener i
             watch.stop();
         }
     }
+
+    @Override
+    public void contextInitialized(final ServletContextEvent sce) {
+        String contextPath = sce.getServletContext().getContextPath();
+        if (contextPath == null || contextPath.isEmpty()) {
+            contextPath = "/";
+        }
+        counter = Repository.INSTANCE.getCounter(new Counter.Key(new Role("sessions-" + contextPath,
Unit.UNARY), "session"));
+    }
+
+    @Override
+    public void contextDestroyed(final ServletContextEvent sce) {
+        // no-op
+    }
 }

Added: commons/sandbox/monitoring/trunk/web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer?rev=1532257&view=auto
==============================================================================
--- commons/sandbox/monitoring/trunk/web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
(added)
+++ commons/sandbox/monitoring/trunk/web/src/main/resources/META-INF/services/javax.servlet.ServletContainerInitializer
Tue Oct 15 09:15:56 2013
@@ -0,0 +1 @@
+org.apache.commons.monitoring.web.registration.WebMonitoringInitializer



Mime
View raw message