Pierre De Rop | f0ea274 | 2010-01-15 22:57:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm.samples.annotation; |
| 20 | |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 21 | import java.util.concurrent.CopyOnWriteArrayList; |
| 22 | |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 23 | import org.apache.felix.dm.annotation.api.Property; |
Pierre De Rop | ee1c807 | 2010-09-08 19:40:35 +0000 | [diff] [blame] | 24 | import org.apache.felix.dm.annotation.api.Component; |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 25 | import org.apache.felix.dm.annotation.api.ServiceDependency; |
Pierre De Rop | 1968b54 | 2010-02-14 17:21:06 +0000 | [diff] [blame] | 26 | import org.apache.felix.dm.annotation.api.Start; |
| 27 | import org.apache.felix.dm.annotation.api.Stop; |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 28 | import org.apache.felix.service.command.CommandProcessor; |
| 29 | import org.apache.felix.service.command.Descriptor; |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 30 | import org.osgi.service.log.LogService; |
| 31 | |
| 32 | /** |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 33 | * Felix "spellcheck" Gogo Shell Command. This command allows to check if some given words are valid or not. |
| 34 | * This command will be activated only if (at least) one DictionaryService has been injected. |
| 35 | * To create a Dictionary Service, you have to go the the web console and define a "Dictionary Services" factory |
| 36 | * configuration instance, which will fire an instantiation of the corresponding dictionary service. |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 37 | */ |
Pierre De Rop | ee1c807 | 2010-09-08 19:40:35 +0000 | [diff] [blame] | 38 | @Component(provides={SpellChecker.class}, |
| 39 | properties={@Property(name=CommandProcessor.COMMAND_SCOPE, value="dmsample.annotation"), |
| 40 | @Property(name=CommandProcessor.COMMAND_FUNCTION, values={"spellcheck"})}) |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 41 | public class SpellChecker |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 42 | { |
| 43 | /** |
| 44 | * We'll use the OSGi log service for logging. If no log service is available, then we'll use a NullObject. |
| 45 | */ |
| 46 | @ServiceDependency(required = false) |
| 47 | private LogService m_log; |
| 48 | |
| 49 | /** |
| 50 | * We'll store all Dictionaries is a CopyOnWrite list, in order to avoid method synchronization. |
| 51 | */ |
| 52 | private CopyOnWriteArrayList<DictionaryService> m_dictionaries = new CopyOnWriteArrayList<DictionaryService>(); |
| 53 | |
| 54 | /** |
| 55 | * Inject a dictionary into this service. |
| 56 | * @param serviceProperties the dictionary OSGi service properties |
| 57 | * @param dictionary the new dictionary |
| 58 | */ |
| 59 | @ServiceDependency(removed = "removeDictionary") |
Pierre De Rop | 2b6d2aa | 2010-01-18 16:05:04 +0000 | [diff] [blame] | 60 | protected void addDictionary(DictionaryService dictionary) |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 61 | { |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 62 | m_dictionaries.add(dictionary); |
| 63 | } |
Pierre De Rop | 1968b54 | 2010-02-14 17:21:06 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Lifecycle method callback, used to check if our service has been activated. |
| 67 | */ |
| 68 | @Start |
| 69 | protected void start() { |
| 70 | m_log.log(LogService.LOG_WARNING, "Spell Checker started"); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Lifecycle method callback, used to check if our service has been activated. |
| 75 | */ |
| 76 | @Stop |
| 77 | protected void stop() { |
| 78 | m_log.log(LogService.LOG_WARNING, "Spell Checker stopped"); |
| 79 | } |
| 80 | |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 81 | /** |
| 82 | * Remove a dictionary from our service. |
| 83 | * @param dictionary |
| 84 | */ |
| 85 | protected void removeDictionary(DictionaryService dictionary) |
| 86 | { |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 87 | m_dictionaries.remove(dictionary); |
| 88 | } |
| 89 | |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 90 | // --- Gogo Shell command |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 91 | |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 92 | @Descriptor("checks if word is found from an available dictionary") |
| 93 | public void spellcheck(@Descriptor("the word to check")String word) |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 94 | { |
Pierre De Rop | 9eca99b | 2010-07-04 21:07:59 +0000 | [diff] [blame] | 95 | m_log.log(LogService.LOG_INFO, "Checking spelling of word \"" + word |
Pierre De Rop | 2b6d2aa | 2010-01-18 16:05:04 +0000 | [diff] [blame] | 96 | + "\" using the following dictionaries: " + m_dictionaries); |
| 97 | |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 98 | for (DictionaryService dictionary : m_dictionaries) |
| 99 | { |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 100 | if (dictionary.checkWord(word)) |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 101 | { |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 102 | System.out.println("word " + word + " is correct"); |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | } |
Pierre De Rop | cf7d7a6 | 2010-06-21 09:46:37 +0000 | [diff] [blame] | 106 | System.err.println("word " + word + " is incorrect"); |
Pierre De Rop | f5793eb | 2010-01-15 22:31:42 +0000 | [diff] [blame] | 107 | } |
| 108 | } |