tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1297652 - in /openejb/trunk/openejb: container/openejb-core/src/main/java/org/apache/openejb/ container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ container/openejb-core/src/main/java/org/apache/openejb/config/ contai...
Date Tue, 06 Mar 2012 19:00:28 GMT
Author: rmannibucau
Date: Tue Mar  6 19:00:27 2012
New Revision: 1297652

URL: http://svn.apache.org/viewvc?rev=1297652&view=rev
Log:
another spring-data example with openejb meta, more work to make dynamic beans more integrated
with CDI

Added:
    openejb/trunk/openejb/examples/spring-data-proxy-meta/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/pom.xml
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/User.java
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/Metatype.java
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/SpringRepository.java
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/framework/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/framework/SpringDataProxy.java
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/resources/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/resources/META-INF/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/resources/META-INF/persistence.xml
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/org/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/org/superbiz/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/org/superbiz/dynamic/
    openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckClasses.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
    openejb/trunk/openejb/examples/pom.xml

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java?rev=1297652&r1=1297651&r2=1297652&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/BeanContext.java
Tue Mar  6 19:00:27 2012
@@ -85,7 +85,7 @@ public class BeanContext extends Deploym
     public static final String USER_INTERCEPTOR_SEPARATOR = ",| |;";
 
     public boolean isDynamicallyImplemented() {
-        return getBeanClass().equals(getLocalInterface());
+        return getBeanClass().equals(getLocalInterface()); // faster than utility method
in util.proxy
     }
 
     public interface BusinessLocalHome extends javax.ejb.EJBLocalHome {

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java?rev=1297652&r1=1297651&r2=1297652&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanBuilder.java
Tue Mar  6 19:00:27 2012
@@ -25,11 +25,11 @@ import org.apache.openejb.api.Proxy;
 import org.apache.openejb.core.cmp.CmpUtil;
 import org.apache.openejb.loader.SystemInstance;
 import org.apache.openejb.spi.ContainerSystem;
-import org.apache.openejb.util.AnnotationUtil;
 import org.apache.openejb.util.Duration;
 import org.apache.openejb.util.Index;
 import org.apache.openejb.util.Messages;
 import org.apache.openejb.util.SafeToolkit;
+import org.apache.xbean.finder.MetaAnnotatedClass;
 
 import javax.ejb.TimedObject;
 import javax.ejb.Timer;
@@ -94,7 +94,7 @@ class EnterpriseBeanBuilder {
         Class<?> proxy = null;
         if (ejbClass.isInterface()) { // dynamic proxy implementation
             local = ejbClass;
-            Proxy proxyAnnotation = AnnotationUtil.getAnnotation(Proxy.class, ejbClass);
+            Proxy proxyAnnotation = new MetaAnnotatedClass<Class<?>>(ejbClass).getAnnotation(Proxy.class);
             if (proxyAnnotation != null) {
                 proxy = proxyAnnotation.value();
             }

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=1297652&r1=1297651&r2=1297652&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
Tue Mar  6 19:00:27 2012
@@ -112,10 +112,10 @@ import org.apache.openejb.jee.WebApp;
 import org.apache.openejb.jee.WebserviceDescription;
 import org.apache.openejb.jee.oejb3.OpenejbJar;
 import org.apache.openejb.loader.SystemInstance;
-import org.apache.openejb.util.AnnotationUtil;
 import org.apache.openejb.util.Join;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
+import org.apache.openejb.util.proxy.DynamicProxyImplFactory;
 import org.apache.xbean.finder.Annotated;
 import org.apache.xbean.finder.AnnotationFinder;
 import org.apache.xbean.finder.IAnnotationFinder;
@@ -1420,8 +1420,8 @@ public class AnnotationDeployer implemen
             }
 
             // not a dynamic proxy implemented bean
-            if (beanClass.get().getAnnotation(PersistenceContext.class) == null
-                    && beanClass.get().getAnnotation(Proxy.class) == null
+            if (beanClass.getAnnotation(PersistenceContext.class) == null
+                    && beanClass.getAnnotation(Proxy.class) == null
                     && beanClass.get().isInterface()) {
                 ejbModule.getValidation().fail(ejbName, "interfaceAnnotatedAsBean", annotationClass.getSimpleName(),
beanClass.get().getName());
                 return false;
@@ -1901,6 +1901,8 @@ public class AnnotationDeployer implemen
                     continue;
                 }
 
+                boolean dynamicBean = DynamicProxyImplFactory.isKnownDynamicallyImplemented(clazz);
+                final MetaAnnotatedClass<?> metaClass = new MetaAnnotatedClass(clazz);
 
                 final AnnotationFinder finder;
                 final AnnotationFinder annotationFinder;
@@ -1917,8 +1919,14 @@ public class AnnotationDeployer implemen
                     annotationFinder = af.select(names);
                     finder = af.select(clazz.getName());
                 } else {
-                    annotationFinder = createFinder(clazz);
-                    finder = new AnnotationFinder(new ClassesArchive(clazz));
+                    if (!dynamicBean) {
+                        annotationFinder = createFinder(clazz);
+                        finder = new AnnotationFinder(new ClassesArchive(clazz));
+                    } else {
+                        final Class<?>[] classes = new Class<?>[] { clazz, metaClass.getAnnotation(Proxy.class).value()
};
+                        annotationFinder = createFinder(classes);
+                        finder = new AnnotationFinder(new ClassesArchive(classes));
+                    }
                 }
 
                 /*
@@ -2735,9 +2743,7 @@ public class AnnotationDeployer implemen
                         && all.remote.isEmpty()
                         ) {
 
-                    if (interfaces.size() == 0 ||
-                        (AnnotationUtil.getAnnotation(PersistenceContext.class, clazz) !=
null
-                            && AnnotationUtil.getAnnotation(Proxy.class, clazz) !=
null)) {
+                    if (interfaces.size() == 0 || DynamicProxyImplFactory.isKnownDynamicallyImplemented(clazz))
{
                         // No interfaces?  Then @LocalBean
 
                         sessionBean.setLocalBean(new Empty());
@@ -3460,7 +3466,7 @@ public class AnnotationDeployer implemen
             }
 
             boolean localbean = isKnownLocalBean(interfce);
-            boolean dynamicallyImplemented = isKnownDynamicallyImplemented(interfce);
+            boolean dynamicallyImplemented = DynamicProxyImplFactory.isKnownDynamicallyImplemented(interfce);
 
             if ((!localbean) && interfce != null && !isValidEjbInterface(name,
interfce, ejbRef.getName())) {
                 return;
@@ -3565,12 +3571,6 @@ public class AnnotationDeployer implemen
             return "java:comp/env/" + refName;
         }
 
-        private boolean isKnownDynamicallyImplemented(Class<?> clazz) {
-            return clazz.isInterface()
-                && (AnnotationUtil.getAnnotation(PersistenceContext.class, clazz)
!= null
-                    || AnnotationUtil.getAnnotation(Proxy.class, clazz) != null);
-        }
-
         private boolean isKnownLocalBean(Class clazz) {
             if (clazz.isAnnotation()) return false;
             if (clazz.isArray()) return false;

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckClasses.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckClasses.java?rev=1297652&r1=1297651&r2=1297652&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckClasses.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckClasses.java
Tue Mar  6 19:00:27 2012
@@ -18,7 +18,6 @@ package org.apache.openejb.config.rules;
 
 import org.apache.openejb.OpenEJBException;
 import org.apache.openejb.OpenEJBRuntimeException;
-import org.apache.openejb.api.Proxy;
 import org.apache.openejb.config.EjbModule;
 import org.apache.openejb.jee.EnterpriseBean;
 import org.apache.openejb.jee.EntityBean;
@@ -27,6 +26,7 @@ import org.apache.openejb.jee.RemoteBean
 import org.apache.openejb.jee.SessionBean;
 import org.apache.openejb.util.SafeToolkit;
 import org.apache.openejb.util.Strings;
+import org.apache.openejb.util.proxy.DynamicProxyImplFactory;
 import org.apache.xbean.finder.ClassFinder;
 
 import javax.ejb.EJBHome;
@@ -36,7 +36,6 @@ import javax.ejb.EJBObject;
 import javax.ejb.Local;
 import javax.ejb.Remote;
 import javax.jws.WebService;
-import javax.persistence.PersistenceContext;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
@@ -216,8 +215,7 @@ public class CheckClasses extends Valida
         String ejbName = b.getEjbName();
 
         Class<?> beanClass = lookForClass(b.getEjbClass(), "ejb-class", ejbName);
-        boolean isDynamicProxyImpl = beanClass.getAnnotation(PersistenceContext.class) !=
null
-            || beanClass.getAnnotation(Proxy.class) != null;
+        boolean isDynamicProxyImpl = DynamicProxyImplFactory.isKnownDynamicallyImplemented(beanClass);
 
         if (beanClass == null) return null;
         

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java?rev=1297652&r1=1297651&r2=1297652&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
(original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/proxy/DynamicProxyImplFactory.java
Tue Mar  6 19:00:27 2012
@@ -19,16 +19,27 @@ package org.apache.openejb.util.proxy;
 
 import org.apache.openejb.BeanContext;
 import org.apache.openejb.OpenEJBRuntimeException;
+import org.apache.openejb.api.Proxy;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
+import org.apache.xbean.finder.Annotated;
+import org.apache.xbean.finder.MetaAnnotatedClass;
 
 import javax.naming.NamingException;
 import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
 import java.lang.reflect.Method;
 
 public class DynamicProxyImplFactory {
     private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB, BeanContext.class);
 
+    public static boolean isKnownDynamicallyImplemented(Class<?> clazz) {
+        final Annotated<Class<?>> metaClass = new MetaAnnotatedClass(clazz);
+        return clazz.isInterface()
+                && (metaClass.getAnnotation(PersistenceContext.class) != null
+                || metaClass.getAnnotation(Proxy.class) != null);
+    }
+
     public static Object newProxy(BeanContext context, java.lang.reflect.InvocationHandler
invocationHandler) {
         if (invocationHandler instanceof QueryProxy) {
             String emLookupName = context.getInjections().get(context.getInjections().size()
- 1).getJndiName();

Modified: openejb/trunk/openejb/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/pom.xml?rev=1297652&r1=1297651&r2=1297652&view=diff
==============================================================================
--- openejb/trunk/openejb/examples/pom.xml (original)
+++ openejb/trunk/openejb/examples/pom.xml Tue Mar  6 19:00:27 2012
@@ -87,6 +87,7 @@
     <module>simple-webservice</module>
     <module>spring-integration</module>
     <module>spring-data-proxy</module>
+    <module>spring-data-proxy-meta</module>
     <module>telephone-stateful</module>
     <module>testcase-injection</module>
     <module>testing-security-2</module>

Added: openejb/trunk/openejb/examples/spring-data-proxy-meta/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/pom.xml?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/pom.xml (added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/pom.xml Tue Mar  6 19:00:27 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>spring-data-proxy-meta</artifactId>
+  <packaging>jar</packaging>
+  <version>1.1-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: Spring Data Meta</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-meta/src/main/java/org/superbiz/dynamic/User.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/User.java?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/User.java
(added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/User.java
Tue Mar  6 19:00:27 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-meta/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java
(added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/UserSpringDataDao.java
Tue Mar  6 19:00:27 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.superbiz.dynamic;
+
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.repository.query.Param;
+import org.superbiz.dynamic.api.SpringRepository;
+
+@SpringRepository
+public interface UserSpringDataDao extends JpaRepository<User, Long> {
+    User findByName(@Param("name") final String name);
+}

Added: openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/Metatype.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/Metatype.java?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/Metatype.java
(added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/Metatype.java
Tue Mar  6 19:00:27 2012
@@ -0,0 +1,13 @@
+package org.superbiz.dynamic.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Metatype
+@Retention(RetentionPolicy.RUNTIME)
+@Target(ElementType.ANNOTATION_TYPE)
+public @interface Metatype {
+}
+

Added: openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/SpringRepository.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/SpringRepository.java?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/SpringRepository.java
(added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/api/SpringRepository.java
Tue Mar  6 19:00:27 2012
@@ -0,0 +1,19 @@
+package org.superbiz.dynamic.api;
+
+import org.apache.openejb.api.Proxy;
+import org.superbiz.dynamic.framework.SpringDataProxy;
+
+import javax.ejb.Stateless;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Metatype
+@Stateless
+@Proxy(SpringDataProxy.class)
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface SpringRepository {
+}
+

Added: openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/framework/SpringDataProxy.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/framework/SpringDataProxy.java?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/framework/SpringDataProxy.java
(added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/java/org/superbiz/dynamic/framework/SpringDataProxy.java
Tue Mar  6 19:00:27 2012
@@ -0,0 +1,31 @@
+package org.superbiz.dynamic.framework;
+
+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(unitName = "dynamic")
+    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-meta/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/resources/META-INF/persistence.xml?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/resources/META-INF/persistence.xml
(added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/src/main/resources/META-INF/persistence.xml
Tue Mar  6 19:00:27 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-meta/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java?rev=1297652&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
(added)
+++ openejb/trunk/openejb/examples/spring-data-proxy-meta/src/test/java/org/superbiz/dynamic/DynamicUserDaoTest.java
Tue Mar  6 19:00:27 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-meta");
+        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();
+    }
+}



Mime
View raw message