changed the annotation sample code with a SpellCheck Felix Shell command: the command uses two dictionaries in order to check spelling (english dictionary, and french dictionary)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@899834 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/samples/annotation/pom.xml b/dependencymanager/samples/annotation/pom.xml
index c26307c..345d401 100644
--- a/dependencymanager/samples/annotation/pom.xml
+++ b/dependencymanager/samples/annotation/pom.xml
@@ -9,6 +9,21 @@
 

   <dependencies>

     <dependency>

+      <groupId>org.osgi</groupId>

+      <artifactId>org.osgi.core</artifactId>

+      <version>4.1.0</version>

+    </dependency>

+    <dependency>

+      <groupId>org.osgi</groupId>

+      <artifactId>org.osgi.compendium</artifactId>

+      <version>4.1.0</version>

+    </dependency>

+    <dependency>

+      <groupId>org.apache.felix</groupId>

+      <artifactId>org.apache.felix.shell</artifactId>

+      <version>1.4.1</version>

+    </dependency>

+    <dependency>

       <groupId>org.apache.felix</groupId>

       <artifactId>org.apache.felix.dependencymanager.annotation</artifactId>

       <version>3.0.0-SNAPSHOT</version>

@@ -53,7 +68,6 @@
           </execution>

         </executions>

       </plugin>

-

     </plugins>

   </build>

 </project>

diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java
new file mode 100644
index 0000000..33a1695
--- /dev/null
+++ b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java
@@ -0,0 +1,18 @@
+package org.apache.felix.dm.samples.annotation;
+
+/**
+ * A simple service interface that defines a dictionary service. A dictionary
+ * service simply verifies the existence of a word. 
+ * 
+ * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
+ */
+public interface DictionaryService
+{
+    /**
+     * Check for the existence of a word.
+     * 
+     * @param word the word to be checked.
+     * @return true if the word is in the dictionary, false otherwise.
+     */
+    public boolean checkWord(String word);
+}
diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java
new file mode 100644
index 0000000..60e01ed
--- /dev/null
+++ b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java
@@ -0,0 +1,39 @@
+package org.apache.felix.dm.samples.annotation;
+
+import java.util.Dictionary;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.felix.dm.annotation.api.ConfigurationDependency;
+import org.apache.felix.dm.annotation.api.Service;
+
+/**
+ * An English Dictionary Service. We'll be configured using OSGi Config Admin.
+ */
+@Service(properties={"language=en"})
+public class EnglishDictionary implements DictionaryService
+{
+    private CopyOnWriteArrayList<String> m_words = new CopyOnWriteArrayList<String>();
+    
+    /**
+     * Our service will be initialized from ConfigAdmin, so we define here a configuration dependency
+     * (by default, our PID is our full class name).
+     * @param config The configuration where we'll lookup our words list (key="words").
+     */
+    @ConfigurationDependency
+    protected void updated(Dictionary<String, ?> config) {
+        m_words.clear();
+        List<String> words = (List<String>) config.get("words");
+        for (String word : words) {
+            m_words.add(word);
+        }
+    }
+           
+    /**
+     * Check if a word exists if the list of words we have been configured from ConfigAdmin.
+     */
+    public boolean checkWord(String word)
+    {
+        return m_words.contains(word);
+    }
+}
diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java
new file mode 100644
index 0000000..8bae71a
--- /dev/null
+++ b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java
@@ -0,0 +1,63 @@
+package org.apache.felix.dm.samples.annotation;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Hashtable;
+import java.util.List;
+
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.dm.annotation.api.Start;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.log.LogService;
+
+/**
+ * This service creates a Configuration for the EnglishDictionary service, using OSGi ConfigAdmin.
+ */
+@Service
+public class EnglishDictionaryConfiguration
+{
+    /**
+     * OSGi Configuration Admin Service.
+     */
+    @ServiceDependency
+    ConfigurationAdmin m_cm;
+
+    /**
+     * OSGi log Service (null object if the no log service available).
+     */
+    @ServiceDependency(required = false)
+    LogService m_log;
+
+    /**
+     * All our dependencies are satisfied: configure the EnglishDictionary service.
+     * We'll use the EnglishDictionary full java class name as the PID.
+     */
+    @Start
+    protected void start()
+    {
+        try
+        {
+            String PID = EnglishDictionary.class.getName();
+            Configuration config = m_cm.getConfiguration(PID, null);
+            if (config.getBundleLocation() != null)
+            {
+                config.setBundleLocation(null);
+            }
+
+            config.update(new Hashtable<String, List<String>>()
+            {
+                {
+                    put("words", Arrays.asList("hello", "world"));
+                }
+            });
+            m_log.log(LogService.LOG_INFO, "registered configuration for PID " + PID);
+        }
+        catch (IOException e)
+        {
+            m_log.log(LogService.LOG_WARNING,
+                "unexpected exception while initializing english dictionary configuration", e);
+        }
+    }
+}
diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java
new file mode 100644
index 0000000..7a00a75
--- /dev/null
+++ b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java
@@ -0,0 +1,23 @@
+package org.apache.felix.dm.samples.annotation;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.felix.dm.annotation.api.Service;
+
+/**
+ * A French Dictionary Service.
+ */
+@Service(properties={"language=fr"})
+public class FrenchDictionary implements DictionaryService
+{
+    private List<String> m_words = Arrays.asList("bonjour", "salut");
+               
+    /**
+     * Check if a word exists if the list of words we have been configured from ConfigAdmin.
+     */
+    public boolean checkWord(String word)
+    {
+        return m_words.contains(word);
+    }
+}
diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceConsumer.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceConsumer.java
deleted file mode 100644
index 108bc6a..0000000
--- a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceConsumer.java
+++ /dev/null
@@ -1,31 +0,0 @@
-package org.apache.felix.dm.samples.annotation;
-
-import org.apache.felix.dm.annotation.api.Service;
-import org.apache.felix.dm.annotation.api.ServiceDependency;
-import org.apache.felix.dm.annotation.api.Start;
-import org.apache.felix.dm.annotation.api.Stop;
-
-@Service
-public class ServiceConsumer
-{
-    @ServiceDependency
-    private volatile ServiceInterface m_service;
-
-    @ServiceDependency
-    protected void bind(ServiceInterface si)
-    {
-    }
-
-    @Start
-    protected void start()
-    {
-        System.out.println("ServiceConsumer.start");
-        m_service.doService();
-    }
-
-    @Stop
-    protected void stop()
-    {
-        System.out.println("ServiceConsumer.stop");
-    }
-}
diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceInterface.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceInterface.java
deleted file mode 100644
index d358177..0000000
--- a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceInterface.java
+++ /dev/null
@@ -1,5 +0,0 @@
-package org.apache.felix.dm.samples.annotation;
-
-public interface ServiceInterface {
-  public void doService();
-}
diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceProvider.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceProvider.java
deleted file mode 100644
index af5e276..0000000
--- a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceProvider.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package org.apache.felix.dm.samples.annotation;
-
-import org.apache.felix.dm.annotation.api.Service;
-import org.apache.felix.dm.annotation.api.Start;
-import org.apache.felix.dm.annotation.api.Stop;
-
-@Service
-public class ServiceProvider implements ServiceInterface
-{
-    public ServiceProvider()
-    {
-    }
-
-    @Start
-    public void start()
-    {
-        System.out.println("ServiceProvider.start");
-    }
-
-    @Stop
-    public void stop()
-    {
-        System.out.println("ServiceProvider.stop");
-    }
-
-    public void doService()
-    {
-        System.out.println("ServiceProvider.doService()");
-    }
-}
diff --git a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
new file mode 100644
index 0000000..aead8f9
--- /dev/null
+++ b/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
@@ -0,0 +1,88 @@
+package org.apache.felix.dm.samples.annotation;
+
+import java.io.PrintStream;
+import java.util.Map;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.felix.dm.annotation.api.Service;
+import org.apache.felix.dm.annotation.api.ServiceDependency;
+import org.apache.felix.shell.Command;
+import org.osgi.service.log.LogService;
+
+/**
+ * Felix "spellcheck" Shell Command, used to check correct word spelling.
+ */
+@Service
+public class SpellChecker implements Command
+{
+    /**
+     * We'll use the OSGi log service for logging. If no log service is available, then we'll use a NullObject.
+     */
+    @ServiceDependency(required = false)
+    private LogService m_log;
+
+    /**
+     * We'll store all Dictionaries is a CopyOnWrite list, in order to avoid method synchronization.
+     */
+    private CopyOnWriteArrayList<DictionaryService> m_dictionaries = new CopyOnWriteArrayList<DictionaryService>();
+
+    /**
+     * Inject a dictionary into this service.
+     * @param serviceProperties the dictionary OSGi service properties
+     * @param dictionary the new dictionary
+     */
+    @ServiceDependency(removed = "removeDictionary")
+    protected void addDictionary(Map<String, String> serviceProperties, DictionaryService dictionary)
+    {
+        m_log.log(LogService.LOG_INFO, "added dictionary: " + dictionary + " (language="
+            + serviceProperties.get("language") + ")");
+        m_dictionaries.add(dictionary);
+    }
+
+    /**
+     * Remove a dictionary from our service.
+     * @param dictionary
+     */
+    protected void removeDictionary(DictionaryService dictionary)
+    {
+        m_log.log(LogService.LOG_INFO, "added dictionary: " + dictionary);
+        m_dictionaries.remove(dictionary);
+    }
+
+    // --- Felix Shell Command interface ---
+
+    public String getName()
+    {
+        return "spellcheck";
+    }
+
+    public String getUsage()
+    {
+        return "spellcheck word";
+    }
+
+    public String getShortDescription()
+    {
+        return "Spell checker application using DependencyManager annotations";
+    }
+
+    public void execute(String commandLine, PrintStream out, PrintStream err)
+    {
+        String[] tokens = commandLine.split(" ");
+        if (tokens == null || tokens.length < 2)
+        {
+            err.println("Invalid parameters: " + commandLine + ". Usage: " + getUsage());
+            return;
+        }
+        String word = tokens[1];
+        for (DictionaryService dictionary : m_dictionaries)
+        {
+            if (dictionary.checkWord(tokens[1]))
+            {
+                out.println("word " + word + " is correct");
+                return;
+            }
+        }
+        err.println("word " + word + " is incorrect");
+    }
+}