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 | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 26 | import org.apache.felix.dm.annotation.api.ServiceDependency; |
| 27 | import org.apache.felix.dm.annotation.api.Start; |
| 28 | import org.osgi.service.log.LogService; |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 29 | |
| 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 Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 33 | * a ManagedServiceFactory in the registry, and also supports meta types for configuring this |
| 34 | * service from WebConsole. |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 35 | * |
| 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 Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 52 | cardinality=0), |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 53 | @PropertyMetaData( |
| 54 | heading="Dictionary words", |
Pierre De Rop | b14972d | 2010-09-14 19:42:56 +0000 | [diff] [blame] | 55 | 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 Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 56 | defaults={"hello", "world"}, |
Pierre De Rop | b14972d | 2010-09-14 19:42:56 +0000 | [diff] [blame] | 57 | id=DictionaryImpl.WORDS, |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 58 | cardinality=Integer.MAX_VALUE) |
| 59 | } |
| 60 | ) |
| 61 | public class DictionaryImpl implements DictionaryService |
| 62 | { |
| 63 | /** |
Pierre De Rop | b14972d | 2010-09-14 19:42:56 +0000 | [diff] [blame] | 64 | * 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 Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 70 | * 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 Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 76 | * 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 Rop | 2f824c0 | 2010-05-25 19:18:14 +0000 | [diff] [blame] | 87 | * Our service will be initialized from ConfigAdmin. |
Pierre De Rop | b14972d | 2010-09-14 19:42:56 +0000 | [diff] [blame] | 88 | * @param config The configuration where we'll lookup our words list (key=".words"). |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 89 | */ |
| 90 | protected void updated(Dictionary<String, ?> config) { |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 91 | m_lang = (String) config.get("lang"); |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 92 | m_words.clear(); |
Pierre De Rop | b14972d | 2010-09-14 19:42:56 +0000 | [diff] [blame] | 93 | String[] words = (String[]) config.get(WORDS); |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 94 | for (String word : words) { |
| 95 | m_words.add(word); |
| 96 | } |
| 97 | } |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 98 | |
| 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 Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 108 | |
| 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 Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 116 | |
| 117 | @Override |
| 118 | public String toString() |
| 119 | { |
| 120 | return "Dictionary: language=" + m_lang + ", words=" + m_words; |
| 121 | } |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 122 | } |