Author: rmannibucau
Date: Wed Aug 15 20:59:06 2012
New Revision: 1373628

URL: http://svn.apache.org/viewvc?rev=1373628&view=rev
Log:
OPENEJB-1891 openjpa embedded logging format + logging sql with execution 
duration

Added:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/AbstractSQLLogger.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/TimeWatcherExecutor.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingPreparedSqlStatement.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JuliLogStreamFactory.java

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/AbstractSQLLogger.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/AbstractSQLLogger.java?rev=1373628&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/AbstractSQLLogger.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/AbstractSQLLogger.java
 Wed Aug 15 20:59:06 2012
@@ -0,0 +1,23 @@
+/*
+ * 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.openejb.resource.jdbc.logging;
+
+public abstract class AbstractSQLLogger {
+    protected String format(final String query, final long ms) {
+        return query + " --> " + ms + "ms";
+    }
+}

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingPreparedSqlStatement.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingPreparedSqlStatement.java?rev=1373628&r1=1373627&r2=1373628&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingPreparedSqlStatement.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingPreparedSqlStatement.java
 Wed Aug 15 20:59:06 2012
@@ -22,14 +22,11 @@ import org.apache.openejb.util.Logger;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.sql.PreparedStatement;
-import java.sql.Statement;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
-public class LoggingPreparedSqlStatement implements InvocationHandler {
+public class LoggingPreparedSqlStatement extends AbstractSQLLogger implements 
InvocationHandler {
     private static final Logger LOGGER = 
Logger.getInstance(LogCategory.OPENEJB_SQL, LoggingPreparedSqlStatement.class);
 
     private final PreparedStatement delegate;
@@ -43,17 +40,18 @@ public class LoggingPreparedSqlStatement
 
     @Override
     public Object invoke(final Object proxy, final Method method, final 
Object[] args) throws Throwable {
-        final Object result = method.invoke(delegate, args);
         final String mtdName = method.getName();
+        final boolean execute = mtdName.startsWith("execute");
+
+        final TimeWatcherExecutor.TimerWatcherResult result = 
TimeWatcherExecutor.execute(method, delegate, args, execute);
+
         if (mtdName.startsWith("set") && args.length >= 2 && 
(args[0].getClass().equals(Integer.TYPE) || 
args[0].getClass().equals(Integer.class))) {
             parameters.add(new Parameter(mtdName.substring(3), (Integer) 
args[0], args[1]));
-        } else if (mtdName.startsWith("execute")) {
+        } else if (execute) {
             String str = sql;
             if (str.contains("?")) {
                 Collections.sort(parameters);
-                final Iterator<Parameter> it = parameters.iterator();
-                while (it.hasNext()) {
-                    final Parameter param = it.next();
+                for (Parameter param : parameters) {
                     try {
                         str = str.replaceFirst("\\?", param.value.toString());
                     } catch (Exception e) {
@@ -61,9 +59,10 @@ public class LoggingPreparedSqlStatement
                     }
                 }
             }
-            LOGGER.info(str);
+            LOGGER.info(format(str, result.getDuration()));
         }
-        return result;
+
+        return result.getResult();
     }
 
     protected static class Parameter implements Comparable<Parameter> {

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java?rev=1373628&r1=1373627&r2=1373628&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java
 Wed Aug 15 20:59:06 2012
@@ -23,7 +23,7 @@ import java.lang.reflect.InvocationHandl
 import java.lang.reflect.Method;
 import java.sql.Statement;
 
-public class LoggingSqlStatement implements InvocationHandler {
+public class LoggingSqlStatement extends AbstractSQLLogger implements 
InvocationHandler {
     private static final Logger LOGGER = 
Logger.getInstance(LogCategory.OPENEJB_SQL, LoggingSqlStatement.class);
 
     private final Statement delegate;
@@ -34,11 +34,14 @@ public class LoggingSqlStatement impleme
 
     @Override
     public Object invoke(final Object proxy, final Method method, final 
Object[] args) throws Throwable {
-        final Object result = method.invoke(delegate, args);
         final String mtdName = method.getName();
-        if (mtdName.startsWith("execute") && args != null && args.length > 0) {
-            LOGGER.info((String) args[0]);
+        final boolean execute = mtdName.startsWith("execute") && args != null 
&& args.length > 0;
+
+        final TimeWatcherExecutor.TimerWatcherResult result = 
TimeWatcherExecutor.execute(method, delegate, args, execute);
+        if (execute) {
+            LOGGER.info(format((String) args[0], result.getDuration()));
         }
-        return result;
+
+        return result.getResult();
     }
 }

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/TimeWatcherExecutor.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/TimeWatcherExecutor.java?rev=1373628&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/TimeWatcherExecutor.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/TimeWatcherExecutor.java
 Wed Aug 15 20:59:06 2012
@@ -0,0 +1,57 @@
+/*
+ * 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.openejb.resource.jdbc.logging;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.concurrent.TimeUnit;
+
+public final class TimeWatcherExecutor {
+    private TimeWatcherExecutor() {
+        // no-op
+    }
+
+    public static TimerWatcherResult execute(final Method mtd, final Object 
instance, final Object[] args, boolean watch) throws InvocationTargetException, 
IllegalAccessException {
+        long start = 0, duration = 0;
+        if (watch) {
+            start = System.nanoTime();
+        }
+        final Object result = mtd.invoke(instance, args);
+        if (watch) {
+            duration = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - 
start);
+        }
+        return new TimerWatcherResult(duration, result);
+    }
+
+    public static class TimerWatcherResult {
+        private final Object result;
+        private final long duration;
+
+        public TimerWatcherResult(long duration, Object result) {
+            this.duration = duration;
+            this.result = result;
+        }
+
+        public Object getResult() {
+            return result;
+        }
+
+        public long getDuration() {
+            return duration;
+        }
+    }
+}

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JuliLogStreamFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JuliLogStreamFactory.java?rev=1373628&r1=1373627&r2=1373628&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JuliLogStreamFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JuliLogStreamFactory.java
 Wed Aug 15 20:59:06 2012
@@ -130,6 +130,7 @@ public class JuliLogStreamFactory implem
                     || name.toLowerCase().contains("cxf")
                     || name.toLowerCase().contains("timer")
                     || name.startsWith("org.apache.")
+                    || name.startsWith("openjpa.")
                     || name.startsWith("net.sf.ehcache.")
                     || name.startsWith("org.quartz.")
                     || name.startsWith("org.hibernate.");


Reply via email to