Search in sources :

Example 1 with ServiceListener

use of org.osgi.framework.ServiceListener in project sling by apache.

the class FSClassLoaderProvider method activate.

/**
	 * Activate this component. Create the root directory.
	 *
	 * @param componentContext
	 * @throws MalformedURLException
	 * @throws InvalidSyntaxException
	 * @throws MalformedObjectNameException
	 */
@Activate
protected void activate(final ComponentContext componentContext) throws MalformedURLException, InvalidSyntaxException, MalformedObjectNameException {
    // get the file root
    this.root = new File(componentContext.getBundleContext().getDataFile(""), "classes");
    this.root.mkdirs();
    this.rootURL = this.root.toURI().toURL();
    this.callerBundle = componentContext.getUsingBundle();
    classLoaderWriterListeners.clear();
    if (classLoaderWriterServiceListener != null) {
        componentContext.getBundleContext().removeServiceListener(classLoaderWriterServiceListener);
        classLoaderWriterServiceListener = null;
    }
    classLoaderWriterServiceListener = new ServiceListener() {

        @Override
        public void serviceChanged(ServiceEvent event) {
            ServiceReference<ClassLoaderWriterListener> reference = (ServiceReference<ClassLoaderWriterListener>) event.getServiceReference();
            if (event.getType() == ServiceEvent.MODIFIED || event.getType() == ServiceEvent.REGISTERED) {
                classLoaderWriterListeners.put(getId(reference), reference);
            } else {
                classLoaderWriterListeners.remove(getId(reference));
            }
        }

        private Long getId(ServiceReference<ClassLoaderWriterListener> reference) {
            return (Long) reference.getProperty(Constants.SERVICE_ID);
        }
    };
    componentContext.getBundleContext().addServiceListener(classLoaderWriterServiceListener, LISTENER_FILTER);
    // handle the MBean Installation
    if (mbeanRegistration != null) {
        mbeanRegistration.unregister();
        mbeanRegistration = null;
    }
    Hashtable<String, String> jmxProps = new Hashtable<String, String>();
    jmxProps.put("type", "ClassLoader");
    jmxProps.put("name", "FSClassLoader");
    final Hashtable<String, Object> mbeanProps = new Hashtable<String, Object>();
    mbeanProps.put(Constants.SERVICE_DESCRIPTION, "Apache Sling FSClassLoader Controller Service");
    mbeanProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
    mbeanProps.put("jmx.objectname", new ObjectName("org.apache.sling.classloader", jmxProps));
    mbeanRegistration = componentContext.getBundleContext().registerService(FSClassLoaderMBean.class.getName(), new FSClassLoaderMBeanImpl(this, componentContext.getBundleContext()), mbeanProps);
}
Also used : ClassLoaderWriterListener(org.apache.sling.commons.classloader.ClassLoaderWriterListener) ServiceListener(org.osgi.framework.ServiceListener) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) ObjectName(javax.management.ObjectName) ServiceEvent(org.osgi.framework.ServiceEvent) File(java.io.File) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with ServiceListener

use of org.osgi.framework.ServiceListener in project felix by apache.

the class LogWrapper method setContext.

/**
 * Set the <tt>BundleContext</tt> of the bundle. This method registers a service
 * listener for LogServices with the framework that are subsequently used to
 * log messages.
 * <p>
 * If the bundle context is <code>null</code>, the service listener is
 * unregistered and all remaining references to LogServices dropped before
 * internally clearing the bundle context field.
 *
 *  @param context The context of the bundle.
 */
public static void setContext(final BundleContext context) {
    LogWrapper logWrapper = LogWrapperLoader.m_singleton;
    // context is removed, unregister and drop references
    if (context == null) {
        if (logWrapper.m_logServiceListener != null) {
            logWrapper.m_context.removeServiceListener(logWrapper.m_logServiceListener);
            logWrapper.m_logServiceListener = null;
        }
        logWrapper.removeLoggerRefs();
    }
    // set field
    logWrapper.setBundleContext(context);
    // context is set, register and get existing services
    if (context != null) {
        try {
            ServiceListener listener = new ServiceListener() {

                // Add a newly available LogService reference to the singleton.
                @Override
                public void serviceChanged(final ServiceEvent event) {
                    if (ServiceEvent.REGISTERED == event.getType()) {
                        LogWrapperLoader.m_singleton.addLoggerRef(event.getServiceReference());
                    }
                // unregistered services are handled in the next log operation.
                }
            };
            context.addServiceListener(listener, "(" + Constants.OBJECTCLASS + "=org.osgi.service.log.LogService)");
            logWrapper.m_logServiceListener = listener;
            // Add all available LogService references to the singleton.
            final ServiceReference[] refs = context.getServiceReferences("org.osgi.service.log.LogService", null);
            if (null != refs) {
                for (int i = 0; i < refs.length; i++) {
                    logWrapper.addLoggerRef(refs[i]);
                }
            }
        } catch (InvalidSyntaxException e) {
        // this never happens
        }
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) ServiceEvent(org.osgi.framework.ServiceEvent) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with ServiceListener

use of org.osgi.framework.ServiceListener in project felix by apache.

the class LogWrapper method setContext.

/**
 * Set the <tt>BundleContext</tt> of the bundle. This method registers a service
 * listener for LogServices with the framework that are subsequently used to
 * log messages.
 * <p>
 * If the bundle context is <code>null</code>, the service listener is
 * unregistered and all remaining references to LogServices dropped before
 * internally clearing the bundle context field.
 *
 *  @param context The context of the bundle.
 */
public static void setContext(final BundleContext context) {
    final LogWrapper logWrapper = LogWrapperLoader.SINGLETON;
    // context is removed, unregister and drop references
    if (context == null) {
        if (logWrapper.logServiceListener != null) {
            logWrapper.context.removeServiceListener(logWrapper.logServiceListener);
            logWrapper.logServiceListener = null;
        }
        logWrapper.removeLoggerRefs();
    }
    // set field
    logWrapper.setBundleContext(context);
    // context is set, register and get existing services
    if (context != null) {
        try {
            final ServiceListener listener = new ServiceListener() {

                // Add a newly available LogService reference to the singleton.
                public void serviceChanged(final ServiceEvent event) {
                    if (ServiceEvent.REGISTERED == event.getType()) {
                        LogWrapperLoader.SINGLETON.addLoggerRef(event.getServiceReference());
                    } else if (ServiceEvent.UNREGISTERING == event.getType()) {
                        LogWrapperLoader.SINGLETON.removeLoggerRef(event.getServiceReference());
                    }
                }
            };
            context.addServiceListener(listener, "(" + Constants.OBJECTCLASS + "=org.osgi.service.log.LogService)");
            logWrapper.logServiceListener = listener;
            // Add all available LogService references to the singleton.
            final ServiceReference[] refs = context.getServiceReferences("org.osgi.service.log.LogService", null);
            if (null != refs) {
                for (int i = 0; i < refs.length; i++) {
                    logWrapper.addLoggerRef(refs[i]);
                }
            }
        } catch (InvalidSyntaxException e) {
        // this never happens
        }
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) ServiceEvent(org.osgi.framework.ServiceEvent) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 4 with ServiceListener

use of org.osgi.framework.ServiceListener in project felix by apache.

the class EventDispatcher method invokeServiceListenerCallback.

private static void invokeServiceListenerCallback(Bundle bundle, final EventListener l, Filter filter, Object acc, final EventObject event, final Dictionary<String, ?> oldProps) {
    // STOPPING, and ACTIVE bundles.
    if ((bundle.getState() != Bundle.STARTING) && (bundle.getState() != Bundle.STOPPING) && (bundle.getState() != Bundle.ACTIVE)) {
        return;
    }
    // Check that the bundle has permission to get at least
    // one of the service interfaces; the objectClass property
    // of the service stores its service interfaces.
    ServiceReference ref = ((ServiceEvent) event).getServiceReference();
    boolean hasPermission = true;
    if (hasPermission) {
        // Dispatch according to the filter.
        boolean matched = (filter == null) || filter.match(((ServiceEvent) event).getServiceReference());
        if (matched) {
            ((ServiceListener) l).serviceChanged((ServiceEvent) event);
        } else // matched previously.
        if (((ServiceEvent) event).getType() == ServiceEvent.MODIFIED) {
            if (filter.match(oldProps)) {
                final ServiceEvent se = new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, ((ServiceEvent) event).getServiceReference());
                ((ServiceListener) l).serviceChanged(se);
            }
        }
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) ServiceEvent(org.osgi.framework.ServiceEvent) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with ServiceListener

use of org.osgi.framework.ServiceListener in project felix by apache.

the class EventDispatcher method invokeServiceListenerCallback.

private static void invokeServiceListenerCallback(Bundle bundle, final EventListener l, Filter filter, Object acc, final EventObject event, final Dictionary oldProps) {
    // STOPPING, and ACTIVE bundles.
    if ((bundle.getState() != Bundle.STARTING) && (bundle.getState() != Bundle.STOPPING) && (bundle.getState() != Bundle.ACTIVE)) {
        return;
    }
    // Check that the bundle has permission to get at least
    // one of the service interfaces; the objectClass property
    // of the service stores its service interfaces.
    ServiceReference ref = ((ServiceEvent) event).getServiceReference();
    boolean hasPermission = true;
    Object sm = System.getSecurityManager();
    if ((acc != null) && (sm != null)) {
        try {
            ServicePermission perm = new ServicePermission(ref, ServicePermission.GET);
            ((SecurityManager) sm).checkPermission(perm, acc);
        } catch (Exception ex) {
            hasPermission = false;
        }
    }
    if (hasPermission) {
        // Dispatch according to the filter.
        boolean matched;
        if (l instanceof UnfilteredServiceListener) {
            // An UnfilteredServiceListener always matches, regardless of the filter.
            // The filter is still passed on to the Service Registry Hooks.
            matched = true;
        } else {
            matched = (filter == null) || filter.match(((ServiceEvent) event).getServiceReference());
        }
        if (matched) {
            if ((l instanceof AllServiceListener) || Util.isServiceAssignable(bundle, ((ServiceEvent) event).getServiceReference())) {
                if (System.getSecurityManager() != null) {
                    AccessController.doPrivileged(new PrivilegedAction() {

                        @Override
                        public Object run() {
                            ((ServiceListener) l).serviceChanged((ServiceEvent) event);
                            return null;
                        }
                    });
                } else {
                    ((ServiceListener) l).serviceChanged((ServiceEvent) event);
                }
            }
        } else // matched previously.
        if (((ServiceEvent) event).getType() == ServiceEvent.MODIFIED) {
            if (filter.match(oldProps)) {
                final ServiceEvent se = new ServiceEvent(ServiceEvent.MODIFIED_ENDMATCH, ((ServiceEvent) event).getServiceReference());
                if (System.getSecurityManager() != null) {
                    AccessController.doPrivileged(new PrivilegedAction() {

                        @Override
                        public Object run() {
                            ((ServiceListener) l).serviceChanged(se);
                            return null;
                        }
                    });
                } else {
                    ((ServiceListener) l).serviceChanged(se);
                }
            }
        }
    }
}
Also used : AllServiceListener(org.osgi.framework.AllServiceListener) UnfilteredServiceListener(org.osgi.framework.UnfilteredServiceListener) ServiceListener(org.osgi.framework.ServiceListener) PrivilegedAction(java.security.PrivilegedAction) ServiceEvent(org.osgi.framework.ServiceEvent) ServicePermission(org.osgi.framework.ServicePermission) EventObject(java.util.EventObject) ServiceReference(org.osgi.framework.ServiceReference) UnfilteredServiceListener(org.osgi.framework.UnfilteredServiceListener) AllServiceListener(org.osgi.framework.AllServiceListener)

Aggregations

ServiceListener (org.osgi.framework.ServiceListener)28 ServiceEvent (org.osgi.framework.ServiceEvent)21 ServiceReference (org.osgi.framework.ServiceReference)16 Bundle (org.osgi.framework.Bundle)8 Test (org.junit.Test)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)6 IOException (java.io.IOException)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 ArrayList (java.util.ArrayList)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BundleContext (org.osgi.framework.BundleContext)4 BundleException (org.osgi.framework.BundleException)4 Hashtable (java.util.Hashtable)3 Dictionary (java.util.Dictionary)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ObjectName (javax.management.ObjectName)2