You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2006/08/10 15:11:21 UTC

svn commit: r430375 - in /incubator/felix/trunk/examples: ./ spellcheckscr/ spellcheckscr/src/ spellcheckscr/src/main/ spellcheckscr/src/main/java/ spellcheckscr/src/main/java/org/ spellcheckscr/src/main/java/org/apache/ spellcheckscr/src/main/java/org...

Author: rickhall
Date: Thu Aug 10 06:11:19 2006
New Revision: 430375

URL: http://svn.apache.org/viewvc?rev=430375&view=rev
Log:
Example from Didier that uses SCR.

Added:
    incubator/felix/trunk/examples/spellcheckscr/   (with props)
    incubator/felix/trunk/examples/spellcheckscr/pom.xml   (with props)
    incubator/felix/trunk/examples/spellcheckscr/src/
    incubator/felix/trunk/examples/spellcheckscr/src/main/
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java   (with props)
    incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java   (with props)
    incubator/felix/trunk/examples/spellcheckscr/src/main/resources/
    incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/
    incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties   (with props)
    incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml   (with props)
Modified:
    incubator/felix/trunk/examples/pom.xml

Modified: incubator/felix/trunk/examples/pom.xml
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/examples/pom.xml?rev=430375&r1=430374&r2=430375&view=diff
==============================================================================
--- incubator/felix/trunk/examples/pom.xml (original)
+++ incubator/felix/trunk/examples/pom.xml Thu Aug 10 06:11:19 2006
@@ -18,6 +18,7 @@
     <module>spellcheckservice</module>
     <module>spellcheckclient</module>
     <module>spellcheckbinder</module>
+    <module>spellcheckscr</module>
   </modules>
 
   <dependencies>

Propchange: incubator/felix/trunk/examples/spellcheckscr/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu Aug 10 06:11:19 2006
@@ -0,0 +1,11 @@
+target
+*.ipr
+*.iws
+*.iml
+.classpath
+.project
+.settings
+.deployables
+.wtpmodules
+*.log
+

Added: incubator/felix/trunk/examples/spellcheckscr/pom.xml
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/examples/spellcheckscr/pom.xml?rev=430375&view=auto
==============================================================================
--- incubator/felix/trunk/examples/spellcheckscr/pom.xml (added)
+++ incubator/felix/trunk/examples/spellcheckscr/pom.xml Thu Aug 10 06:11:19 2006
@@ -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>

Propchange: incubator/felix/trunk/examples/spellcheckscr/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java?rev=430375&view=auto
==============================================================================
--- incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java (added)
+++ incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java Thu Aug 10 06:11:19 2006
@@ -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
+{
+}

Propchange: incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/Activator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java?rev=430375&view=auto
==============================================================================
--- incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java (added)
+++ incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java Thu Aug 10 06:11:19 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.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()] );
+    }
+}

Propchange: incubator/felix/trunk/examples/spellcheckscr/src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties?rev=430375&view=auto
==============================================================================
--- incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties (added)
+++ incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties Thu Aug 10 06:11:19 2006
@@ -0,0 +1,9 @@
+
+service.pid=org.apache.felix.examples.spellcheckscr
+# service.ranking=10
+service.vendor=Apache Software Foundation
+
+foo=bar
+tic=tac toe
+
+

Propchange: incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml
URL: http://svn.apache.org/viewvc/incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml?rev=430375&view=auto
==============================================================================
--- incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml (added)
+++ incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml Thu Aug 10 06:11:19 2006
@@ -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>

Propchange: incubator/felix/trunk/examples/spellcheckscr/src/main/resources/OSGI-INF/component.xml
------------------------------------------------------------------------------
    svn:eol-style = native