You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2016/04/27 02:11:39 UTC

svn commit: r1741126 - in /felix/trunk/examples/spellcheckscr: pom.xml src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java src/main/resources/

Author: cziegeler
Date: Wed Apr 27 00:11:39 2016
New Revision: 1741126

URL: http://svn.apache.org/viewvc?rev=1741126&view=rev
Log:
Update SCR example

Removed:
    felix/trunk/examples/spellcheckscr/src/main/resources/
Modified:
    felix/trunk/examples/spellcheckscr/pom.xml
    felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java

Modified: felix/trunk/examples/spellcheckscr/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/examples/spellcheckscr/pom.xml?rev=1741126&r1=1741125&r2=1741126&view=diff
==============================================================================
--- felix/trunk/examples/spellcheckscr/pom.xml (original)
+++ felix/trunk/examples/spellcheckscr/pom.xml Wed Apr 27 00:11:39 2016
@@ -17,14 +17,10 @@
  under the License.
 -->
 <project>
-  <properties>
-    <description>A bundle that registers a spell checking service based on Service Component Runtime.</description>
-  </properties>  
-
   <parent>
     <groupId>org.apache.felix</groupId>
-    <artifactId>felix</artifactId>
-    <version>1.0.4</version>
+    <artifactId>felix-parent</artifactId>
+    <version>4</version>
     <relativePath>../../pom/pom.xml</relativePath>
   </parent>
 
@@ -33,23 +29,29 @@
   <name>Apache Felix Example Spell Check w/ SCR</name>
   <artifactId>org.apache.felix.examples.spellcheckscr</artifactId>
   <version>0.9.0-SNAPSHOT</version>
-  <description>${description}</description>
+  <description>A bundle that registers a spell checking service based on Service Component Runtime.</description>
 
   <dependencies>
     <dependency>
-      <groupId>${pom.groupId}</groupId>
+      <groupId>org.osgi</groupId>
       <artifactId>org.osgi.core</artifactId>
-      <version>1.0.0</version>
+      <version>6.0.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.service.component.annotations</artifactId>
+      <version>1.3.0</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
-      <groupId>${pom.groupId}</groupId>
+      <groupId>org.apache.felix</groupId>
       <artifactId>org.apache.felix.examples.dictionaryservice</artifactId>
       <version>0.9.0-SNAPSHOT</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
-      <groupId>${pom.groupId}</groupId>
+      <groupId>org.apache.felix</groupId>
       <artifactId>org.apache.felix.examples.spellcheckservice</artifactId>
       <version>0.9.0-SNAPSHOT</version>
       <scope>provided</scope>
@@ -61,16 +63,11 @@
       <plugin>
         <groupId>org.apache.felix</groupId>
         <artifactId>maven-bundle-plugin</artifactId>
-        <version>1.4.0</version>
+        <version>3.0.1</version>
         <extensions>true</extensions>
         <configuration>
           <instructions>
-            <Private-Package>${pom.artifactId}.*</Private-Package>
-            <Import-Package>*</Import-Package>
-            <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
-            <Bundle-Description>${description}</Bundle-Description>
             <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
-            <Service-Component>OSGI-INF/component.xml</Service-Component>
           </instructions>
         </configuration>
       </plugin>

Modified: felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java?rev=1741126&r1=1741125&r2=1741126&view=diff
==============================================================================
--- felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java (original)
+++ felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java Wed Apr 27 00:11:39 2016
@@ -1,5 +1,5 @@
 /*
- *   Copyright 2006 The Apache Software Foundation
+ *   Copyright 2006-2016 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.
@@ -18,71 +18,49 @@ package org.apache.felix.examples.spellc
 
 
 import java.util.ArrayList;
+import java.util.List;
 import java.util.StringTokenizer;
 
 import org.apache.felix.examples.dictionaryservice.DictionaryService;
 import org.apache.felix.examples.spellcheckservice.SpellCheckService;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
 
 
 /**
- * This class re-implements the spell check service of Example 5. This service
- * implementation behaves exactly like the one in Example 5, specifically, it
+ * This class re-implements the spell check service of Example 6. This service
+ * implementation behaves exactly like the one in Example 6, 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 Component Runtime. Notice that there is no OSGi references in the
- * application code; intead, the metadata.xml file describes the service
+ * application code; instead, the annotations describe the service
  * dependencies to the Service Component Runtime, which automatically manages them and it
  * also automatically registers the spell check services as appropriate.
- * 
+ *
  * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
  */
+@Component
 public class SpellCheckServiceImpl implements SpellCheckService
 {
-    // List of service objects.
-    private ArrayList m_svcObjList = new ArrayList();
-
-
     /**
-     * This method is used by the Service Component Runtime to add new dictionaries to the
-     * spell check service.
-     * 
-     * @param dictionary
-     *            the dictionary to add to the spell check service.
+     * List of service objects.
+     *
+     * This field is managed by the Service Component Runtime and updated
+     * with the current set of available dictionary services.
+     * At least one dictionary service is required.
      */
-    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 Component Runtime 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 );
-        }
-    }
-
+    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.AT_LEAST_ONE)
+    private volatile List<DictionaryService> m_svcObjList;
 
     /**
      * 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.
@@ -95,37 +73,34 @@ public class SpellCheckServiceImpl imple
             return null;
         }
 
-        ArrayList errorList = new ArrayList();
+        List<String> errorList = new ArrayList<String>();
 
         // Tokenize the passage using spaces and punctionation.
         StringTokenizer st = new StringTokenizer( passage, " ,.!?;:" );
 
-        // Lock the service list.
-        synchronized ( m_svcObjList )
+        // Put the current set of services in a local field
+        // the field m_svcObjList might be modified concurrently
+        final List<DictionaryService> localServices = m_svcObjList;
+
+        // Loop through each word in the passage.
+        while ( st.hasMoreTokens() )
         {
-            // Loop through each word in the passage.
-            while ( st.hasMoreTokens() )
-            {
-                String word = st.nextToken();
-                boolean correct = false;
+            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++ )
+            // Check each available dictionary for the current word.
+            for(final DictionaryService dictionary : localServices) {
+                if ( dictionary.checkWord( word ) )
                 {
-                    DictionaryService dictionary = ( DictionaryService ) m_svcObjList.get( i );
-
-                    if ( dictionary.checkWord( word ) )
-                    {
-                        correct = true;
-                    }
+                    correct = true;
                 }
+            }
 
-                // If the word is not correct, then add it
-                // to the incorrect word list.
-                if ( !correct )
-                {
-                    errorList.add( word );
-                }
+            // If the word is not correct, then add it
+            // to the incorrect word list.
+            if ( !correct )
+            {
+                errorList.add( word );
             }
         }
 
@@ -136,6 +111,6 @@ public class SpellCheckServiceImpl imple
         }
 
         // Return the array of incorrect words.
-        return ( String[] ) errorList.toArray( new String[errorList.size()] );
+        return errorList.toArray( new String[errorList.size()] );
     }
 }