finishing up spell checkclient
git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@383978 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.examples.spellcheckclient/pom.xml b/org.apache.felix.examples.spellcheckclient/pom.xml
index 8943f6b..098f625 100644
--- a/org.apache.felix.examples.spellcheckclient/pom.xml
+++ b/org.apache.felix.examples.spellcheckclient/pom.xml
@@ -6,8 +6,8 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>osgi-bundle</packaging>
- <name>Apache Felix Examples: Dictionary Client</name>
- <artifactId>org.apache.felix.examples.dictionaryclient</artifactId>
+ <name>Apache Felix Examples: Spell Check Client</name>
+ <artifactId>org.apache.felix.examples.spellcheckclient</artifactId>
<dependencies>
<dependency>
<groupId>${pom.groupId}</groupId>
@@ -17,7 +17,7 @@
</dependency>
<dependency>
<groupId>${pom.groupId}</groupId>
- <artifactId>org.apache.felix.examples.dictionaryservice</artifactId>
+ <artifactId>org.apache.felix.examples.spellcheckservice</artifactId>
<version>${pom.version}</version>
<scope>provided</scope>
</dependency>
@@ -31,16 +31,16 @@
<extensions>true</extensions>
<configuration>
<osgiManifest>
- <bundleName>Dictionary Client Example</bundleName>
+ <bundleName>Spell Check Client Example</bundleName>
<bundleVendor>Apache Software Foundation</bundleVendor>
<bundleDescription>
- A bundle using the dictionary service if it finds it at startup.
+ A bundle using the spell check service.
</bundleDescription>
<bundleActivator>
- org.apache.felix.examples.dictionaryclient.Activator
+ org.apache.felix.examples.spellcheckclient.Activator
</bundleActivator>
<importPackage>
- org.apache.felix.examples.dictionaryservice
+ org.apache.felix.examples.spellcheckservice
</importPackage>
</osgiManifest>
</configuration>
diff --git a/org.apache.felix.examples.spellcheckclient/src/main/java/org/apache/felix/examples/dictionaryclient/Activator.java b/org.apache.felix.examples.spellcheckclient/src/main/java/org/apache/felix/examples/dictionaryclient/Activator.java
deleted file mode 100644
index 49e8a0e..0000000
--- a/org.apache.felix.examples.spellcheckclient/src/main/java/org/apache/felix/examples/dictionaryclient/Activator.java
+++ /dev/null
@@ -1,119 +0,0 @@
-/*
- * Copyright 2006 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-package org.apache.felix.examples.dictionaryclient;
-
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-
-import org.apache.felix.examples.dictionaryservice.DictionaryService;
-
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-
-
-/**
- * This class implements a bundle that uses a dictionary service to check for
- * the proper spelling of a word by check for its existence in the dictionary.
- * This bundle uses the first service that it finds and does not monitor the
- * dynamic availability of the service (i.e., it does not listen for the arrival
- * or departure of dictionary services). When starting this bundle, the thread
- * calling the start() method is used to read words from standard input. You can
- * stop checking words by entering an empty line, but to start checking words
- * again you must stop and then restart the bundle.
- *
- * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
- */
-public class Activator implements BundleActivator
-{
- /**
- * Implements BundleActivator.start(). Queries for all available dictionary
- * services. If none are found it simply prints a message and returns,
- * otherwise it reads words from standard input and checks for their
- * existence from the first dictionary that it finds. (NOTE: It is very bad
- * practice to use the calling thread to perform a lengthy process like
- * this; this is only done for the purpose of the tutorial.)
- *
- * @param context the framework context for the bundle.
- */
- public void start( BundleContext context ) throws Exception
- {
- // Query for all service references matching any language.
- ServiceReference[] refs = context.getServiceReferences( DictionaryService.class.getName(), "(Language=*)" );
-
- if ( refs != null )
- {
- try
- {
- System.out.println( "Enter a blank line to exit." );
- BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
- String word = "";
-
- // Loop endlessly.
- while ( true )
- {
- // Ask the user to enter a word.
- System.out.print( "Enter word: " );
- word = in.readLine();
-
- // If the user entered a blank line, then
- // exit the loop.
- if ( word.length() == 0 )
- {
- break;
- }
-
- // First, get a dictionary service and then check
- // if the word is correct.
- DictionaryService dictionary = ( DictionaryService ) context.getService( refs[0] );
- if ( dictionary.checkWord( word ) )
- {
- System.out.println( "Correct." );
- }
- else
- {
- System.out.println( "Incorrect." );
- }
-
- // Unget the dictionary service.
- context.ungetService( refs[0] );
- }
- }
- catch ( IOException ex )
- {
- }
- }
- else
- {
- System.out.println( "Couldn't find any dictionary service..." );
- }
- }
-
-
- /**
- * Implements BundleActivator.stop(). Does nothing since the framework will
- * automatically unget any used services.
- *
- * @param context the framework context for the bundle.
- */
- public void stop( BundleContext context )
- {
- // NOTE: The service is automatically released.
- }
-}
diff --git a/org.apache.felix.examples.spellcheckclient/src/main/java/org/apache/felix/examples/spellcheckclient/Activator.java b/org.apache.felix.examples.spellcheckclient/src/main/java/org/apache/felix/examples/spellcheckclient/Activator.java
new file mode 100644
index 0000000..a8b2000
--- /dev/null
+++ b/org.apache.felix.examples.spellcheckclient/src/main/java/org/apache/felix/examples/spellcheckclient/Activator.java
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.felix.examples.spellcheckclient;
+
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+import org.apache.felix.examples.spellcheckservice.SpellCheckService;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+
+
+/**
+ *
+ *
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class Activator implements BundleActivator, ServiceListener
+{
+ // Bundle's context.
+ private BundleContext m_context = null;
+
+ // The service reference being used.
+ private ServiceReference m_ref = null;
+
+ // The service object being used.
+ private SpellCheckService m_checker = null;
+
+
+ /**
+ * Implements BundleActivator.start(). Adds itself as a listener for service
+ * events, then queries for all available spell check services. If none are
+ * found it goes into its normal "passage checking loop" and waits for a
+ * spell check service to arrive. Once it has a spell check service it reads
+ * passages from standard input and checks their spelling using the spell
+ * check service. (NOTE: It is very bad practice to use the calling thread
+ * to perform a lengthy process like this; this is only done for the purpose
+ * of the tutorial.)
+ *
+ * @param context the framework context for the bundle.
+ */
+ public void start( BundleContext context ) throws Exception
+ {
+ m_context = context;
+
+ // Listen for events pertaining to dictionary services.
+ m_context.addServiceListener( this, "(objectClass=" + SpellCheckService.class.getName() + ")" );
+
+ // Query for a spell check service.
+ m_ref = m_context.getServiceReference( SpellCheckService.class.getName() );
+
+ // If we found a spell check service, then get
+ // a reference so we can use it.
+ if ( m_ref != null )
+ {
+ m_checker = ( SpellCheckService ) m_context.getService( m_ref );
+ }
+
+ try
+ {
+ System.out.println( "Enter a blank line to exit." );
+ String passage = "";
+ BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
+
+ // Loop endlessly.
+ while ( true )
+ {
+ // Ask the user to enter a passage.
+ System.out.print( "Enter passage: " );
+ passage = in.readLine();
+
+ // If the user entered a blank line, then
+ // exit the loop.
+ if ( passage.length() == 0 )
+ {
+ break;
+ }
+ // If there is no spell checker, then say so.
+ else if ( m_checker == null )
+ {
+ System.out.println( "No spell checker available." );
+ }
+ // Otherwise check passage and print misspelled words.
+ else
+ {
+ String[] errors = m_checker.check( passage );
+
+ if ( errors == null )
+ {
+ System.out.println( "Passage is correct." );
+ }
+ else
+ {
+ System.out.println( "Incorrect word(s):" );
+ for ( int i = 0; i < errors.length; i++ )
+ {
+ System.out.println( " " + errors[i] );
+ }
+ }
+ }
+ }
+ }
+ catch ( Exception ex )
+ {
+ }
+ }
+
+
+ /**
+ * Implements BundleActivator.stop(). Does nothing since the framework will
+ * automatically unget any used services.
+ *
+ * @param context the framework context for the bundle.
+ */
+ public void stop( BundleContext context )
+ {
+ // NOTE: The service is automatically released.
+ }
+
+
+ /**
+ * Implements ServiceListener.serviceChanged(). Checks to see if the service
+ * we are using is leaving or tries to get a service if we need one.
+ *
+ * @param event the fired service event.
+ */
+ public void serviceChanged( ServiceEvent event )
+ {
+ // If a spell check service was registered, see if we
+ // need one. If so, get a reference to it.
+ if ( event.getType() == ServiceEvent.REGISTERED )
+ {
+ if ( m_ref == null )
+ {
+ // Get a reference to the service object.
+ m_ref = event.getServiceReference();
+ m_checker = ( SpellCheckService ) m_context.getService( m_ref );
+ }
+ }
+ // If a spell check service was unregistered, see if it
+ // was the one we were using. If so, unget the service
+ // and try to query to get another one.
+ else if ( event.getType() == ServiceEvent.UNREGISTERING )
+ {
+ if ( event.getServiceReference() == m_ref )
+ {
+ // Unget service object and null references.
+ m_context.ungetService( m_ref );
+ m_ref = null;
+ m_checker = null;
+
+ // Query to see if we can get another service.
+ m_ref = m_context.getServiceReference( SpellCheckService.class.getName() );
+ if ( m_ref != null )
+ {
+ // Get a reference to the service object.
+ m_checker = ( SpellCheckService ) m_context.getService( m_ref );
+ }
+ }
+ }
+ }
+}
diff --git a/pom.xml b/pom.xml
index 3d0de7c..d39dc86 100644
--- a/pom.xml
+++ b/pom.xml
@@ -20,6 +20,7 @@
<module>org.apache.felix.examples.dictionaryclient</module>
<module>org.apache.felix.examples.dictionaryclient2</module>
<module>org.apache.felix.examples.spellcheckservice</module>
+ <module>org.apache.felix.examples.spellcheckclient</module>
</modules>
<repositories>