Alex Karasulu | e2412e3 | 2006-03-07 19:42:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | package org.apache.felix.examples.spellcheckclient; |
| 18 | |
| 19 | |
| 20 | import java.io.BufferedReader; |
| 21 | import java.io.InputStreamReader; |
| 22 | |
| 23 | import org.apache.felix.examples.spellcheckservice.SpellCheckService; |
| 24 | import org.osgi.framework.BundleActivator; |
| 25 | import org.osgi.framework.BundleContext; |
| 26 | import org.osgi.framework.ServiceEvent; |
| 27 | import org.osgi.framework.ServiceListener; |
| 28 | import org.osgi.framework.ServiceReference; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * |
| 33 | * |
| 34 | * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a> |
| 35 | */ |
| 36 | public class Activator implements BundleActivator, ServiceListener |
| 37 | { |
| 38 | // Bundle's context. |
| 39 | private BundleContext m_context = null; |
| 40 | |
| 41 | // The service reference being used. |
| 42 | private ServiceReference m_ref = null; |
| 43 | |
| 44 | // The service object being used. |
| 45 | private SpellCheckService m_checker = null; |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Implements BundleActivator.start(). Adds itself as a listener for service |
| 50 | * events, then queries for all available spell check services. If none are |
| 51 | * found it goes into its normal "passage checking loop" and waits for a |
| 52 | * spell check service to arrive. Once it has a spell check service it reads |
| 53 | * passages from standard input and checks their spelling using the spell |
| 54 | * check service. (NOTE: It is very bad practice to use the calling thread |
| 55 | * to perform a lengthy process like this; this is only done for the purpose |
| 56 | * of the tutorial.) |
| 57 | * |
| 58 | * @param context the framework context for the bundle. |
| 59 | */ |
| 60 | public void start( BundleContext context ) throws Exception |
| 61 | { |
| 62 | m_context = context; |
| 63 | |
| 64 | // Listen for events pertaining to dictionary services. |
| 65 | m_context.addServiceListener( this, "(objectClass=" + SpellCheckService.class.getName() + ")" ); |
| 66 | |
| 67 | // Query for a spell check service. |
| 68 | m_ref = m_context.getServiceReference( SpellCheckService.class.getName() ); |
| 69 | |
| 70 | // If we found a spell check service, then get |
| 71 | // a reference so we can use it. |
| 72 | if ( m_ref != null ) |
| 73 | { |
| 74 | m_checker = ( SpellCheckService ) m_context.getService( m_ref ); |
| 75 | } |
| 76 | |
| 77 | try |
| 78 | { |
| 79 | System.out.println( "Enter a blank line to exit." ); |
| 80 | String passage = ""; |
| 81 | BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); |
| 82 | |
| 83 | // Loop endlessly. |
| 84 | while ( true ) |
| 85 | { |
| 86 | // Ask the user to enter a passage. |
| 87 | System.out.print( "Enter passage: " ); |
| 88 | passage = in.readLine(); |
| 89 | |
| 90 | // If the user entered a blank line, then |
| 91 | // exit the loop. |
| 92 | if ( passage.length() == 0 ) |
| 93 | { |
| 94 | break; |
| 95 | } |
| 96 | // If there is no spell checker, then say so. |
| 97 | else if ( m_checker == null ) |
| 98 | { |
| 99 | System.out.println( "No spell checker available." ); |
| 100 | } |
| 101 | // Otherwise check passage and print misspelled words. |
| 102 | else |
| 103 | { |
| 104 | String[] errors = m_checker.check( passage ); |
| 105 | |
| 106 | if ( errors == null ) |
| 107 | { |
| 108 | System.out.println( "Passage is correct." ); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | System.out.println( "Incorrect word(s):" ); |
| 113 | for ( int i = 0; i < errors.length; i++ ) |
| 114 | { |
| 115 | System.out.println( " " + errors[i] ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | catch ( Exception ex ) |
| 122 | { |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * Implements BundleActivator.stop(). Does nothing since the framework will |
| 129 | * automatically unget any used services. |
| 130 | * |
| 131 | * @param context the framework context for the bundle. |
| 132 | */ |
| 133 | public void stop( BundleContext context ) |
| 134 | { |
| 135 | // NOTE: The service is automatically released. |
| 136 | } |
| 137 | |
| 138 | |
| 139 | /** |
| 140 | * Implements ServiceListener.serviceChanged(). Checks to see if the service |
| 141 | * we are using is leaving or tries to get a service if we need one. |
| 142 | * |
| 143 | * @param event the fired service event. |
| 144 | */ |
| 145 | public void serviceChanged( ServiceEvent event ) |
| 146 | { |
| 147 | // If a spell check service was registered, see if we |
| 148 | // need one. If so, get a reference to it. |
| 149 | if ( event.getType() == ServiceEvent.REGISTERED ) |
| 150 | { |
| 151 | if ( m_ref == null ) |
| 152 | { |
| 153 | // Get a reference to the service object. |
| 154 | m_ref = event.getServiceReference(); |
| 155 | m_checker = ( SpellCheckService ) m_context.getService( m_ref ); |
| 156 | } |
| 157 | } |
| 158 | // If a spell check service was unregistered, see if it |
| 159 | // was the one we were using. If so, unget the service |
| 160 | // and try to query to get another one. |
| 161 | else if ( event.getType() == ServiceEvent.UNREGISTERING ) |
| 162 | { |
| 163 | if ( event.getServiceReference() == m_ref ) |
| 164 | { |
| 165 | // Unget service object and null references. |
| 166 | m_context.ungetService( m_ref ); |
| 167 | m_ref = null; |
| 168 | m_checker = null; |
| 169 | |
| 170 | // Query to see if we can get another service. |
| 171 | m_ref = m_context.getServiceReference( SpellCheckService.class.getName() ); |
| 172 | if ( m_ref != null ) |
| 173 | { |
| 174 | // Get a reference to the service object. |
| 175 | m_checker = ( SpellCheckService ) m_context.getService( m_ref ); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |