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 | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 54 | propagate = false, |
| 55 | heading = "Aspect Dictionary", |
| 56 | description = "Declare here some additional english words", |
| 57 | metadata = { |
| 58 | @PropertyMetaData(heading = "Dictionary aspect words", |
| 59 | description = "Declare here the list of english words to be added into the default english dictionary", |
| 60 | defaults = { "aspect" }, |
| 61 | id = "words", |
| 62 | cardinality = Integer.MAX_VALUE) |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 63 | } |
| 64 | ) |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 65 | protected void updated(Dictionary<String, ?> config) |
| 66 | { |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 67 | m_words.clear(); |
| 68 | String[] words = (String[]) config.get("words"); |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 69 | for (String word : words) |
| 70 | { |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 71 | m_words.add(word); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 76 | * Our Aspect Service is starting and is about to be registered in the OSGi regsitry. |
| 77 | */ |
| 78 | @Start |
| 79 | protected void start() |
| 80 | { |
| 81 | m_log.log(LogService.LOG_INFO, "Starting aspect Dictionary with words: " + m_words |
| 82 | + "; original dictionary service=" + m_originalDictionary); |
| 83 | } |
| 84 | |
| 85 | /** |
Pierre De Rop | a97580d | 2010-05-24 21:42:18 +0000 | [diff] [blame] | 86 | * Checks if a word is found from our custom word list. if not, delegate to the decorated |
| 87 | * dictionary. |
| 88 | */ |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 89 | public boolean checkWord(String word) |
| 90 | { |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 91 | m_log.log(LogService.LOG_INFO, "DictionaryAspect: checking word " + word |
| 92 | + " (original dictionary=" + m_originalDictionary + ")"); |
| 93 | if (m_words.contains(word)) |
| 94 | { |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | return m_originalDictionary.checkWord(word); |
| 98 | } |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 99 | |
| 100 | public String toString() |
| 101 | { |
| 102 | return "DictionaryAspect: words=" + m_words + "; original dictionary=" |
| 103 | + m_originalDictionary; |
| 104 | } |
Pierre De Rop | 7f35e94 | 2010-02-08 18:26:18 +0000 | [diff] [blame] | 105 | } |