Author: pderop
Date: Fri Jan 15 22:31:42 2010
New Revision: 899834
URL: http://svn.apache.org/viewvc?rev=899834&view=rev
Log:
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)
Added:
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
Removed:
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceConsumer.java
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceInterface.java
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/ServiceProvider.java
Modified:
felix/trunk/dependencymanager/samples/annotation/pom.xml
Modified: felix/trunk/dependencymanager/samples/annotation/pom.xml
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples/annotation/pom.xml?rev=899834&r1=899833&r2=899834&view=diff
==============================================================================
--- felix/trunk/dependencymanager/samples/annotation/pom.xml (original)
+++ felix/trunk/dependencymanager/samples/annotation/pom.xml Fri Jan 15
22:31:42 2010
@@ -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>
Added:
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java?rev=899834&view=auto
==============================================================================
---
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java
(added)
+++
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryService.java
Fri Jan 15 22:31:42 2010
@@ -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:[email protected]">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);
+}
Added:
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java?rev=899834&view=auto
==============================================================================
---
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java
(added)
+++
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java
Fri Jan 15 22:31:42 2010
@@ -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);
+ }
+}
Added:
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java?rev=899834&view=auto
==============================================================================
---
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java
(added)
+++
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java
Fri Jan 15 22:31:42 2010
@@ -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);
+ }
+ }
+}
Added:
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java?rev=899834&view=auto
==============================================================================
---
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java
(added)
+++
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java
Fri Jan 15 22:31:42 2010
@@ -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);
+ }
+}
Added:
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java?rev=899834&view=auto
==============================================================================
---
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
(added)
+++
felix/trunk/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
Fri Jan 15 22:31:42 2010
@@ -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");
+ }
+}