got the fifth example working
git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@383928 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.examples.spellcheckservice/pom.xml b/org.apache.felix.examples.spellcheckservice/pom.xml
index 5491f6a..9e9f810 100644
--- a/org.apache.felix.examples.spellcheckservice/pom.xml
+++ b/org.apache.felix.examples.spellcheckservice/pom.xml
@@ -15,6 +15,12 @@
<version>${pom.version}</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>${pom.groupId}</groupId>
+ <artifactId>org.apache.felix.examples.dictionaryservice</artifactId>
+ <version>${pom.version}</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
@@ -33,6 +39,9 @@
<bundleActivator>
org.apache.felix.examples.spellcheckservice.impl.Activator
</bundleActivator>
+ <importPackage>
+ org.apache.felix.examples.dictionaryservice
+ </importPackage>
<exportPackage>
org.apache.felix.examples.spellcheckservice
</exportPackage>
diff --git a/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java b/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java
index 2553b1d..d1c5813 100644
--- a/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java
+++ b/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java
@@ -18,9 +18,23 @@
/**
+ * A simple service interface that defines a spell check service. A spell check
+ * service checks the spelling of all words in a given passage. A passage is any
+ * number of words separated by a space character and the following punctuation
+ * marks: comma, period, exclamation mark, question mark, semi-colon, and colon.
*
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
public interface SpellCheckService
{
-}
+ /**
+ * Checks a given passage for spelling errors. A passage is any number of
+ * words separated by a space and any of the following punctuation marks:
+ * comma (,), period (.), exclamation mark (!), question mark (?),
+ * semi-colon (;), and colon(:).
+ *
+ * @param passage the passage to spell check.
+ * @return An array of misspelled words or null if no words are misspelled.
+ */
+ public String[] check( String passage );
+}
\ No newline at end of file
diff --git a/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java b/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java
index 23cb275..5de8ebb 100644
--- a/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java
+++ b/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java
@@ -17,23 +17,249 @@
package org.apache.felix.examples.spellcheckservice.impl;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.StringTokenizer;
+
+import org.apache.felix.examples.dictionaryservice.DictionaryService;
+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;
+import org.osgi.framework.ServiceRegistration;
/**
- *
+ * This class implements a bundle that implements a spell check service. The
+ * spell check service uses all available dictionary services to check for the
+ * existence of words in a given sentence. This bundle not only monitors the
+ * dynamic availability of dictionary services, but it manages the aggregation
+ * of all available dictionary services as they arrive and depart. The spell
+ * check service is only registered if there are dictionary services available,
+ * thus the spell check service will appear and disappear as dictionary services
+ * appear and disappear, respectively.
*
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
-public class Activator implements BundleActivator
+public class Activator implements BundleActivator, ServiceListener
{
- public void start( BundleContext arg0 ) throws Exception
+ // Bundle's context.
+ private BundleContext m_context = null;
+
+ // List of available dictionary service references.
+ private ArrayList m_refList = new ArrayList();
+
+ // Maps service references to service objects.
+ private HashMap m_refToObjMap = new HashMap();
+
+ // The spell check service registration.
+ private ServiceRegistration m_reg = null;
+
+
+ /**
+ * Implements BundleActivator.start(). Adds itself as a service listener and
+ * queries for all currently available dictionary services. Any available
+ * dictionary services are added to the service reference list. If
+ * dictionary services are found, then the spell check service is
+ * registered.
+ *
+ * @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=" + DictionaryService.class.getName() + ")"
+ + "(Language=*))" );
+
+ // Query for all dictionary services.
+ ServiceReference[] refs = m_context.getServiceReferences( DictionaryService.class.getName(), "(Language=*)" );
+
+ // Add any dictionaries to the service reference list.
+ if ( refs != null )
+ {
+ // Lock the list.
+ synchronized ( m_refList )
+ {
+ for ( int i = 0; i < refs.length; i++ )
+ {
+ // Get the service object.
+ Object service = m_context.getService( refs[i] );
+
+ // Make that the service is not being duplicated.
+ if ( ( service != null ) && ( m_refToObjMap.get( refs[i] ) == null ) )
+ {
+ // Add to the reference list.
+ m_refList.add( refs[i] );
+ // Map reference to service object for easy look up.
+ m_refToObjMap.put( refs[i], service );
+ }
+ }
+
+ // Register spell check service if there are any dictionary services.
+ if ( m_refList.size() > 0 )
+ {
+ m_reg = m_context.registerService( SpellCheckService.class.getName(),
+ new SpellCheckServiceImpl(), null );
+ }
+ }
+ }
}
- public void stop( BundleContext arg0 ) throws Exception
+ /**
+ * Implements BundleActivator.stop(). Does nothing since the framework will
+ * automatically unregister any registered services, release any used
+ * services, and remove any event listeners.
+ *
+ * @param context
+ * the framework context for the bundle.
+ */
+ public void stop( BundleContext context )
{
+ // NOTE: The services automatically released.
+ }
+
+
+ /**
+ * Implements ServiceListener.serviceChanged(). Monitors the arrival and
+ * departure of dictionary services, adding and removing them from the
+ * service reference list, respectively. In the case where no more
+ * dictionary services are available, the spell check service is registered.
+ * As soon as any dictionary spell check becomes available, the spell check
+ * service is reregistered.
+ *
+ * @param event the fired service event.
+ */
+ public void serviceChanged( ServiceEvent event )
+ {
+ // Add the new dictionary service to the service list.
+ if ( event.getType() == ServiceEvent.REGISTERED )
+ {
+ synchronized ( m_refList )
+ {
+ // Get the service object.
+ Object service = m_context.getService( event.getServiceReference() );
+
+ // Make that the service is not being duplicated.
+ if ( ( service != null ) && ( m_refToObjMap.get( event.getServiceReference() ) == null ) )
+ {
+ // Add to the reference list.
+ m_refList.add( event.getServiceReference() );
+ // Map reference to service object for easy look up.
+ m_refToObjMap.put( event.getServiceReference(), service );
+
+ // Register spell check service if necessary.
+ if ( m_reg == null )
+ {
+ m_reg = m_context.registerService( SpellCheckService.class.getName(),
+ new SpellCheckServiceImpl(), null );
+ }
+ }
+ else if ( service != null )
+ {
+ m_context.ungetService( event.getServiceReference() );
+ }
+ }
+ }
+ // Remove the departing service from the service list.
+ else if ( event.getType() == ServiceEvent.UNREGISTERING )
+ {
+ synchronized ( m_refList )
+ {
+ // Make sure the service is in the list.
+ if ( m_refToObjMap.get( event.getServiceReference() ) != null )
+ {
+ // Unget the service object.
+ m_context.ungetService( event.getServiceReference() );
+ // Remove service reference.
+ m_refList.remove( event.getServiceReference() );
+ // Remove service reference from map.
+ m_refToObjMap.remove( event.getServiceReference() );
+
+ // If there are no more dictionary services,
+ // then unregister spell check service.
+ if ( m_refList.size() == 0 )
+ {
+ m_reg.unregister();
+ m_reg = null;
+ }
+ }
+ }
+ }
+ }
+
+
+ /**
+ * A private inner class that implements a spell check service; see
+ * SpellCheckService for details of the service.
+ */
+ private class SpellCheckServiceImpl implements SpellCheckService
+ {
+ /**
+ * Implements SpellCheckService.check(). Checks the given passage for
+ * misspelled words.
+ *
+ * @param passage
+ * the passage to spell check.
+ * @return An array of misspelled words or null if no words are
+ * misspelled.
+ */
+ public String[] check( String passage )
+ {
+ // No misspelled words for an empty string.
+ if ( ( passage == null ) || ( passage.length() == 0 ) )
+ {
+ return null;
+ }
+
+ ArrayList errorList = new ArrayList();
+
+ // Tokenize the passage using spaces and punctionation.
+ StringTokenizer st = new StringTokenizer( passage, " ,.!?;:" );
+
+ // Lock the service list.
+ synchronized ( m_refList )
+ {
+ // Loop through each word in the passage.
+ while ( st.hasMoreTokens() )
+ {
+ String word = st.nextToken();
+
+ boolean correct = false;
+
+ // Check each available dictionary for the current word.
+ for ( int i = 0; ( !correct ) && ( i < m_refList.size() ); i++ )
+ {
+ DictionaryService dictionary = ( DictionaryService ) m_refToObjMap.get( m_refList.get( i ) );
+
+ if ( dictionary.checkWord( word ) )
+ {
+ correct = true;
+ }
+ }
+
+ // If the word is not correct, then add it
+ // to the incorrect word list.
+ if ( !correct )
+ {
+ errorList.add( word );
+ }
+ }
+ }
+
+ // Return null if no words are incorrect.
+ if ( errorList.size() == 0 )
+ {
+ return null;
+ }
+
+ // Return the array of incorrect words.
+ return ( String[] ) errorList.toArray( new String[errorList.size()] );
+ }
}
}
diff --git a/pom.xml b/pom.xml
index b7ce98e..3d0de7c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -19,6 +19,7 @@
<module>org.apache.felix.examples.frenchdictionary</module>
<module>org.apache.felix.examples.dictionaryclient</module>
<module>org.apache.felix.examples.dictionaryclient2</module>
+ <module>org.apache.felix.examples.spellcheckservice</module>
</modules>
<repositories>