Example from Didier that uses SCR.


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@430375 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/examples/spellcheckscr/pom.xml b/examples/spellcheckscr/pom.xml
new file mode 100644
index 0000000..940bfe2
--- /dev/null
+++ b/examples/spellcheckscr/pom.xml
@@ -0,0 +1,68 @@
+<project>
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix</artifactId>
+    <version>0.8.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>osgi-bundle</packaging>
+  <name>Apache Felix Examples: Spell Check w/ SCR</name>
+  <artifactId>org.apache.felix.examples.spellcheckscr</artifactId>
+  <dependencies>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.core</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>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.scr</artifactId>
+      <version>${pom.version}</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>
+            <entries>
+              <property>
+                <name>Service-Component</name>
+                <value>OSGI-INF/component.xml</value>
+              </property>
+            </entries>
+            <bundleName>Spell Check w/ SCR Example</bundleName>
+            <bundleVendor>Apache Software Foundation</bundleVendor>
+            <bundleDescription>
+              A bundle that registers a spell checking service based on Service Component Runtime.
+            </bundleDescription>
+            <bundleActivator>
+              org.apache.felix.examples.spellcheckscr.Activator
+            </bundleActivator>
+            <importPackage>
+              org.osgi.framework, org.apache.felix.examples.dictionaryservice, org.apache.felix.scr, org.apache.felix.examples.spellcheckservice
+            </importPackage>
+          </osgiManifest>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java b/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java
new file mode 100644
index 0000000..58596bb
--- /dev/null
+++ b/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.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.spellcheckscr;
+
+
+import org.apache.felix.scr.GenericActivator;
+
+
+/**
+ * This example re-implements the spell check service of Example 5 using the
+ * Apache Felix Service Component Runtime.
+ * The Service Component Runtime greatly simplifies creating OSGi applications
+ * by essentially eliminating the need to write OSGi-related code;
+ * instead of writing OSGi code for your 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 extends GenericActivator
+{
+}
diff --git a/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java b/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java
new file mode 100644
index 0000000..e5e4edd
--- /dev/null
+++ b/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/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.spellcheckscr;
+
+
+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 Component Runtime. Notice that there is no OSGi references in the
+ * application code; intead, the metadata.xml file describes 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: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 Component Runtime 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 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 );
+        }
+    }
+
+
+    /**
+     * 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/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties b/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties
new file mode 100644
index 0000000..467c98c
--- /dev/null
+++ b/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties
@@ -0,0 +1,9 @@
+
+service.pid=org.apache.felix.examples.spellcheckscr
+# service.ranking=10
+service.vendor=Apache Software Foundation
+
+foo=bar
+tic=tac toe
+
+
diff --git a/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml b/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml
new file mode 100644
index 0000000..624b2a7
--- /dev/null
+++ b/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<component name="spellcheckscr">
+
+  <!-- xmlns="http://www.osgi.org/xmlns/scr/v1.0.0" -->
+  <!--
+     This metadata file instructs the Apache Felix Service Component
+     Runtime 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.
+    -->
+	<implementation class="org.apache.felix.examples.spellcheckscr.SpellCheckServiceImpl"/>
+
+	<property name="category" value="demo" type="String"/>
+	
+	<properties entry="OSGI-INF/component.properties"/>	
+	
+	<service>
+		<provide interface="org.apache.felix.examples.spellcheckservice.SpellCheckService"/>
+	</service>
+
+	<reference name="DICTONARY"
+	    interface="org.apache.felix.examples.dictionaryservice.DictionaryService"
+	    filter="(Language=*)"
+	    cardinality="1..n"
+	    policy="dynamic"
+	    bind="addDictionary"
+	    unbind="removeDictionary"
+	/>
+</component>