Repository: deltaspike Updated Branches: refs/heads/master 34b5304f5 -> 53790137b
DELTASPIKE-420 Refactored JPA-module hook for externally resolved EM Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/53790137 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/53790137 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/53790137 Branch: refs/heads/master Commit: 53790137bb25a1293653a657682426c8f44b00a5 Parents: 34b5304 Author: Thomas Hug <[email protected]> Authored: Mon Mar 3 10:47:24 2014 +0100 Committer: Thomas Hug <[email protected]> Committed: Mon Mar 3 10:52:35 2014 +0100 ---------------------------------------------------------------------- .../impl/tx/ThreadLocalEntityManagerHolder.java | 58 ++++++++++++++++++++ .../impl/tx/TransactionalQueryRunnerTest.java | 7 +++ .../jpa/QueryStringExtractorFactoryTest.java | 16 +++++- .../DefaultEntityManagerHolder.java | 54 ++++++++++++++++++ .../impl/entitymanager/EntityManagerHolder.java | 25 ++------- 5 files changed, 139 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/53790137/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/tx/ThreadLocalEntityManagerHolder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/tx/ThreadLocalEntityManagerHolder.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/tx/ThreadLocalEntityManagerHolder.java new file mode 100644 index 0000000..959040d --- /dev/null +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/tx/ThreadLocalEntityManagerHolder.java @@ -0,0 +1,58 @@ +/* + * 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.deltaspike.data.impl.tx; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Specializes; +import javax.persistence.EntityManager; + +import org.apache.deltaspike.jpa.impl.entitymanager.DefaultEntityManagerHolder; + +@Specializes +@ApplicationScoped +public class ThreadLocalEntityManagerHolder extends DefaultEntityManagerHolder +{ + + private final ThreadLocal<EntityManager> holder = new ThreadLocal<EntityManager>(); + + @Override + public void set(EntityManager entityManager) + { + holder.set(entityManager); + } + + @Override + public boolean isSet() + { + return get() != null; + } + + @Override + public EntityManager get() + { + return holder.get(); + } + + @Override + public void dispose() + { + holder.remove(); + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/53790137/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/tx/TransactionalQueryRunnerTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/tx/TransactionalQueryRunnerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/tx/TransactionalQueryRunnerTest.java index c6c7346..98d6878 100644 --- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/tx/TransactionalQueryRunnerTest.java +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/tx/TransactionalQueryRunnerTest.java @@ -114,6 +114,13 @@ public class TransactionalQueryRunnerTest assertTrue(wrapper.isRunInNonTx()); } + @Test + @InSequence(10) + public void should_cleanup() throws Exception + { + repository.deleteAll(); + } + @Before public void init() { http://git-wip-us.apache.org/repos/asf/deltaspike/blob/53790137/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/util/jpa/QueryStringExtractorFactoryTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/util/jpa/QueryStringExtractorFactoryTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/util/jpa/QueryStringExtractorFactoryTest.java index c77b175..0c2e4f3 100644 --- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/util/jpa/QueryStringExtractorFactoryTest.java +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/util/jpa/QueryStringExtractorFactoryTest.java @@ -99,6 +99,16 @@ public class QueryStringExtractorFactoryTest new FakeQueryInvocationHandler()); } + private static Object createInstance(Method method) throws Exception + { + // EclipseLink specific + Class<?> returnType = Class.forName("org.eclipse.persistence.queries.DataReadQuery"); + Object instance = returnType.newInstance(); + Method setter = returnType.getMethod("setJPQLString", String.class); + setter.invoke(instance, QUERY_STRING); + return instance; + } + private static class FakeQueryInvocationHandler implements InvocationHandler { @@ -107,7 +117,11 @@ public class QueryStringExtractorFactoryTest { if (!method.getReturnType().equals(String.class)) { - return createProxy(method.getReturnType()); + if (method.getReturnType().isInterface()) + { + return createProxy(method.getReturnType()); + } + return createInstance(method); } return QUERY_STRING; // we don't care of the result actually } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/53790137/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/DefaultEntityManagerHolder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/DefaultEntityManagerHolder.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/DefaultEntityManagerHolder.java new file mode 100644 index 0000000..d0995f3 --- /dev/null +++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/DefaultEntityManagerHolder.java @@ -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.apache.deltaspike.jpa.impl.entitymanager; + +import javax.persistence.EntityManager; + +/** + * Empty holder. Override and specialize in using module. + * Currently only used by the data module. + */ +public class DefaultEntityManagerHolder implements EntityManagerHolder +{ + + @Override + public void set(EntityManager entityManager) + { + throw new UnsupportedOperationException( + "Default implementation does not store an EntityManager"); + } + + @Override + public boolean isSet() + { + return false; + } + + @Override + public EntityManager get() + { + return null; + } + + @Override + public void dispose() + { + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/53790137/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerHolder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerHolder.java b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerHolder.java index 2d3a5a4..af4f589 100644 --- a/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerHolder.java +++ b/deltaspike/modules/jpa/impl/src/main/java/org/apache/deltaspike/jpa/impl/entitymanager/EntityManagerHolder.java @@ -18,32 +18,17 @@ */ package org.apache.deltaspike.jpa.impl.entitymanager; -import javax.enterprise.context.ApplicationScoped; import javax.persistence.EntityManager; -@ApplicationScoped -public class EntityManagerHolder +public interface EntityManagerHolder { - private final ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>(); + void set(EntityManager entityManager); - public void set(EntityManager entityManager) - { - this.entityManager.set(entityManager); - } + boolean isSet(); - public boolean isSet() - { - return get() != null; - } + EntityManager get(); - public EntityManager get() - { - return entityManager.get(); - } + void dispose(); - public void dispose() - { - entityManager.remove(); - } }
