You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2013/04/30 09:05:57 UTC

svn commit: r1477487 - /tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/TomEEDataSourceRealm.java

Author: rmannibucau
Date: Tue Apr 30 07:05:57 2013
New Revision: 1477487

URL: http://svn.apache.org/r1477487
Log:
TOMEE-263 fixing the context in which the datasources are looked up in TomEEDataSourceRealm

Modified:
    tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/TomEEDataSourceRealm.java

Modified: tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/TomEEDataSourceRealm.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/TomEEDataSourceRealm.java?rev=1477487&r1=1477486&r2=1477487&view=diff
==============================================================================
--- tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/TomEEDataSourceRealm.java (original)
+++ tomee/tomee/trunk/tomee/tomee-catalina/src/main/java/org/apache/tomee/catalina/realm/TomEEDataSourceRealm.java Tue Apr 30 07:05:57 2013
@@ -21,6 +21,7 @@ import org.apache.naming.ContextBindings
 import org.apache.openejb.config.AutoConfig;
 import org.apache.openejb.config.ConfigurationFactory;
 import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
@@ -31,6 +32,8 @@ import java.util.Collection;
 import java.util.Properties;
 
 public class TomEEDataSourceRealm extends DataSourceRealm {
+    private static final String OPENEJB_RESOURCE_PREFIX = "java:openejb/Resource/";
+
     private String fullName = null;
 
     @Override
@@ -38,26 +41,30 @@ public class TomEEDataSourceRealm extend
         try { // parent behavior
             final Context context;
             if (fullName != null) {
-                context = ContextBindings.getClassLoader();
-            } else if (localDataSource) {
+                return DataSource.class.cast(openejbContext().lookup(fullName)).getConnection();
+            }
+
+            // parent behavior
+            if (localDataSource) {
                 context = Context.class.cast(ContextBindings.getClassLoader().lookup("comp/env"));
             } else {
                 context = getServer().getGlobalNamingContext();
             }
             return DataSource.class.cast(context.lookup(dataSourceName)).getConnection();
-        } catch (final SQLException e) { // user set full name
+        } catch (final Exception e) {
             try { // try globally
                 return getConnection(dataSourceName);
-            } catch (final SQLException e2) {
+            } catch (final Exception e2) {
                 try { // try globally but with custom subspace
-                    return getConnection("java:openejb/Resource/" + dataSourceName);
+                    return getConnection(OPENEJB_RESOURCE_PREFIX + dataSourceName);
                 } catch (final Exception e3) {
-                    final Collection<String> ids = SystemInstance.get().getComponent(ConfigurationFactory.class).getResourceIds(DataSource.class.getName(), new Properties());
+                    // try to guess the real name
+                    final Collection<String> ids = new ConfigurationFactory(true).getResourceIds(DataSource.class.getName(), new Properties());
                     final String id = AutoConfig.findResourceId(ids, dataSourceName);
 
                     if (id != null) {
-                        try { // try globally but with custom subspace
-                            return getConnection("java:openejb/Resource/" + id);
+                        try {
+                            return getConnection(OPENEJB_RESOURCE_PREFIX + id);
                         } catch (final Exception e4) {
                             containerLog.error(sm.getString("dataSourceRealm.exception"), e);
                         }
@@ -65,18 +72,18 @@ public class TomEEDataSourceRealm extend
                         containerLog.error(sm.getString("dataSourceRealm.exception"), e);
                     }
                 }
-            } catch (final Exception e2) {
-                containerLog.error(sm.getString("dataSourceRealm.exception"), e);
             }
-        } catch (final Exception e) {
-            containerLog.error(sm.getString("dataSourceRealm.exception"), e);
         }
         return null;
     }
 
     private Connection getConnection(final String name) throws SQLException, NamingException {
-        final Connection c = DataSource.class.cast(Context.class.cast(ContextBindings.getClassLoader()).lookup(name)).getConnection();
+        final Connection c = DataSource.class.cast(openejbContext().lookup(name)).getConnection();
         fullName = name; // if here, the default name needed to be changed to match requested resource so update it for next calls
         return c;
     }
+
+    private static Context openejbContext() {
+        return SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext();
+    }
 }