Return-Path: X-Original-To: apmail-felix-commits-archive@www.apache.org Delivered-To: apmail-felix-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 4BCA9186E0 for ; Wed, 27 Apr 2016 00:11:43 +0000 (UTC) Received: (qmail 94275 invoked by uid 500); 27 Apr 2016 00:11:43 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 94233 invoked by uid 500); 27 Apr 2016 00:11:43 -0000 Mailing-List: contact commits-help@felix.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@felix.apache.org Delivered-To: mailing list commits@felix.apache.org Received: (qmail 94224 invoked by uid 99); 27 Apr 2016 00:11:43 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 27 Apr 2016 00:11:43 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 88205C09E1 for ; Wed, 27 Apr 2016 00:11:42 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.804 X-Spam-Level: X-Spam-Status: No, score=0.804 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.996] autolearn=disabled Received: from mx2-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id SR9TX0qVvyO1 for ; Wed, 27 Apr 2016 00:11:41 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx2-lw-eu.apache.org (ASF Mail Server at mx2-lw-eu.apache.org) with ESMTP id DB2AD5F472 for ; Wed, 27 Apr 2016 00:11:40 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 9ABE3E0316 for ; Wed, 27 Apr 2016 00:11:39 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 970743A0216 for ; Wed, 27 Apr 2016 00:11:39 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1741126 - in /felix/trunk/examples/spellcheckscr: pom.xml src/main/java/org/apache/felix/examples/spellcheckscr/SpellCheckServiceImpl.java src/main/resources/ Date: Wed, 27 Apr 2016 00:11:39 -0000 To: commits@felix.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160427001139.970743A0216@svn01-us-west.apache.org> 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. --> - - A bundle that registers a spell checking service based on Service Component Runtime. - - org.apache.felix - felix - 1.0.4 + felix-parent + 4 ../../pom/pom.xml @@ -33,23 +29,29 @@ Apache Felix Example Spell Check w/ SCR org.apache.felix.examples.spellcheckscr 0.9.0-SNAPSHOT - ${description} + A bundle that registers a spell checking service based on Service Component Runtime. - ${pom.groupId} + org.osgi org.osgi.core - 1.0.0 + 6.0.0 + provided + + + org.osgi + org.osgi.service.component.annotations + 1.3.0 provided - ${pom.groupId} + org.apache.felix org.apache.felix.examples.dictionaryservice 0.9.0-SNAPSHOT provided - ${pom.groupId} + org.apache.felix org.apache.felix.examples.spellcheckservice 0.9.0-SNAPSHOT provided @@ -61,16 +63,11 @@ org.apache.felix maven-bundle-plugin - 1.4.0 + 3.0.1 true - ${pom.artifactId}.* - * - ${pom.artifactId} - ${description} The Apache Software Foundation - OSGI-INF/component.xml 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 Felix Project Team */ +@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 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 errorList = new ArrayList(); // 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 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()] ); } }