Simplified sample code. Use MetaType for allowing the EnglishDictionary through ConfigAdmin/WebConsole
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@907332 13f79535-47bb-0310-9956-ffa450edef68
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
index 6163be2..9426a3f 100644
--- 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
@@ -19,19 +19,42 @@
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.Param;
+import org.apache.felix.dm.annotation.api.Properties;
+import org.apache.felix.dm.annotation.api.Property;
import org.apache.felix.dm.annotation.api.Service;
/**
- * An English Dictionary Service. We'll be configured using OSGi Config Admin.
+ * An English Dictionary Service. We provide here our Properties MetaData in order to let webconsole configure us.
+ * You must configure the PID that corresponds to this class through web console in order to activate this service.
*/
@Service(properties={@Param(name="language", value="en")})
+@Properties(
+ heading="English Dictionary",
+ description = "Configuration for the EnglishDictionary Service",
+ properties={
+ @Property(heading="English Words",
+ description="Declares here some valid english words",
+ defaults={"hello", "world"},
+ id=EnglishDictionary.WORDS,
+ cardinality=Integer.MAX_VALUE
+ )
+ }
+)
public class EnglishDictionary implements DictionaryService
{
+ /**
+ * The id of our Configuration Admin property key.
+ */
+ public final static String WORDS = "words";
+
+ /**
+ * 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>();
/**
@@ -42,14 +65,14 @@
@ConfigurationDependency
protected void updated(Dictionary<String, ?> config) {
m_words.clear();
- List<String> words = (List<String>) config.get("words");
+ 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.
+ * Check if a word exists if the list of words we have been configured from ConfigAdmin/WebConsole.
*/
public boolean checkWord(String 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
deleted file mode 100644
index 07842b7..0000000
--- a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/EnglishDictionaryConfiguration.java
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * 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.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);
- 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
deleted file mode 100644
index fec78ac..0000000
--- a/dependencymanager/samples/annotation/src/main/java/org/apache/felix/dm/samples/annotation/FrenchDictionary.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.Arrays;
-import java.util.List;
-
-import org.apache.felix.dm.annotation.api.Param;
-import org.apache.felix.dm.annotation.api.Service;
-
-/**
- * A French Dictionary Service.
- */
-@Service(properties={@Param(name="language", value="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);
- }
-}