Author: rmannibucau
Date: Sat Aug 11 16:25:07 2012
New Revision: 1371973

URL: http://svn.apache.org/viewvc?rev=1371973&view=rev
Log:
LogSql parameter for datasources (or global property openejb.jdbc.log) + 
resetting our custom logmanager at shutdown to avoid to loose logging between 
tests (tomee call reset when undeploying)

Added:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingCallableSqlStatement.java
    
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/LoggingSqlConnection.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlDataSource.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourceFactory.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/pool/DefaultDataSourceCreator.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JuliLogStreamFactory.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/LogCategory.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java?rev=1371973&r1=1371972&r2=1371973&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/OpenEjbContainer.java
 Sat Aug 11 16:25:07 2012
@@ -43,6 +43,7 @@ import org.apache.openejb.loader.SystemI
 import org.apache.openejb.spi.ContainerSystem;
 import org.apache.openejb.util.Exceptions;
 import org.apache.openejb.util.Join;
+import org.apache.openejb.util.JuliLogStreamFactory;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.OptionsLog;
@@ -64,6 +65,7 @@ import javax.servlet.ServletContext;
 import javax.servlet.http.HttpSession;
 import javax.validation.ValidationException;
 import java.io.File;
+import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.net.MalformedURLException;
 import java.net.URL;
@@ -76,6 +78,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
+import java.util.logging.LogManager;
 
 import static org.apache.openejb.cdi.ScopeHelper.startContexts;
 import static org.apache.openejb.cdi.ScopeHelper.stopContexts;
@@ -87,6 +90,23 @@ public class OpenEjbContainer extends EJ
 
     static {
         Core.warmup();
+
+        // if tomee embedded was ran we'll lost log otherwise
+        final String logManger = 
System.getProperty("java.util.logging.manager");
+        if (logManger != null) {
+            try {
+                
Thread.currentThread().getContextClassLoader().loadClass(logManger);
+            } catch (Exception ignored) {
+                final Field field;
+                try {
+                    field = LogManager.class.getDeclaredField("manager");
+                    field.setAccessible(true);
+                    field.set(null, new 
JuliLogStreamFactory.OpenEJBLogManager());
+                } catch (Exception ignore) {
+                    // ignore
+                }
+            }
+        }
     }
 
     public static final String OPENEJB_EMBEDDED_REMOTABLE = 
"openejb.embedded.remotable";

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java?rev=1371973&r1=1371972&r2=1371973&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/ConfigurationFactory.java
 Sat Aug 11 16:25:07 2012
@@ -152,7 +152,7 @@ public class ConfigurationFactory implem
                 SystemInstance.get().setComponent(DataSourceCreator.class, new 
DefaultDataSourceCreator());
             } else {
                 try {
-                    SystemInstance.get().setComponent(DataSourceCreator.class, 
DataSourceFactory.creator(creator));
+                    SystemInstance.get().setComponent(DataSourceCreator.class, 
DataSourceFactory.creator(creator, false));
                 } catch (Exception e) {
                     logger.error("can't load " + creator + " will use the 
default creator", e);
                     SystemInstance.get().setComponent(DataSourceCreator.class, 
new DefaultDataSourceCreator());

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourceFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourceFactory.java?rev=1371973&r1=1371972&r2=1371973&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourceFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourceFactory.java
 Sat Aug 11 16:25:07 2012
@@ -19,7 +19,10 @@ package org.apache.openejb.resource.jdbc
 import org.apache.openejb.loader.IO;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.resource.XAResourceWrapper;
+import org.apache.openejb.resource.jdbc.dbcp.DbcpDataSourceCreator;
+import org.apache.openejb.resource.jdbc.logging.LoggingSqlDataSource;
 import org.apache.openejb.resource.jdbc.pool.DataSourceCreator;
+import org.apache.openejb.resource.jdbc.pool.DefaultDataSourceCreator;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 import org.apache.xbean.recipe.ObjectRecipe;
@@ -27,6 +30,8 @@ import org.apache.xbean.recipe.Option;
 
 import javax.sql.DataSource;
 import java.io.IOException;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -38,6 +43,8 @@ import java.util.TreeMap;
 public class DataSourceFactory {
     private static final Logger LOGGER = 
Logger.getInstance(LogCategory.OPENEJB, DataSourceFactory.class);
 
+    public static final String LOG_SQL_PROPERTY = "LogSql";
+    public static final String GLOBAL_LOG_SQL_PROPERTY = "openejb.jdbc.log";
     public static final String POOL_PROPERTY = "openejb.datasource.pool";
     public static final String DATA_SOURCE_CREATOR_PROP = "DataSourceCreator";
 
@@ -62,10 +69,12 @@ public class DataSourceFactory {
             managed = Boolean.parseBoolean((String) 
properties.remove("transactional")) || managed;
         }
 
-        final DataSourceCreator creator = 
creator(properties.remove(DATA_SOURCE_CREATOR_PROP));
+        boolean logSql = 
SystemInstance.get().getOptions().get(GLOBAL_LOG_SQL_PROPERTY,
+                "true".equalsIgnoreCase((String) 
properties.remove(LOG_SQL_PROPERTY)));
+        final DataSourceCreator creator = 
creator(properties.remove(DATA_SOURCE_CREATOR_PROP), logSql);
 
 
-        final DataSource ds;
+        DataSource ds;
         if (createDataSourceFromClass(impl)) { // opposed to "by driver"
             trimNotSupportedDataSourceProperties(properties);
 
@@ -78,13 +87,13 @@ public class DataSourceFactory {
             final DataSource dataSource = (DataSource) recipe.create();
 
             if (managed) {
-                if (useDbcp(properties)) {
+                if (usePool(properties)) {
                     ds = creator.poolManaged(name, dataSource, properties);
                 } else {
                     ds = creator.managed(name, dataSource);
                 }
             } else {
-                if (useDbcp(properties)) {
+                if (usePool(properties)) {
                     ds = creator.pool(name, dataSource, properties);
                 } else {
                     ds = dataSource;
@@ -103,20 +112,38 @@ public class DataSourceFactory {
             }
         }
 
+        // ds and creator are associated here, not after the proxying of the 
next if if active
         creatorByDataSource.put(ds, creator);
+
+        if (logSql) {
+            ds = (DataSource) 
Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
+                    new Class<?>[] { DataSource.class }, new 
LoggingSqlDataSource(ds));
+        }
+
         return ds;
     }
 
-    public static DataSourceCreator creator(final Object creatorName) {
+    public static DataSourceCreator creator(final Object creatorName, boolean 
willBeProxied) {
         final DataSourceCreator defaultCreator = 
SystemInstance.get().getComponent(DataSourceCreator.class);
+        final ClassLoader loader = 
Thread.currentThread().getContextClassLoader();
         if (creatorName != null && creatorName instanceof String
                 && (defaultCreator == null || 
!creatorName.equals(defaultCreator.getClass().getName()))) {
             String clazz = KNOWN_CREATORS.get(creatorName);
             if (clazz == null) {
                 clazz = (String) creatorName;
             }
+            if (willBeProxied && 
clazz.equals(DefaultDataSourceCreator.class.getName())) {
+                clazz = DbcpDataSourceCreator.class.getName();
+            }
             try {
-                return (DataSourceCreator) 
Thread.currentThread().getContextClassLoader().loadClass(clazz).newInstance();
+                return (DataSourceCreator) 
loader.loadClass(clazz).newInstance();
+            } catch (Throwable e) {
+                LOGGER.error("can't create '" + creatorName + "', the default 
one will be used: " + defaultCreator, e);
+            }
+        }
+        if (defaultCreator instanceof DefaultDataSourceCreator && 
willBeProxied) {
+            try { // this one is proxiable, not the default one (legacy)
+                return (DataSourceCreator) 
loader.loadClass(DbcpDataSourceCreator.class.getName()).newInstance();
             } catch (Throwable e) {
                 LOGGER.error("can't create '" + creatorName + "', the default 
one will be used: " + defaultCreator, e);
             }
@@ -128,7 +155,7 @@ public class DataSourceFactory {
         return DataSource.class.isAssignableFrom(impl) && 
!SystemInstance.get().getOptions().get("org.apache.openejb.resource.jdbc.hot.deploy",
 false);
     }
 
-    private static boolean useDbcp(final Properties properties) {
+    private static boolean usePool(final Properties properties) {
         return "true".equalsIgnoreCase(properties.getProperty(POOL_PROPERTY, 
"true"));
     }
 
@@ -145,7 +172,8 @@ public class DataSourceFactory {
     }
 
     // TODO: should we get a get and a clear method instead of a single one?
-    public static ObjectRecipe forgetRecipe(final Object object, final 
ObjectRecipe defaultValue) {
+    public static ObjectRecipe forgetRecipe(final Object rawObject, final 
ObjectRecipe defaultValue) {
+        final Object object = realInstance(rawObject);
         final DataSourceCreator creator = creatorByDataSource.get(object);
         ObjectRecipe recipe = null;
         if (creator != null) {
@@ -158,6 +186,23 @@ public class DataSourceFactory {
     }
 
     public static void destroy(final Object o) throws Throwable {
-        creatorByDataSource.remove(o).destroy(o);
+        final Object instance = realInstance(o);
+        creatorByDataSource.remove(instance).destroy(instance);
+    }
+
+    // remove proxy added by us in front of the datasource returned by the 
creator
+    private static Object realInstance(final Object o) {
+        if (o == null || !(o instanceof DataSource)) {
+            return o;
+        }
+
+        if (Proxy.isProxyClass(o.getClass())) {
+            final InvocationHandler handler = Proxy.getInvocationHandler(o);
+            if (handler instanceof LoggingSqlDataSource) {
+                return ((LoggingSqlDataSource) handler).getDelegate();
+            }
+        }
+
+        return o;
     }
 }

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingCallableSqlStatement.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingCallableSqlStatement.java?rev=1371973&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingCallableSqlStatement.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingCallableSqlStatement.java
 Sat Aug 11 16:25:07 2012
@@ -0,0 +1,27 @@
+/*
+ * 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.sql.PreparedStatement;
+
+public class LoggingCallableSqlStatement extends LoggingPreparedSqlStatement {
+    public LoggingCallableSqlStatement(final PreparedStatement result, final 
String query) {
+        super(result, query);
+    }
+
+    // TODO: manage in/out parameters
+}

Added: 
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=1371973&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingPreparedSqlStatement.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingPreparedSqlStatement.java
 Sat Aug 11 16:25:07 2012
@@ -0,0 +1,90 @@
+/*
+ * 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 org.apache.openejb.util.LogCategory;
+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 {
+    private static final Logger LOGGER = 
Logger.getInstance(LogCategory.OPENEJB_SQL, LoggingPreparedSqlStatement.class);
+
+    private final PreparedStatement delegate;
+    private final String sql;
+    private final List<Parameter> parameters = new ArrayList<Parameter>();
+
+    public LoggingPreparedSqlStatement(final PreparedStatement result, final 
String query) {
+        delegate = result;
+        sql= query;
+    }
+
+    @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("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")) {
+            String str = sql;
+            if (str.contains("?")) {
+                Collections.sort(parameters);
+                final Iterator<Parameter> it = parameters.iterator();
+                while (it.hasNext()) {
+                    final Parameter param = it.next();
+                    try {
+                        str = str.replaceFirst("\\?", param.value.toString());
+                    } catch (Exception e) {
+                        str = str.replaceFirst("\\?", 
param.value.getClass().getName());
+                    }
+                }
+            }
+            LOGGER.info(str);
+        }
+        return result;
+    }
+
+    protected static class Parameter implements Comparable<Parameter> {
+        private String type;
+        private int key;
+        private Object value;
+
+        public Parameter(String type, int key, Object value) {
+            this.type = type;
+            this.key = key;
+            this.value = value;
+        }
+
+        @Override
+        public int compareTo(final Parameter o) {
+            return key - o.key;
+        }
+
+        @Override
+        public String toString() {
+            return value + " (" + type + ")";
+        }
+    }
+}

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlConnection.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlConnection.java?rev=1371973&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlConnection.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlConnection.java
 Sat Aug 11 16:25:07 2012
@@ -0,0 +1,61 @@
+/*
+ * 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.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.Statement;
+
+public class LoggingSqlConnection implements InvocationHandler {
+    private static final Class<?>[] INTERFACES_STATEMENT = new Class<?>[] { 
Statement.class };
+    private static final Class<?>[] INTERFACES_PREPARED = new Class<?>[] { 
PreparedStatement.class };
+    private static final Class<?>[] INTERFACES_CALLABLE = new Class<?>[] { 
CallableStatement.class };
+
+    private final Connection delegate;
+
+    public LoggingSqlConnection(final Connection connection) {
+        delegate = connection;
+    }
+
+    @Override
+    public Object invoke(final Object proxy, final Method method, final 
Object[] args) throws Throwable {
+        final Object result = method.invoke(delegate, args);
+
+        final String mtd = method.getName();
+
+        if ("createStatement".equals(mtd)) {
+            return 
Proxy.newProxyInstance(delegate.getClass().getClassLoader(), 
INTERFACES_STATEMENT,
+                    new LoggingSqlStatement((Statement) result));
+        }
+
+        if ("prepareStatement".equals(mtd)) {
+            return 
Proxy.newProxyInstance(delegate.getClass().getClassLoader(), 
INTERFACES_PREPARED,
+                    new LoggingPreparedSqlStatement((PreparedStatement) 
result, (String) args[0]));
+        }
+
+        if ("prepareCall".equals(mtd)) {
+            return 
Proxy.newProxyInstance(delegate.getClass().getClassLoader(), 
INTERFACES_CALLABLE,
+                    new LoggingCallableSqlStatement((CallableStatement) 
result, (String) args[0]));
+        }
+
+        return result;
+    }
+}

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlDataSource.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlDataSource.java?rev=1371973&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlDataSource.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlDataSource.java
 Sat Aug 11 16:25:07 2012
@@ -0,0 +1,47 @@
+/*
+ * 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 javax.sql.DataSource;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.sql.Connection;
+
+public class LoggingSqlDataSource implements InvocationHandler {
+    private static final Class<?>[] INTERFACES = new Class<?>[]{ 
Connection.class };
+
+    private DataSource delegate;
+
+    public LoggingSqlDataSource(final DataSource ds) {
+        delegate = ds;
+    }
+
+    @Override
+    public Object invoke(final Object proxy, final Method method, final 
Object[] args) throws Throwable {
+        final Object result = method.invoke(delegate, args);
+        if ("getConnection".equals(method.getName())) {
+            return Proxy.newProxyInstance(delegate.getClass().getClassLoader(),
+                    INTERFACES, new LoggingSqlConnection((Connection) result));
+        }
+        return result;
+    }
+
+    public DataSource getDelegate() {
+        return delegate;
+    }
+}

Added: 
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=1371973&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/logging/LoggingSqlStatement.java
 Sat Aug 11 16:25:07 2012
@@ -0,0 +1,44 @@
+/*
+ * 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 org.apache.openejb.util.LogCategory;
+import org.apache.openejb.util.Logger;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.sql.Statement;
+
+public class LoggingSqlStatement implements InvocationHandler {
+    private static final Logger LOGGER = 
Logger.getInstance(LogCategory.OPENEJB_SQL, LoggingSqlStatement.class);
+
+    private final Statement delegate;
+
+    public LoggingSqlStatement(final Statement result) {
+        delegate = result;
+    }
+
+    @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]);
+        }
+        return result;
+    }
+}

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/pool/DefaultDataSourceCreator.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/pool/DefaultDataSourceCreator.java?rev=1371973&r1=1371972&r2=1371973&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/pool/DefaultDataSourceCreator.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/pool/DefaultDataSourceCreator.java
 Sat Aug 11 16:25:07 2012
@@ -20,6 +20,7 @@ import org.apache.openejb.resource.XARes
 import org.apache.openejb.resource.jdbc.dbcp.BasicDataSource;
 import org.apache.openejb.resource.jdbc.dbcp.BasicManagedDataSource;
 import org.apache.openejb.resource.jdbc.dbcp.DbcpDataSource;
+import org.apache.openejb.resource.jdbc.dbcp.DbcpDataSourceCreator;
 import org.apache.openejb.resource.jdbc.dbcp.DbcpManagedDataSource;
 import org.apache.openejb.resource.jdbc.dbcp.ManagedDataSourceWithRecovery;
 import org.apache.xbean.recipe.ObjectRecipe;
@@ -27,7 +28,8 @@ import org.apache.xbean.recipe.ObjectRec
 import javax.sql.DataSource;
 import java.util.Properties;
 
-public class DefaultDataSourceCreator implements DataSourceCreator {
+// TODO: remove it and replace it with 
org.apache.openejb.resource.jdbc.dbcp.DbcpDataSourceCreator
+public class DefaultDataSourceCreator extends DbcpDataSourceCreator {
     @Override
     public DataSource managed(final String name, final DataSource ds) {
         return new DbcpManagedDataSource(name, ds);
@@ -70,6 +72,11 @@ public class DefaultDataSourceCreator im
     }
 
     @Override
+    protected void doDestroy(final DataSource dataSource) throws Throwable {
+        ((org.apache.commons.dbcp.BasicDataSource) dataSource).close();
+    }
+
+    @Override
     public ObjectRecipe clearRecipe(final Object object) {
         return null; // no recipe here
     }

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=1371973&r1=1371972&r2=1371973&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
 Sat Aug 11 16:25:07 2012
@@ -67,7 +67,29 @@ public class JuliLogStreamFactory implem
         return !System.getProperty("java.class.path").contains("idea_rt"); // 
TODO: eclipse, netbeans
     }
 
+    // TODO: mange conf by classloader? see tomcat log manager
     public static class OpenEJBLogManager extends LogManager {
+        static {
+            final LogManager mgr = LogManager.getLogManager();
+            if (mgr instanceof OpenEJBLogManager) {
+                Runtime.getRuntime().addShutdownHook(new Thread() {
+                    @Override
+                    public void run() {
+                        ((OpenEJBLogManager) mgr).forceReset();
+                    }
+                });
+            }
+        }
+
+        public void forceReset() {
+            super.reset();
+        }
+
+        @Override
+        public void reset() throws SecurityException {
+            // no-op
+        }
+
         @Override
         public String getProperty(final String name) {
             final String parentValue = super.getProperty(name);

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/LogCategory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/LogCategory.java?rev=1371973&r1=1371972&r2=1371973&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/LogCategory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/LogCategory.java
 Sat Aug 11 16:25:07 2012
@@ -57,6 +57,7 @@ public final class LogCategory {
        public static final LogCategory TIMER = new LogCategory( "Timer");
        public static final LogCategory HTTPSERVER = 
OPENEJB_SERVER.createChild("http");
        public static final LogCategory SERVICEPOOL = 
OPENEJB_SERVER.createChild("pool");
+    public static final LogCategory OPENEJB_SQL = OPENEJB.createChild("sql");
 
     private LogCategory(String name){
                this.name = name;


Reply via email to