You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by gp...@apache.org on 2012/01/01 18:21:18 UTC

git commit: DELTASPIKE-41 example usage of BeanProvider

Updated Branches:
  refs/heads/master a725d08c1 -> 406830383


DELTASPIKE-41 example usage of BeanProvider


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

Branch: refs/heads/master
Commit: 406830383507096e14891bcaf0136b4407714ee2
Parents: a725d08
Author: gpetracek <gp...@apache.org>
Authored: Sun Jan 1 17:57:46 2012 +0100
Committer: gpetracek <gp...@apache.org>
Committed: Sun Jan 1 18:19:48 2012 +0100

----------------------------------------------------------------------
 .../deltaspike/core/api/provider/BeanProvider.java |   21 ++++++++++-
 .../beanmanagement/SimpleBeanLookupExample.java    |   29 +++++++++++++--
 .../example/echo/DefaultEchoService.java           |    2 +
 .../example/optional/OptionalService.java          |   26 +++++++++++++
 4 files changed, 74 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/40683038/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
index 84a02ab..39d9b4f 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
@@ -127,7 +127,9 @@ public final class BeanProvider
     }
 
     /**
-     * <p>Get a list of Contextual References by it's type independent of the qualifier.
+     * <p>Get a list of Contextual References by it's type independent of the qualifier
+     * (including dependent scoped beans).
+     *
      * You can use this method to get all contextual references of a given type.
      * A 'Contextual Reference' is a proxy which will automatically resolve
      * the correct contextual instance when you access any method.</p>
@@ -146,6 +148,23 @@ public final class BeanProvider
      * @param type the type of the bean in question
      * @param optional if <code>true</code> it will return an empty list if no bean could be found or created.
      *                 Otherwise it will throw an {@code IllegalStateException}
+     * @param <T> target type
+     * @return the resolved list of Contextual Reference or an empty-list if optional is true
+     */
+    public static <T> List<T> getContextualReferences(Class<T> type,
+                                                      boolean optional)
+    {
+        return getContextualReferences(type, optional, true);
+    }
+
+    /**
+     * <p>Get a list of Contextual References by it's type independent of the qualifier.
+     *
+     * Further details are available at {@link #getContextualReferences(Class, boolean)}
+     *
+     * @param type the type of the bean in question
+     * @param optional if <code>true</code> it will return an empty list if no bean could be found or created.
+     *                 Otherwise it will throw an {@code IllegalStateException}
      * @param includeDefaultScopedBeans specifies if dependent scoped beans should be included in the in the result
      * @param <T> target type
      * @return the resolved list of Contextual Reference or an empty-list if optional is true

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/40683038/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/beanmanagement/SimpleBeanLookupExample.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/beanmanagement/SimpleBeanLookupExample.java b/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/beanmanagement/SimpleBeanLookupExample.java
index 76df4b1..32dd5a4 100644
--- a/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/beanmanagement/SimpleBeanLookupExample.java
+++ b/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/beanmanagement/SimpleBeanLookupExample.java
@@ -20,7 +20,9 @@ package org.apache.deltaspike.example.beanmanagement;
 
 import org.apache.deltaspike.core.api.provider.BeanProvider;
 import org.apache.deltaspike.example.CdiContainer;
+import org.apache.deltaspike.example.echo.DefaultEchoService;
 import org.apache.deltaspike.example.echo.EchoService;
+import org.apache.deltaspike.example.optional.OptionalService;
 
 import java.util.List;
 import java.util.logging.Logger;
@@ -40,11 +42,11 @@ public class SimpleBeanLookupExample
     {
         CdiContainer.start();
 
-        List<EchoService> echoServiceList = BeanProvider.getContextualReferences(EchoService.class, false, true);
+        List<EchoService> echoServiceList = BeanProvider.getContextualReferences(EchoService.class, false);
 
         for(EchoService echoService : echoServiceList)
         {
-            LOG.info(echoService.echo("Hello CDI beans!"));
+            LOG.info(echoService.echo("Hello CDI bean!"));
         }
 
         LOG.info("---");
@@ -53,9 +55,30 @@ public class SimpleBeanLookupExample
 
         for(EchoService echoService : echoServiceList)
         {
-            LOG.info(echoService.echo("Hello non dependent CDI scoped beans!"));
+            LOG.info(echoService.echo("Hello non dependent CDI scoped bean!"));
         }
 
+        LOG.info("---");
+
+        EchoService defaultEchoService = BeanProvider.getContextualReference(DefaultEchoService.class, false);
+
+        LOG.info(defaultEchoService.echo("Hello explicitly resolved CDI bean!"));
+
+        defaultEchoService = BeanProvider.getContextualReference(EchoService.class, false, "defaultEchoService");
+
+        LOG.info(defaultEchoService.echo("Hello CDI bean resolved by name!"));
+
+        OptionalService optionalService = BeanProvider.getContextualReference(OptionalService.class, true);
+        
+        if(optionalService == null)
+        {
+            LOG.info("No (optional) implementation found for " + OptionalService.class.getName());
+        }
+        else 
+        {
+            LOG.severe("Unexpected implementation found: " + optionalService.getClass().getName());
+        }
+        
         CdiContainer.stop();
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/40683038/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/echo/DefaultEchoService.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/echo/DefaultEchoService.java b/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/echo/DefaultEchoService.java
index d722a1d..e0f4887 100644
--- a/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/echo/DefaultEchoService.java
+++ b/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/echo/DefaultEchoService.java
@@ -19,11 +19,13 @@
 package org.apache.deltaspike.example.echo;
 
 import javax.enterprise.context.Dependent;
+import javax.inject.Named;
 
 /**
  * Default implementation
  */
 @Dependent
+@Named("defaultEchoService")
 public class DefaultEchoService implements EchoService
 {
     /**

http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/40683038/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/optional/OptionalService.java
----------------------------------------------------------------------
diff --git a/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/optional/OptionalService.java b/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/optional/OptionalService.java
new file mode 100644
index 0000000..dc8ef6a
--- /dev/null
+++ b/deltaspike/examples/jse-owb-examples/src/main/java/org/apache/deltaspike/example/optional/OptionalService.java
@@ -0,0 +1,26 @@
+/*
+ * 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.example.optional;
+
+/**
+ * Interface without implementation to show the lookup of optional implementations
+ */
+public interface OptionalService
+{
+}