You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ak...@apache.org on 2006/03/07 22:17:09 UTC

svn commit: r384015 - in /incubator/felix/trunk: org.apache.felix.examples.spellcheckbinder/ org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/ org.apache.felix.examples.spellcheckbinder/src/main/resour...

Author: akarasulu
Date: Tue Mar  7 13:17:07 2006
New Revision: 384015

URL: http://svn.apache.org/viewcvs?rev=384015&view=rev
Log:
getting service binder working

Added:
    incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java
    incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/resources/
    incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml
Modified:
    incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/pom.xml
    incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java
    incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java

Modified: incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/pom.xml
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/pom.xml?rev=384015&r1=384014&r2=384015&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/pom.xml (original)
+++ incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/pom.xml Tue Mar  7 13:17:07 2006
@@ -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

Modified: incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java?rev=384015&r1=384014&r2=384015&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java (original)
+++ incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/Activator.java Tue Mar  7 13:17:07 2006
@@ -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
 {
 }

Added: incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java?rev=384015&view=auto
==============================================================================
--- incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java (added)
+++ incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/java/org/apache/felix/examples/spellcheckbinder/SpellCheckServiceImpl.java Tue Mar  7 13:17:07 2006
@@ -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()] );
+    }
+}

Added: incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml?rev=384015&view=auto
==============================================================================
--- incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml (added)
+++ incubator/felix/trunk/org.apache.felix.examples.spellcheckbinder/src/main/resources/metadata.xml Tue Mar  7 13:17:07 2006
@@ -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>
+
+

Modified: incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java?rev=384015&r1=384014&r2=384015&view=diff
==============================================================================
--- incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java (original)
+++ incubator/felix/trunk/tools/maven2/maven-osgi-plugin/src/main/java/org/apache/felix/tools/maven/plugin/OsgiManifest.java Tue Mar  7 13:17:07 2006
@@ -17,8 +17,9 @@
 
 package org.apache.felix.tools.maven.plugin;
 
-import java.util.HashMap;
-import java.util.Map;
+
+import java.util.Properties;
+
 
 /**
  * Hold values for an OSGi jar "bundle" manifest.
@@ -46,9 +47,9 @@
 
     // private String bundleClasspath;
 
-    private Map entries = new HashMap();
+    private Properties entries = new Properties();
 
-    public Map getEntries()
+    public Properties getEntries()
     {
         if ( getBundleActivator() != null )
         {