visan / javamelody

Automatically exported from code.google.com/p/javamelody
0 stars 0 forks source link

Externalized Configuration #463

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Java Melody reads configuration from 3 sources, system properties, context init 
params and filter init params, none of them allows webapp to configure 
monitoring based on some external configuration during context initialization. 
Although system properties can be used for this purpose, they are shared by all 
webapps in JVM, which can result in conflict.

An easy way to implement this feature is to read configuration from servlet 
context attributes as well. 

Here is patch implementing the feature:

diff --git a/src/main/java/net/bull/javamelody/Parameters.java 
b/src/main/java/net/bull/javamelody/Parameters.java
index a9a0a4d..c80a26f 100644
--- a/src/main/java/net/bull/javamelody/Parameters.java
+++ b/src/main/java/net/bull/javamelody/Parameters.java
@@ -447,6 +447,12 @@
                return result;
            }
        }
+       if (servletContext != null) {
+           Object attribute = servletContext.getAttribute(globalName);
+           if (attribute instanceof String) {
+               return (String) attribute;
+           }
+       }
        if (filterConfig != null) {
            result = filterConfig.getInitParameter(parameterName);
            if (result != null) {

Original issue reported on code.google.com by michal.b...@gmail.com on 5 Mar 2015 at 1:44

GoogleCodeExporter commented 9 years ago
I am not against this change, but I have 3 questions.

As far as I know, the only way to set a servletContext attribute is to call the 
servletContext.setAttribute(name, value) method, isn't it?
For example, servletContext.setAttribute("javamelody.log", "true")

So, to externalize configuration, you still have to read the names and values 
of parameters from some resource or file, and to call 
servletContext.setAttribute for each one?

And, is there a reason to not use servletContext.setInitParameter(name, value) 
instead?

Original comment by evernat@free.fr on 6 Mar 2015 at 9:41

GoogleCodeExporter commented 9 years ago
Yes, that's exactly what I'm doing during context initalization, scan 
configuration params and copy those with "javamelody." to javamelody 
configuration. I'm adding javamelody to existing project, configuration loading 
is already implemented.

My project is build upon gwt, which is still using Servlet 2.5 API 
specification, therefore ServletContext.setInitParameter is not available, 
because it was added only in Servlet 3.0 API specification. 

Original comment by michal.b...@gmail.com on 9 Mar 2015 at 12:50

GoogleCodeExporter commented 9 years ago
ok, this is committed in trunk (revision 4043) and ready for the next release 
(1.56).

Original comment by evernat@free.fr on 13 Mar 2015 at 6:49