Search in sources :

Example 1 with ServiceListener

use of org.apache.aries.blueprint.utils.ServiceListener in project aries by apache.

the class ServiceRecipe method unregister.

public void unregister() {
    ServiceRegistration reg = registration.get();
    if (reg != null) {
        LOGGER.debug("Unregistering service {}", name);
        // set to null before actually unregistering the service
        if (listeners != null) {
            LOGGER.debug("Calling listeners for service unregistration");
            for (ServiceListener listener : listeners) {
                listener.unregister(service, registrationProperties);
            }
        }
        ServiceUtil.safeUnregisterService(reg);
        registration.compareAndSet(reg, null);
    }
}
Also used : ServiceListener(org.apache.aries.blueprint.utils.ServiceListener) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with ServiceListener

use of org.apache.aries.blueprint.utils.ServiceListener in project aries by apache.

the class CmManagedServiceFactory method preUnregister.

protected void preUnregister(Object service, Dictionary properties, ServiceRegistration registration) {
    if (listeners != null && !listeners.isEmpty()) {
        Hashtable props = new Hashtable();
        JavaUtils.copy(properties, props);
        for (ServiceListener listener : listeners) {
            listener.unregister(service, props);
        }
    }
}
Also used : ServiceListener(org.apache.aries.blueprint.utils.ServiceListener) Hashtable(java.util.Hashtable)

Example 3 with ServiceListener

use of org.apache.aries.blueprint.utils.ServiceListener in project aries by apache.

the class CmManagedServiceFactory method postRegister.

protected void postRegister(Object service, Dictionary properties, ServiceRegistration registration) {
    if (listeners != null && !listeners.isEmpty()) {
        Hashtable props = new Hashtable();
        JavaUtils.copy(properties, props);
        for (ServiceListener listener : listeners) {
            listener.register(service, props);
        }
    }
}
Also used : ServiceListener(org.apache.aries.blueprint.utils.ServiceListener) Hashtable(java.util.Hashtable)

Example 4 with ServiceListener

use of org.apache.aries.blueprint.utils.ServiceListener in project aries by apache.

the class ServiceRecipe method createService.

private void createService() {
    try {
        if (service == null) {
            LOGGER.debug("Creating service instance");
            //We can't use the BlueprintRepository because we don't know what interfaces
            //to use yet! We have to be a bit smarter.
            ExecutionContext old = ExecutionContext.Holder.setContext(blueprintContainer.getRepository());
            try {
                Object o = serviceRecipe.create();
                if (o instanceof Convertible) {
                    o = blueprintContainer.getRepository().convert(o, new ReifiedType(Object.class));
                    validateClasses(o);
                } else if (o instanceof UnwrapperedBeanHolder) {
                    UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) o;
                    if (holder.unwrapperedBean instanceof ServiceFactory) {
                        //If a service factory is used, make sure the proxy classes implement this
                        //interface so that later on, internalGetService will create the real
                        //service from it.
                        LOGGER.debug("{} implements ServiceFactory, creating proxy that also implements this", holder.unwrapperedBean);
                        Collection<Class<?>> cls = getClassesForProxying(holder.unwrapperedBean);
                        cls.add(blueprintContainer.loadClass("org.osgi.framework.ServiceFactory"));
                        o = BeanRecipe.wrap(holder, cls);
                    } else {
                        validateClasses(holder.unwrapperedBean);
                        o = BeanRecipe.wrap(holder, getClassesForProxying(holder.unwrapperedBean));
                    }
                } else if (!(o instanceof ServiceFactory)) {
                    validateClasses(o);
                }
                service = o;
            } catch (Exception e) {
                LOGGER.error("Error retrieving service from " + this, e);
                throw new ComponentDefinitionException(e);
            } finally {
                ExecutionContext.Holder.setContext(old);
            }
            LOGGER.debug("Service created: {}", service);
        }
        // When the service is first requested, we need to create listeners and call them
        if (!initialServiceRegistration && listeners == null) {
            LOGGER.debug("Creating listeners");
            if (listenersRecipe != null) {
                listeners = (List) createRecipe(listenersRecipe);
            } else {
                listeners = Collections.emptyList();
            }
            LOGGER.debug("Listeners created: {}", listeners);
            if (registration.get() != null) {
                LOGGER.debug("Calling listeners for initial service registration");
                for (ServiceListener listener : listeners) {
                    listener.register(service, registrationProperties);
                }
            } else {
                LOGGER.debug("Calling listeners for initial service unregistration");
                for (ServiceListener listener : listeners) {
                    listener.unregister(service, registrationProperties);
                }
            }
        }
    } catch (RuntimeException e) {
        LOGGER.error("Error retrieving service from " + this, e);
        throw e;
    }
}
Also used : ExecutionContext(org.apache.aries.blueprint.di.ExecutionContext) Convertible(org.apache.aries.blueprint.container.AggregateConverter.Convertible) ServiceListener(org.apache.aries.blueprint.utils.ServiceListener) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) ServiceFactory(org.osgi.framework.ServiceFactory) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) Collection(java.util.Collection) ComponentDefinitionException(org.osgi.service.blueprint.container.ComponentDefinitionException) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Aggregations

ServiceListener (org.apache.aries.blueprint.utils.ServiceListener)4 Hashtable (java.util.Hashtable)2 Collection (java.util.Collection)1 Convertible (org.apache.aries.blueprint.container.AggregateConverter.Convertible)1 UnwrapperedBeanHolder (org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)1 ExecutionContext (org.apache.aries.blueprint.di.ExecutionContext)1 ServiceFactory (org.osgi.framework.ServiceFactory)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)1 ReifiedType (org.osgi.service.blueprint.container.ReifiedType)1