@Override public void removedService(@SuppressWarnings("rawtypes") ServiceReference reference, Object event) { ServiceEventImpl evt = (ServiceEventImpl) event; m_component.handleEvent(this, EventType.REMOVED, evt); }
public ServiceEventImpl(ComponentContext ctx, ServiceReference<?> reference, Object service) { super(service); m_service = service; m_bundle = ctx.getBundle(); m_bundleContext = ctx.getBundleContext(); m_serviceObjects = (m_bundleContext != null) ? m_bundleContext.getServiceObjects(reference) : null; m_reference = reference; m_logger = ctx.getLogger(); }
private void invokeCallbacks(Component component, Object callbackInstance, String callback, String defaultCallback) { boolean logIfNotFound = (callback != null); callback = callback != null ? callback : defaultCallback; ComponentContext ctx = (ComponentContext) component; Object[] instances = callbackInstance != null ? new Object[] { callbackInstance } : ctx.getInstances(); ctx.invokeCallbackMethod(instances, callback, new Class[][] {{ Component.class }, {}}, new Object[][] {{ component }, {}}, logIfNotFound); }
} catch (IllegalStateException e) { if (m_component.isActive()) { m_component.getLogger().warn("could not handle service dependency for component %s", e, m_component.getComponentDeclaration().getClassName()); m_component.handleEvent(this, EventType.SWAPPED, evt, newEvt); } else { addedService(newReference, newService);
/** * Triggers component activation when the future has completed. * @param result * @param error */ private void completed(F result, Throwable error) { if (error != null) { super.getComponentContext().getLogger().log(LogService.LOG_ERROR, "completable future failed", error); } else { // Will trigger component activation (if other dependencies are satisfied), and our invokeCallback method will then be called. m_component.handleEvent(this, EventType.ADDED, new Event(result)); } }
if (m_accept2Type != null) { Object componentInstance = Stream.of(getComponentContext().getInstances()) .filter(instance -> Helpers.getClass(instance).equals(m_accept2Type)) .findFirst() } else { m_accept2.accept(getComponentContext().getInstance(), result); super.getComponentContext().getLogger().log(LogService.LOG_ERROR, "completable future failed", exc);
private Object getDefaultImplementation() { if (m_defaultImplementation != null) { if (m_defaultImplementation instanceof Class) { try { m_defaultImplementationInstance = ((Class<?>) m_defaultImplementation).newInstance(); } catch (Throwable e) { m_component.getLogger().err("Could not create default implementation instance of class %s.", e, m_defaultImplementation); } } else { m_defaultImplementationInstance = m_defaultImplementation; } } return m_defaultImplementationInstance; }
public void invoke(String method, Event e, Object[] instances) { // specific for this type of dependency m_component.invokeCallbackMethod(instances, method, new Class[][] { {} }, new Object[][] { {} }); }
@Override public Object addingService(@SuppressWarnings("rawtypes") ServiceReference reference) { try { ServiceEventImpl event = new ServiceEventImpl(m_component, reference, null); if (obtainServiceBeforeInjecting()) { Object service = event.getEvent(); // will dereference the service object. if (service == null) { // service concurrently removed, ignore return null; } } return event; } catch (IllegalStateException e) { // most likely our bundle is being stopped. Only log an exception if our component is enabled. if (m_component.isActive()) { m_component.getLogger().warn("could not handle service dependency for component %s", e, m_component.getComponentDeclaration().getClassName()); } return null; } }
@SuppressWarnings("unchecked") public Dictionary<String, Object> getProperties() { ResourceEventImpl re = (ResourceEventImpl) m_component.getDependencyEvent(this); if (re != null) { URL resource = re.getResource(); m_component.getLogger().warn("Exception while invoking callback method", e.getCause()); m_component.getLogger().warn("Exception while trying to invoke callback method", e); props.put(key, resourceProperties.get(key).toString()); } else { m_component.getLogger().warn( "Custom resource property is overlapping with the default resource property for key: %s", key);
private ConfigurationDependencyImpl createConfigurationDependency(Object service) { BundleContext bc = mock(BundleContext.class); Logger mockLogger = mock(Logger.class); ComponentContext component = mock(ComponentContext.class); when(component.getExecutor()).thenReturn(Executors.newSingleThreadExecutor()); when(component.getLogger()).thenReturn(mockLogger); ConfigurationDependencyImpl result = new ConfigurationDependencyImpl(bc, mockLogger); result.setCallback(service, "updated").setPid("does.not.matter"); result.setComponentContext(component); result.start(); return result; }
@Override public void start() { Dictionary<String, Object> props = null; if (m_trackedResource != null) { props = new Hashtable<>(); props.put(ResourceHandler.URL, m_trackedResource); } else { if (m_resourceFilter != null) { props = new Hashtable<>(); props.put(ResourceHandler.FILTER, m_resourceFilter); } } m_registration = m_component.getBundleContext().registerService(ResourceHandler.class.getName(), this, props); super.start(); }
m_component.invokeCallback(instances, swapMethod, new Class[][]{ {m_trackedServiceName, m_trackedServiceName}, m_component.getLogger().err("Could not invoke swap callback", e);
Object mainComponentInstance = m_component.getInstance(); if (mainComponentInstance instanceof AbstractDecorator || m_component.injectionDisabled()) { return;
@SuppressWarnings("rawtypes") @Override public void updated(Dictionary settings) throws ConfigurationException { // Handle the update in the component executor thread. Any exception thrown during the component updated callback will be // synchronously awaited and re-thrown to the CM thread. // We schedule the update in the component executor in order to avoid race conditions, // like when the component is stopping while we receive a configuration update, or if the component restarts // while the configuration is being updated, or if the getProperties method is invoked while we are losing the configuration ... // there are many racy situations, and the safe way to handle them is to schedule the updated callback in the component executor. InvocationUtil.invokeUpdated(m_component.getExecutor(), () -> doUpdated(settings)); }
/** * Returns the dependency callback instances * @return the dependency callback instances */ public Object[] getInstances() { if (m_callbackInstance == null) { return m_component.getInstances(); } else { return new Object[] { m_callbackInstance }; } }
private Bundle getNullObject() { if (m_nullObject == null) { try { m_nullObject = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { Bundle.class }, new DefaultNullObject()); } catch (Throwable e) { m_component.getLogger().err("Could not create null object for Bundle.", e); } } return (Bundle) m_nullObject; }
/** * Injects the completed future result in a method by reflection. * We try to find a method which has in its signature a parameter that is compatible with the future result * (including any interfaces the result may implements). * * @param result the result of the completable future. */ private void injectByReflection(Object result) { List<Class<?>> types = new ArrayList<>(); Class<?> currentClazz = result.getClass(); while (currentClazz != null && currentClazz != Object.class) { types.add(currentClazz); Stream.of(currentClazz.getInterfaces()).forEach(types::add); currentClazz = currentClazz.getSuperclass(); } Class<?>[][] classes = new Class<?>[types.size() + 1][1]; Object[][] results = new Object[types.size() + 1][1]; for (int i = 0; i < types.size(); i ++) { classes[i] = new Class<?>[] { types.get(i) }; results[i] = new Object[] { result }; } classes[types.size()] = new Class<?>[0]; results[types.size()] = new Object[0]; m_component.invokeCallbackMethod(getInstances(), m_add, classes, results); } }
@SuppressWarnings("unchecked") @Override public Dictionary<String, Object> getProperties() { ServiceEventImpl se = (ServiceEventImpl) m_component.getDependencyEvent(this); if (se != null) { if (m_propagateCallbackInstance != null && m_propagateCallbackMethod != null) { return (Dictionary<String, Object>) InvocationUtil.invokeCallbackMethod(m_propagateCallbackInstance, m_propagateCallbackMethod, callbackInfo.m_sigs, callbackInfo.m_args); } catch (InvocationTargetException e) { m_component.getLogger().warn("Exception while invoking callback method", e.getCause()); } catch (Throwable e) { m_component.getLogger().warn("Exception while trying to invoke callback method", e);
@Override public void start() { BundleContext context = m_component.getBundleContext(); if (context != null) { // If null, we are in a test environment Properties props = new Properties(); props.put(Constants.SERVICE_PID, m_pid); ManagedService ms = this; if (m_metaType != null) { ms = m_metaType; props.put(MetaTypeProvider.METATYPE_PID, m_pid); String[] ifaces = new String[] { ManagedService.class.getName(), MetaTypeProvider.class.getName() }; m_registration = context.registerService(ifaces, ms, toR6Dictionary(props)); } else { m_registration = context.registerService(ManagedService.class.getName(), ms, toR6Dictionary(props)); } } super.start(); }