getting service binder working
git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@384015 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.examples.spellcheckbinder/pom.xml b/org.apache.felix.examples.spellcheckbinder/pom.xml
index 482c979..02ea07a 100644
--- a/org.apache.felix.examples.spellcheckbinder/pom.xml
+++ b/org.apache.felix.examples.spellcheckbinder/pom.xml
@@ -43,11 +43,16 @@
<extensions>true</extensions>
<configuration>
<osgiManifest>
+ <entries>
+ <property>
+ <name>Metadata-Location</name>
+ <value>metadata.xml</value>
+ </property>
+ </entries>
<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.
+ A bundle that registers a spell checking service based on service binder.
</bundleDescription>
<bundleActivator>
org.apache.felix.examples.spellcheckbinder.Activator
diff --git a/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java
index 217177b..cedeea7 100644
--- a/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java
+++ b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java
@@ -17,25 +17,23 @@
package org.apache.felix.examples.spellcheckbinder;
-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;
+import org.ungoverned.gravity.servicebinder.GenericActivator;
/**
+ * This example re-implements the spell check service of Example 5 using the
+ * Gravity Service Binder. The Service Binder greatly simplifies creating OSGi
+ * applications by essentially eliminating the need to write OSGi-related code;
+ * instead of writing OSGi code for you bundle, you create a simple XML file to
+ * describe your bundle's service dependencies. This class extends the generic
+ * bundle activator; it does not provide any additional functionality. All
+ * functionality for service-related tasks, such as look-up and binding, is
+ * handled by the generic activator base class using data from the metadata.xml
+ * file. All application functionality is defined in the
+ * SpellCheckServiceImpl.java file.
*
* @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
*/
-public class Activator
+public class Activator extends GenericActivator
{
}
diff --git a/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java
new file mode 100644
index 0000000..37497f0
--- /dev/null
+++ b/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java
@@ -0,0 +1,141 @@
+/*
+ * 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.spellcheckbinder;
+
+
+import java.util.ArrayList;
+import java.util.StringTokenizer;
+
+import org.apache.felix.examples.dictionaryservice.DictionaryService;
+import org.apache.felix.examples.spellcheckservice.SpellCheckService;
+
+
+/**
+ * This class re-implements the spell check service of Example 5. This service
+ * implementation behaves exactly like the one in Example 5, specifically, it
+ * aggregates all available dictionary services, monitors their dynamic
+ * availability, and only offers the spell check service if there are dictionary
+ * services available. The service implementation is greatly simplified, though,
+ * by using the Service Binder. Notice that there is no OSGi references in the
+ * application code; intead, the metadata.xml file describes the service
+ * dependencies to the Service Binder, which automatically manages them and it
+ * also automatically registers the spell check services as appropriate.
+ *
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class SpellCheckServiceImpl implements SpellCheckService
+{
+ // List of service objects.
+ private ArrayList m_svcObjList = new ArrayList();
+
+
+ /**
+ * This method is used by the Service Binder to add new dictionaries to the
+ * spell check service.
+ *
+ * @param dictionary
+ * the dictionary to add to the spell check service.
+ */
+ public void addDictionary( DictionaryService dictionary )
+ {
+ // Lock list and add service object.
+ synchronized ( m_svcObjList )
+ {
+ m_svcObjList.add( dictionary );
+ }
+ }
+
+
+ /**
+ * This method is used by the Service Binder to remove dictionaries from the
+ * spell check service.
+ *
+ * @param dictionary
+ * the dictionary to remove from the spell check service.
+ */
+ public void removeDictionary( DictionaryService dictionary )
+ {
+ // Lock list and remove service object.
+ synchronized ( m_svcObjList )
+ {
+ m_svcObjList.remove( dictionary );
+ }
+ }
+
+
+ /**
+ * 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 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_svcObjList )
+ {
+ // 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_svcObjList.size() ); i++ )
+ {
+ DictionaryService dictionary = ( DictionaryService ) m_svcObjList.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/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml b/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml
new file mode 100644
index 0000000..2bf1bf8
--- /dev/null
+++ b/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<bundle>
+ <!--
+ This metadata file instructs the Gravity Service Binder to
+ create one instance of "SpellCheckServiceImpl". It also
+ tells the generic activator that this instance implements the
+ "SpellCheckService" service interface and that it has an
+ aggregate dependency on "DictionaryService" services. Since
+ the service dependency on dictionary services has a lower
+ cardinality of one, the generic activator will create the instance
+ and offer its spell check service only when there is at least
+ one dictionary service available. The service dependency is
+ "dynamic", which means that dictionary service availability
+ will be monitored dynamically at runtime and it also tells the
+ generic activator which methods to call when adding and removing
+ dictionary services.
+ -->
+ <instance class="org.apache.felix.examples.spellcheckbinder.SpellCheckServiceImpl">
+ <service interface="org.apache.felix.examples.spellcheckservice.SpellCheckService"/>
+ <requires
+ service="org.apache.felix.examples.dictionaryservice.DictionaryService"
+ filter="(Language=*)"
+ cardinality="1..n"
+ policy="dynamic"
+ bind-method="addDictionary"
+ unbind-method="removeDictionary"
+ />
+ </instance>
+</bundle>
+
+