From 75ee56c07d753b6e4ecc4bc4aee42c7c3ab66269 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Thu, 4 Jul 2013 15:41:16 +0200 Subject: [PATCH] Moves part that handles PopupMessageHandler OSGI services to abstract systray impl. --- .../jdic/SystrayServiceJdicImpl.java | 144 +-------------- .../systray/AbstractSystrayService.java | 164 ++++++++++++++++++ .../service/systray/systray.manifest.mf | 4 +- 3 files changed, 170 insertions(+), 142 deletions(-) diff --git a/src/net/java/sip/communicator/impl/osdependent/jdic/SystrayServiceJdicImpl.java b/src/net/java/sip/communicator/impl/osdependent/jdic/SystrayServiceJdicImpl.java index 2b32f425e..60d87fe03 100644 --- a/src/net/java/sip/communicator/impl/osdependent/jdic/SystrayServiceJdicImpl.java +++ b/src/net/java/sip/communicator/impl/osdependent/jdic/SystrayServiceJdicImpl.java @@ -121,6 +121,8 @@ public class SystrayServiceJdicImpl */ public SystrayServiceJdicImpl() { + super(OsDependentActivator.bundleContext); + SystemTray systray; try @@ -320,68 +322,8 @@ public void popupMenuCanceled(PopupMenuEvent e) pmh, null); } - try - { - OsDependentActivator.bundleContext.addServiceListener( - new ServiceListenerImpl(), - "(objectclass=" - + PopupMessageHandler.class.getName() - + ")"); - } - catch (Exception e) - { - logger.warn(e); - } - - // now we look if some handler has been registered before we start - // to listen - ServiceReference[] handlerRefs = null; - try - { - handlerRefs - = OsDependentActivator.bundleContext.getServiceReferences( - PopupMessageHandler.class.getName(), - null); - } - catch (InvalidSyntaxException ise) - { - logger.error("Error while retrieving service refs", ise); - } - if (handlerRefs != null) - { - String configuredHandler - = (String) cfg.getProperty("systray.POPUP_HANDLER"); - - for (ServiceReference handlerRef : handlerRefs) - { - PopupMessageHandler handler - = (PopupMessageHandler) - OsDependentActivator.bundleContext.getService( - handlerRef); - String handlerName = handler.getClass().getName(); - - if (!containsHandler(handlerName)) - { - addPopupHandler(handler); - if (logger.isInfoEnabled()) - { - logger.info( - "added the following popup handler : " - + handler); - } - if ((configuredHandler != null) - && configuredHandler.equals( - handler.getClass().getName())) - { - setActivePopupMessageHandler(handler); - } - } - } - - if (configuredHandler == null) - selectBestPopupMessageHandler(); - } + initHandlers(); /* * Either we have an incorrect configuration value or the default pop-up @@ -565,86 +507,6 @@ public void popupMessageClicked(SystrayPopupMessageEvent evt) } } - /** An implementation of ServiceListener we will use */ - private class ServiceListenerImpl - implements ServiceListener - { - - /** - * implements ServiceListener.serviceChanged - * @param serviceEvent - */ - public void serviceChanged(ServiceEvent serviceEvent) - { - try - { - PopupMessageHandler handler = - (PopupMessageHandler) OsDependentActivator.bundleContext. - getService(serviceEvent.getServiceReference()); - - if (serviceEvent.getType() == ServiceEvent.REGISTERED) - { - if (!containsHandler(handler.getClass().getName())) - { - if (logger.isInfoEnabled()) - logger.info( - "adding the following popup handler : " + handler); - addPopupHandler(handler); - } - else - logger.warn("the following popup handler has not " + - "been added since it is already known : " + handler); - - String configuredHandler - = (String) cfg.getProperty("systray.POPUP_HANDLER"); - - if ((configuredHandler == null) - && ((getActivePopupHandler() == null) - || (handler.getPreferenceIndex() - > getActivePopupHandler() - .getPreferenceIndex()))) - { - // The user doesn't have a preferred handler set and new - // handler with better preference index has arrived, - // thus setting it as active. - setActivePopupMessageHandler(handler); - } - if ((configuredHandler != null) - && configuredHandler.equals( - handler.getClass().getName())) - { - // The user has a preferred handler set and it just - // became available, thus setting it as active - setActivePopupMessageHandler(handler); - } - } - else if (serviceEvent.getType() == ServiceEvent.UNREGISTERING) - { - if (logger.isInfoEnabled()) - logger.info( - "removing the following popup handler : " + handler); - removePopupHandler(handler); - PopupMessageHandler activeHandler = getActivePopupHandler(); - if (activeHandler == handler) - { - activeHandler.removePopupMessageListener( - popupMessageListener); - setActivePopupMessageHandler(null); - - // We just lost our default handler, so we replace it - // with the one that has the highest preference index. - selectBestPopupMessageHandler(); - } - } - } - catch (IllegalStateException e) - { - if (logger.isDebugEnabled()) - logger.debug(e); - } - } - } - /** * Implements a ServiceListener which waits for an * UIService implementation to become available, invokes diff --git a/src/net/java/sip/communicator/service/systray/AbstractSystrayService.java b/src/net/java/sip/communicator/service/systray/AbstractSystrayService.java index 637ec0529..29e885865 100644 --- a/src/net/java/sip/communicator/service/systray/AbstractSystrayService.java +++ b/src/net/java/sip/communicator/service/systray/AbstractSystrayService.java @@ -8,6 +8,8 @@ import net.java.sip.communicator.service.systray.event.*; import net.java.sip.communicator.util.*; +import org.jitsi.service.configuration.*; +import org.osgi.framework.*; import java.util.*; @@ -31,6 +33,11 @@ public abstract class AbstractSystrayService private final Logger logger = Logger.getLogger(AbstractSystrayService.class); + /** + * OSGI bundle context + */ + private final BundleContext bundleContext; + /** * The popup handler currently used to show popup messages */ @@ -48,6 +55,16 @@ public abstract class AbstractSystrayService */ private List earlyAddedListeners = null; + /** + * Creates new instance of AbstractSystrayService. + * @param bundleContext OSGI bundle context that will be used by this + * instance + */ + public AbstractSystrayService(BundleContext bundleContext) + { + this.bundleContext = bundleContext; + } + /** * Registers given PopupMessageHandler. * @param handler the PopupMessageHandler to be registered. @@ -198,4 +215,151 @@ public void selectBestPopupMessageHandler() setActivePopupMessageHandler(preferredHandler); } } + + /** + * Initializes popup handler by searching registered services for class + * PopupMessageHandler. + */ + protected void initHandlers() + { + // Listens for new popup handlers + try + { + bundleContext.addServiceListener( + new ServiceListenerImpl(), + "(objectclass=" + + PopupMessageHandler.class.getName() + + ")"); + } + catch (Exception e) + { + logger.warn(e); + } + // now we look if some handler has been registered before we start + // to listen + ServiceReference[] handlerRefs + = ServiceUtils.getServiceReferences( + bundleContext, + PopupMessageHandler.class); + + if (handlerRefs != null) + { + ConfigurationService config + = ServiceUtils.getService( + bundleContext, + ConfigurationService.class); + + String configuredHandler + = (String) config.getProperty("systray.POPUP_HANDLER"); + + for (ServiceReference handlerRef : handlerRefs) + { + PopupMessageHandler handler + = (PopupMessageHandler) bundleContext + .getService(handlerRef); + String handlerName = handler.getClass().getName(); + + if (!containsHandler(handlerName)) + { + addPopupHandler(handler); + if (logger.isInfoEnabled()) + { + logger.info( + "added the following popup handler : " + + handler); + } + if ((configuredHandler != null) + && configuredHandler.equals( + handler.getClass().getName())) + { + setActivePopupMessageHandler(handler); + } + } + } + + if (configuredHandler == null) + selectBestPopupMessageHandler(); + } + } + + /** An implementation of ServiceListener we will use */ + private class ServiceListenerImpl + implements ServiceListener + { + + /** + * implements ServiceListener.serviceChanged + * @param serviceEvent + */ + public void serviceChanged(ServiceEvent serviceEvent) + { + try + { + PopupMessageHandler handler + = (PopupMessageHandler) bundleContext + .getService(serviceEvent.getServiceReference()); + + if (serviceEvent.getType() == ServiceEvent.REGISTERED) + { + if (!containsHandler(handler.getClass().getName())) + { + if (logger.isInfoEnabled()) + logger.info("adding the following popup handler : " + + handler); + addPopupHandler(handler); + } + else + logger.warn("the following popup handler has not " + + "been added since it is already known : " + + handler); + + ConfigurationService cfg + = ServiceUtils.getService( bundleContext, + ConfigurationService.class); + String configuredHandler + = (String) cfg.getProperty("systray.POPUP_HANDLER"); + + if ((configuredHandler == null) + && ((getActivePopupHandler() == null) + || (handler.getPreferenceIndex() + > getActivePopupHandler().getPreferenceIndex()))) + { + // The user doesn't have a preferred handler set and new + // handler with better preference index has arrived, + // thus setting it as active. + setActivePopupMessageHandler(handler); + } + if ((configuredHandler != null) + && configuredHandler.equals( + handler.getClass().getName())) + { + // The user has a preferred handler set and it just + // became available, thus setting it as active + setActivePopupMessageHandler(handler); + } + } + else if (serviceEvent.getType() == ServiceEvent.UNREGISTERING) + { + if (logger.isInfoEnabled()) + logger.info("removing the following popup handler : " + + handler); + removePopupHandler(handler); + PopupMessageHandler activeHandler = getActivePopupHandler(); + if (activeHandler == handler) + { + setActivePopupMessageHandler(null); + + // We just lost our default handler, so we replace it + // with the one that has the highest preference index. + selectBestPopupMessageHandler(); + } + } + } + catch (IllegalStateException e) + { + if (logger.isDebugEnabled()) + logger.debug(e); + } + } + } } diff --git a/src/net/java/sip/communicator/service/systray/systray.manifest.mf b/src/net/java/sip/communicator/service/systray/systray.manifest.mf index 1bc52f7f1..5a8c6579c 100644 --- a/src/net/java/sip/communicator/service/systray/systray.manifest.mf +++ b/src/net/java/sip/communicator/service/systray/systray.manifest.mf @@ -3,6 +3,8 @@ Bundle-Description: Systray service Bundle-Vendor: jitsi.org Bundle-Version: 0.0.1 System-Bundle: yes -Import-Package: net.java.sip.communicator.util +Import-Package: net.java.sip.communicator.util, + org.jitsi.service.configuration, + org.osgi.framework Export-Package: net.java.sip.communicator.service.systray, net.java.sip.communicator.service.systray.event