Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 1 | /* |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more contributor license |
| 3 | * agreements. See the NOTICE file distributed with this work for additional information |
| 4 | * regarding copyright ownership. The ASF licenses this file to you under the Apache License, |
| 5 | * Version 2.0 (the "License"); you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless |
| 7 | * required by applicable law or agreed to in writing, software distributed under the License is |
| 8 | * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 9 | * or implied. See the License for the specific language governing permissions and limitations |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 10 | * under the License. |
| 11 | */ |
| 12 | package org.apache.felix.dm.samples.annotation; |
| 13 | |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 14 | import java.util.Dictionary; |
| 15 | import java.util.concurrent.CopyOnWriteArrayList; |
| 16 | |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 17 | import org.apache.felix.dm.annotation.api.AspectService; |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 18 | import org.apache.felix.dm.annotation.api.PropertyMetaData; |
Pierre De Rop | 2f824c0 | 2010-05-25 19:18:14 +0000 | [diff] [blame] | 19 | import org.apache.felix.dm.annotation.api.ConfigurationDependency; |
| 20 | import org.apache.felix.dm.annotation.api.ServiceDependency; |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 21 | import org.apache.felix.dm.annotation.api.Start; |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 22 | import org.osgi.service.log.LogService; |
| 23 | |
| 24 | /** |
Pierre De Rop | d4ac53c | 2010-07-04 21:27:13 +0000 | [diff] [blame] | 25 | * This aspect applies to the English DictionaryService, and allows to decorate it with some |
| 26 | * custom English words, which are configurable from WebConsole. |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 27 | */ |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 28 | @AspectService(ranking = 10, filter = "(lang=en)") |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 29 | public class DictionaryAspect implements DictionaryService |
| 30 | { |
| 31 | /** |
| 32 | * This is the service this aspect is applying to. |
| 33 | */ |
| 34 | private volatile DictionaryService m_originalDictionary; |
| 35 | |
| 36 | /** |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 37 | * We store all configured words in a thread-safe data structure, because ConfigAdmin may |
| 38 | * invoke our updated method at any time. |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 39 | */ |
| 40 | private CopyOnWriteArrayList<String> m_words = new CopyOnWriteArrayList<String>(); |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 41 | |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 42 | /** |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 43 | * We'll use the OSGi log service for logging. If no log service is available, then we'll |
| 44 | * use a NullObject. |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 45 | */ |
| 46 | @ServiceDependency(required = false) |
| 47 | private LogService m_log; |
| 48 | |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 49 | /** |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 50 | * Defines a configuration dependency for retrieving our english custom words (by default, |
| 51 | * our PID is our full class name). |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 52 | */ |
| 53 | @ConfigurationDependency( |
Pierre De Rop | 6408b0d | 2011-05-07 10:19:57 +0000 | [diff] [blame] | 54 | pid="DictionaryAspectPID", |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 55 | propagate = false, |
| 56 | heading = "Aspect Dictionary", |
| 57 | description = "Declare here some additional english words", |
| 58 | metadata = { |
| 59 | @PropertyMetaData(heading = "Dictionary aspect words", |
| 60 | description = "Declare here the list of english words to be added into the default english dictionary", |
| 61 | defaults = { "aspect" }, |
| 62 | id = "words", |
| 63 | cardinality = Integer.MAX_VALUE) |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 64 | } |
| 65 | ) |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 66 | protected void updated(Dictionary<String, ?> config) |
| 67 | { |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 68 | m_words.clear(); |
| 69 | String[] words = (String[]) config.get("words"); |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 70 | for (String word : words) |
| 71 | { |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 72 | m_words.add(word); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 77 | * Our Aspect Service is starting and is about to be registered in the OSGi regsitry. |
| 78 | */ |
| 79 | @Start |
| 80 | protected void start() |
| 81 | { |
| 82 | m_log.log(LogService.LOG_INFO, "Starting aspect Dictionary with words: " + m_words |
| 83 | + "; original dictionary service=" + m_originalDictionary); |
| 84 | } |
| 85 | |
| 86 | /** |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 87 | * Checks if a word is found from our custom word list. if not, delegate to the decorated |
| 88 | * dictionary. |
| 89 | */ |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 90 | public boolean checkWord(String word) |
| 91 | { |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 92 | m_log.log(LogService.LOG_INFO, "DictionaryAspect: checking word " + word |
| 93 | + " (original dictionary=" + m_originalDictionary + ")"); |
| 94 | if (m_words.contains(word)) |
| 95 | { |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | return m_originalDictionary.checkWord(word); |
| 99 | } |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 100 | |
| 101 | public String toString() |
| 102 | { |
| 103 | return "DictionaryAspect: words=" + m_words + "; original dictionary=" |
| 104 | + m_originalDictionary; |
| 105 | } |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 106 | } |