blob: 0e7060c100b2044d26534cbe814806b2f4551d62 [file] [log] [blame]
Pierre De Ropfaca2892016-01-31 23:27:05 +00001/*
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
10 * under the License.
11 */
12package org.apache.felix.dm.lambda.samples.dictionary;
13
Pierre De Ropfaca2892016-01-31 23:27:05 +000014import java.util.concurrent.CopyOnWriteArrayList;
15
16import org.osgi.service.log.LogService;
17
Pierre De Ropfaca2892016-01-31 23:27:05 +000018/**
19 * This aspect applies to the English DictionaryService, and allows to decorate it with some
20 * custom English words, which are configurable from WebConsole.
21 *
22 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
23 */
24public class DictionaryAspect implements DictionaryService {
25 /**
26 * This is the service this aspect is applying to.
27 */
28 private volatile DictionaryService m_originalDictionary;
29
30 /**
31 * We store all configured words in a thread-safe data structure, because ConfigAdmin may
32 * invoke our updated method at any time.
33 */
34 private CopyOnWriteArrayList<String> m_words = new CopyOnWriteArrayList<String>();
35
36 /**
37 * We'll use the OSGi log service for logging. If no log service is available, then we'll
38 * use a NullObject.
39 */
40 private LogService m_log;
41
42 /**
Pierre De Rop11527502016-02-18 21:07:16 +000043 * Defines a configuration dependency for retrieving our english custom words.
44 * Dependency Manager will inject a dynamic proxy that implements our DictionaryAspectConfiguration interface.
45 * The dynamic proxy is used to wrap the actual Dictionary configuration.
46 * The pid for our configuration is by default the fqdn of our DictionaryAspectConfiguration interface.
Pierre De Ropfaca2892016-01-31 23:27:05 +000047 */
Pierre De Rop11527502016-02-18 21:07:16 +000048 protected void addWords(DictionaryAspectConfiguration cnf) {
49 if (cnf != null) {
Pierre De Ropfaca2892016-01-31 23:27:05 +000050 m_words.clear();
51 for (String word : cnf.words()) {
52 m_words.add(word);
53 }
54 }
55 }
56
57 /**
58 * Our Aspect Service is starting and is about to be registered in the OSGi regsitry.
59 */
60 protected void start() {
61 m_log.log(LogService.LOG_INFO, "Starting aspect Dictionary with words: " + m_words
62 + "; original dictionary service=" + m_originalDictionary);
63 }
64
65 /**
66 * Checks if a word is found from our custom word list. if not, delegate to the decorated
67 * dictionary.
68 */
69 public boolean checkWord(String word) {
70 m_log.log(LogService.LOG_INFO, "DictionaryAspect: checking word " + word + " (original dictionary="
71 + m_originalDictionary + ")");
72 if (m_words.contains(word)) {
73 return true;
74 }
75 return m_originalDictionary.checkWord(word);
76 }
77
78 public String toString() {
79 return "DictionaryAspect: words=" + m_words + "; original dictionary=" + m_originalDictionary;
80 }
81}