Return-Path: Delivered-To: apmail-felix-commits-archive@www.apache.org Received: (qmail 68979 invoked from network); 2 Oct 2008 08:08:03 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 2 Oct 2008 08:08:03 -0000 Received: (qmail 55798 invoked by uid 500); 2 Oct 2008 08:08:01 -0000 Delivered-To: apmail-felix-commits-archive@felix.apache.org Received: (qmail 55780 invoked by uid 500); 2 Oct 2008 08:08:01 -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 55770 invoked by uid 99); 2 Oct 2008 08:08:01 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Oct 2008 01:08:01 -0700 X-ASF-Spam-Status: No, hits=-2000.0 required=10.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; Thu, 02 Oct 2008 08:07:07 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5245E23889A0; Thu, 2 Oct 2008 01:07:42 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r701028 - /felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/EventDispatcher.java Date: Thu, 02 Oct 2008 08:07:42 -0000 To: commits@felix.apache.org From: clement@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081002080742.5245E23889A0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: clement Date: Thu Oct 2 01:07:41 2008 New Revision: 701028 URL: http://svn.apache.org/viewvc?rev=701028&view=rev Log: Forget to add the internal event dispatcher in my previous commit. Added: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/EventDispatcher.java Added: felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/EventDispatcher.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/EventDispatcher.java?rev=701028&view=auto ============================================================================== --- felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/EventDispatcher.java (added) +++ felix/trunk/ipojo/core/src/main/java/org/apache/felix/ipojo/EventDispatcher.java Thu Oct 2 01:07:41 2008 @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.ipojo; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceEvent; +import org.osgi.framework.ServiceListener; + +/** + * iPOJO Internal event dispatcher. + * This class provides an internal service event dispatcher in order to tackle the + * event storm that can happen when starting large-scale applications. + * @see Extender + * @author Felix Project Team + */ +public class EventDispatcher implements ServiceListener { + + private Map m_listeners; + private BundleContext m_context; + + private static EventDispatcher m_dispatcher; + public static EventDispatcher getDispatcher() { + return m_dispatcher; + } + + public EventDispatcher(BundleContext bc) { + m_context = bc; + m_listeners = new HashMap(); + } + + public void start() { + // Only one thread can call the start method. + m_context.addServiceListener(this); + m_dispatcher = this; // Set the dispatcher. + } + + public void stop() { + synchronized(this) { + m_dispatcher = null; + m_context.removeServiceListener(this); + m_listeners.clear(); + } + } + + public void serviceChanged(ServiceEvent event) { + String[] itfs = (String[]) event.getServiceReference().getProperty(Constants.OBJECTCLASS); + for (int s = 0; s < itfs.length; s++) { + List list; + synchronized (this) { + List stored = (List) m_listeners.get(itfs[s]); + if (stored == null) { + return; // Nothing to do + } + // Creates a new list (stack confinement) + list = new ArrayList(stored); + } + for (int i = 0; i < list.size(); i++) { + ((ServiceListener) list.get(i)).serviceChanged(event); + } + } + } + + public void addListener(String itf, ServiceListener listener) { + synchronized (this) { + List list = (List) m_listeners.get(itf); + if (list == null) { + list = new ArrayList(1); + list.add(listener); + m_listeners.put(itf, list); + } else { + list.add(listener); + } + } + } + + public boolean removeListener(ServiceListener listener) { + boolean removed = false; + synchronized (this) { + Set keys = m_listeners.keySet(); + Iterator it = keys.iterator(); + while (it.hasNext()) { + String itf = (String) it.next(); + List list = (List) m_listeners.get(itf); + removed = removed || list.remove(listener); + } + } + return removed; + } + +}