Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 19 | package org.apache.felix.dm.samples.annotation; |
| 20 | |
| 21 | import java.util.Dictionary; |
| 22 | import java.util.concurrent.CopyOnWriteArrayList; |
| 23 | |
| 24 | import org.apache.felix.dm.annotation.api.PropertyMetaData; |
Pierre De Rop | 2f824c0 | 2010-05-25 19:18:14 +0000 | [diff] [blame] | 25 | import org.apache.felix.dm.annotation.api.FactoryConfigurationAdapterService; |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 26 | |
| 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 Rop | 98ab87a | 2010-06-11 06:20:51 +0000 | [diff] [blame] | 30 | * a ManagedServiceFactory in the registry, and also supports meta types for describing metadata of |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 31 | * 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 Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 52 | description="Declare here the list of words supported by this dictionary.", |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 53 | defaults={"hello", "world"}, |
| 54 | id="words", |
| 55 | cardinality=Integer.MAX_VALUE) |
| 56 | } |
| 57 | ) |
| 58 | public 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 Rop | 2f824c0 | 2010-05-25 19:18:14 +0000 | [diff] [blame] | 67 | * Our service will be initialized from ConfigAdmin. |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 68 | * @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 | } |