deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject [1/6] git commit: DELTASPIKE-391 Cleanup of dependent beans.
Date Wed, 02 Oct 2013 12:45:52 GMT
Updated Branches:
  refs/heads/master f8936831e -> e9038460f


DELTASPIKE-391 Cleanup of dependent beans.


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/92c0e773
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/92c0e773
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/92c0e773

Branch: refs/heads/master
Commit: 92c0e773d344c78808bce5ce923e3000368c68fe
Parents: f893683
Author: Thomas Hug <Thomas.Hug@ctp-consulting.com>
Authored: Wed Oct 2 10:54:25 2013 +0200
Committer: Thomas Hug <Thomas.Hug@ctp-consulting.com>
Committed: Wed Oct 2 11:10:39 2013 +0200

----------------------------------------------------------------------
 .../data/impl/builder/DelegateQueryBuilder.java | 41 +++++++++++-------
 .../data/impl/builder/QueryBuilderFactory.java  | 39 +++++++++++------
 .../impl/handler/CdiQueryContextHolder.java     |  1 +
 .../impl/handler/CdiQueryInvocationContext.java | 17 ++++++++
 .../data/impl/handler/EntityManagerLookup.java  | 17 ++++----
 .../data/impl/handler/QueryHandler.java         |  2 +-
 .../data/impl/util/bean/BeanDestroyable.java    | 44 ++++++++++++++++++++
 .../util/bean/DependentProviderDestroyable.java | 39 +++++++++++++++++
 .../data/impl/util/bean/Destroyable.java        | 24 +++++++++++
 9 files changed, 189 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
index 65cbf05..8ed2361 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/DelegateQueryBuilder.java
@@ -20,31 +20,34 @@ package org.apache.deltaspike.data.impl.builder;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.Set;
 
-import javax.enterprise.inject.Any;
-import javax.enterprise.inject.Instance;
+import javax.enterprise.context.Dependent;
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.inject.spi.BeanManager;
 import javax.inject.Inject;
 
+import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
 import org.apache.deltaspike.data.impl.handler.QueryInvocationException;
 import org.apache.deltaspike.data.impl.meta.MethodType;
 import org.apache.deltaspike.data.impl.meta.QueryInvocation;
+import org.apache.deltaspike.data.impl.util.bean.BeanDestroyable;
 import org.apache.deltaspike.data.spi.DelegateQueryHandler;
 
 @QueryInvocation(MethodType.DELEGATE)
 public class DelegateQueryBuilder extends QueryBuilder
 {
-
     @Inject
-    @Any
-    private Instance<DelegateQueryHandler> delegates;
+    private BeanManager beanManager;
 
     @Override
     public Object execute(CdiQueryInvocationContext context)
     {
         try
         {
-            DelegateQueryHandler delegate = selectDelegate(context.getMethod());
+            DelegateQueryHandler delegate = selectDelegate(context);
             if (delegate != null)
             {
                 return invoke(delegate, context);
@@ -57,29 +60,39 @@ public class DelegateQueryBuilder extends QueryBuilder
         throw new QueryInvocationException("No DelegateQueryHandler found", context);
     }
 
-    private DelegateQueryHandler selectDelegate(Method method)
+    private DelegateQueryHandler selectDelegate(CdiQueryInvocationContext context)
     {
-        for (DelegateQueryHandler delegate : delegates)
+        Set<Bean<DelegateQueryHandler>> beans = BeanProvider
+                .getBeanDefinitions(DelegateQueryHandler.class, true, true);
+        for (Bean<DelegateQueryHandler> bean : beans)
         {
-            if (contains(delegate, method))
+            if (contains(bean.getBeanClass(), context.getMethod()))
             {
-                return delegate;
+                if (bean.getScope().equals(Dependent.class))
+                {
+                    CreationalContext<DelegateQueryHandler> cc = beanManager.createCreationalContext(bean);
+                    DelegateQueryHandler instance = (DelegateQueryHandler) beanManager.getReference(
+                            bean, DelegateQueryHandler.class, cc);
+                    context.addDestroyable(new BeanDestroyable<DelegateQueryHandler>(bean,
instance, cc));
+                    return instance;
+                }
+                return (DelegateQueryHandler) BeanProvider.getContextualReference(bean.getBeanClass());
             }
         }
         return null;
     }
 
-    private boolean contains(Object obj, Method method)
+    private boolean contains(Class<?> obj, Method method)
     {
         return extract(obj, method) != null;
     }
 
-    private Method extract(Object obj, Method method)
+    private Method extract(Class<?> obj, Method method)
     {
         try
         {
             String name = method.getName();
-            return obj != null ? obj.getClass().getMethod(name, method.getParameterTypes())
: null;
+            return obj != null ? obj.getMethod(name, method.getParameterTypes()) : null;
         }
         catch (NoSuchMethodException e)
         {
@@ -106,7 +119,7 @@ public class DelegateQueryBuilder extends QueryBuilder
     protected Object invoke(Object target, Method method, Object[] args) throws InvocationTargetException,
             IllegalAccessException
     {
-        Method extract = extract(target, method);
+        Method extract = extract(target.getClass(), method);
         return extract.invoke(target, args);
     }
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilderFactory.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilderFactory.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilderFactory.java
index b37c9b8..56ecbeb 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilderFactory.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilderFactory.java
@@ -18,33 +18,48 @@
  */
 package org.apache.deltaspike.data.impl.builder;
 
-import java.io.Serializable;
+import static org.apache.deltaspike.data.impl.meta.MethodType.ANNOTATED;
+import static org.apache.deltaspike.data.impl.meta.MethodType.DELEGATE;
+import static org.apache.deltaspike.data.impl.meta.MethodType.PARSE;
 
-import javax.enterprise.inject.Any;
-import javax.enterprise.inject.Instance;
-import javax.inject.Inject;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
 
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.api.provider.DependentProvider;
 import org.apache.deltaspike.data.api.QueryResult;
-import org.apache.deltaspike.data.impl.meta.RepositoryMethod;
+import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
+import org.apache.deltaspike.data.impl.meta.MethodType;
 import org.apache.deltaspike.data.impl.meta.QueryInvocationLiteral;
+import org.apache.deltaspike.data.impl.meta.RepositoryMethod;
+import org.apache.deltaspike.data.impl.util.bean.DependentProviderDestroyable;
 
 public class QueryBuilderFactory implements Serializable
 {
 
     private static final long serialVersionUID = 1L;
 
-    @Inject
-    @Any
-    private Instance<QueryBuilder> queryBuilder;
+    private static final Map<MethodType, QueryInvocationLiteral> LITERALS =
+            new HashMap<MethodType, QueryInvocationLiteral>()
+            {
+                {
+                    put(ANNOTATED, new QueryInvocationLiteral(ANNOTATED));
+                    put(DELEGATE, new QueryInvocationLiteral(DELEGATE));
+                    put(PARSE, new QueryInvocationLiteral(PARSE));
+                }
+            };
 
-    public QueryBuilder build(RepositoryMethod method)
+    public QueryBuilder build(RepositoryMethod method, CdiQueryInvocationContext context)
     {
-        QueryBuilder builder = queryBuilder.select(new QueryInvocationLiteral(method.getMethodType())).get();
+        DependentProvider<QueryBuilder> builder = BeanProvider.getDependent(
+                QueryBuilder.class, LITERALS.get(method.getMethodType()));
+        context.addDestroyable(new DependentProviderDestroyable(builder));
         if (method.returns(QueryResult.class))
         {
-            return new WrappedQueryBuilder(builder);
+            return new WrappedQueryBuilder(builder.get());
         }
-        return builder;
+        return builder.get();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryContextHolder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryContextHolder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryContextHolder.java
index c17e4f9..a3eab9e 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryContextHolder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryContextHolder.java
@@ -40,6 +40,7 @@ public class CdiQueryContextHolder
 
     public void dispose()
     {
+        context.get().cleanup();
         context.remove();
     }
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
index ac1bdf0..ac130f3 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
@@ -27,6 +27,7 @@ import javax.persistence.Query;
 
 import org.apache.deltaspike.data.impl.meta.RepositoryMethod;
 import org.apache.deltaspike.data.impl.param.Parameters;
+import org.apache.deltaspike.data.impl.util.bean.Destroyable;
 import org.apache.deltaspike.data.spi.QueryInvocationContext;
 
 public class CdiQueryInvocationContext implements QueryInvocationContext
@@ -41,6 +42,7 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
     private final RepositoryMethod repoMethod;
     private final List<QueryStringPostProcessor> queryPostProcessors;
     private final List<JpaQueryPostProcessor> jpaPostProcessors;
+    private final List<Destroyable> cleanup;
 
     private String queryString;
 
@@ -56,6 +58,7 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
         this.entityClass = repoMethod.getRepository().getEntityClass();
         this.queryPostProcessors = new LinkedList<QueryStringPostProcessor>();
         this.jpaPostProcessors = new LinkedList<JpaQueryPostProcessor>();
+        this.cleanup = new LinkedList<Destroyable>();
     }
 
     @Override
@@ -139,6 +142,20 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
         return result;
     }
 
+    public void addDestroyable(Destroyable destroyable)
+    {
+        cleanup.add(destroyable);
+    }
+
+    public void cleanup()
+    {
+        for (Destroyable destroy : cleanup)
+        {
+            destroy.destroy();
+        }
+        cleanup.clear();
+    }
+
     public Object executeQuery(Query jpaQuery)
     {
         return repoMethod.getQueryProcessor().executeQuery(jpaQuery);

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerLookup.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerLookup.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerLookup.java
index 6d866ca..25885aa 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerLookup.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityManagerLookup.java
@@ -23,6 +23,8 @@ import javax.enterprise.inject.Instance;
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
 
+import org.apache.deltaspike.core.api.provider.BeanProvider;
+import org.apache.deltaspike.core.api.provider.DependentProvider;
 import org.apache.deltaspike.data.api.EntityManagerResolver;
 import org.apache.deltaspike.data.impl.meta.RepositoryComponent;
 
@@ -33,27 +35,26 @@ public class EntityManagerLookup
     @Any
     private Instance<EntityManager> entityManager;
 
-    @Inject
-    @Any
-    private Instance<EntityManagerResolver> entityManagerResolver;
-
     public EntityManager lookupFor(RepositoryComponent repository)
     {
         if (repository.hasEntityManagerResolver())
         {
-            EntityManagerResolver resolver = lookupResolver(repository.getEntityManagerResolverClass());
-            EntityManager result = resolver.resolveEntityManager();
+            DependentProvider<? extends EntityManagerResolver> resolver =
+                    lookupResolver(repository.getEntityManagerResolverClass());
+            EntityManager result = resolver.get().resolveEntityManager();
             if (repository.getEntityManagerFlushMode() != null)
             {
                 result.setFlushMode(repository.getEntityManagerFlushMode());
             }
+            resolver.destroy();
         }
         return entityManager.get();
     }
 
-    private EntityManagerResolver lookupResolver(
+    private DependentProvider<? extends EntityManagerResolver> lookupResolver(
             Class<? extends EntityManagerResolver> resolverClass)
     {
-        return entityManagerResolver.select(resolverClass).get();
+        DependentProvider<? extends EntityManagerResolver> resolver = BeanProvider.getDependent(resolverClass);
+        return resolver;
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
index fc23a77..8b4e522 100755
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/QueryHandler.java
@@ -72,7 +72,7 @@ public class QueryHandler implements Serializable, InvocationHandler
             RepositoryComponent repo = components.lookupComponent(candidates);
             RepositoryMethod repoMethod = components.lookupMethod(repo.getRepositoryClass(),
method);
             queryContext = createContext(proxy, method, args, repo, repoMethod);
-            QueryBuilder builder = queryBuilder.build(repoMethod);
+            QueryBuilder builder = queryBuilder.build(repoMethod, queryContext);
             Object result = builder.execute(queryContext);
             return result;
         }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/BeanDestroyable.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/BeanDestroyable.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/BeanDestroyable.java
new file mode 100644
index 0000000..9e6c88b
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/BeanDestroyable.java
@@ -0,0 +1,44 @@
+/*
+ * 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.util.bean;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.Bean;
+
+public class BeanDestroyable<T> implements Destroyable
+{
+
+    private final Bean<T> bean;
+    private final T instance;
+    private final CreationalContext<T> creationalContext;
+
+    public BeanDestroyable(Bean<T> bean, T instance, CreationalContext<T> creationalContext)
+    {
+        this.bean = bean;
+        this.instance = instance;
+        this.creationalContext = creationalContext;
+    }
+
+    @Override
+    public void destroy()
+    {
+        bean.destroy(instance, creationalContext);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/DependentProviderDestroyable.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/DependentProviderDestroyable.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/DependentProviderDestroyable.java
new file mode 100644
index 0000000..b2610e1
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/DependentProviderDestroyable.java
@@ -0,0 +1,39 @@
+/*
+ * 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.util.bean;
+
+import org.apache.deltaspike.core.api.provider.DependentProvider;
+
+public class DependentProviderDestroyable implements Destroyable
+{
+
+    private final DependentProvider<?> dependent;
+
+    public DependentProviderDestroyable(DependentProvider<?> dependent)
+    {
+        this.dependent = dependent;
+    }
+
+    @Override
+    public void destroy()
+    {
+        dependent.destroy();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/92c0e773/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/Destroyable.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/Destroyable.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/Destroyable.java
new file mode 100644
index 0000000..6f1c931
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/bean/Destroyable.java
@@ -0,0 +1,24 @@
+/*
+ * 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.util.bean;
+
+public interface Destroyable
+{
+    void destroy();
+}


Mime
View raw message