tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject tomee git commit: TOMEE-2053 datasource override app support through ejb-jar.xml
Date Tue, 06 Jun 2017 14:25:46 GMT
Repository: tomee
Updated Branches:
  refs/heads/master 8625749df -> 68aef6ffe


TOMEE-2053 datasource override app support through ejb-jar.xml


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/68aef6ff
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/68aef6ff
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/68aef6ff

Branch: refs/heads/master
Commit: 68aef6ffe0b871f03c06c2427a2e935b1a502644
Parents: 8625749
Author: rmannibucau <rmannibucau@apache.org>
Authored: Tue Jun 6 16:25:35 2017 +0200
Committer: rmannibucau <rmannibucau@apache.org>
Committed: Tue Jun 6 16:25:35 2017 +0200

----------------------------------------------------------------------
 .../openejb/config/XmlDataSourceTest.java       | 80 ++++++++++++++++++++
 .../java/org/apache/openejb/jee/DataSource.java |  6 +-
 2 files changed, 84 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/68aef6ff/container/openejb-core/src/test/java/org/apache/openejb/config/XmlDataSourceTest.java
----------------------------------------------------------------------
diff --git a/container/openejb-core/src/test/java/org/apache/openejb/config/XmlDataSourceTest.java
b/container/openejb-core/src/test/java/org/apache/openejb/config/XmlDataSourceTest.java
new file mode 100644
index 0000000..41c7afa
--- /dev/null
+++ b/container/openejb-core/src/test/java/org/apache/openejb/config/XmlDataSourceTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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.config;
+
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.StatelessBean;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.testing.Module;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.annotation.Resource;
+import javax.annotation.sql.DataSourceDefinition;
+import javax.ejb.EJB;
+import javax.ejb.Stateless;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import static org.junit.Assert.assertEquals;
+
+@RunWith(ApplicationComposer.class)
+public class XmlDataSourceTest {
+    @EJB
+    private Bean bean;
+
+    @Resource(name = "java:comp/env/foo")
+    private DataSource ds;
+
+    @Test
+    public void run() throws SQLException {
+        bean.run();
+
+        /* TODO: support it? this is kind of weird as a usage with jpa etc, can break a lot
of things
+        try (final Connection c = ds.getConnection()) {
+            assertEquals("jdbc:hsqldb:mem:fake", c.getMetaData().getURL());
+        }
+        */
+    }
+
+    @Module
+    public EjbJar app() {
+        final EjbJar ejbJar = new EjbJar();
+        final StatelessBean statelessBean = ejbJar.addEnterpriseBean(new StatelessBean(Bean.class));
+        final org.apache.openejb.jee.DataSource ds = new org.apache.openejb.jee.DataSource();
+        ds.setName("java:comp/env/foo");
+        ds.setUrl("jdbc:hsqldb:mem:override");
+        ds.setClassName("org.hsqldb.jdbcDriver");
+        statelessBean.getDataSourceMap().put("foo", ds);
+        return ejbJar;
+    }
+
+    @Stateless
+    @DataSourceDefinition(name = "java:comp/env/foo", url = "jdbc:hsqldb:mem:fake", className
= "org.hsqldb.jdbcDriver")
+    public static class Bean {
+        @Resource(name = "java:comp/env/foo")
+        private DataSource ds;
+
+        public void run() throws SQLException {
+            try (final Connection c = ds.getConnection()) {
+                assertEquals("jdbc:hsqldb:mem:override", c.getMetaData().getURL());
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/68aef6ff/container/openejb-jee/src/main/java/org/apache/openejb/jee/DataSource.java
----------------------------------------------------------------------
diff --git a/container/openejb-jee/src/main/java/org/apache/openejb/jee/DataSource.java b/container/openejb-jee/src/main/java/org/apache/openejb/jee/DataSource.java
index 48b14f0..6dcd393 100644
--- a/container/openejb-jee/src/main/java/org/apache/openejb/jee/DataSource.java
+++ b/container/openejb-jee/src/main/java/org/apache/openejb/jee/DataSource.java
@@ -384,7 +384,9 @@ public class DataSource implements Keyable<String> {
     @Override
     public String getKey() {
         final String name = getName();
-        if (name == null || name.startsWith("java:")) return name;
-        return "java:comp/env/" + name;
+        if (name.startsWith("java:comp/env/")) {
+            return name.substring("java:comp/env/".length());
+        }
+        return name;
     }
 }


Mime
View raw message