Author: pderop
Date: Mon May 24 21:42:18 2010
New Revision: 947821
URL: http://svn.apache.org/viewvc?rev=947821&view=rev
Log:
reworked the sample in order to show a FactoryConfigurationAdapterService
annotation usage example
Added:
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryImpl.java
Removed:
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionary.java
Modified:
felix/trunk/dependencymanager/samples.annotation/README
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryAspect.java
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
Modified: felix/trunk/dependencymanager/samples.annotation/README
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples.annotation/README?rev=947821&r1=947820&r2=947821&view=diff
==============================================================================
--- felix/trunk/dependencymanager/samples.annotation/README (original)
+++ felix/trunk/dependencymanager/samples.annotation/README Mon May 24 21:42:18
2010
@@ -3,17 +3,25 @@ This sample illustrates the usage of the
Sample description:
This sample shows a basic "SpellChecker" application which provides a Felix
"spellcheck" shell
- command. The SpellChecker class is a Felix Shell command (it provides the
"Command" service),
+ command. The SpellChecker class is a Felix Shell command (it provides a
"Command" service),
which accepts a string as parameter. So, when you type a string, the command
just says if the
specified string has been found from one of its injected dictionaries. The
SpellChecker class has a
- required/multiple dependency over any available DictionaryService.
Currently, there is one
- "EnglishDictionary" which implements the DictionaryService. The
EnglishDictionary service uses
- MetaType, allowing to configure english words through ConfigAdmin and
WebConsole.
+ required/multiple (1..N) dependency over any available DictionaryService.
+
+ A DictionaryService is defined using a FactoryConfigurationAdapterService
annotation, allowing to
+ instantiate many DictionaryService service instances from webconsole. This
annotation actually registers
+ a ManagedServiceFactory into the Registry, and you can specify the meta type
informations regarding
+ the properties metadata.
+
So, before testing, you first have to go to webconsole Configuration panel,
and specify some
- english words in the configuration for the "English Dictionary" PID. Then,
go to the felix shell,
- and you will then see the "spellcheck" command (when typing "help").
- Notice that in the sample, you will also find an Aspect Service
(DictionaryAspect.java), which
- decorates the EnglishDictionary service, by adding an "aspect" word in the
dictionary.
+ dictionaries (see the DictionaryService Factory PID).
+ Then, go to the felix shell, and you will then see the "spellcheck" command
(when typing "help").
+
+ Notice that in the sample, you will also find a DictionaryAspect Service
(DictionaryAspect.java), which
+ decorates the EnglishDictionary service, by adding some additional words to
*all* provided
+ DictionaryService services. The DictionaryAspect also show how to use a
ConfigurationDependency annotation,
+ allowing to configure the words from webconsole. A ConfigurationDependency
actually registers a ManagedService object
+ in the Registry, which also implements a MetaTypeProvider service for
metatype support.
How to test:
@@ -31,15 +39,16 @@ How to test:
2) Start felix
3) Go to web console, in the Configuration pannel
- 4) Edit the "English Dictionary" Configuration, add some words, then click
on "save".
- At this point, the "EnglishDictionary" service will be enabled and the
SpellCheck component
- will be injected with it. Then you should see the "spellcheck" command,
when typing "help" on the shell.
+ 4) Edit the "Dictionary Services" Configuration, add some dictionaries. By
default, an English dictionary is
+ displayed. Just click on "save", then click on your refresh web browser:
you will see a new dictionary service
+ instance. At this point, a DictionaryService service will be enabled
(with the service property "lang=en"),
+ and the SpellCheck component will be injected with it. Then you should
see the "spellcheck" command, when typing
+ "help" on the shell.
5) Just type "spellcheck hello", and the command should reply a fantastic
message, like "word hello is correct".
-
-
-
-
-
-
+ 6) You can also click on the "Aspect Dictionary" button, in order to
decorate *all* Dictionaries using some custom words.
+ By default, the "aspect" word is pre configured, but you can click on the
"+" button in order to add more words.
+ Then click on Save. At this point, all DictionaryService instances will
be decorated with the aspect service.
+ So, now, if you type "spellcheck aspect", then the message: "word aspect
is correct" should be displayed.
+
Modified:
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryAspect.java
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryAspect.java?rev=947821&r1=947820&r2=947821&view=diff
==============================================================================
---
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryAspect.java
(original)
+++
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryAspect.java
Mon May 24 21:42:18 2010
@@ -18,12 +18,18 @@
*/
package org.apache.felix.dm.samples.annotation;
+import java.util.Dictionary;
+import java.util.concurrent.CopyOnWriteArrayList;
+
import org.apache.felix.dm.annotation.api.AspectService;
+import org.apache.felix.dm.annotation.api.PropertyMetaData;
+import org.apache.felix.dm.annotation.api.dependency.ConfigurationDependency;
import org.apache.felix.dm.annotation.api.dependency.ServiceDependency;
import org.osgi.service.log.LogService;
/**
- * This aspect wraps a Dictionary and checks the "aspect" word on behalf of it.
+ * This aspect applies to all available Dictionary Services, and checks some
custom words, configurable from
+ * config admin.
*/
@AspectService( ranking = 10 )
public class DictionaryAspect implements DictionaryService
@@ -34,15 +40,51 @@ public class DictionaryAspect implements
private volatile DictionaryService m_originalDictionary;
/**
+ * We store all configured words in a thread-safe data structure, because
ConfigAdmin
+ * may invoke our updated method at any time.
+ */
+ private CopyOnWriteArrayList<String> m_words = new
CopyOnWriteArrayList<String>();
+
+ /**
* 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;
+ /**
+ * Defines a configuration dependency for retrieving our custo words (by
default, our PID is our full class name).
+ * This annotation actually provides a ManagedService into the registry,
and you can specify meta information regarding
+ * all the configuration properties (required by webconsole).
+ */
+ @ConfigurationDependency(
+ heading="Aspect Dictionary",
+ description="Declare here some words to check",
+ propagate=false,
+ metadata={
+ @PropertyMetaData(
+ heading="Dictionary aspect words",
+ description="Declare here the list of words supported by this
dictionary aspect. ",
+ defaults={"aspect"},
+ id="words",
+ cardinality=Integer.MAX_VALUE)
+ }
+ )
+ protected void updated(Dictionary<String, ?> config) {
+ m_words.clear();
+ String[] words = (String[]) config.get("words");
+ for (String word : words) {
+ m_words.add(word);
+ }
+ }
+
+ /**
+ * Checks if a word is found from our custom word list. if not, delegate
to the decorated
+ * dictionary.
+ */
public boolean checkWord(String word)
{
m_log.log(LogService.LOG_DEBUG, "DictionaryAspect: checking word " +
word);
- if (word.equals("aspect")) {
+ if ("aspect".equals(word)) {
return true;
}
return m_originalDictionary.checkWord(word);
Added:
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryImpl.java
URL:
http://svn.apache.org/viewvc/felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryImpl.java?rev=947821&view=auto
==============================================================================
---
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryImpl.java
(added)
+++
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/DictionaryImpl.java
Mon May 24 21:42:18 2010
@@ -0,0 +1,87 @@
+/*
+ * 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.felix.dm.samples.annotation;
+
+import java.util.Dictionary;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.felix.dm.annotation.api.PropertyMetaData;
+import
org.apache.felix.dm.annotation.api.adapter.FactoryConfigurationAdapterService;
+
+/**
+ * A Dictionary Service. This service uses a
FactoryConfigurationAdapterService annotation,
+ * allowing to instantiate this service from webconsole. This annotation will
actually register
+ * a ManagedServiceFactory in the registry, and also support meta types for
describing metadata of
+ * all configuration properties.
+ *
+ * You must configure at least one Dictionary from web console, since the
SpellCheck won't start if no Dictionary
+ * Service is available.
+ */
+...@factoryconfigurationadapterservice(
+ factoryPid="DictionaryServiceFactory",
+ propagate=true,
+ updated="updated",
+ heading="Dictionary Services",
+ description="Declare here some Dictionary instances, allowing to
instantiates some DictionaryService services for a given dictionary language",
+ metadata={
+ @PropertyMetaData(
+ heading="Dictionary Language",
+ description="Declare here the language supported by this
dictionary. " +
+ "This property will be propagated with the Dictionary Service
properties.",
+ defaults={"en"},
+ id="lang",
+ cardinality=1),
+ @PropertyMetaData(
+ heading="Dictionary words",
+ description="Declare here the list of words supported by this
dictionary. " +
+ "This property is private and won't be propagated along with
the dictionary service property.",
+ defaults={"hello", "world"},
+ id="words",
+ cardinality=Integer.MAX_VALUE)
+ }
+)
+public class DictionaryImpl implements DictionaryService
+{
+ /**
+ * We store all configured words in a thread-safe data structure, because
ConfigAdmin
+ * may invoke our updated method at any time.
+ */
+ 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").
+ */
+ protected void updated(Dictionary<String, ?> config) {
+ m_words.clear();
+ String[] words = (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/WebConsole.
+ */
+ public boolean checkWord(String word)
+ {
+ return m_words.contains(word);
+ }
+}
Modified:
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=947821&r1=947820&r2=947821&view=diff
==============================================================================
---
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
(original)
+++
felix/trunk/dependencymanager/samples.annotation/src/main/java/org/apache/felix/dm/samples/annotation/SpellChecker.java
Mon May 24 21:42:18 2010
@@ -22,9 +22,9 @@ import java.io.PrintStream;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.felix.dm.annotation.api.Service;
-import org.apache.felix.dm.annotation.api.dependency.ServiceDependency;
import org.apache.felix.dm.annotation.api.Start;
import org.apache.felix.dm.annotation.api.Stop;
+import org.apache.felix.dm.annotation.api.dependency.ServiceDependency;
import org.apache.felix.shell.Command;
import org.osgi.service.log.LogService;