blob: 9b1c34651a061147d3ebc296672c244c397f09ec [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 Ropa97580d2010-05-24 21:42:18 +000026
27/**
28 * A Dictionary Service. This service uses a FactoryConfigurationAdapterService annotation,
29 * allowing to instantiate this service from webconsole. This annotation will actually register
Pierre De Rop98ab87a2010-06-11 06:20:51 +000030 * a ManagedServiceFactory in the registry, and also supports meta types for describing metadata of
Pierre De Ropa97580d2010-05-24 21:42:18 +000031 * all configuration properties.
32 *
33 * You must configure at least one Dictionary from web console, since the SpellCheck won't start if no Dictionary
34 * Service is available.
35 */
36@FactoryConfigurationAdapterService(
37 factoryPid="DictionaryServiceFactory",
38 propagate=true,
39 updated="updated",
40 heading="Dictionary Services",
41 description="Declare here some Dictionary instances, allowing to instantiates some DictionaryService services for a given dictionary language",
42 metadata={
43 @PropertyMetaData(
44 heading="Dictionary Language",
45 description="Declare here the language supported by this dictionary. " +
46 "This property will be propagated with the Dictionary Service properties.",
47 defaults={"en"},
48 id="lang",
49 cardinality=1),
50 @PropertyMetaData(
51 heading="Dictionary words",
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000052 description="Declare here the list of words supported by this dictionary.",
Pierre De Ropa97580d2010-05-24 21:42:18 +000053 defaults={"hello", "world"},
54 id="words",
55 cardinality=Integer.MAX_VALUE)
56 }
57)
58public class DictionaryImpl implements DictionaryService
59{
60 /**
61 * We store all configured words in a thread-safe data structure, because ConfigAdmin
62 * may invoke our updated method at any time.
63 */
64 private CopyOnWriteArrayList<String> m_words = new CopyOnWriteArrayList<String>();
65
66 /**
Pierre De Rop2f824c02010-05-25 19:18:14 +000067 * Our service will be initialized from ConfigAdmin.
Pierre De Ropa97580d2010-05-24 21:42:18 +000068 * @param config The configuration where we'll lookup our words list (key="words").
69 */
70 protected void updated(Dictionary<String, ?> config) {
71 m_words.clear();
72 String[] words = (String[]) config.get("words");
73 for (String word : words) {
74 m_words.add(word);
75 }
76 }
77
78 /**
79 * Check if a word exists if the list of words we have been configured from ConfigAdmin/WebConsole.
80 */
81 public boolean checkWord(String word)
82 {
83 return m_words.contains(word);
84 }
85}