Author: ggerla
Date: Wed May 27 23:08:13 2015
New Revision: 1682135

URL: http://svn.apache.org/r1682135
Log:
- Added support for PersistenceUnit annotation
- Added tests for EntityManager and EntityManagerFactory injection
- Create EmfProxyFactory to manage the EMF creation
- Added classes to test supplier and em blueprint integration

Added:
    
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/itest/testbundle/service/impl/CarServiceWithEmfImpl.java
    
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
    
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxy.java
    
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxyFactory.java
Removed:
    
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintTest.java
Modified:
    
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/resources/OSGI-INF/blueprint/config.xml
    
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
    
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/impl/JpaBeanProcessor.java

Added: 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/itest/testbundle/service/impl/CarServiceWithEmfImpl.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/itest/testbundle/service/impl/CarServiceWithEmfImpl.java?rev=1682135&view=auto
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/itest/testbundle/service/impl/CarServiceWithEmfImpl.java
 (added)
+++ 
aries/trunk/jpa/itests/jpa-container-blueprint-testbundle/src/main/java/org/apache/aries/jpa/itest/testbundle/service/impl/CarServiceWithEmfImpl.java
 Wed May 27 23:08:13 2015
@@ -0,0 +1,71 @@
+/*
+ * 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 WARRANTIESOR 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.itest.testbundle.service.impl;
+
+import java.util.Collection;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnit;
+
+import org.apache.aries.jpa.itest.testbundle.entities.Car;
+import org.apache.aries.jpa.itest.testbundle.service.CarService;
+
+
+public class CarServiceWithEmfImpl implements CarService {
+
+       @PersistenceUnit(unitName="test_unit_blueprint")
+       EntityManagerFactory emf;
+       
+       @Override
+       public Car getCar(String id) {
+               EntityManager em = emf.createEntityManager();
+               return em.find(Car.class, id);
+       }
+
+       @Override
+       public void addCar(Car car) {
+               EntityManager em = emf.createEntityManager();
+               em.persist(car);
+               em.flush();
+       }
+
+       public Collection<Car> getCars() {
+               EntityManager em = emf.createEntityManager();
+               return em.createQuery("select c from Car c", Car.class)
+                       .getResultList();
+       }
+
+       @Override
+       public void updateCar(Car car) {
+               EntityManager em = emf.createEntityManager();
+               em.persist(car);
+       }
+
+       @Override
+       public void deleteCar(String id) {
+               EntityManager em = emf.createEntityManager();
+               em.remove(getCar(id));
+       }
+
+       public void setEmf(EntityManagerFactory emf) {
+               this.emf = emf;
+       }
+       
+}

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=1682135&r1=1682134&r2=1682135&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
 Wed May 27 23:08:13 2015
@@ -7,9 +7,39 @@
 
        <jpa:enable />
 
-       <service ref="carService" 
interface="org.apache.aries.jpa.itest.testbundle.service.CarService">
+       <service ref="carServiceEmf"
+               
interface="org.apache.aries.jpa.itest.testbundle.service.CarService">
+               <service-properties>
+                       <entry key="type" value="emf" />
+               </service-properties>
        </service>
-       <bean id="carService" 
class="org.apache.aries.jpa.itest.testbundle.service.impl.CarServiceWithSupplierImpl">
+       
+       <bean id="carServiceEmf"
+               
class="org.apache.aries.jpa.itest.testbundle.service.impl.CarServiceWithEmfImpl">
+               <tx:transaction method="*" value="Required" />
+       </bean>
+       
+       <service ref="carServiceEm"
+               
interface="org.apache.aries.jpa.itest.testbundle.service.CarService">
+               <service-properties>
+                       <entry key="type" value="em" />
+               </service-properties>
+       </service>
+       
+       <bean id="carServiceEm"
+               
class="org.apache.aries.jpa.itest.testbundle.service.impl.CarServiceImpl">
+               <tx:transaction method="*" value="Required" />
+       </bean>
+       
+       <service ref="carServiceEmSupplier"
+               
interface="org.apache.aries.jpa.itest.testbundle.service.CarService">
+               <service-properties>
+                       <entry key="type" value="supplier" />
+               </service-properties>
+       </service>
+       
+       <bean id="carServiceEmSupplier"
+               
class="org.apache.aries.jpa.itest.testbundle.service.impl.CarServiceWithSupplierImpl">
                <tx:transaction method="*" value="Required" />
        </bean>
     

Added: 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmTest.java?rev=1682135&view=auto
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmTest.java
 (added)
+++ 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmTest.java
 Wed May 27 23:08:13 2015
@@ -0,0 +1,102 @@
+/*  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 java.util.HashMap;
+import java.util.Map;
+
+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 BlueprintWithEmTest extends AbstractJPAItest {
+    
+       CarService carService;
+
+       @Test
+    public void testEmfAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "em");
+               carService = getServie(CarService.class, filters);
+               
+               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());
+    }
+
+       @Test
+    public void testEmAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "em");
+               carService = getServie(CarService.class, filters);
+               
+               resolveBundles();
+        Car c = new Car();
+        c.setColour("Blue");
+        c.setNumberPlate("AB11EM");
+        c.setNumberOfSeats(7);
+        c.setEngineSize(1900);
+
+        carService.addCar(c);
+
+        Car car2 = carService.getCar("AB11EM");
+        Assert.assertEquals(c.getNumberPlate(), car2.getNumberPlate());
+    }
+       
+       @Test
+    public void testSupplierAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "supplier");
+               carService = getServie(CarService.class, filters);
+               
+               resolveBundles();
+        Car c = new Car();
+        c.setColour("Blue");
+        c.setNumberPlate("AB11SUPPLIER");
+        c.setNumberOfSeats(7);
+        c.setEngineSize(1900);
+
+        carService.addCar(c);
+
+        Car car2 = carService.getCar("AB11SUPPLIER");
+        Assert.assertEquals(c.getNumberPlate(), car2.getNumberPlate());
+    }
+
+
+       @Configuration
+    public Option[] configuration() {
+        return new Option[] {
+            baseOptions(), //
+            ariesJpa20(), //
+            hibernate(), //
+            derbyDSF(), //
+            testBundleBlueprint(),
+            //debug()
+        };
+    }
+}

Added: 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmfTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmfTest.java?rev=1682135&view=auto
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmfTest.java
 (added)
+++ 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithEmfTest.java
 Wed May 27 23:08:13 2015
@@ -0,0 +1,102 @@
+/*  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 java.util.HashMap;
+import java.util.Map;
+
+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 BlueprintWithEmfTest extends AbstractJPAItest {
+    
+       CarService carService;
+
+       @Test
+    public void testEmfAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "emf");
+               carService = getServie(CarService.class, filters);
+               
+               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());
+    }
+
+       @Test
+    public void testEmAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "em");
+               carService = getServie(CarService.class, filters);
+               
+               resolveBundles();
+        Car c = new Car();
+        c.setColour("Blue");
+        c.setNumberPlate("AB11EM");
+        c.setNumberOfSeats(7);
+        c.setEngineSize(1900);
+
+        carService.addCar(c);
+
+        Car car2 = carService.getCar("AB11EM");
+        Assert.assertEquals(c.getNumberPlate(), car2.getNumberPlate());
+    }
+       
+       @Test
+    public void testSupplierAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "supplier");
+               carService = getServie(CarService.class, filters);
+               
+               resolveBundles();
+        Car c = new Car();
+        c.setColour("Blue");
+        c.setNumberPlate("AB11SUPPLIER");
+        c.setNumberOfSeats(7);
+        c.setEngineSize(1900);
+
+        carService.addCar(c);
+
+        Car car2 = carService.getCar("AB11SUPPLIER");
+        Assert.assertEquals(c.getNumberPlate(), car2.getNumberPlate());
+    }
+
+
+       @Configuration
+    public Option[] configuration() {
+        return new Option[] {
+            baseOptions(), //
+            ariesJpa20(), //
+            hibernate(), //
+            derbyDSF(), //
+            testBundleBlueprint(),
+            //debug()
+        };
+    }
+}

Added: 
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/BlueprintWithSupplierTest.java?rev=1682135&view=auto
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSupplierTest.java
 (added)
+++ 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/BlueprintWithSupplierTest.java
 Wed May 27 23:08:13 2015
@@ -0,0 +1,102 @@
+/*  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 java.util.HashMap;
+import java.util.Map;
+
+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 BlueprintWithSupplierTest extends AbstractJPAItest {
+    
+       CarService carService;
+
+       @Test
+    public void testEmfAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "supplier");
+               carService = getServie(CarService.class, filters);
+               
+               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());
+    }
+
+       @Test
+    public void testEmAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "em");
+               carService = getServie(CarService.class, filters);
+               
+               resolveBundles();
+        Car c = new Car();
+        c.setColour("Blue");
+        c.setNumberPlate("AB11EM");
+        c.setNumberOfSeats(7);
+        c.setEngineSize(1900);
+
+        carService.addCar(c);
+
+        Car car2 = carService.getCar("AB11EM");
+        Assert.assertEquals(c.getNumberPlate(), car2.getNumberPlate());
+    }
+       
+       @Test
+    public void testSupplierAddQuery() throws Exception {
+               Map<String,String> filters = new HashMap<String,String>();
+               filters.put("type", "supplier");
+               carService = getServie(CarService.class, filters);
+               
+               resolveBundles();
+        Car c = new Car();
+        c.setColour("Blue");
+        c.setNumberPlate("AB11SUPPLIER");
+        c.setNumberOfSeats(7);
+        c.setEngineSize(1900);
+
+        carService.addCar(c);
+
+        Car car2 = carService.getCar("AB11SUPPLIER");
+        Assert.assertEquals(c.getNumberPlate(), car2.getNumberPlate());
+    }
+
+
+       @Configuration
+    public Option[] configuration() {
+        return new Option[] {
+            baseOptions(), //
+            ariesJpa20(), //
+            hibernate(), //
+            derbyDSF(), //
+            testBundleBlueprint(),
+            //debug()
+        };
+    }
+}

Modified: 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java?rev=1682135&r1=1682134&r2=1682135&view=diff
==============================================================================
--- 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
 (original)
+++ 
aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
 Wed May 27 23:08:13 2015
@@ -8,6 +8,8 @@ import static org.ops4j.pax.exam.CoreOpt
 import static org.ops4j.pax.exam.CoreOptions.vmOption;
 import static org.ops4j.pax.exam.CoreOptions.when;
 
+import java.util.Map;
+
 import javax.persistence.EntityManagerFactory;
 
 import org.apache.aries.itest.AbstractIntegrationTest;
@@ -39,6 +41,20 @@ public abstract class AbstractJPAItest e
         return context().getService(EntityManagerFactory.class, 
"osgi.unit.name=" + name);
     }
 
+    protected <T> T getServie(Class<T> type, Map<String, String> filters) {
+               if (filters.size() > 0) {
+                       String filterS = "(&";
+                       for (String key : filters.keySet()) {
+                               String value = filters.get(key);
+                               filterS += String.format("(%s=%s)", key, value);
+                       }
+                       filterS += ")";
+                       return context().getService(type, filterS);
+               } else {
+                       return context().getService(type);
+               }
+       }
+
     @SuppressWarnings("rawtypes")
     protected ServiceReference[] getEMFRefs(String name) throws 
InvalidSyntaxException {
         return 
bundleContext.getAllServiceReferences(EntityManagerFactory.class.getName(), 
"(osgi.unit.name=" + name + ")");

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=1682135&r1=1682134&r2=1682135&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
 Wed May 27 23:08:13 2015
@@ -23,13 +23,16 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
 import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceUnit;
 
 import org.apache.aries.blueprint.BeanProcessor;
 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
 import org.apache.aries.blueprint.Interceptor;
 import org.apache.aries.jpa.blueprint.supplier.impl.EmProxyFactory;
 import org.apache.aries.jpa.blueprint.supplier.impl.EmSupplierProxy;
+import org.apache.aries.jpa.blueprint.supplier.impl.EmfProxyFactory;
 import org.apache.aries.jpa.supplier.EmSupplier;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
@@ -40,11 +43,13 @@ 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> proxies;
+    private Map<Object, EmSupplierProxy> emProxies;
+    private Map<Object, EntityManagerFactory> emfProxies;
     private ComponentDefinitionRegistry cdr;
 
     public JpaBeanProcessor() {
-        proxies = new ConcurrentHashMap<Object, EmSupplierProxy>();
+        emProxies = new ConcurrentHashMap<Object, EmSupplierProxy>();
+        emfProxies = new ConcurrentHashMap<Object, EntityManagerFactory>();
     }
 
     public void setCdr(ComponentDefinitionRegistry cdr) {
@@ -52,9 +57,13 @@ public class JpaBeanProcessor implements
     }
 
     public void afterDestroy(Object bean, String beanName) {
-        EmSupplierProxy proxy = proxies.get(bean);
-        if (proxy != null) {
-            proxy.close();
+        EmSupplierProxy emProxy = emProxies.get(bean);
+        if (emProxy != null) {
+            emProxy.close();
+        }
+        EntityManagerFactory emfProxy = emfProxies.get(bean);
+        if (emfProxy != null) {
+               emfProxy.close();
         }
     }
 
@@ -67,32 +76,54 @@ public class JpaBeanProcessor implements
 
     public Object beforeInit(Object bean, String beanName, BeanCreator 
beanCreator, BeanMetadata beanData) {
         Class<?> c = bean.getClass();
-        Field field = getEmSupplierField(c);
+        Field field = getPersistenceField(c);
         if (field == null) {
             return bean;
         }
-        PersistenceContext pcAnn = 
field.getAnnotation(PersistenceContext.class);
-        if (pcAnn == 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);
 
-        field.setAccessible(true);
-        BundleContext context = FrameworkUtil.getBundle(c).getBundleContext();
         EmSupplierProxy supplierProxy = new EmSupplierProxy(context, 
pcAnn.unitName());
-        proxies.put(bean, supplierProxy);
+               emProxies.put(bean, supplierProxy);
         try {
-            field.set(bean, getProxy(field, supplierProxy));
+                   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 getProxy(Field field, EmSupplierProxy supplierProxy) {
+    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) {
@@ -104,11 +135,14 @@ public class JpaBeanProcessor implements
         }
     }
 
-    private Field getEmSupplierField(Class<?> c) {
+    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;
     }

Added: 
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxy.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxy.java?rev=1682135&view=auto
==============================================================================
--- 
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxy.java
 (added)
+++ 
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxy.java
 Wed May 27 23:08:13 2015
@@ -0,0 +1,75 @@
+/*
+ * 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 WARRANTIESOR 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.supplier.impl;
+
+import static org.osgi.service.jpa.EntityManagerFactoryBuilder.JPA_UNIT_NAME;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.persistence.EntityManagerFactory;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class EmfProxy implements InvocationHandler {
+       private ServiceTracker<EntityManagerFactory, EntityManagerFactory> 
tracker;
+
+       public EmfProxy(BundleContext context, String unitName) {
+               String filterS = String.format("(&(objectClass=%s)(%s=%s))",
+                               EntityManagerFactory.class.getName(), 
JPA_UNIT_NAME, unitName);
+               Filter filter;
+               try {
+                       filter = FrameworkUtil.createFilter(filterS);
+               } catch (InvalidSyntaxException e) {
+                       throw new IllegalStateException(e);
+               }
+               tracker = new ServiceTracker<>(context, filter, null);
+               tracker.open();
+       }
+
+       private EntityManagerFactory getEntityManagerFactory() {
+               try {
+                       return tracker.waitForService(10000);
+               } catch (InterruptedException e) {
+                       throw new IllegalStateException(e);
+               }
+       }
+
+       @Override
+       public Object invoke(Object proxy, Method method, Object[] args)
+                       throws Throwable {
+               Object res = null;
+
+               EntityManagerFactory delegate = getEntityManagerFactory();
+
+               try {
+                       res = method.invoke(delegate, args);
+               } catch (IllegalArgumentException e) {
+                       new IllegalStateException(e);
+               } catch (InvocationTargetException e) {
+                       new IllegalStateException(e);
+               }
+               return res;
+       }
+}

Added: 
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxyFactory.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxyFactory.java?rev=1682135&view=auto
==============================================================================
--- 
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxyFactory.java
 (added)
+++ 
aries/trunk/jpa/jpa-blueprint/src/main/java/org/apache/aries/jpa/blueprint/supplier/impl/EmfProxyFactory.java
 Wed May 27 23:08:13 2015
@@ -0,0 +1,35 @@
+/*
+ * 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 WARRANTIESOR 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.supplier.impl;
+
+import java.lang.reflect.Proxy;
+
+import javax.persistence.EntityManagerFactory;
+
+import org.osgi.framework.BundleContext;
+
+public class EmfProxyFactory {
+
+    public static EntityManagerFactory create(BundleContext context, String 
unitName) {
+       ClassLoader cl = EntityManagerFactory.class.getClassLoader();
+        Class<?>[] ifAr = new Class[] { EntityManagerFactory.class };
+        
+        return  (EntityManagerFactory) Proxy.newProxyInstance(cl, ifAr, new 
EmfProxy(context, unitName));
+    }
+}


Reply via email to