blob: 76914f0ffed5f6a3586807c038d72ab93e26791b [file] [log] [blame]
Pierre De Rop7f35e942010-02-08 18:26:18 +00001/*
Pierre De Rop9eca99b2010-07-04 21:07:59 +00002 * 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 Rop7f35e942010-02-08 18:26:18 +000010 * under the License.
11 */
12package org.apache.felix.dm.samples.annotation;
13
Pierre De Ropa97580d2010-05-24 21:42:18 +000014import java.util.Dictionary;
15import java.util.concurrent.CopyOnWriteArrayList;
16
Pierre De Rop7f35e942010-02-08 18:26:18 +000017import org.apache.felix.dm.annotation.api.AspectService;
Pierre De Ropa97580d2010-05-24 21:42:18 +000018import org.apache.felix.dm.annotation.api.PropertyMetaData;
Pierre De Rop2f824c02010-05-25 19:18:14 +000019import org.apache.felix.dm.annotation.api.ConfigurationDependency;
20import org.apache.felix.dm.annotation.api.ServiceDependency;
Pierre De Rop9eca99b2010-07-04 21:07:59 +000021import org.apache.felix.dm.annotation.api.Start;
Pierre De Rop7f35e942010-02-08 18:26:18 +000022import org.osgi.service.log.LogService;
23
24/**
Pierre De Ropd4ac53c2010-07-04 21:27:13 +000025 * 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 Rop7f35e942010-02-08 18:26:18 +000027 */
Pierre De Rop9eca99b2010-07-04 21:07:59 +000028@AspectService(ranking = 10, filter = "(lang=en)")
Pierre De Rop7f35e942010-02-08 18:26:18 +000029public 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 Rop9eca99b2010-07-04 21:07:59 +000037 * We store all configured words in a thread-safe data structure, because ConfigAdmin may
38 * invoke our updated method at any time.
Pierre De Ropa97580d2010-05-24 21:42:18 +000039 */
40 private CopyOnWriteArrayList<String> m_words = new CopyOnWriteArrayList<String>();
Pierre De Rop9eca99b2010-07-04 21:07:59 +000041
Pierre De Ropa97580d2010-05-24 21:42:18 +000042 /**
Pierre De Rop9eca99b2010-07-04 21:07:59 +000043 * We'll use the OSGi log service for logging. If no log service is available, then we'll
44 * use a NullObject.
Pierre De Rop7f35e942010-02-08 18:26:18 +000045 */
46 @ServiceDependency(required = false)
47 private LogService m_log;
48
Pierre De Ropa97580d2010-05-24 21:42:18 +000049 /**
Pierre De Rop9eca99b2010-07-04 21:07:59 +000050 * Defines a configuration dependency for retrieving our english custom words (by default,
51 * our PID is our full class name).
Pierre De Ropa97580d2010-05-24 21:42:18 +000052 */
53 @ConfigurationDependency(
Pierre De Rop6408b0d2011-05-07 10:19:57 +000054 pid="DictionaryAspectPID",
Pierre De Rop9eca99b2010-07-04 21:07:59 +000055 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 Ropa97580d2010-05-24 21:42:18 +000064 }
65 )
Pierre De Rop9eca99b2010-07-04 21:07:59 +000066 protected void updated(Dictionary<String, ?> config)
67 {
Pierre De Ropa97580d2010-05-24 21:42:18 +000068 m_words.clear();
69 String[] words = (String[]) config.get("words");
Pierre De Rop9eca99b2010-07-04 21:07:59 +000070 for (String word : words)
71 {
Pierre De Ropa97580d2010-05-24 21:42:18 +000072 m_words.add(word);
73 }
74 }
75
76 /**
Pierre De Rop9eca99b2010-07-04 21:07:59 +000077 * 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 Ropa97580d2010-05-24 21:42:18 +000087 * Checks if a word is found from our custom word list. if not, delegate to the decorated
88 * dictionary.
89 */
Pierre De Rop7f35e942010-02-08 18:26:18 +000090 public boolean checkWord(String word)
91 {
Pierre De Rop9eca99b2010-07-04 21:07:59 +000092 m_log.log(LogService.LOG_INFO, "DictionaryAspect: checking word " + word
93 + " (original dictionary=" + m_originalDictionary + ")");
94 if (m_words.contains(word))
95 {
Pierre De Rop7f35e942010-02-08 18:26:18 +000096 return true;
97 }
98 return m_originalDictionary.checkWord(word);
99 }
Pierre De Rop9eca99b2010-07-04 21:07:59 +0000100
101 public String toString()
102 {
103 return "DictionaryAspect: words=" + m_words + "; original dictionary="
104 + m_originalDictionary;
105 }
Pierre De Rop7f35e942010-02-08 18:26:18 +0000106}