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 C71AE11EEE for ; Wed, 23 Jul 2014 10:21:53 +0000 (UTC) Received: (qmail 51172 invoked by uid 500); 23 Jul 2014 10:21:53 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 51134 invoked by uid 500); 23 Jul 2014 10:21:53 -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 51125 invoked by uid 99); 23 Jul 2014 10:21:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2014 10:21:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 Jul 2014 10:21:51 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7891B238890D; Wed, 23 Jul 2014 10:21:31 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1612809 - in /felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler: EventHandlerProxy.java EventHandlerTracker.java Date: Wed, 23 Jul 2014 10:21:31 -0000 To: commits@felix.apache.org From: cziegeler@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140723102131.7891B238890D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cziegeler Date: Wed Jul 23 10:21:30 2014 New Revision: 1612809 URL: http://svn.apache.org/r1612809 Log: FELIX-3511 - Use java.concurrent from Java 6 Modified: felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerProxy.java felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerTracker.java Modified: felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerProxy.java URL: http://svn.apache.org/viewvc/felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerProxy.java?rev=1612809&r1=1612808&r2=1612809&view=diff ============================================================================== --- felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerProxy.java (original) +++ felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerProxy.java Wed Jul 23 10:21:30 2014 @@ -43,7 +43,7 @@ import org.osgi.service.event.EventHandl public class EventHandlerProxy { /** The service reference for the event handler. */ - private final ServiceReference reference; + private final ServiceReference reference; /** The handler context. */ private final EventHandlerTracker.HandlerContext handlerContext; @@ -73,7 +73,7 @@ public class EventHandlerProxy { * @param reference Reference to the EventHandler */ public EventHandlerProxy(final EventHandlerTracker.HandlerContext context, - final ServiceReference reference) + final ServiceReference reference) { this.handlerContext = context; this.reference = reference; @@ -283,7 +283,7 @@ public class EventHandlerProxy { { try { - this.handler = (EventHandler)this.handlerContext.bundleContext.getService(this.reference); + this.handler = this.handlerContext.bundleContext.getService(this.reference); if ( this.handler != null ) { this.checkTimeout(this.handler.getClass().getName()); Modified: felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerTracker.java URL: http://svn.apache.org/viewvc/felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerTracker.java?rev=1612809&r1=1612808&r2=1612809&view=diff ============================================================================== --- felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerTracker.java (original) +++ felix/trunk/eventadmin/impl/src/main/java/org/apache/felix/eventadmin/impl/handler/EventHandlerTracker.java Wed Jul 23 10:21:30 2014 @@ -18,13 +18,13 @@ */ package org.apache.felix.eventadmin.impl.handler; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; @@ -40,17 +40,17 @@ import org.osgi.util.tracker.ServiceTrac public class EventHandlerTracker extends ServiceTracker { /** The proxies in this list match all events. */ - private volatile List matchingAllEvents; + private final List matchingAllEvents; /** This is a map for exact topic matches. The key is the topic, * the value is a list of proxies. */ - private volatile Map> matchingTopic; + private final Map> matchingTopic; /** This is a map for wildcard topics. The key is the prefix of the topic, * the value is a list of proxies */ - private volatile Map> matchingPrefixTopic; + private final Map> matchingPrefixTopic; /** The context for the proxies. */ @@ -60,9 +60,9 @@ public class EventHandlerTracker extends super(context, EventHandler.class.getName(), null); // we start with empty collections - this.matchingAllEvents = new ArrayList(); - this.matchingTopic = new HashMap>(); - this.matchingPrefixTopic = new HashMap>(); + this.matchingAllEvents = new CopyOnWriteArrayList(); + this.matchingTopic = new ConcurrentHashMap>(); + this.matchingPrefixTopic = new ConcurrentHashMap>(); } /** @@ -109,7 +109,7 @@ public class EventHandlerTracker extends * @see org.osgi.util.tracker.ServiceTracker#addingService(org.osgi.framework.ServiceReference) */ @Override - public EventHandlerProxy addingService(final ServiceReference reference) { + public EventHandlerProxy addingService(final ServiceReference reference) { final EventHandlerProxy proxy = new EventHandlerProxy(this.handlerContext, reference); if ( proxy.update() ) { this.put(proxy); @@ -121,7 +121,7 @@ public class EventHandlerTracker extends * @see org.osgi.util.tracker.ServiceTracker#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object) */ @Override - public void modifiedService(final ServiceReference reference, final EventHandlerProxy proxy) { + public void modifiedService(final ServiceReference reference, final EventHandlerProxy proxy) { this.remove(proxy); if ( proxy.update() ) { this.put(proxy); @@ -132,22 +132,23 @@ public class EventHandlerTracker extends * @see org.osgi.util.tracker.ServiceTracker#removedService(org.osgi.framework.ServiceReference, java.lang.Object) */ @Override - public void removedService(final ServiceReference reference, final EventHandlerProxy proxy) { + public void removedService(final ServiceReference reference, final EventHandlerProxy proxy) { this.remove(proxy); proxy.dispose(); } private void updateMap(final Map> proxyListMap, final String key, final EventHandlerProxy proxy, final boolean add) { List proxies = proxyListMap.get(key); - if (proxies == null) { + if (proxies == null) + { if ( !add ) { return; } - proxies = new ArrayList(); - } else { - proxies = new ArrayList(proxies); + proxies = new CopyOnWriteArrayList(); + proxyListMap.put(key, proxies); } + if ( add ) { proxies.add(proxy); @@ -155,120 +156,68 @@ public class EventHandlerTracker extends else { proxies.remove(proxy); - } - if ( proxies.size() == 0 ) - { - proxyListMap.remove(key); - } - else - { - proxyListMap.put(key, proxies); + if ( proxies.size() == 0 ) + { + proxyListMap.remove(key); + } } } /** * Check the topics of the event handler and put it into the * corresponding collections. - * We always create new collections - while this is "expensive" - * it allows us to read from them unsynced */ private synchronized void put(final EventHandlerProxy proxy) { final String[] topics = proxy.getTopics(); if ( topics == null ) { - final List newMatchingAllEvents = new ArrayList(this.matchingAllEvents); - newMatchingAllEvents.add(proxy); - this.matchingAllEvents = newMatchingAllEvents; + this.matchingAllEvents.add(proxy); } else { - Map> newMatchingTopic = null; - Map> newMatchingPrefixTopic = null; for(int i = 0; i < topics.length; i++) { final String topic = topics[i]; if ( topic.endsWith("/*") ) { // prefix topic: we remove the /* - if ( newMatchingPrefixTopic == null ) - { - newMatchingPrefixTopic = new HashMap>(this.matchingPrefixTopic); - } - final String prefix = topic.substring(0, topic.length() - 2); - this.updateMap(newMatchingPrefixTopic, prefix, proxy, true); + this.updateMap(this.matchingPrefixTopic, prefix, proxy, true); } else { // exact match - if ( newMatchingTopic == null ) - { - newMatchingTopic = new HashMap>(this.matchingTopic); - } - - this.updateMap(newMatchingTopic, topic, proxy, true); + this.updateMap(this.matchingTopic, topic, proxy, true); } } - if ( newMatchingTopic != null ) - { - this.matchingTopic = newMatchingTopic; - } - if ( newMatchingPrefixTopic != null ) - { - this.matchingPrefixTopic = newMatchingPrefixTopic; - } } } /** * Check the topics of the event handler and remove it from the * corresponding collections. - * We always create new collections - while this is "expensive" - * it allows us to read from them unsynced */ private synchronized void remove(final EventHandlerProxy proxy) { final String[] topics = proxy.getTopics(); if ( topics == null ) { - final List newMatchingAllEvents = new ArrayList(this.matchingAllEvents); - newMatchingAllEvents.remove(proxy); - this.matchingAllEvents = newMatchingAllEvents; + this.matchingAllEvents.remove(proxy); } else { - Map> newMatchingTopic = null; - Map> newMatchingPrefixTopic = null; for(int i = 0; i < topics.length; i++) { final String topic = topics[i]; if ( topic.endsWith("/*") ) { // prefix topic: we remove the /* - if ( newMatchingPrefixTopic == null ) - { - newMatchingPrefixTopic = new HashMap>(this.matchingPrefixTopic); - } - final String prefix = topic.substring(0, topic.length() - 2); - this.updateMap(newMatchingPrefixTopic, prefix, proxy, false); + this.updateMap(this.matchingPrefixTopic, prefix, proxy, false); } else { // exact match - if ( newMatchingTopic == null ) - { - newMatchingTopic = new HashMap>(this.matchingTopic); - } - - this.updateMap(newMatchingTopic, topic, proxy, false); + this.updateMap(this.matchingTopic, topic, proxy, false); } } - if ( newMatchingTopic != null ) - { - this.matchingTopic = newMatchingTopic; - } - if ( newMatchingPrefixTopic != null ) - { - this.matchingPrefixTopic = newMatchingPrefixTopic; - } } } @@ -284,13 +233,7 @@ public class EventHandlerTracker extends final Set handlers = new HashSet(); // Add all handlers matching everything - for(final EventHandlerProxy p : this.matchingAllEvents) - { - if ( p.canDeliver(event) ) - { - handlers.add(p); - } - } + this.checkHandlerAndAdd(handlers, this.matchingAllEvents, event); // Now check for prefix matches if ( !this.matchingPrefixTopic.isEmpty() ) @@ -299,26 +242,28 @@ public class EventHandlerTracker extends while (pos != -1) { final String prefix = topic.substring(0, pos); - List proxies = this.matchingPrefixTopic.get(prefix); - if (proxies != null) - { - for(final EventHandlerProxy p : proxies) - { - if ( p.canDeliver(event) ) - { - handlers.add(p); - } - } - } + this.checkHandlerAndAdd(handlers, this.matchingPrefixTopic.get(prefix), event); pos = prefix.lastIndexOf('/'); } } // Add the handlers for matching topic names - List proxies = this.matchingTopic.get(topic); - if (proxies != null) - { + this.checkHandlerAndAdd(handlers, this.matchingTopic.get(topic), event); + + return handlers; + } + + /** + * Checks each handler from the proxy list if it can deliver the event + * If the event can be delivered, the proxy is added to the handlers. + */ + private void checkHandlerAndAdd( final Set handlers, + final List proxies, + final Event event) + { + if ( proxies != null ) + { for(final EventHandlerProxy p : proxies) { if ( p.canDeliver(event) ) @@ -326,9 +271,7 @@ public class EventHandlerTracker extends handlers.add(p); } } - } - - return handlers; + } } /**