blob: 735382e1c66bc28c6aca358e27ce05b015ef06fd [file] [log] [blame]
Pierre De Ropf0ea2742010-01-15 22:57:25 +00001/*
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 Ropf5793eb2010-01-15 22:31:42 +000019package org.apache.felix.dm.samples.annotation;
20
Pierre De Ropf5793eb2010-01-15 22:31:42 +000021import java.util.concurrent.CopyOnWriteArrayList;
22
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000023import org.apache.felix.dm.annotation.api.Property;
Pierre De Ropf5793eb2010-01-15 22:31:42 +000024import org.apache.felix.dm.annotation.api.Service;
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000025import org.apache.felix.dm.annotation.api.ServiceDependency;
Pierre De Rop1968b542010-02-14 17:21:06 +000026import org.apache.felix.dm.annotation.api.Start;
27import org.apache.felix.dm.annotation.api.Stop;
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000028import org.apache.felix.service.command.CommandProcessor;
29import org.apache.felix.service.command.Descriptor;
Pierre De Ropf5793eb2010-01-15 22:31:42 +000030import org.osgi.service.log.LogService;
31
32/**
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000033 * 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 Ropf5793eb2010-01-15 22:31:42 +000037 */
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000038@Service(provide={SpellChecker.class},
39 properties={@Property(name=CommandProcessor.COMMAND_SCOPE, value="dmsample.annotation"),
40 @Property(name=CommandProcessor.COMMAND_FUNCTION, values={"spellcheck"})})
41public class SpellChecker
Pierre De Ropf5793eb2010-01-15 22:31:42 +000042{
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 Rop2b6d2aa2010-01-18 16:05:04 +000060 protected void addDictionary(DictionaryService dictionary)
Pierre De Ropf5793eb2010-01-15 22:31:42 +000061 {
Pierre De Ropf5793eb2010-01-15 22:31:42 +000062 m_dictionaries.add(dictionary);
63 }
Pierre De Rop1968b542010-02-14 17:21:06 +000064
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 Ropf5793eb2010-01-15 22:31:42 +000081 /**
82 * Remove a dictionary from our service.
83 * @param dictionary
84 */
85 protected void removeDictionary(DictionaryService dictionary)
86 {
Pierre De Ropf5793eb2010-01-15 22:31:42 +000087 m_dictionaries.remove(dictionary);
88 }
89
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000090 // --- Gogo Shell command
Pierre De Ropf5793eb2010-01-15 22:31:42 +000091
Pierre De Ropcf7d7a62010-06-21 09:46:37 +000092 @Descriptor("checks if word is found from an available dictionary")
93 public void spellcheck(@Descriptor("the word to check")String word)
Pierre De Ropf5793eb2010-01-15 22:31:42 +000094 {
Pierre De Rop9eca99b2010-07-04 21:07:59 +000095 m_log.log(LogService.LOG_INFO, "Checking spelling of word \"" + word
Pierre De Rop2b6d2aa2010-01-18 16:05:04 +000096 + "\" using the following dictionaries: " + m_dictionaries);
97
Pierre De Ropf5793eb2010-01-15 22:31:42 +000098 for (DictionaryService dictionary : m_dictionaries)
99 {
Pierre De Ropcf7d7a62010-06-21 09:46:37 +0000100 if (dictionary.checkWord(word))
Pierre De Ropf5793eb2010-01-15 22:31:42 +0000101 {
Pierre De Ropcf7d7a62010-06-21 09:46:37 +0000102 System.out.println("word " + word + " is correct");
Pierre De Ropf5793eb2010-01-15 22:31:42 +0000103 return;
104 }
105 }
Pierre De Ropcf7d7a62010-06-21 09:46:37 +0000106 System.err.println("word " + word + " is incorrect");
Pierre De Ropf5793eb2010-01-15 22:31:42 +0000107 }
108}