aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gge...@apache.org
Subject svn commit: r1689090 - in /aries/trunk/jpa: itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/ itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/ jpa-blueprint/src/main/java/org/apache/ari...
Date Fri, 03 Jul 2015 21:00:49 GMT
Author: ggerla
Date: Fri Jul  3 21:00:49 2015
New Revision: 1689090

URL: http://svn.apache.org/r1689090
Log:
- Implemented ARIES-1344 scanning all jpa annotated fields
- Add integration test to verify new functionality

Added:
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithMultiAnnotationTest.java
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSingleAnnotationTest.java
      - copied, changed from r1688844, aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSupplierTest.java
Removed:
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmTest.java
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmfTest.java
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSupplierTest.java
Modified:
    aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
    aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java

Modified: aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml?rev=1689090&r1=1689089&r2=1689090&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
(original)
+++ aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
Fri Jul  3 21:00:49 2015
@@ -42,6 +42,18 @@
 		class="org.apache.aries.jpa.itest.testbundle.service.impl.CarServiceWithSupplierImpl">
 		<tx:transaction method="*" value="Required" />
 	</bean>
+	
+	<service ref="carServiceMulti"
+		interface="org.apache.aries.jpa.itest.testbundle.service.CarService">
+		<service-properties>
+			<entry key="type" value="multiannotation" />
+		</service-properties>
+	</service>
+	
+	<bean id="carServiceMulti"
+		class="org.apache.aries.jpa.itest.testbundle.service.impl.CarServiceWithMultiAnnotationImpl">
+		<tx:transaction method="*" value="Required" />
+	</bean>
     
 </blueprint>
 

Added: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithMultiAnnotationTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithMultiAnnotationTest.java?rev=1689090&view=auto
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithMultiAnnotationTest.java
(added)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithMultiAnnotationTest.java
Fri Jul  3 21:00:49 2015
@@ -0,0 +1,60 @@
+/*  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.aries.jpa.blueprint.aries.itest;
+
+import org.apache.aries.jpa.itest.AbstractJPAItest;
+import org.apache.aries.jpa.itest.testbundle.entities.Car;
+import org.apache.aries.jpa.itest.testbundle.service.CarService;
+import org.junit.Assert;
+import org.junit.Test;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+
+public class BlueprintWithMultiAnnotationTest extends AbstractJPAItest {
+    
+	@Test
+    public void testMultiAnnotationQuery() throws Exception {
+		CarService carService = getService(CarService.class, "(type=multiannotation)");
+		
+		resolveBundles();
+        Car c = new Car();
+        c.setColour("Blue");
+        c.setNumberPlate("AB11EMF");
+        c.setNumberOfSeats(7);
+        c.setEngineSize(1900);
+
+        carService.addCar(c);
+
+        Car car2 = carService.getCar("AB11EMF");
+        Assert.assertEquals(c.getNumberPlate(), car2.getNumberPlate());
+        
+        carService.deleteCar("AB11EMF");
+        car2 = carService.getCar("AB11EMF");
+        Assert.assertNull(car2);
+    }
+
+	@Configuration
+    public Option[] configuration() {
+        return new Option[] {
+            baseOptions(), //
+            ariesJpa20(), //
+            hibernate(), //
+            derbyDSF(), //
+            testBundleBlueprint(),
+            //debug()
+        };
+    }
+}

Copied: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSingleAnnotationTest.java
(from r1688844, aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSupplierTest.java)
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSingleAnnotationTest.java?p2=aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSingleAnnotationTest.java&p1=aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSupplierTest.java&r1=1688844&r2=1689090&rev=1689090&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSupplierTest.java
(original)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSingleAnnotationTest.java
Fri Jul  3 21:00:49 2015
@@ -23,11 +23,11 @@ import org.junit.Test;
 import org.ops4j.pax.exam.Configuration;
 import org.ops4j.pax.exam.Option;
 
-public class BlueprintWithSupplierTest extends AbstractJPAItest {
+public class BlueprintWithSingleAnnotationTest extends AbstractJPAItest {
 
 	@Test
     public void testEmfAddQuery() throws Exception {
-		CarService carService = getService(CarService.class, "(type=supplier)");
+		CarService carService = getService(CarService.class, "(type=emf)");
 		
 		resolveBundles();
         Car c = new Car();

Modified: aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java?rev=1689090&r1=1689089&r2=1689090&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java
(original)
+++ aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java
Fri Jul  3 21:00:49 2015
@@ -19,6 +19,8 @@
 package org.apache.aries.jpa.blueprint.impl;
 
 import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -41,110 +43,125 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class JpaBeanProcessor implements BeanProcessor {
-    private static final Logger LOGGER = LoggerFactory.getLogger(JpaInterceptor.class);
-    public static final String JPA_PROCESSOR_BEAN_NAME = "org_apache_aries_jpan";
-    private Map<Object, EmSupplierProxy> emProxies;
-    private Map<Object, EntityManagerFactory> emfProxies;
-    private ComponentDefinitionRegistry cdr;
-
-    public JpaBeanProcessor() {
-        emProxies = new ConcurrentHashMap<Object, EmSupplierProxy>();
-        emfProxies = new ConcurrentHashMap<Object, EntityManagerFactory>();
-    }
-
-    public void setCdr(ComponentDefinitionRegistry cdr) {
-        this.cdr = cdr;
-    }
-
-    public void afterDestroy(Object bean, String beanName) {
-        EmSupplierProxy emProxy = emProxies.get(bean);
-        if (emProxy != null) {
-            emProxy.close();
-        }
-        EntityManagerFactory emfProxy = emfProxies.get(bean);
-        if (emfProxy != null) {
-        	emfProxy.close();
-        }
-    }
-
-    public Object afterInit(Object bean, String beanName, BeanCreator beanCreator, BeanMetadata
beanData) {
-        return bean;
-    }
-
-    public void beforeDestroy(Object bean, String beanName) {
-    }
-
-    public Object beforeInit(Object bean, String beanName, BeanCreator beanCreator, BeanMetadata
beanData) {
-        Class<?> c = bean.getClass();
-        Field field = getPersistenceField(c);
-        if (field == null) {
-            return bean;
-        }
-        BundleContext context = FrameworkUtil.getBundle(c).getBundleContext();
-        field.setAccessible(true);
-
-        PersistenceContext pcAnn = field.getAnnotation(PersistenceContext.class);
-        if (pcAnn != null) {
-        LOGGER.debug("Adding jpa/jta interceptor bean {} with class {}", beanName, c);
-
-        EmSupplierProxy supplierProxy = new EmSupplierProxy(context, pcAnn.unitName());
-	        emProxies.put(bean, supplierProxy);
-        try {
-	            field.set(bean, getEmProxy(field, supplierProxy));
-        } catch (Exception e) {
-            throw new IllegalStateException("Error setting field " + field, e);
-        }
-        Interceptor interceptor = new JpaInterceptor(supplierProxy);
-        cdr.registerInterceptorWithComponent(beanData, interceptor);
-        } else {
-        	PersistenceUnit puAnn = field.getAnnotation(PersistenceUnit.class);
-        	if(puAnn != null) {
-        		LOGGER.debug("Adding emf proxy");
-        		
-    	        EntityManagerFactory emfProxy = EmfProxyFactory.create(context, puAnn.unitName());

-    	        emfProxies.put(bean, emfProxy);
-    	        try {
-    	            field.set(bean, getEmfProxy(field, emfProxy));
-    	        } catch (Exception e) {
-    	            throw new IllegalStateException("Error setting field " + field, e);
-    	        }	
-        	}
-        }
-        return bean;
-    }
-
-    private Object getEmfProxy(Field field, EntityManagerFactory supplierProxy) {
-        if (field.getType() == EntityManagerFactory.class) {
-            return supplierProxy;
-        } else {
-            throw new IllegalStateException(
-                                            "Field with @PersistenceUnit is not of type EntityManagerFactory
"
-                                                + field);
-        }
-    }
-
-    private Object getEmProxy(Field field, EmSupplierProxy supplierProxy) {
-        if (field.getType() == EmSupplier.class) {
-            return supplierProxy;
-        } else if (field.getType() == EntityManager.class) {
-            return EmProxyFactory.create(supplierProxy);
-        } else {
-            throw new IllegalStateException(
-                                            "Field with @PersistenceContext is not of type
EntityManager or EmSupplier "
-                                                + field);
-        }
-    }
-
-    private Field getPersistenceField(Class<?> c) {
-        for (Field field : c.getDeclaredFields()) {
-            if (field.getAnnotation(PersistenceContext.class) != null) {
-                return field;
-            }
-            if (field.getAnnotation(PersistenceUnit.class) != null) {
-                return field;
-            }
-        }
-        return null;
-    }
+	private static final Logger LOGGER = LoggerFactory
+			.getLogger(JpaInterceptor.class);
+	public static final String JPA_PROCESSOR_BEAN_NAME = "org_apache_aries_jpan";
+	private Map<Object, EmSupplierProxy> emProxies;
+	private Map<Object, EntityManagerFactory> emfProxies;
+	private ComponentDefinitionRegistry cdr;
+
+	public JpaBeanProcessor() {
+		emProxies = new ConcurrentHashMap<Object, EmSupplierProxy>();
+		emfProxies = new ConcurrentHashMap<Object, EntityManagerFactory>();
+	}
+
+	public void setCdr(ComponentDefinitionRegistry cdr) {
+		this.cdr = cdr;
+	}
+
+	public void afterDestroy(Object bean, String beanName) {
+		EmSupplierProxy emProxy = emProxies.get(bean);
+		if (emProxy != null) {
+			emProxy.close();
+		}
+		EntityManagerFactory emfProxy = emfProxies.get(bean);
+		if (emfProxy != null) {
+			emfProxy.close();
+		}
+	}
+
+	public Object afterInit(Object bean, String beanName,
+			BeanCreator beanCreator, BeanMetadata beanData) {
+		return bean;
+	}
+
+	public void beforeDestroy(Object bean, String beanName) {
+	}
+
+	public Object beforeInit(Object bean, String beanName,
+			BeanCreator beanCreator, BeanMetadata beanData) {
+		Class<?> c = bean.getClass();
+		List<Field> jpaAnnotated = new ArrayList<Field>();
+		getPersistenceFields(c, jpaAnnotated);
+
+		for (Field field : jpaAnnotated) {
+			if (field == null) {
+				return bean;
+			}
+			BundleContext context = FrameworkUtil.getBundle(c)
+					.getBundleContext();
+			field.setAccessible(true);
+
+			PersistenceContext pcAnn = field
+					.getAnnotation(PersistenceContext.class);
+			if (pcAnn != null) {
+				LOGGER.debug(
+						"Adding jpa/jta interceptor bean {} with class {}",
+						beanName, c);
+
+				EmSupplierProxy supplierProxy = new EmSupplierProxy(context,
+						pcAnn.unitName());
+				emProxies.put(bean, supplierProxy);
+				try {
+					field.set(bean, getEmProxy(field, supplierProxy));
+				} catch (Exception e) {
+					throw new IllegalStateException("Error setting field "
+							+ field, e);
+				}
+				Interceptor interceptor = new JpaInterceptor(supplierProxy);
+				cdr.registerInterceptorWithComponent(beanData, interceptor);
+			} else {
+				PersistenceUnit puAnn = field
+						.getAnnotation(PersistenceUnit.class);
+				if (puAnn != null) {
+					LOGGER.debug("Adding emf proxy");
+
+					EntityManagerFactory emfProxy = EmfProxyFactory.create(
+							context, puAnn.unitName());
+					emfProxies.put(bean, emfProxy);
+					try {
+						field.set(bean, getEmfProxy(field, emfProxy));
+					} catch (Exception e) {
+						throw new IllegalStateException("Error setting field "
+								+ field, e);
+					}
+				}
+			}
+		}
+		return bean;
+	}
+
+	private Object getEmfProxy(Field field, EntityManagerFactory supplierProxy) {
+		if (field.getType() == EntityManagerFactory.class) {
+			return supplierProxy;
+		} else {
+			throw new IllegalStateException(
+					"Field with @PersistenceUnit is not of type EntityManagerFactory "
+							+ field);
+		}
+	}
+
+	private Object getEmProxy(Field field, EmSupplierProxy supplierProxy) {
+		if (field.getType() == EmSupplier.class) {
+			return supplierProxy;
+		} else if (field.getType() == EntityManager.class) {
+			return EmProxyFactory.create(supplierProxy);
+		} else {
+			throw new IllegalStateException(
+					"Field with @PersistenceContext is not of type EntityManager or EmSupplier "
+							+ field);
+		}
+	}
+
+	private void getPersistenceFields(Class<?> c, List<Field> jpaAnnotated) {
+		for (Field field : c.getDeclaredFields()) {
+			if (field.getAnnotation(PersistenceContext.class) != null
+					|| field.getAnnotation(PersistenceUnit.class) != null) {
+				if (jpaAnnotated != null) {
+					jpaAnnotated.add(field);
+				}
+			}
+		}
+	}
 
 }



Mime
View raw message