You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rm...@apache.org on 2013/07/29 08:20:58 UTC
svn commit: r1507950 [3/8] - in /commons/sandbox/monitoring/trunk: ./ aop/
aop/src/main/java/org/apache/commons/monitoring/aop/
aop/src/main/java/org/apache/commons/monitoring/instrumentation/
aop/src/main/resources/ aop/src/test/java/org/ aop/src/test...
Modified: commons/sandbox/monitoring/trunk/core/src/test/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatchTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/test/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatchTest.java?rev=1507950&r1=1507949&r2=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/core/src/test/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatchTest.java (original)
+++ commons/sandbox/monitoring/trunk/core/src/test/java/org/apache/commons/monitoring/stopwatches/DefaultStopWatchTest.java Mon Jul 29 06:20:54 2013
@@ -17,18 +17,28 @@
package org.apache.commons.monitoring.stopwatches;
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertTrue;
-
-import org.apache.commons.monitoring.StopWatch;
-import org.apache.commons.monitoring.monitors.NullMonitor;
+import org.apache.commons.monitoring.Role;
+import org.apache.commons.monitoring.counter.Counter;
+import org.apache.commons.monitoring.counter.Unit;
+import org.apache.commons.monitoring.monitors.Monitor;
+import org.apache.commons.monitoring.Visitor;
+import org.apache.commons.monitoring.util.ClassLoaders;
import org.junit.Test;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
/**
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
-public class DefaultStopWatchTest
-{
+public class DefaultStopWatchTest {
private long time;
/**
@@ -39,23 +49,22 @@ public class DefaultStopWatchTest
*/
@Test
public void computeTime()
- throws Exception
- {
+ throws Exception {
time = 0;
StopWatch stopWatch = new MockTimeWatch();
time++;
stopWatch.pause();
- assertTrue( stopWatch.isPaused() );
- System.out.println( stopWatch.toString() );
+ assertTrue(stopWatch.isPaused());
+ System.out.println(stopWatch.toString());
time++;
stopWatch.resume();
- assertTrue( !stopWatch.isPaused() );
- System.out.println( stopWatch.toString() );
+ assertTrue(!stopWatch.isPaused());
+ System.out.println(stopWatch.toString());
time++;
stopWatch.stop();
- assertEquals( 2, stopWatch.getElapsedTime() );
- assertTrue( stopWatch.isStoped() );
- System.out.println( stopWatch.toString() );
+ assertEquals(2, stopWatch.getElapsedTime());
+ assertTrue(stopWatch.isStoped());
+ System.out.println(stopWatch.toString());
}
/**
@@ -66,55 +75,123 @@ public class DefaultStopWatchTest
*/
@Test
public void supportUnexpectedCalls()
- throws Exception
- {
+ throws Exception {
time = 0;
StopWatch stopWatch = new MockTimeWatch();
// resume the non-paused watch
- assertTrue( !stopWatch.isPaused() );
+ assertTrue(!stopWatch.isPaused());
stopWatch.resume();
- assertTrue( !stopWatch.isPaused() );
+ assertTrue(!stopWatch.isPaused());
// pause the watch multiple times
time++;
stopWatch.pause();
- assertEquals( 1, stopWatch.getElapsedTime() );
- assertTrue( stopWatch.isPaused() );
+ assertEquals(1, stopWatch.getElapsedTime());
+ assertTrue(stopWatch.isPaused());
time++;
stopWatch.pause();
- assertEquals( 1, stopWatch.getElapsedTime() );
- assertTrue( stopWatch.isPaused() );
+ assertEquals(1, stopWatch.getElapsedTime());
+ assertTrue(stopWatch.isPaused());
stopWatch.stop();
- assertEquals( 1, stopWatch.getElapsedTime() );
- assertTrue( stopWatch.isStoped() );
+ assertEquals(1, stopWatch.getElapsedTime());
+ assertTrue(stopWatch.isStoped());
// Unexpected use after stopped
stopWatch.resume();
- assertEquals( 1, stopWatch.getElapsedTime() );
- assertTrue( stopWatch.isStoped() );
+ assertEquals(1, stopWatch.getElapsedTime());
+ assertTrue(stopWatch.isStoped());
stopWatch.pause();
- assertEquals( 1, stopWatch.getElapsedTime() );
- assertTrue( stopWatch.isStoped() );
+ assertEquals(1, stopWatch.getElapsedTime());
+ assertTrue(stopWatch.isStoped());
stopWatch.stop();
- assertEquals( 1, stopWatch.getElapsedTime() );
- assertTrue( stopWatch.isStoped() );
+ assertEquals(1, stopWatch.getElapsedTime());
+ assertTrue(stopWatch.isStoped());
}
- private class MockTimeWatch
- extends DefaultStopWatch
- {
- public MockTimeWatch()
- {
- super( new NullMonitor() );
+ private class MockTimeWatch extends CounterStopWatch {
+ public MockTimeWatch() {
+ super(new NullMonitor());
}
@Override
- protected long nanotime()
- {
+ protected long nanotime() {
return time;
}
}
+
+ public static class NullMonitor implements Monitor {
+ private static final Unit NULL = new Unit("null") {
+ @Override
+ public boolean isCompatible(Unit unit) {
+ return true;
+ }
+ };
+
+ private static final Role NOP_COUNTER = new Role("NopCounter", NULL);
+
+ private static final Role NOP_GAUGE = new Role("NopGauge", NULL);
+
+ private static final Counter counter = Counter.class.cast(Proxy.newProxyInstance(ClassLoaders.current(), new Class<?>[] { Counter.class }, new InvocationHandler() {
+ @Override
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+ return null;
+ }
+ }));
+
+ private Collection<Counter> counters = Arrays.asList(counter);
+
+ public Counter getCounter(String role) {
+ return counter;
+ }
+
+ public Counter getCounter(Role role) {
+ return counter;
+ }
+
+ public Key getKey() {
+ return new Key("noOp", null);
+ }
+
+ public Counter getMetric(String role) {
+ return counter;
+ }
+
+ public Counter getMetric(Role role) {
+ return counter;
+ }
+
+ public Collection<Counter> getCounters() {
+ return counters;
+ }
+
+ public Collection<Role> getRoles() {
+ return Arrays.asList(NOP_COUNTER, NOP_GAUGE);
+ }
+
+ public void reset() {
+ // NoOp
+ }
+
+ @Override
+ public AtomicInteger currentConcurrency() {
+ return new AtomicInteger();
+ }
+
+ @Override
+ public void updateConcurrency(int concurrency) {
+ // no-op
+ }
+
+ @Override
+ public int getMaxConcurrency() {
+ return 0;
+ }
+
+ public void accept(Visitor visitor) {
+ // NoOp
+ }
+ }
}
Added: commons/sandbox/monitoring/trunk/cxf/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cxf/pom.xml?rev=1507950&view=auto
==============================================================================
--- commons/sandbox/monitoring/trunk/cxf/pom.xml (added)
+++ commons/sandbox/monitoring/trunk/cxf/pom.xml Mon Jul 29 06:20:54 2013
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>commons-monitoring-parent</artifactId>
+ <groupId>org.apache.commons.monitoring</groupId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>commons-monitoring-cxf</artifactId>
+ <name>Commons Monitoring (Sandbox) :: CXF Integration</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.commons.monitoring</groupId>
+ <artifactId>commons-monitoring-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-transports-http</artifactId>
+ <version>${cxf.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.cxf</groupId>
+ <artifactId>cxf-rt-bindings-soap</artifactId>
+ <version>${cxf.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-servlet_2.5_spec</artifactId>
+ </dependency>
+ </dependencies>
+
+ <properties>
+ <cxf.version>2.6.9</cxf.version>
+ </properties>
+</project>
Copied: commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringFeature.java (from r1506987, commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/HttpMonitoringFeature.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringFeature.java?p2=commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringFeature.java&p1=commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/HttpMonitoringFeature.java&r1=1506987&r2=1507950&rev=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/HttpMonitoringFeature.java (original)
+++ commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringFeature.java Mon Jul 29 06:20:54 2013
@@ -15,19 +15,17 @@
* limitations under the License.
*/
-package org.apache.commons.monitoring.instrumentation.cxf;
+package org.apache.commons.monitoring.cxf;
/**
* Variant of MonitoringFeature to enable monitoring of CXF web services EndPoints when deployed as HttpServlet
- *
+ *
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
public class HttpMonitoringFeature
- extends MonitoringFeature
-{
+ extends MonitoringFeature {
@Override
- protected MonitoringInInterceptor getMonitoringInInterceptor()
- {
+ protected MonitoringInInterceptor getMonitoringInInterceptor() {
return new HttpMonitoringInInterceptor();
}
}
Copied: commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringInInterceptor.java (from r1506987, commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/HttpMonitoringInInterceptor.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringInInterceptor.java?p2=commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringInInterceptor.java&p1=commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/HttpMonitoringInInterceptor.java&r1=1506987&r2=1507950&rev=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/HttpMonitoringInInterceptor.java (original)
+++ commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/HttpMonitoringInInterceptor.java Mon Jul 29 06:20:54 2013
@@ -15,48 +15,37 @@
* limitations under the License.
*/
-package org.apache.commons.monitoring.instrumentation.cxf;
-
-import javax.servlet.http.HttpServletRequest;
+package org.apache.commons.monitoring.cxf;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
+import javax.servlet.http.HttpServletRequest;
+
/**
* A variant of MonitoringInInterceptor dedicated to web service endpoint based on HttpServlet - most commonly used, but
* not required.
- * <p>
+ * <p/>
* When no SOAPAction Header is set, the monitor name is extracted from servlet PathInfo
- *
+ *
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
-public class HttpMonitoringInInterceptor
- extends MonitoringInInterceptor
-{
- public HttpMonitoringInInterceptor()
- {
+public class HttpMonitoringInInterceptor extends MonitoringInInterceptor {
+ public HttpMonitoringInInterceptor() {
super();
}
- public HttpMonitoringInInterceptor( String phase )
- {
- super( phase );
+ public HttpMonitoringInInterceptor(final String phase) {
+ super(phase);
}
- /**
- * {@inheritDoc}
- *
- * @see org.apache.commons.monitoring.instrumentation.cxf.MonitoringInInterceptor#getMonitorName(org.apache.cxf.binding.soap.SoapMessage)
- */
@Override
- protected String getMonitorName( SoapMessage message )
- {
- String soapAction = getSoapAction( message );
- if ( soapAction != null )
- {
+ protected String getMonitorName(SoapMessage message) {
+ String soapAction = getSoapAction(message);
+ if (soapAction != null) {
return soapAction;
}
- HttpServletRequest request = (HttpServletRequest) message.get( AbstractHTTPDestination.HTTP_REQUEST );
+ HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
return request.getPathInfo();
}
}
Copied: commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringFeature.java (from r1506987, commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringFeature.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringFeature.java?p2=commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringFeature.java&p1=commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringFeature.java&r1=1506987&r2=1507950&rev=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringFeature.java (original)
+++ commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringFeature.java Mon Jul 29 06:20:54 2013
@@ -15,66 +15,44 @@
* limitations under the License.
*/
-package org.apache.commons.monitoring.instrumentation.cxf;
+package org.apache.commons.monitoring.cxf;
-import org.apache.commons.monitoring.Repository;
import org.apache.cxf.Bus;
import org.apache.cxf.feature.AbstractFeature;
import org.apache.cxf.interceptor.InterceptorProvider;
/**
* CXF feature to enable web service monitoring.
- *
+ * <p/>
* <pre>
* <jaxws:endpoint implementor="#myServiceBean" address="/myService" wsdlLocation="wsdl/myService.wsdl">
* <jaxws:features>
- * <bean class="org.apache.commons.monitoring.instrumentation.cxf.MonitoringFeature" />
+ * <bean class="org.apache.commons.monitoring.cxf.MonitoringFeature" />
* </jaxws:features>
* </jaxws:endpoint>
* </pre>
- *
+ *
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
-public class MonitoringFeature
- extends AbstractFeature
-{
- private Repository repository;
-
+public class MonitoringFeature extends AbstractFeature {
private String category = "soap";
- private String domain;
-
@Override
- protected void initializeProvider( InterceptorProvider provider, Bus bus )
- {
- MonitoringInInterceptor in = getMonitoringInInterceptor();
- in.setRepository( repository );
- in.setCategory( category );
- in.setDomain( domain );
- MonitoringOutInterceptor out = new MonitoringOutInterceptor();
- provider.getInInterceptors().add( in );
- provider.getInFaultInterceptors().add( in );
- provider.getOutInterceptors().add( out );
- provider.getOutFaultInterceptors().add( out );
+ protected void initializeProvider(InterceptorProvider provider, Bus bus) {
+ final MonitoringInInterceptor in = getMonitoringInInterceptor();
+ in.setCategory(category);
+ final MonitoringOutInterceptor out = new MonitoringOutInterceptor();
+ provider.getInInterceptors().add(in);
+ provider.getInFaultInterceptors().add(in);
+ provider.getOutInterceptors().add(out);
+ provider.getOutFaultInterceptors().add(out);
}
- protected MonitoringInInterceptor getMonitoringInInterceptor()
- {
+ protected MonitoringInInterceptor getMonitoringInInterceptor() {
return new MonitoringInInterceptor();
}
- public void setRepository( Repository repository )
- {
- this.repository = repository;
- }
-
- public void setCategory( String category )
- {
+ public void setCategory(final String category) {
this.category = category;
}
-
- public void setDomain( String domain )
- {
- this.domain = domain;
- }
}
Copied: commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringInInterceptor.java (from r1506987, commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringInInterceptor.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringInInterceptor.java?p2=commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringInInterceptor.java&p1=commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringInInterceptor.java&r1=1506987&r2=1507950&rev=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringInInterceptor.java (original)
+++ commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringInInterceptor.java Mon Jul 29 06:20:54 2013
@@ -15,14 +15,11 @@
* limitations under the License.
*/
-package org.apache.commons.monitoring.instrumentation.cxf;
+package org.apache.commons.monitoring.cxf;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.commons.monitoring.Monitor;
-import org.apache.commons.monitoring.Repository;
-import org.apache.commons.monitoring.StopWatch;
+import org.apache.commons.monitoring.monitors.Monitor;
+import org.apache.commons.monitoring.repositories.Repository;
+import org.apache.commons.monitoring.stopwatches.StopWatch;
import org.apache.cxf.binding.soap.Soap11;
import org.apache.cxf.binding.soap.Soap12;
import org.apache.cxf.binding.soap.SoapBindingConstants;
@@ -34,165 +31,110 @@ import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.Phase;
+import java.util.List;
+import java.util.Map;
+
/**
* A CXF Interceptor to apply monitoring on incoming messages.
- * <p>
+ * <p/>
* The monitor name is set based on message SOAPAction header (if set).
- *
+ *
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
-public class MonitoringInInterceptor
- extends AbstractSoapInterceptor
-{
- private Repository repository;
-
+public class MonitoringInInterceptor extends AbstractSoapInterceptor {
private String category = "soap";
- private String domain;
-
- public MonitoringInInterceptor()
- {
- super( Phase.READ );
+ public MonitoringInInterceptor() {
+ super(Phase.READ);
}
- public MonitoringInInterceptor( String phase )
- {
- super( phase );
+ public MonitoringInInterceptor(final String phase) {
+ super(phase);
}
- public void handleMessage( SoapMessage message )
- throws Fault
- {
- Monitor monitor =
- repository.getMonitor( getMonitorName( message ), getMonitorCategory( message ), getMonitorDomain( message ) );
- StopWatch stopWatch = repository.start( monitor );
- message.getExchange().put( StopWatch.class, stopWatch );
+ public void handleMessage(final SoapMessage message) throws Fault {
+ final Monitor monitor = Repository.INSTANCE.getMonitor(getMonitorName(message), getMonitorCategory(message));
+ StopWatch stopWatch = Repository.INSTANCE.start(monitor);
+ message.getExchange().put(StopWatch.class, stopWatch);
}
/**
* Detect the monitor name from incoming message
- * <p>
+ * <p/>
* May be overriden to use another Strategy to attach a Monitor to a SoapMessage
- *
+ *
* @param message
* @return
*/
- protected String getMonitorName( SoapMessage message )
- {
- String soapAction = getSoapAction( message );
+ protected String getMonitorName(final SoapMessage message) {
+ String soapAction = getSoapAction(message);
return soapAction != null ? soapAction : "unknown";
}
/**
- * Detect the monitor domain from incoming message
- * <p>
- * May be overriden to use another Strategy to attach a Monitor to a SoapMessage
- *
- * @param message
- * @return
- */
- protected String getMonitorDomain( SoapMessage message )
- {
- return domain;
- }
-
- /**
* Detect the monitor category from incoming message
- * <p>
+ * <p/>
* May be overriden to use another Strategy to attach a Monitor to a SoapMessage
- *
+ *
* @param message
* @return
*/
- protected String getMonitorCategory( SoapMessage message )
- {
+ protected String getMonitorCategory(final SoapMessage message) {
return category;
}
/**
* Retrieve the SOAPAction header
- *
- * @see SoapActionInInterceptor
+ *
* @param message the incoming message
* @return the soapaction if detected
+ * @see SoapActionInInterceptor
*/
- protected String getSoapAction( SoapMessage message )
- {
- if ( message.getVersion() instanceof Soap11 )
- {
- Map<String, List<String>> headers = CastUtils.cast( (Map) message.get( Message.PROTOCOL_HEADERS ) );
- if ( headers != null )
- {
- List<String> sa = headers.get( SoapBindingConstants.SOAP_ACTION );
- if ( sa != null && sa.size() > 0 )
- {
- String action = sa.get( 0 );
- if ( action.startsWith( "\"" ) )
- {
- action = action.substring( 1, action.length() - 1 );
+ protected String getSoapAction(final SoapMessage message) {
+ if (message.getVersion() instanceof Soap11) {
+ final Map<String, List<String>> headers = CastUtils.cast((Map) message.get(Message.PROTOCOL_HEADERS));
+ if (headers != null) {
+ final List<String> sa = headers.get(SoapBindingConstants.SOAP_ACTION);
+ if (sa != null && sa.size() > 0) {
+ String action = sa.get(0);
+ if (action.startsWith("\"")) {
+ action = action.substring(1, action.length() - 1);
}
return action;
}
}
- }
- else if ( message.getVersion() instanceof Soap12 )
- {
- String ct = (String) message.get( Message.CONTENT_TYPE );
-
- if ( ct != null )
- {
- int start = ct.indexOf( "action=" );
- if ( start != -1 )
- {
+ } else if (message.getVersion() instanceof Soap12) {
+ final String ct = (String) message.get(Message.CONTENT_TYPE);
+
+ if (ct != null) {
+ int start = ct.indexOf("action=");
+ if (start != -1) {
start += 7;
int end;
- if ( ct.charAt( start ) == '\"' )
- {
+ if (ct.charAt(start) == '\"') {
start += 1;
- end = ct.indexOf( '\"', start );
- }
- else
- {
- end = ct.indexOf( ';', start );
- if ( end == -1 )
- {
+ end = ct.indexOf('\"', start);
+ } else {
+ end = ct.indexOf(';', start);
+ if (end == -1) {
end = ct.length();
}
}
- return ct.substring( start, end );
+ return ct.substring(start, end);
}
}
}
return null;
}
- public void handleFault( SoapMessage message )
- {
- StopWatch stopWatch = (StopWatch) message.getExchange().get( StopWatch.class );
- stopWatch.stop();
- }
-
- /**
- * @param repository The monitoring repository
- */
- public void setRepository( Repository repository )
- {
- this.repository = repository;
+ public void handleFault(final SoapMessage message) {
+ message.getExchange().get(StopWatch.class).stop();
}
/**
* @param category The monitoring category
*/
- public void setCategory( String category )
- {
+ public void setCategory(final String category) {
this.category = category;
}
-
- /**
- * @param domain The monitoring domain
- */
- public void setDomain( String domain )
- {
- this.domain = domain;
- }
}
Copied: commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringOutInterceptor.java (from r1506987, commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringOutInterceptor.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringOutInterceptor.java?p2=commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringOutInterceptor.java&p1=commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringOutInterceptor.java&r1=1506987&r2=1507950&rev=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/cxf/MonitoringOutInterceptor.java (original)
+++ commons/sandbox/monitoring/trunk/cxf/src/main/java/org/apache/commons/monitoring/cxf/MonitoringOutInterceptor.java Mon Jul 29 06:20:54 2013
@@ -15,9 +15,9 @@
* limitations under the License.
*/
-package org.apache.commons.monitoring.instrumentation.cxf;
+package org.apache.commons.monitoring.cxf;
-import org.apache.commons.monitoring.StopWatch;
+import org.apache.commons.monitoring.stopwatches.StopWatch;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.interceptor.Fault;
@@ -26,40 +26,31 @@ import org.apache.cxf.phase.Phase;
/**
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
-public class MonitoringOutInterceptor
- extends AbstractSoapInterceptor
-{
-
- public MonitoringOutInterceptor()
- {
- super( Phase.SEND );
+public class MonitoringOutInterceptor extends AbstractSoapInterceptor {
+ public MonitoringOutInterceptor() {
+ super(Phase.SEND);
}
-
- public MonitoringOutInterceptor( String phase )
- {
- super( phase );
+ public MonitoringOutInterceptor(final String phase) {
+ super(phase);
}
- public void handleMessage( SoapMessage message )
- throws Fault
- {
- stop( message );
+ public void handleMessage(final SoapMessage message)
+ throws Fault {
+ stop(message);
}
@Override
- public void handleFault( SoapMessage message )
- {
- stop( message );
+ public void handleFault(final SoapMessage message) {
+ stop(message);
}
- protected final long stop( SoapMessage message )
- {
- StopWatch stopWatch = message.getExchange().get( StopWatch.class );
- if ( stopWatch != null )
- {
+ protected final long stop(final SoapMessage message) {
+ final StopWatch stopWatch = message.getExchange().get(StopWatch.class);
+ if (stopWatch != null) {
stopWatch.stop();
+ return stopWatch.getElapsedTime();
}
- return stopWatch.getElapsedTime();
+ return -1;
}
}
Modified: commons/sandbox/monitoring/trunk/jdbc/pom.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/pom.xml?rev=1507950&r1=1507949&r2=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/pom.xml (original)
+++ commons/sandbox/monitoring/trunk/jdbc/pom.xml Mon Jul 29 06:20:54 2013
@@ -16,15 +16,16 @@
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>commons-monitoring-parent</artifactId>
<groupId>org.apache.commons.monitoring</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <groupId>org.apache.commons.monitoring</groupId>
<artifactId>commons-monitoring-jdbc</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <name>Commons Monitoring (Sandbox) :: JDBC</name>
<properties>
<!-- Java6 required for JDBC 4 -->
@@ -36,6 +37,18 @@
<dependency>
<groupId>org.apache.commons.monitoring</groupId>
<artifactId>commons-monitoring-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>2.2.9</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
Modified: commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/ConnectionClosedCallBack.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/ConnectionClosedCallBack.java?rev=1507950&r1=1507949&r2=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/ConnectionClosedCallBack.java (original)
+++ commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/ConnectionClosedCallBack.java Mon Jul 29 06:20:54 2013
@@ -20,7 +20,6 @@ package org.apache.commons.monitoring.jd
/**
* CallBack on connection beeing closed
*/
-public interface ConnectionClosedCallBack
-{
+public interface ConnectionClosedCallBack {
void onConnectionClosed();
}
\ No newline at end of file
Modified: commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredConnection.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredConnection.java?rev=1507950&r1=1507949&r2=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredConnection.java (original)
+++ commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredConnection.java Mon Jul 29 06:20:54 2013
@@ -17,381 +17,102 @@
package org.apache.commons.monitoring.jdbc;
-import java.sql.Array;
-import java.sql.Blob;
+import org.apache.commons.monitoring.Role;
+import org.apache.commons.monitoring.counter.Unit;
+import org.apache.commons.monitoring.monitors.Monitor;
+import org.apache.commons.monitoring.repositories.Repository;
+import org.apache.commons.monitoring.stopwatches.CounterStopWatch;
+import org.apache.commons.monitoring.stopwatches.StopWatch;
+import org.apache.commons.monitoring.util.ClassLoaders;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import java.sql.CallableStatement;
-import java.sql.Clob;
import java.sql.Connection;
-import java.sql.DatabaseMetaData;
-import java.sql.NClob;
import java.sql.PreparedStatement;
-import java.sql.SQLClientInfoException;
-import java.sql.SQLException;
-import java.sql.SQLWarning;
-import java.sql.SQLXML;
-import java.sql.Savepoint;
import java.sql.Statement;
-import java.sql.Struct;
-import java.util.Map;
-import java.util.Properties;
-
-import org.apache.commons.monitoring.Repository;
-import org.apache.commons.monitoring.jdbc.MonitoredCallableStatement;
/**
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
-public class MonitoredConnection
- implements Connection
-{
-
- /** target connection */
- private Connection connection;
+public class MonitoredConnection implements InvocationHandler {
+ private final static Role OPEN_CONECTIONS =
+ new Role("open connections", Unit.UNARY);
- private Repository repository;
+ private final static Role CONECTION_DURATION =
+ new Role("connection duration", Unit.Time.NANOSECOND);
- private ConnectionClosedCallBack callBack;
+ /**
+ * target connection
+ */
+ private Connection connection;
+ private StopWatch stopWatch;
/**
* @param connection target connection
- * @param monitor monitor for opened connections
*/
- public MonitoredConnection( Connection connection, Repository repository, ConnectionClosedCallBack callBack )
- {
- super();
+ public MonitoredConnection(final Connection connection, final StopWatch stopWatch) {
this.connection = connection;
- this.repository = repository;
- this.callBack = callBack;
- }
-
- public void close()
- throws SQLException
- {
- connection.close();
- callBack.onConnectionClosed();
- }
-
- public Statement createStatement()
- throws SQLException
- {
- return monitor( connection.createStatement() );
- }
-
- public Statement createStatement( int resultSetType, int resultSetConcurrency, int resultSetHoldability )
- throws SQLException
- {
- return monitor( connection.createStatement( resultSetType, resultSetConcurrency, resultSetHoldability ) );
- }
-
- public Statement createStatement( int resultSetType, int resultSetConcurrency )
- throws SQLException
- {
- return monitor( connection.createStatement( resultSetType, resultSetConcurrency ) );
- }
-
- public CallableStatement prepareCall( String sql, int resultSetType, int resultSetConcurrency,
- int resultSetHoldability )
- throws SQLException
- {
- return monitor( connection.prepareCall( sql, resultSetType, resultSetConcurrency, resultSetHoldability ), sql );
- }
-
- public CallableStatement prepareCall( String sql, int resultSetType, int resultSetConcurrency )
- throws SQLException
- {
- return monitor( connection.prepareCall( sql, resultSetType, resultSetConcurrency ), sql );
- }
-
- public CallableStatement prepareCall( String sql )
- throws SQLException
- {
- return monitor( connection.prepareCall( sql ), sql );
- }
-
- public PreparedStatement prepareStatement( String sql, int resultSetType, int resultSetConcurrency,
- int resultSetHoldability )
- throws SQLException
- {
- return monitor( connection.prepareStatement( sql, resultSetType, resultSetConcurrency, resultSetHoldability ),
- sql );
- }
-
- public PreparedStatement prepareStatement( String sql, int resultSetType, int resultSetConcurrency )
- throws SQLException
- {
- return monitor( connection.prepareStatement( sql, resultSetType, resultSetConcurrency ), sql );
- }
-
- public PreparedStatement prepareStatement( String sql, int autoGeneratedKeys )
- throws SQLException
- {
- return monitor( connection.prepareStatement( sql, autoGeneratedKeys ), sql );
+ this.stopWatch = stopWatch;
}
- public PreparedStatement prepareStatement( String sql, int[] columnIndexes )
- throws SQLException
- {
- return monitor( connection.prepareStatement( sql, columnIndexes ), sql );
- }
+ @Override
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+ final String name = method.getName();
+ if ("close".equals(name)) {
+ connection.close();
+ stopWatch.stop();
+ return null;
+ }
+
+ if (name.startsWith("prepare") || name.startsWith("create")) {
+ final Class<?> returnType = method.getReturnType();
+ if (CallableStatement.class.equals(returnType)) {
+ return monitor(CallableStatement.class.cast(doInvoke(method, args)), (String) args[0]);
+ } else if (PreparedStatement.class.equals(returnType)) {
+ return monitor(PreparedStatement.class.cast(doInvoke(method, args)), (String) args[0]);
+ } else if (Statement.class.equals(returnType)) {
+ return monitor(Statement.class.cast(doInvoke(method, args)));
+ }
+ }
- public PreparedStatement prepareStatement( String sql, String[] columnNames )
- throws SQLException
- {
- return monitor( connection.prepareStatement( sql, columnNames ), sql );
+ return doInvoke(method, args);
}
- public PreparedStatement prepareStatement( String sql )
- throws SQLException
- {
- return monitor( connection.prepareStatement( sql ), sql );
+ private Object doInvoke(final Method method, final Object[] args) throws IllegalAccessException, InvocationTargetException {
+ return method.invoke(connection, args);
}
/**
* @param statement traget Statement
* @return monitored Statement
*/
- private Statement monitor( Statement statement )
- {
- return new MonitoredStatement( statement, repository );
+ private Statement monitor(final Statement statement) {
+ return Statement.class.cast(Proxy.newProxyInstance(ClassLoaders.current(), new Class<?>[]{Statement.class}, new MonitoredStatement(statement)));
}
/**
* @param statement traget PreparedStatement
- * @param sql SQL Query
+ * @param sql SQL Query
* @return monitored PreparedStatement
*/
- private PreparedStatement monitor( PreparedStatement statement, String sql )
- {
- return new MonitoredPreparedStatement( statement, sql, repository );
+ private PreparedStatement monitor(final PreparedStatement statement, final String sql) {
+ return PreparedStatement.class.cast(Proxy.newProxyInstance(ClassLoaders.current(), new Class<?>[]{PreparedStatement.class}, new MonitoredPreparedStatement(statement, sql)));
}
/**
* @param statement target PreparedStatement
- * @param sql SQL Query
+ * @param sql SQL Query
* @return Monitored CallableStatement
*/
- private CallableStatement monitor( CallableStatement statement, String sql )
- {
- return new MonitoredCallableStatement( statement, sql, repository );
- }
-
- // --- delegates methods ---
-
- public void clearWarnings()
- throws SQLException
- {
- connection.clearWarnings();
- }
-
- public void commit()
- throws SQLException
- {
- connection.commit();
- }
-
- public boolean getAutoCommit()
- throws SQLException
- {
- return connection.getAutoCommit();
- }
-
- public String getCatalog()
- throws SQLException
- {
- return connection.getCatalog();
- }
-
- public int getHoldability()
- throws SQLException
- {
- return connection.getHoldability();
- }
-
- public DatabaseMetaData getMetaData()
- throws SQLException
- {
- return connection.getMetaData();
- }
-
- public int getTransactionIsolation()
- throws SQLException
- {
- return connection.getTransactionIsolation();
- }
-
- public Map<String, Class<?>> getTypeMap()
- throws SQLException
- {
- return connection.getTypeMap();
- }
-
- public SQLWarning getWarnings()
- throws SQLException
- {
- return connection.getWarnings();
- }
-
- public boolean isClosed()
- throws SQLException
- {
- return connection.isClosed();
- }
-
- public boolean isReadOnly()
- throws SQLException
- {
- return connection.isReadOnly();
- }
-
- public String nativeSQL( String sql )
- throws SQLException
- {
- return connection.nativeSQL( sql );
- }
-
- public void releaseSavepoint( Savepoint savepoint )
- throws SQLException
- {
- connection.releaseSavepoint( savepoint );
- }
-
- public void rollback()
- throws SQLException
- {
- connection.rollback();
- }
-
- public void rollback( Savepoint savepoint )
- throws SQLException
- {
- connection.rollback( savepoint );
- }
-
- public void setAutoCommit( boolean autoCommit )
- throws SQLException
- {
- connection.setAutoCommit( autoCommit );
- }
-
- public void setCatalog( String catalog )
- throws SQLException
- {
- connection.setCatalog( catalog );
- }
-
- public void setHoldability( int holdability )
- throws SQLException
- {
- connection.setHoldability( holdability );
- }
-
- public void setReadOnly( boolean readOnly )
- throws SQLException
- {
- connection.setReadOnly( readOnly );
- }
-
- public Savepoint setSavepoint()
- throws SQLException
- {
- return connection.setSavepoint();
- }
-
- public Savepoint setSavepoint( String name )
- throws SQLException
- {
- return connection.setSavepoint( name );
- }
-
- public void setTransactionIsolation( int level )
- throws SQLException
- {
- connection.setTransactionIsolation( level );
- }
-
- public void setTypeMap( Map<String, Class<?>> map )
- throws SQLException
- {
- connection.setTypeMap( map );
- }
-
- // --- jdbc4 ----
-
- public Array createArrayOf( String typeName, Object[] elements )
- throws SQLException
- {
- return connection.createArrayOf( typeName, elements );
- }
-
- public Blob createBlob()
- throws SQLException
- {
- return connection.createBlob();
- }
-
- public Clob createClob()
- throws SQLException
- {
- return connection.createClob();
- }
-
- public NClob createNClob()
- throws SQLException
- {
- return connection.createNClob();
- }
-
- public SQLXML createSQLXML()
- throws SQLException
- {
- return connection.createSQLXML();
- }
-
- public Struct createStruct( String typeName, Object[] attributes )
- throws SQLException
- {
- return connection.createStruct( typeName, attributes );
- }
-
- public Properties getClientInfo()
- throws SQLException
- {
- return connection.getClientInfo();
- }
-
- public String getClientInfo( String name )
- throws SQLException
- {
- return connection.getClientInfo( name );
- }
-
- public boolean isValid( int timeout )
- throws SQLException
- {
- return connection.isValid( timeout );
- }
-
- public boolean isWrapperFor( Class<?> iface )
- throws SQLException
- {
- return connection.isWrapperFor( iface );
- }
-
- public void setClientInfo( Properties properties )
- throws SQLClientInfoException
- {
- connection.setClientInfo( properties );
- }
-
- public void setClientInfo( String name, String value )
- throws SQLClientInfoException
- {
- connection.setClientInfo( name, value );
+ private CallableStatement monitor(final CallableStatement statement, final String sql) {
+ return CallableStatement.class.cast(Proxy.newProxyInstance(ClassLoaders.current(), new Class<?>[]{CallableStatement.class}, new MonitoredPreparedStatement(statement, sql)));
}
- public <T> T unwrap( Class<T> iface )
- throws SQLException
- {
- return connection.unwrap( iface );
+ public static Connection monitor(final Connection connection, final Monitor monitor) {
+ final StopWatch stopWatch = new CounterStopWatch(monitor);
+ return Connection.class.cast(Proxy.newProxyInstance(ClassLoaders.current(), new Class<?>[]{Connection.class}, new MonitoredConnection(connection, stopWatch)));
}
}
Modified: commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredDataSource.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredDataSource.java?rev=1507950&r1=1507949&r2=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredDataSource.java (original)
+++ commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredDataSource.java Mon Jul 29 06:20:54 2013
@@ -18,103 +18,129 @@
package org.apache.commons.monitoring.jdbc;
+import org.apache.commons.monitoring.monitors.Monitor;
+import org.apache.commons.monitoring.repositories.Repository;
+
+import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
-import javax.sql.DataSource;
-
-import org.apache.commons.monitoring.Repository;
-
/**
* @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
*/
-public class MonitoredDataSource extends AbstractMonitoredDataSource
- implements DataSource
-{
+public class MonitoredDataSource implements DataSource {
+ /**
+ * delegate DataSource
+ */
+ private DataSource dataSource;
+
+ /**
+ * dataSource name
+ */
+ private String dataSourceName = DataSource.class.getName();
+ private Monitor monitor;
+
/**
* Constructor
- *
+ *
* @param dataSource the datasource to monitor
*/
- public MonitoredDataSource( DataSource dataSource, Repository repository )
- {
- super( dataSource, repository );
+ public MonitoredDataSource(final DataSource dataSource) {
+ this.dataSource = dataSource;
+ this.monitor = Repository.INSTANCE.getMonitor(dataSourceName, "jdbc");
}
- public MonitoredDataSource()
- {
+ public MonitoredDataSource() {
super();
}
+ public void setDataSource(final DataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ /**
+ * @param dataSourceName the dataSourceName to set
+ */
+ public void setDataSourceName(final String dataSourceName) {
+ this.dataSourceName = dataSourceName;
+ }
+
+ /**
+ * @param monitor the monitor to set
+ */
+ public void setMonitor(final Monitor monitor) {
+ this.monitor = monitor;
+ }
+
+ protected Connection monitor(final Connection connection) {
+ return MonitoredConnection.monitor(connection, monitor);
+ }
+
+ /**
+ * @return the dataSource
+ */
+ protected DataSource getDataSource() {
+ return dataSource;
+ }
+
/**
* {@inheritDoc}
- *
+ *
* @see javax.sql.DataSource#getConnection()
*/
public Connection getConnection()
- throws SQLException
- {
- Connection connection = getDataSource().getConnection();
- return monitor( connection );
+ throws SQLException {
+ return monitor(getDataSource().getConnection());
}
/**
* {@inheritDoc}
- *
+ *
* @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
*/
- public Connection getConnection( String username, String password )
- throws SQLException
- {
- Connection connection = getDataSource().getConnection( username, password );
- return monitor( connection );
+ public Connection getConnection(String username, String password)
+ throws SQLException {
+ return monitor(getDataSource().getConnection(username, password));
}
public int getLoginTimeout()
- throws SQLException
- {
+ throws SQLException {
return getDataSource().getLoginTimeout();
}
public PrintWriter getLogWriter()
- throws SQLException
- {
+ throws SQLException {
return getDataSource().getLogWriter();
}
- public void setLoginTimeout( int seconds )
- throws SQLException
- {
- getDataSource().setLoginTimeout( seconds );
+ public void setLoginTimeout(int seconds)
+ throws SQLException {
+ getDataSource().setLoginTimeout(seconds);
}
- public void setLogWriter( PrintWriter out )
- throws SQLException
- {
- getDataSource().setLogWriter( out );
+ public void setLogWriter(PrintWriter out)
+ throws SQLException {
+ getDataSource().setLogWriter(out);
}
// --- jdbc4 ----
- public boolean isWrapperFor( Class<?> iface )
- throws SQLException
- {
- return getDataSource().isWrapperFor( iface );
+ public boolean isWrapperFor(Class<?> iface)
+ throws SQLException {
+ return getDataSource().isWrapperFor(iface);
}
- public <T> T unwrap( Class<T> iface )
- throws SQLException
- {
- return getDataSource().unwrap( iface );
+ public <T> T unwrap(Class<T> iface)
+ throws SQLException {
+ return getDataSource().unwrap(iface);
}
public Logger getParentLogger()
- throws SQLFeatureNotSupportedException
- {
- return null;
+ throws SQLFeatureNotSupportedException {
+ return Logger.getLogger("commons-monitoring.datasource");
}
}
Modified: commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredPreparedStatement.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredPreparedStatement.java?rev=1507950&r1=1507949&r2=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredPreparedStatement.java (original)
+++ commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredPreparedStatement.java Mon Jul 29 06:20:54 2013
@@ -16,421 +16,40 @@
*/
package org.apache.commons.monitoring.jdbc;
-import java.io.InputStream;
-import java.io.Reader;
-import java.math.BigDecimal;
-import java.net.URL;
-import java.sql.Array;
-import java.sql.Blob;
-import java.sql.Clob;
-import java.sql.Date;
-import java.sql.NClob;
-import java.sql.ParameterMetaData;
-import java.sql.PreparedStatement;
-import java.sql.Ref;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.RowId;
-import java.sql.SQLException;
-import java.sql.SQLXML;
-import java.sql.Time;
-import java.sql.Timestamp;
-import java.util.Calendar;
+import org.apache.commons.monitoring.repositories.Repository;
+import org.apache.commons.monitoring.stopwatches.StopWatch;
-import org.apache.commons.monitoring.Repository;
-import org.apache.commons.monitoring.StopWatch;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.PreparedStatement;
/**
* @author ndeloof
- *
*/
-public class MonitoredPreparedStatement
- extends MonitoredStatement
- implements PreparedStatement
-{
- private PreparedStatement statement;
+public class MonitoredPreparedStatement extends MonitoredStatement {
+ private final PreparedStatement statement;
+ private final String sql;
- protected String sql;
-
- /**
- * @param statement
- * @param repository
- */
- public MonitoredPreparedStatement( PreparedStatement statement, String sql, Repository repository )
- {
- super( statement, repository );
+ public MonitoredPreparedStatement(final PreparedStatement statement, final String sql) {
+ super(statement);
this.statement = statement;
this.sql = sql;
}
- // --- delegate methods ---
-
- public final void addBatch()
- throws SQLException
- {
- statement.addBatch();
- }
-
- public final void clearParameters()
- throws SQLException
- {
- statement.clearParameters();
- }
-
- public final boolean execute()
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.execute();
+ @Override
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+ final String name = method.getName();
+ if ((args == null || args.length == 0) && name.startsWith("execute")) {
+ final StopWatch stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getMonitor(sql, "jdbc"));
+ try {
+ return method.invoke(statement, args);
+ } catch (final InvocationTargetException e) {
+ throw extractSQLException(e);
+ } finally {
+ stopWatch.stop();
+ }
}
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final ResultSet executeQuery()
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.executeQuery();
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final int executeUpdate()
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.executeUpdate();
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final ResultSetMetaData getMetaData()
- throws SQLException
- {
- return statement.getMetaData();
- }
-
- public final ParameterMetaData getParameterMetaData()
- throws SQLException
- {
- return statement.getParameterMetaData();
- }
-
- public final void setArray( int i, Array x )
- throws SQLException
- {
- statement.setArray( i, x );
- }
-
- public final void setAsciiStream( int parameterIndex, InputStream x, int length )
- throws SQLException
- {
- statement.setAsciiStream( parameterIndex, x, length );
- }
-
- public final void setBigDecimal( int parameterIndex, BigDecimal x )
- throws SQLException
- {
- statement.setBigDecimal( parameterIndex, x );
- }
-
- public final void setBinaryStream( int parameterIndex, InputStream x, int length )
- throws SQLException
- {
- statement.setBinaryStream( parameterIndex, x, length );
- }
-
- public final void setBlob( int i, Blob x )
- throws SQLException
- {
- statement.setBlob( i, x );
- }
-
- public final void setBoolean( int parameterIndex, boolean x )
- throws SQLException
- {
- statement.setBoolean( parameterIndex, x );
- }
-
- public final void setByte( int parameterIndex, byte x )
- throws SQLException
- {
- statement.setByte( parameterIndex, x );
- }
-
- public final void setBytes( int parameterIndex, byte[] x )
- throws SQLException
- {
- statement.setBytes( parameterIndex, x );
- }
-
- public final void setCharacterStream( int parameterIndex, Reader reader, int length )
- throws SQLException
- {
- statement.setCharacterStream( parameterIndex, reader, length );
- }
-
- public final void setClob( int i, Clob x )
- throws SQLException
- {
- statement.setClob( i, x );
- }
-
- public final void setDate( int parameterIndex, Date x, Calendar cal )
- throws SQLException
- {
- statement.setDate( parameterIndex, x, cal );
- }
-
- public final void setDate( int parameterIndex, Date x )
- throws SQLException
- {
- statement.setDate( parameterIndex, x );
- }
-
- public final void setDouble( int parameterIndex, double x )
- throws SQLException
- {
- statement.setDouble( parameterIndex, x );
- }
-
- public final void setFloat( int parameterIndex, float x )
- throws SQLException
- {
- statement.setFloat( parameterIndex, x );
- }
-
- public final void setInt( int parameterIndex, int x )
- throws SQLException
- {
- statement.setInt( parameterIndex, x );
- }
-
- public final void setLong( int parameterIndex, long x )
- throws SQLException
- {
- statement.setLong( parameterIndex, x );
- }
-
- public final void setNull( int paramIndex, int sqlType, String typeName )
- throws SQLException
- {
- statement.setNull( paramIndex, sqlType, typeName );
- }
-
- public final void setNull( int parameterIndex, int sqlType )
- throws SQLException
- {
- statement.setNull( parameterIndex, sqlType );
- }
-
- public final void setObject( int parameterIndex, Object x, int targetSqlType, int scale )
- throws SQLException
- {
- statement.setObject( parameterIndex, x, targetSqlType, scale );
- }
-
- public final void setObject( int parameterIndex, Object x, int targetSqlType )
- throws SQLException
- {
- statement.setObject( parameterIndex, x, targetSqlType );
- }
-
- public final void setObject( int parameterIndex, Object x )
- throws SQLException
- {
- statement.setObject( parameterIndex, x );
- }
-
- public final void setRef( int i, Ref x )
- throws SQLException
- {
- statement.setRef( i, x );
- }
-
- public final void setShort( int parameterIndex, short x )
- throws SQLException
- {
- statement.setShort( parameterIndex, x );
- }
-
- public final void setString( int parameterIndex, String x )
- throws SQLException
- {
- statement.setString( parameterIndex, x );
- }
-
- public final void setTime( int parameterIndex, Time x, Calendar cal )
- throws SQLException
- {
- statement.setTime( parameterIndex, x, cal );
- }
-
- public final void setTime( int parameterIndex, Time x )
- throws SQLException
- {
- statement.setTime( parameterIndex, x );
- }
-
- public final void setTimestamp( int parameterIndex, Timestamp x, Calendar cal )
- throws SQLException
- {
- statement.setTimestamp( parameterIndex, x, cal );
- }
-
- public final void setTimestamp( int parameterIndex, Timestamp x )
- throws SQLException
- {
- statement.setTimestamp( parameterIndex, x );
- }
-
- public final void setUnicodeStream( int parameterIndex, InputStream x, int length )
- throws SQLException
- {
- statement.setUnicodeStream( parameterIndex, x, length );
- }
-
- public final void setURL( int parameterIndex, URL x )
- throws SQLException
- {
- statement.setURL( parameterIndex, x );
- }
-
- // --- jdbc 4 ---
-
- public final void setAsciiStream( int parameterIndex, InputStream x, long length )
- throws SQLException
- {
- statement.setAsciiStream( parameterIndex, x, length );
- }
-
- public final void setAsciiStream( int parameterIndex, InputStream x )
- throws SQLException
- {
- statement.setAsciiStream( parameterIndex, x );
- }
-
- public final void setBinaryStream( int parameterIndex, InputStream x, long length )
- throws SQLException
- {
- statement.setBinaryStream( parameterIndex, x, length );
- }
-
- public final void setBinaryStream( int parameterIndex, InputStream x )
- throws SQLException
- {
- statement.setBinaryStream( parameterIndex, x );
- }
-
- public final void setBlob( int parameterIndex, InputStream inputStream, long length )
- throws SQLException
- {
- statement.setBlob( parameterIndex, inputStream, length );
- }
-
- public final void setBlob( int parameterIndex, InputStream inputStream )
- throws SQLException
- {
- statement.setBlob( parameterIndex, inputStream );
- }
-
- public final void setCharacterStream( int parameterIndex, Reader reader, long length )
- throws SQLException
- {
- statement.setCharacterStream( parameterIndex, reader, length );
- }
-
- public final void setCharacterStream( int parameterIndex, Reader reader )
- throws SQLException
- {
- statement.setCharacterStream( parameterIndex, reader );
- }
-
- public final void setClob( int parameterIndex, Reader reader, long length )
- throws SQLException
- {
- statement.setClob( parameterIndex, reader, length );
- }
-
- public final void setClob( int parameterIndex, Reader reader )
- throws SQLException
- {
- statement.setClob( parameterIndex, reader );
- }
-
- public final void setNCharacterStream( int parameterIndex, Reader value, long length )
- throws SQLException
- {
- statement.setNCharacterStream( parameterIndex, value, length );
- }
-
- public final void setNCharacterStream( int parameterIndex, Reader value )
- throws SQLException
- {
- statement.setNCharacterStream( parameterIndex, value );
- }
-
- public final void setNClob( int parameterIndex, NClob value )
- throws SQLException
- {
- statement.setNClob( parameterIndex, value );
- }
-
- public final void setNClob( int parameterIndex, Reader reader, long length )
- throws SQLException
- {
- statement.setNClob( parameterIndex, reader, length );
- }
-
- public final void setNClob( int parameterIndex, Reader reader )
- throws SQLException
- {
- statement.setNClob( parameterIndex, reader );
- }
-
- public final void setNString( int parameterIndex, String value )
- throws SQLException
- {
- statement.setNString( parameterIndex, value );
- }
-
- public final void setRowId( int parameterIndex, RowId x )
- throws SQLException
- {
- statement.setRowId( parameterIndex, x );
- }
-
- public final void setSQLXML( int parameterIndex, SQLXML xmlObject )
- throws SQLException
- {
- statement.setSQLXML( parameterIndex, xmlObject );
+ return super.invoke(proxy, method, args);
}
}
Modified: commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredStatement.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredStatement.java?rev=1507950&r1=1507949&r2=1507950&view=diff
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredStatement.java (original)
+++ commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoredStatement.java Mon Jul 29 06:20:54 2013
@@ -17,415 +17,65 @@
package org.apache.commons.monitoring.jdbc;
-import java.sql.Connection;
-import java.sql.ResultSet;
+import org.apache.commons.monitoring.Role;
+import org.apache.commons.monitoring.monitors.Monitor;
+import org.apache.commons.monitoring.repositories.Repository;
+import org.apache.commons.monitoring.stopwatches.StopWatch;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.sql.SQLException;
-import java.sql.SQLWarning;
import java.sql.Statement;
-import org.apache.commons.monitoring.Monitor;
-import org.apache.commons.monitoring.Repository;
-import org.apache.commons.monitoring.StopWatch;
-
/**
* @author <a href="mailto:ndeloof@sourceforge.net">Nicolas De Loof</a>
*/
-public class MonitoredStatement
- implements Statement
-{
- /** delegate statement */
- private Statement statement;
-
- protected Repository repository;
+public class MonitoredStatement implements InvocationHandler {
+ private final Statement statement;
- public MonitoredStatement( Statement statement, Repository repository )
- {
- super();
+ public MonitoredStatement(final Statement statement) {
this.statement = statement;
- this.repository = repository;
}
- protected SQLException monitor( SQLException sqle )
- {
- String name = "SQLException:" + sqle.getSQLState() + ":" + sqle.getErrorCode();
- Monitor monitor = repository.getMonitor( name, "jdbc" );
- monitor.getCounter( Monitor.FAILURES ).add( 1 );
+ protected SQLException monitor(final SQLException sqle) {
+ final String name = "SQLException:" + sqle.getSQLState() + ":" + sqle.getErrorCode();
+ final Monitor monitor = Repository.INSTANCE.getMonitor(name, "jdbc");
+ monitor.getCounter(Role.FAILURES).add(1);
return sqle;
}
- // --- delegate methods ---
-
- public final void addBatch( String sql )
- throws SQLException
- {
- statement.addBatch( sql );
- }
-
- public final void cancel()
- throws SQLException
- {
- statement.cancel();
- }
-
- public final void clearBatch()
- throws SQLException
- {
- statement.clearBatch();
- }
-
- public final void clearWarnings()
- throws SQLException
- {
- statement.clearWarnings();
- }
-
- public final void close()
- throws SQLException
- {
- statement.close();
- }
-
- public final boolean execute( String sql, int autoGeneratedKeys )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.execute( sql, autoGeneratedKeys );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final boolean execute( String sql, int[] columnIndexes )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.execute( sql, columnIndexes );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final boolean execute( String sql, String[] columnNames )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.execute( sql, columnNames );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final boolean execute( String sql )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.execute( sql );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final int[] executeBatch()
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( "batch", "jdbc" ) );
- try
- {
- return statement.executeBatch();
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final ResultSet executeQuery( String sql )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.executeQuery( sql );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final int executeUpdate( String sql, int autoGeneratedKeys )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.executeUpdate( sql, autoGeneratedKeys );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final int executeUpdate( String sql, int[] columnIndexes )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.executeUpdate( sql, columnIndexes );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final int executeUpdate( String sql, String[] columnNames )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.executeUpdate( sql, columnNames );
- }
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final int executeUpdate( String sql )
- throws SQLException
- {
- StopWatch stopWatch = repository.start( repository.getMonitor( sql, "jdbc" ) );
- try
- {
- return statement.executeUpdate( sql );
+ @Override
+ public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+ final String name = method.getName();
+ if (name.startsWith("execute")) {
+ final StopWatch stopWatch;
+ if (name.endsWith("Batch") && (args == null || args.length == 0)) {
+ stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getMonitor("batch", "jdbc"));
+ } else {
+ stopWatch = Repository.INSTANCE.start(Repository.INSTANCE.getMonitor((String) args[0], "jdbc"));
+ }
+
+ try {
+ return doInvoke(method, args);
+ } catch (final InvocationTargetException e) {
+ throw extractSQLException(e);
+ } finally {
+ stopWatch.stop();
+ }
+ }
+ return doInvoke(method, args);
+ }
+
+ private Object doInvoke(final Method method, final Object[] args) throws IllegalAccessException, InvocationTargetException {
+ return method.invoke(statement, args);
+ }
+
+ protected Throwable extractSQLException(final InvocationTargetException e) throws Throwable {
+ final Throwable th = e.getCause();
+ if (SQLException.class.isInstance(th)) {
+ return monitor(SQLException.class.cast(th));
}
- catch ( SQLException sqle )
- {
- throw monitor( sqle );
- }
- finally
- {
- stopWatch.stop();
- }
- }
-
- public final Connection getConnection()
- throws SQLException
- {
- return statement.getConnection();
- }
-
- public final int getFetchDirection()
- throws SQLException
- {
- return statement.getFetchDirection();
- }
-
- public final int getFetchSize()
- throws SQLException
- {
- return statement.getFetchSize();
- }
-
- public final ResultSet getGeneratedKeys()
- throws SQLException
- {
- return statement.getGeneratedKeys();
- }
-
- public final int getMaxFieldSize()
- throws SQLException
- {
- return statement.getMaxFieldSize();
- }
-
- public final int getMaxRows()
- throws SQLException
- {
- return statement.getMaxRows();
- }
-
- public final boolean getMoreResults()
- throws SQLException
- {
- return statement.getMoreResults();
- }
-
- public final boolean getMoreResults( int current )
- throws SQLException
- {
- return statement.getMoreResults( current );
- }
-
- public final int getQueryTimeout()
- throws SQLException
- {
- return statement.getQueryTimeout();
- }
-
- public final ResultSet getResultSet()
- throws SQLException
- {
- return statement.getResultSet();
- }
-
- public final int getResultSetConcurrency()
- throws SQLException
- {
- return statement.getResultSetConcurrency();
- }
-
- public final int getResultSetHoldability()
- throws SQLException
- {
- return statement.getResultSetHoldability();
- }
-
- public final int getResultSetType()
- throws SQLException
- {
- return statement.getResultSetType();
- }
-
- public final int getUpdateCount()
- throws SQLException
- {
- return statement.getUpdateCount();
- }
-
- public final SQLWarning getWarnings()
- throws SQLException
- {
- return statement.getWarnings();
- }
-
- public final void setCursorName( String name )
- throws SQLException
- {
- statement.setCursorName( name );
- }
-
- public final void setEscapeProcessing( boolean enable )
- throws SQLException
- {
- statement.setEscapeProcessing( enable );
- }
-
- public final void setFetchDirection( int direction )
- throws SQLException
- {
- statement.setFetchDirection( direction );
- }
-
- public final void setFetchSize( int rows )
- throws SQLException
- {
- statement.setFetchSize( rows );
- }
-
- public final void setMaxFieldSize( int max )
- throws SQLException
- {
- statement.setMaxFieldSize( max );
- }
-
- public final void setMaxRows( int max )
- throws SQLException
- {
- statement.setMaxRows( max );
- }
-
- public final void setQueryTimeout( int seconds )
- throws SQLException
- {
- statement.setQueryTimeout( seconds );
- }
-
- // --- jdbc4 ----
-
- public final boolean isClosed()
- throws SQLException
- {
- return statement.isClosed();
- }
-
- public final boolean isPoolable()
- throws SQLException
- {
- return statement.isPoolable();
- }
-
- public final boolean isWrapperFor( Class<?> iface )
- throws SQLException
- {
- return statement.isWrapperFor( iface );
- }
-
- public final void setPoolable( boolean poolable )
- throws SQLException
- {
- statement.setPoolable( poolable );
- }
-
- public final <T> T unwrap( Class<T> iface )
- throws SQLException
- {
- return statement.unwrap( iface );
+ return th;
}
}
Added: commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoringDriver.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoringDriver.java?rev=1507950&view=auto
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoringDriver.java (added)
+++ commons/sandbox/monitoring/trunk/jdbc/src/main/java/org/apache/commons/monitoring/jdbc/MonitoringDriver.java Mon Jul 29 06:20:54 2013
@@ -0,0 +1,96 @@
+/*
+ * 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.jdbc;
+
+import org.apache.commons.monitoring.repositories.Repository;
+
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.sql.DriverPropertyInfo;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.Properties;
+import java.util.logging.Logger;
+
+public class MonitoringDriver implements Driver {
+ static {
+ try {
+ DriverManager.registerDriver(new MonitoringDriver());
+ } catch (final SQLException e) {
+ // no-op
+ }
+ }
+
+ private static final String PREFIX = "jdbc:monitoring:";
+ private static final String DRIVER_SUFFIX = "delegateDriver=";
+
+ public static void load() {
+ } // sexier than Class.forName("org.apache.commons.monitoring.jdbc.MonitoringDriver"); in full java
+
+ @Override
+ public Connection connect(final String url, final Properties info) throws SQLException {
+ if (!acceptsURL(url)) {
+ throw new SQLException("Driver " + MonitoringDriver.class.getName() + " doesn't accept " + url + ". Pattern is jdbc:monitoring:<xxx>:<yyy>?delegateDriver=<zzz>");
+ }
+
+ final int driverIndex = url.indexOf(DRIVER_SUFFIX);
+
+ String realUrl = "jdbc:" + url.substring(PREFIX.length(), driverIndex);
+ if (realUrl.endsWith("?") || realUrl.endsWith("&")) {
+ realUrl = realUrl.substring(0, realUrl.length() - 1);
+ }
+
+ final String realDriver = url.substring(driverIndex + DRIVER_SUFFIX.length());
+ try {
+ final Driver delegate = Driver.class.cast(Class.forName(realDriver).newInstance());
+ return MonitoredConnection.monitor(delegate.connect(realUrl, info), Repository.INSTANCE.getMonitor(Driver.class.getName(), "jdbc"));
+ } catch (final Exception e) {
+ throw new SQLException(e);
+ }
+ }
+
+ @Override
+ public boolean acceptsURL(final String url) throws SQLException {
+ return url != null && url.startsWith(PREFIX) && url.contains(DRIVER_SUFFIX);
+ }
+
+ @Override
+ public DriverPropertyInfo[] getPropertyInfo(final String url, final Properties info) throws SQLException {
+ return new DriverPropertyInfo[0];
+ }
+
+ @Override
+ public int getMajorVersion() {
+ return 1;
+ }
+
+ @Override
+ public int getMinorVersion() {
+ return 0;
+ }
+
+ @Override
+ public boolean jdbcCompliant() {
+ return true;
+ }
+
+ // @Override // java 7
+ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
+ return Logger.getLogger("commons-monitoring.jdbc-driver");
+ }
+}
Added: commons/sandbox/monitoring/trunk/jdbc/src/test/java/org/apache/commons/monitoring/jdbc/HsqlDBTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/jdbc/src/test/java/org/apache/commons/monitoring/jdbc/HsqlDBTest.java?rev=1507950&view=auto
==============================================================================
--- commons/sandbox/monitoring/trunk/jdbc/src/test/java/org/apache/commons/monitoring/jdbc/HsqlDBTest.java (added)
+++ commons/sandbox/monitoring/trunk/jdbc/src/test/java/org/apache/commons/monitoring/jdbc/HsqlDBTest.java Mon Jul 29 06:20:54 2013
@@ -0,0 +1,67 @@
+/*
+ * 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.jdbc;
+
+import org.apache.commons.monitoring.Role;
+import org.apache.commons.monitoring.monitors.Monitor;
+import org.apache.commons.monitoring.repositories.Repository;
+import org.hsqldb.jdbcDriver;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.Statement;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public class HsqlDBTest {
+
+ @BeforeClass
+ public static void init() {
+ MonitoringDriver.load();
+ }
+
+ @Test
+ public void driverMonitoring() throws Exception {
+ final Connection connection = DriverManager.getConnection("jdbc:monitoring:hsqldb:mem:monitoring?delegateDriver=" + jdbcDriver.class.getName(), "SA", "");
+ assertNotNull(connection);
+ assertTrue(Proxy.isProxyClass(connection.getClass()));
+ final InvocationHandler handler = Proxy.getInvocationHandler(connection);
+ assertThat(handler, instanceOf(MonitoredConnection.class));
+
+ final String create = "CREATE TABLE Address (Nr INTEGER, Name VARCHAR(128));";
+ final Statement statement = connection.createStatement();
+ statement.execute(create);
+ assertEquals(1, Repository.INSTANCE.getMonitor(new Monitor.Key(create, "jdbc")).getMaxConcurrency(), 0.);
+
+ final String insert = "INSERT INTO Address (Nr, Name) VALUES(1, 'foo')";
+ final PreparedStatement preparedStatement = connection.prepareStatement(insert);
+ preparedStatement.execute();
+ assertEquals(1, Repository.INSTANCE.getMonitor(new Monitor.Key(insert, "jdbc")).getMaxConcurrency(), 0.);
+ preparedStatement.execute();
+ assertEquals(1, Repository.INSTANCE.getMonitor(new Monitor.Key(insert, "jdbc")).getMaxConcurrency(), 0.);
+ assertEquals(2, Repository.INSTANCE.getMonitor(new Monitor.Key(insert, "jdbc")).getCounter(Role.PERFORMANCES).getHits(), 0.);
+ }
+}