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 2012/03/06 10:11:00 UTC

svn commit: r1297390 - in /openejb/trunk/openejb/examples: ./ spring-data-proxy/ spring-data-proxy/src/ spring-data-proxy/src/main/ spring-data-proxy/src/main/java/ spring-data-proxy/src/main/java/org/ spring-data-proxy/src/main/java/org/superbiz/ spri...

Author: rmannibucau
Date: Tue Mar  6 09:10:59 2012
New Revision: 1297390

URL: http://svn.apache.org/viewvc?rev=1297390&view=rev
Log:
adding spring-data-proxy sample

Added:
    openejb/trunk/openejb/examples/spring-data-proxy/
    openejb/trunk/openejb/examples/spring-data-proxy/pom.xml
    openejb/trunk/openejb/examples/spring-data-proxy/src/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/SpringDataProxy.java
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/User.java
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/resources/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/resources/META-INF/
    openejb/trunk/openejb/examples/spring-data-proxy/src/main/resources/META-INF/persistence.xml
    openejb/trunk/openejb/examples/spring-data-proxy/src/test/
    openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/
    openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/
    openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/superbiz/
    openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/superbiz/dynamic/
    openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
Modified:
    openejb/trunk/openejb/examples/pom.xml

Modified: openejb/trunk/openejb/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/pom.xml?rev=1297390&r1=1297389&r2=1297390&view=diff
==============================================================================
--- openejb/trunk/openejb/examples/pom.xml (original)
+++ openejb/trunk/openejb/examples/pom.xml Tue Mar  6 09:10:59 2012
@@ -86,6 +86,7 @@
     <module>simple-stateless-with-descriptor</module>
     <module>simple-webservice</module>
     <module>spring-integration</module>
+    <module>spring-data-proxy</module>
     <module>telephone-stateful</module>
     <module>testcase-injection</module>
     <module>testing-security-2</module>

Added: openejb/trunk/openejb/examples/spring-data-proxy/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy/pom.xml?rev=1297390&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy/pom.xml (added)
+++ openejb/trunk/openejb/examples/spring-data-proxy/pom.xml Tue Mar  6 09:10:59 2012
@@ -0,0 +1,93 @@
+<?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/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.superbiz</groupId>
+  <artifactId>dynamic-dao-implementation</artifactId>
+  <packaging>jar</packaging>
+  <version>1.1-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: Dynamic DAO Implementation</name>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+  <build>
+    <defaultGoal>install</defaultGoal>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.3.2</version>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <repositories>
+    <repository>
+      <id>apache-m2-snapshot</id>
+      <name>Apache Snapshot Repository</name>
+      <url>http://repository.apache.org/snapshots</url>
+    </repository>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>javaee-api</artifactId>
+      <version>6.0-3</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.data</groupId>
+      <artifactId>spring-data-jpa</artifactId>
+      <version>1.0.3.RELEASE</version>
+    </dependency>
+
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>4.0.0-beta-3-SNAPSHOT</version>
+    </dependency>
+  </dependencies>
+
+  <!--
+  This section allows you to configure where to publish libraries for sharing.
+  It is not required and may be deleted.  For more information see:
+  http://maven.apache.org/plugins/maven-deploy-plugin/
+  -->
+  <distributionManagement>
+    <repository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/repo/</url>
+    </repository>
+    <snapshotRepository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/snapshot-repo/</url>
+    </snapshotRepository>
+  </distributionManagement>
+
+</project>
+

Added: openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/SpringDataProxy.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/SpringDataProxy.java?rev=1297390&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/SpringDataProxy.java (added)
+++ openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/SpringDataProxy.java Tue Mar  6 09:10:59 2012
@@ -0,0 +1,31 @@
+package org.superbiz.dynamic;
+
+import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
+import org.springframework.data.repository.Repository;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicReference;
+
+public class SpringDataProxy implements InvocationHandler {
+    @PersistenceContext
+    private EntityManager em;
+
+    private Class<Repository<?, ?>> implementingInterfaceClass; // implicitly for this kind of proxy
+
+    private final AtomicReference<Repository<?, ?>> repository = new AtomicReference<Repository<?, ?>>();
+
+    @Override
+    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+        if (repository.get() == null) {
+            synchronized (this) {
+                if (repository.get() == null) {
+                    repository.set(new JpaRepositoryFactory(em).getRepository(implementingInterfaceClass));
+                }
+            }
+        }
+        return method.invoke(repository.get(), args);
+    }
+}

Added: openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/User.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/User.java?rev=1297390&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/User.java (added)
+++ openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/User.java Tue Mar  6 09:10:59 2012
@@ -0,0 +1,54 @@
+/*
+ * 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.superbiz.dynamic;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class User {
+    @Id
+    @GeneratedValue
+    private long id;
+    private String name;
+    private int age;
+
+    public long getId() {
+        return id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+}

Added: openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java?rev=1297390&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java (added)
+++ openejb/trunk/openejb/examples/spring-data-proxy/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java Tue Mar  6 09:10:59 2012
@@ -0,0 +1,32 @@
+/*
+ * 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.superbiz.dynamic;
+
+
+import org.apache.openejb.api.Proxy;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.repository.query.Param;
+
+import javax.ejb.Stateless;
+import javax.persistence.PersistenceContext;
+
+@Stateless
+@Proxy(SpringDataProxy.class)
+@PersistenceContext(name = "dynamic")
+public interface UserSpringDataDao extends JpaRepository<User, Long> {
+    User findByName(@Param("name") final String name);
+}

Added: openejb/trunk/openejb/examples/spring-data-proxy/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy/src/main/resources/META-INF/persistence.xml?rev=1297390&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy/src/main/resources/META-INF/persistence.xml (added)
+++ openejb/trunk/openejb/examples/spring-data-proxy/src/main/resources/META-INF/persistence.xml Tue Mar  6 09:10:59 2012
@@ -0,0 +1,32 @@
+<?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.
+-->
+<persistence version="2.0"
+             xmlns="http://java.sun.com/xml/ns/persistence"
+             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+             xsi:schemaLocation="
+      http://java.sun.com/xml/ns/persistence
+      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
+  <persistence-unit name="dynamic" transaction-type="JTA">
+    <jta-data-source>jdbc/dynamicDB</jta-data-source>
+    <class>org.superbiz.dynamic.User</class>
+    <properties>
+      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+    </properties>
+  </persistence-unit>
+</persistence>

Added: openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java?rev=1297390&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java (added)
+++ openejb/trunk/openejb/examples/spring-data-proxy/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java Tue Mar  6 09:10:59 2012
@@ -0,0 +1,92 @@
+/*
+ * 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.superbiz.dynamic;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.inject.Inject;
+import javax.naming.NamingException;
+import java.util.Collection;
+import java.util.Properties;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+
+public class DynamicUserDaoTest {
+    private static EJBContainer container;
+    private static boolean initialized = false;
+
+    @Inject
+    private UserSpringDataDao dao;
+
+    @Test
+    public void findAll() {
+        Collection<User> users = dao.findAll();
+        assertEquals(10, users.size());
+    }
+
+    @Test
+    public void findByName() {
+        User user = dao.findByName("bar-1");
+        assertNotNull(user);
+        assertEquals("bar-1", user.getName());
+    }
+
+    @BeforeClass
+    public static void start() throws Exception {
+        final Properties p = new Properties();
+
+        p.setProperty("openejb.deployments.classpath.include", "spring-data-proxy");
+        p.setProperty("openejb.exclude-include.order", "exclude-include");
+
+        p.setProperty("jdbc/DynamicUserDaoTest", "new://Resource?type=DataSource");
+        p.setProperty("jdbc/DynamicUserDaoTest", "new://Resource?type=DataSource");
+        p.setProperty("jdbc/DynamicUserDaoTest.JdbcDriver", "org.hsqldb.jdbcDriver");
+        p.setProperty("jdbc/DynamicUserDaoTest.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+        p.setProperty("jdbc/DynamicUserDaoTest.UserName", "sa");
+        p.setProperty("jdbc/DynamicUserDaoTest.Password", "");
+
+        container = EJBContainer.createEJBContainer(p);
+    }
+
+    @Before
+    public void injectAndInit() throws NamingException {
+        container.getContext().bind("inject", this);
+        if (!initialized) {
+            for (int i = 0; i < 10; i++) {
+                final User u = new User();
+                u.setAge(i % 4);
+                if (i % 3 == 0) {
+                    u.setName("foo");
+                } else {
+                    u.setName("bar-" + i);
+                }
+                dao.save(u);
+            }
+            initialized = true;
+        }
+    }
+
+    @AfterClass
+    public static void close() {
+        container.close();
+    }
+}