preparing service binder example
git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@383989 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.examples.spellcheckbinder/pom.xml b/org.apache.felix.examples.spellcheckbinder/pom.xml
new file mode 100644
index 0000000..482c979
--- /dev/null
+++ b/org.apache.felix.examples.spellcheckbinder/pom.xml
@@ -0,0 +1,63 @@
+<project>
+ <parent>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>felix</artifactId>
+ <version>0.8-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>osgi-bundle</packaging>
+ <name>Apache Felix Examples: Spell Check w/ Service Binder</name>
+ <artifactId>org.apache.felix.examples.spellcheckbinder</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>${pom.groupId}</groupId>
+ <artifactId>org.osgi</artifactId>
+ <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>
+ <dependency>
+ <groupId>${pom.groupId}</groupId>
+ <artifactId>org.apache.felix.examples.spellcheckservice</artifactId>
+ <version>${pom.version}</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.ungoverned.gravity.servicebinder</groupId>
+ <artifactId>servicebinder</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix.plugins</groupId>
+ <artifactId>maven-osgi-plugin</artifactId>
+ <version>${pom.version}</version>
+ <extensions>true</extensions>
+ <configuration>
+ <osgiManifest>
+ <bundleName>Spell Check w/ ServiceBinder Example</bundleName>
+ <bundleVendor>Apache Software Foundation</bundleVendor>
+ <bundleDescription>
+ A bundle that registers a spell checking service based on
+ service binder.
+ </bundleDescription>
+ <bundleActivator>
+ org.apache.felix.examples.spellcheckbinder.Activator
+ </bundleActivator>
+ <importPackage>
+ org.apache.felix.examples.dictionaryservice, org.ungoverned.gravity.servicebinder, org.apache.felix.examples.spellcheckservice
+ </importPackage>
+ </osgiManifest>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java
new file mode 100644
index 0000000..d1c5813
--- /dev/null
+++ b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java
@@ -0,0 +1,40 @@
+/*
+ * 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.spellcheckservice;
+
+
+/**
+ * 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.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java
new file mode 100644
index 0000000..5de8ebb
--- /dev/null
+++ b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java
@@ -0,0 +1,265 @@
+/*
+ * 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.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, ServiceListener
+{
+ // 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 );
+ }
+ }
+ }
+ }
+
+
+ /**
+ * 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()] );
+ }
+ }
+}