Revert "Revert "Javamelody: Enable monitoring of SQL activity""

This reverts commit 4744bfba055622e7ecebfcbfca600c5cb4f5329f.

Change-Id: Ieb970eb09a306b6b67533c1cff4ae44b0bbcdeb8
diff --git a/BUCK b/BUCK
index cd52cd5..d636c32 100644
--- a/BUCK
+++ b/BUCK
@@ -1,15 +1,45 @@
+INTERCEPTOR = ['src/main/java/com/googlesource/gerrit/plugins/javamelody/MonitoringDataSourceInterceptor.java']
+SRCS = glob(['src/main/java/**/*.java'], excludes = INTERCEPTOR)
+RSRC = glob(['src/main/resources/**/*'])
+DEPS = [
+  '//plugins/javamelody/lib:javamelody',
+  '//plugins/javamelody/lib:jrobin',
+]
+
+manifest_entries = [
+  'Gerrit-PluginName: javamelody',
+  'Gerrit-Module: com.googlesource.gerrit.plugins.javamelody.Module',
+  'Gerrit-HttpModule: com.googlesource.gerrit.plugins.javamelody.HttpModule',
+]
+
 gerrit_plugin(
   name = 'javamelody',
-  srcs = glob(['src/main/java/**/*.java']),
-  resources = glob(['src/main/resources/**/*']),
-  manifest_entries = [
-    'Gerrit-PluginName: javamelody',
-    'Gerrit-Module: com.googlesource.gerrit.plugins.javamelody.Module',
-    'Gerrit-HttpModule: com.googlesource.gerrit.plugins.javamelody.HttpModule',
-  ],
-  deps = [
-    '//plugins/javamelody/lib:javamelody',
-    '//plugins/javamelody/lib:jrobin',
-  ],
+  srcs = SRCS,
+  resources = RSRC,
+  manifest_entries = manifest_entries,
+  deps = DEPS,
 )
 
+gerrit_plugin(
+  name = 'javamelody-nodep',
+  srcs = SRCS,
+  resources = RSRC,
+  manifest_entries = manifest_entries,
+  compile_deps = DEPS,
+)
+
+java_binary(
+  name = 'javamelody-deps',
+  deps = DEPS,
+)
+
+java_binary(
+  name = 'javamelody-datasource-interceptor',
+  deps = [':javamelody-datasource-interceptor-lib'],
+)
+
+java_library2(
+  name = 'javamelody-datasource-interceptor-lib',
+  srcs = INTERCEPTOR,
+  compile_deps = DEPS + ['//gerrit-extension-api:lib'],
+)
\ No newline at end of file
diff --git a/src/main/java/com/googlesource/gerrit/plugins/javamelody/MonitoringDataSourceInterceptor.java b/src/main/java/com/googlesource/gerrit/plugins/javamelody/MonitoringDataSourceInterceptor.java
new file mode 100644
index 0000000..3ea3a98
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/javamelody/MonitoringDataSourceInterceptor.java
@@ -0,0 +1,29 @@
+// Copyright (C) 2014 The Android Open Source Project
+//
+// Licensed 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 com.googlesource.gerrit.plugins.javamelody;
+
+import javax.sql.DataSource;
+
+import net.bull.javamelody.JdbcWrapper;
+
+import com.google.gerrit.extensions.persistence.DataSourceInterceptor;
+
+public class MonitoringDataSourceInterceptor implements DataSourceInterceptor {
+
+  @Override
+  public DataSource intercept(String name, DataSource dataSource) {
+    return JdbcWrapper.SINGLETON.createDataSourceProxy(name, dataSource);
+  }
+}
diff --git a/src/main/resources/Documentation/build.md b/src/main/resources/Documentation/build.md
index 9e3e4f3..b4a8dd6 100644
--- a/src/main/resources/Documentation/build.md
+++ b/src/main/resources/Documentation/build.md
@@ -9,12 +9,29 @@
   buck build plugins/javamelody:javamelody
 ```
 
-The output is created in
+If [database interception](database-monitoring.html) should be activated,
+then the following targets must be used instead:
+
+```
+  buck build plugins/javamelody:javamelody-nodep
+  buck build plugins/javamelody:javamelody-deps
+  buck build plugins/javamelody:javamelody-datasource-interceptor
+```
+
+The output from the former target is:
 
 ```
   buck-out/gen/plugins/javamelody/javamelody.jar
 ```
 
+The output from the later targets are:
+
+```
+  buck-out/gen/plugins/javamelody/javamelody-nodep.jar
+  buck-out/gen/plugins/javamelody/javamelody-deps.jar
+  buck-out/gen/plugins/javamelody/javamelody-datasource-interceptor.jar
+```
+
 This project can be imported into the Eclipse IDE:
 
 ```
diff --git a/src/main/resources/Documentation/database-monitoring.md b/src/main/resources/Documentation/database-monitoring.md
index 09ec050..943d791 100644
--- a/src/main/resources/Documentation/database-monitoring.md
+++ b/src/main/resources/Documentation/database-monitoring.md
@@ -1,68 +1,60 @@
 Database-Monitoring
 ===================
 
-This feature is experimental and is not supported out of the box for Gerrit
-monitoring on Jetty.
-
-Background: JavaMelody only supports JNDI DataSource in Web container (Tomcat).
+JavaMelody supports out of the box JNDI DataSource in Web container (Tomcat).
 Because Gerrit instantiates its data source on its own, JavaMelody can not
 intercept it and therefore no SQL statistic reports can be gathered.
 
-To overcome that problem a data source proxy must be installed. Because the
-instantiation of the data source takes place in Gerrit core, it must be patched.
+To overcome that problem a data source proxy must be installed.
 
-Next problem: JavaMelody JARs (that creates the data source proxies) must be
-available in the bootstrap classpath for Gerrit core to load it.
+Datasource interceptor JAR (that creates the data source proxy) must be
+available in the bootstrap classpath for Gerrit core to load it. Moreover,
+because the interceptor depends on javamelody core library, it must be
+provided in the bootstrap classpath too. In this case the plugin must
+not contain the javamelody core library (shaded jar).
 
-Last problem: we shouldn't package the plugin dependencies inside the
-plugin itself (shaded jar in Maven jargon) and in $gerrit_site/lib. We would
-need to strip it from the plugin.
+Thus the javamelody dependencies must not be packaged in the plugin itself.
 
-Cherry-pick this change and compile Gerrit:
-
-https://gerrit-review.googlesource.com/#/c/52747
-
-Add the following target to BUCK in javamelody plugin:
+Add the following line to `$gerrit_site/etc/gerrit.config` under `database` section:
 
 ```
-gerrit_plugin(
-  name = 'javamelody-no-deps',
-  srcs = glob(['src/main/java/**/*.java']),
-  resources = glob(['src/main/resources/**/*']),
-  manifest_entries = [
-    'Gerrit-PluginName: javamelody',
-    'Gerrit-Module: com.googlesource.gerrit.plugins.javamelody.Module',
-    'Gerrit-HttpModule: com.googlesource.gerrit.plugins.javamelody.HttpModule',
-  ],
-  compile_deps = [
-    '//plugins/javamelody/lib:javamelody',
-    '//plugins/javamelody/lib:jrobin',
-  ],
-)
+dataSourceInterceptorClass = com.googlesource.gerrit.plugins.javamelody.MonitoringDataSourceInterceptor
 ```
 
 Compile the plugin without dependencies:
 
 ```
-buck build plugins/javamelody:javamelody-no-deps
+buck build plugins/javamelody:javamelody-nodep
 ```
 
-Copy the dependencies manually to $gerrit_site/libs:
+Compile the plugin dependencies:
 
 ```
-cp
-~/.gerritcodereview/buck-cache/jrobin-1.5.9.jar-bd9a84484c67de930fa841f23cd6a93108b05cd0
-$gerrit_site/lib/jrobin.jar
-cp
-~/.gerritcodereview/buck-cache/javamelody-core-1.48.0.jar-4c573306061019430a735d9d58f93639f4a0ff0b
-$gerrit_site/lib/javamelody.jar
-
+buck build plugins/javamelody:javamelody-deps
 ```
 
-Deploy plugin without dependencies:
+Compile datasource interceptor:
 
 ```
-cp buck-out/gen/plugins/javamelody/javamelody-no-deps.jar $gerrit_site/plugins
+buck build plugins/javamelody:javamelody-datasource-interceptor
+```
+
+Deploy the datasource-interceptor to `$gerrit_site/libs`:
+
+```
+cp buck-out/gen/plugins/javamelody/javamelody-datasource-interceptor.jar `$gerrit_site/libs`
+```
+
+Deploy the javamelody dependencies to `$gerrit_site/libs`:
+
+```
+cp buck-out/gen/plugins/javamelody/javamelody-deps.jar `$gerrit_site/libs`
+```
+
+Deploy the plugin without dependencies:
+
+```
+cp buck-out/gen/plugins/javamelody/javamelody-nodep.jar `$gerrit_site/plugins`
 ```
 
 Run Gerrit@Jetty and enjoy SQL statistics, a lá: