blob: c44c5bf1e3f9f149a2a3e70a3964b9aeb6f57dfa [file] [log] [blame]
Pierre De Ropa97580d2010-05-24 21:42:18 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.dm.samples.annotation;
20
21import java.util.Dictionary;
22import java.util.concurrent.CopyOnWriteArrayList;
23
24import org.apache.felix.dm.annotation.api.PropertyMetaData;
Pierre De Rop2f824c02010-05-25 19:18:14 +000025import org.apache.felix.dm.annotation.api.FactoryConfigurationAdapterService;
Pierre De Rop9eca99b2010-07-04 21:07:59 +000026import org.apache.felix.dm.annotation.api.ServiceDependency;
27import org.apache.felix.dm.annotation.api.Start;
28import org.osgi.service.log.LogService;
Pierre De Ropa97580d2010-05-24 21:42:18 +000029
30/**
31 * A Dictionary Service. This service uses a FactoryConfigurationAdapterService annotation,
32 * allowing to instantiate this service from webconsole. This annotation will actually register
Pierre De Rop9eca99b2010-07-04 21:07:59 +000033 * a ManagedServiceFactory in the registry, and also supports meta types for configuring this
34 * service from WebConsole.
Pierre De Ropa97580d2010-05-24 21:42:18 +000035 *
36 * You must configure at least one Dictionary from web console, since the SpellCheck won't start if no Dictionary
37 * Service is available.
38 */
39@FactoryConfigurationAdapterService(
40 factoryPid="DictionaryServiceFactory",
41 propagate=true,
42 updated="updated",
43 heading="Dictionary Services",
44 description="Declare here some Dictionary instances, allowing to instantiates some DictionaryService services for a given dictionary language",
45 metadata={
46 @PropertyMetaData(
47 heading="Dictionary Language",
48 description="Declare here the language supported by this dictionary. " +
49 "This property will be propagated with the Dictionary Service properties.",
50 defaults={"en"},
51 id="lang",
Pierre De Rop9eca99b2010-07-04 21:07:59 +000052 cardinality=0),
Pierre De Ropa97580d2010-05-24 21:42:18 +000053 @PropertyMetaData(
54 heading="Dictionary words",
Pierre De Ropb14972d2010-09-14 19:42:56 +000055 description="Declare here the list of words supported by this dictionary. This properties starts with a Dot and won't be propagated with Dictionary OSGi service properties.",
Pierre De Ropa97580d2010-05-24 21:42:18 +000056 defaults={"hello", "world"},
Pierre De Ropb14972d2010-09-14 19:42:56 +000057 id=DictionaryImpl.WORDS,
Pierre De Ropa97580d2010-05-24 21:42:18 +000058 cardinality=Integer.MAX_VALUE)
59 }
60)
61public class DictionaryImpl implements DictionaryService
62{
63 /**
Pierre De Ropb14972d2010-09-14 19:42:56 +000064 * The key of our config admin dictionary values. This key stats with a "." (dot), meaning
65 * that this property won't be propagated along with our OSGi service properties.
66 */
67 final static String WORDS = ".words";
68
69 /**
Pierre De Ropa97580d2010-05-24 21:42:18 +000070 * We store all configured words in a thread-safe data structure, because ConfigAdmin
71 * may invoke our updated method at any time.
72 */
73 private CopyOnWriteArrayList<String> m_words = new CopyOnWriteArrayList<String>();
74
75 /**
Pierre De Rop9eca99b2010-07-04 21:07:59 +000076 * We'll use the OSGi log service for logging. If no log service is available, then we'll use a NullObject.
77 */
78 @ServiceDependency(required = false)
79 private LogService m_log;
80
81 /**
82 * Our Dictionary language.
83 */
84 private String m_lang;
85
86 /**
Pierre De Rop2f824c02010-05-25 19:18:14 +000087 * Our service will be initialized from ConfigAdmin.
Pierre De Ropb14972d2010-09-14 19:42:56 +000088 * @param config The configuration where we'll lookup our words list (key=".words").
Pierre De Ropa97580d2010-05-24 21:42:18 +000089 */
90 protected void updated(Dictionary<String, ?> config) {
Pierre De Rop9eca99b2010-07-04 21:07:59 +000091 m_lang = (String) config.get("lang");
Pierre De Ropa97580d2010-05-24 21:42:18 +000092 m_words.clear();
Pierre De Ropb14972d2010-09-14 19:42:56 +000093 String[] words = (String[]) config.get(WORDS);
Pierre De Ropa97580d2010-05-24 21:42:18 +000094 for (String word : words) {
95 m_words.add(word);
96 }
97 }
Pierre De Rop9eca99b2010-07-04 21:07:59 +000098
99 /**
100 * A new Dictionary Service is starting (because a new factory configuration has been created
101 * from webconsole).
102 */
103 @Start
104 protected void start()
105 {
106 m_log.log(LogService.LOG_INFO, "Starting Dictionary Service with language: " + m_lang);
107 }
Pierre De Ropa97580d2010-05-24 21:42:18 +0000108
109 /**
110 * Check if a word exists if the list of words we have been configured from ConfigAdmin/WebConsole.
111 */
112 public boolean checkWord(String word)
113 {
114 return m_words.contains(word);
115 }
Pierre De Rop9eca99b2010-07-04 21:07:59 +0000116
117 @Override
118 public String toString()
119 {
120 return "Dictionary: language=" + m_lang + ", words=" + m_words;
121 }
Pierre De Ropa97580d2010-05-24 21:42:18 +0000122}