blob: 88567d45dbf17b9839ed0c2265b91b85c629b551 [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 Rop9eca99b2010-07-04 21:07:59 +000054 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 Ropa97580d2010-05-24 21:42:18 +000063 }
64 )
Pierre De Rop9eca99b2010-07-04 21:07:59 +000065 protected void updated(Dictionary<String, ?> config)
66 {
Pierre De Ropa97580d2010-05-24 21:42:18 +000067 m_words.clear();
68 String[] words = (String[]) config.get("words");
Pierre De Rop9eca99b2010-07-04 21:07:59 +000069 for (String word : words)
70 {
Pierre De Ropa97580d2010-05-24 21:42:18 +000071 m_words.add(word);
72 }
73 }
74
75 /**
Pierre De Rop9eca99b2010-07-04 21:07:59 +000076 * 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 Ropa97580d2010-05-24 21:42:18 +000086 * Checks if a word is found from our custom word list. if not, delegate to the decorated
87 * dictionary.
88 */
Pierre De Rop7f35e942010-02-08 18:26:18 +000089 public boolean checkWord(String word)
90 {
Pierre De Rop9eca99b2010-07-04 21:07:59 +000091 m_log.log(LogService.LOG_INFO, "DictionaryAspect: checking word " + word
92 + " (original dictionary=" + m_originalDictionary + ")");
93 if (m_words.contains(word))
94 {
Pierre De Rop7f35e942010-02-08 18:26:18 +000095 return true;
96 }
97 return m_originalDictionary.checkWord(word);
98 }
Pierre De Rop9eca99b2010-07-04 21:07:59 +000099
100 public String toString()
101 {
102 return "DictionaryAspect: words=" + m_words + "; original dictionary="
103 + m_originalDictionary;
104 }
Pierre De Rop7f35e942010-02-08 18:26:18 +0000105}