Search in sources :

Example 11 with EventAdmin

use of org.osgi.service.event.EventAdmin in project karaf by apache.

the class EventAdminListener method beforeExecute.

public void beforeExecute(CommandSession session, CharSequence command) {
    if (command.toString().trim().length() > 0) {
        EventAdmin admin = tracker.getService();
        if (admin != null) {
            Map<String, Object> props = new HashMap<>();
            props.put("command", command.toString());
            Event event = new Event("org/apache/karaf/shell/console/EXECUTING", props);
            admin.postEvent(event);
        }
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) HashMap(java.util.HashMap) Event(org.osgi.service.event.Event)

Example 12 with EventAdmin

use of org.osgi.service.event.EventAdmin in project karaf by apache.

the class EventAdminListener method repositoryEvent.

public void repositoryEvent(RepositoryEvent event) {
    try {
        EventAdmin eventAdmin = tracker.getService();
        if (eventAdmin == null) {
            return;
        }
        Dictionary<String, Object> props = new Hashtable<>();
        props.put(EventConstants.TYPE, event.getType());
        props.put(EventConstants.EVENT, event);
        props.put(EventConstants.TIMESTAMP, System.currentTimeMillis());
        props.put(EventConstants.REPOSITORY_URI, event.getRepository().getURI().toString());
        String topic;
        switch(event.getType()) {
            case RepositoryAdded:
                topic = EventConstants.TOPIC_REPOSITORY_ADDED;
                break;
            case RepositoryRemoved:
                topic = EventConstants.TOPIC_REPOSITORY_REMOVED;
                break;
            default:
                throw new IllegalStateException("Unknown repository event type: " + event.getType());
        }
        eventAdmin.postEvent(new Event(topic, props));
    } catch (IllegalStateException e) {
        LOGGER.warn("Unable to post event to EventAdmin", e);
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event) FeatureEvent(org.apache.karaf.features.FeatureEvent) RepositoryEvent(org.apache.karaf.features.RepositoryEvent)

Example 13 with EventAdmin

use of org.osgi.service.event.EventAdmin in project sling by apache.

the class AdapterManagerImpl method unregisterAdapterFactory.

/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void unregisterAdapterFactory(final ServiceReference<AdapterFactory> reference) {
    synchronized (this.boundAdapterFactories) {
        boundAdapterFactories.remove(reference);
    }
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    boolean factoriesModified = false;
    AdapterFactoryDescriptorMap adfMap = null;
    AdapterFactoryDescriptor removedDescriptor = null;
    for (final String adaptable : adaptables) {
        synchronized (this.descriptors) {
            adfMap = this.descriptors.get(adaptable);
        }
        if (adfMap != null) {
            synchronized (adfMap) {
                AdapterFactoryDescriptor factoryDesc = adfMap.remove(reference);
                if (factoryDesc != null) {
                    factoriesModified = true;
                    // Since the code paths above does not fully guarantee it though, let's keep this check in place
                    if (removedDescriptor != null && removedDescriptor != factoryDesc) {
                        log.error("When unregistering reference {} got duplicate service descriptors {} and {}. Unregistration of {} services may be incomplete.", new Object[] { reference, removedDescriptor, factoryDesc, Adaption.class.getName() });
                    }
                    removedDescriptor = factoryDesc;
                }
            }
        }
    }
    // removed
    if (factoriesModified) {
        this.factoryCache.clear();
    }
    // unregister adaption
    if (removedDescriptor != null) {
        removedDescriptor.getAdaption().unregister();
        if (log.isDebugEnabled()) {
            log.debug("Unregistered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
        }
    }
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        final Dictionary<String, Object> props = new Hashtable<>();
        props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
        props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_REMOVED, props));
    }
}
Also used : Adaption(org.apache.sling.adapter.Adaption) EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Example 14 with EventAdmin

use of org.osgi.service.event.EventAdmin in project sling by apache.

the class MapEntries method sendChangeEvent.

/**
     * Send an OSGi event
     */
private void sendChangeEvent() {
    final EventAdmin local = this.eventAdmin;
    if (local != null) {
        final Event event = new Event(SlingConstants.TOPIC_RESOURCE_RESOLVER_MAPPING_CHANGED, (Dictionary<String, ?>) null);
        local.postEvent(event);
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Event(org.osgi.service.event.Event)

Example 15 with EventAdmin

use of org.osgi.service.event.EventAdmin in project sling by apache.

the class DistributingEventHandlerTest method setup.

@SuppressWarnings("unchecked")
@Before
public void setup() throws Exception {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    Mockito.when(bc.registerService(Mockito.any(String[].class), Mockito.any(), Mockito.any(Dictionary.class))).thenReturn(null);
    final SlingSettingsService otherSettings = Mockito.mock(SlingSettingsService.class);
    Mockito.when(otherSettings.getSlingId()).thenReturn(OTHER_APP_ID);
    final EventAdmin ea = new EventAdmin() {

        @Override
        public void sendEvent(final Event event) {
            this.postEvent(event);
        }

        @Override
        public void postEvent(final Event event) {
            final String topic = event.getTopic();
            if (topic.equals(SlingConstants.TOPIC_RESOURCE_ADDED)) {
                final ResourceChange change = new ResourceChange(ChangeType.ADDED, (String) event.getProperty(SlingConstants.PROPERTY_PATH), false, null, null, null);
                sender.onChange(Collections.singletonList(change));
            } else if (topic.startsWith(TOPIC_PREFIX)) {
                events.add(event);
            }
        }
    };
    final MockResourceResolverFactoryOptions opts = new MockResourceResolverFactoryOptions();
    opts.setEventAdmin(ea);
    final ResourceResolverFactory factory = new MockResourceResolverFactory(opts);
    this.sender = new DistributedEventSender(bc, DistributedEventAdminImpl.DEFAULT_REPOSITORY_PATH, DistributedEventAdminImpl.DEFAULT_REPOSITORY_PATH + "/" + MY_APP_ID, factory, ea);
    this.receiver = new DistributedEventReceiver(bc, DistributedEventAdminImpl.DEFAULT_REPOSITORY_PATH, DistributedEventAdminImpl.DEFAULT_REPOSITORY_PATH + "/" + OTHER_APP_ID, 15, factory, otherSettings);
}
Also used : Dictionary(java.util.Dictionary) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) MockResourceResolverFactory(org.apache.sling.testing.resourceresolver.MockResourceResolverFactory) EventAdmin(org.osgi.service.event.EventAdmin) MockResourceResolverFactoryOptions(org.apache.sling.testing.resourceresolver.MockResourceResolverFactoryOptions) Event(org.osgi.service.event.Event) MockResourceResolverFactory(org.apache.sling.testing.resourceresolver.MockResourceResolverFactory) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange) SlingSettingsService(org.apache.sling.settings.SlingSettingsService) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Aggregations

EventAdmin (org.osgi.service.event.EventAdmin)26 Event (org.osgi.service.event.Event)18 Hashtable (java.util.Hashtable)11 Test (org.junit.Test)7 Adaption (org.apache.sling.adapter.Adaption)4 BundleContext (org.osgi.framework.BundleContext)4 HashMap (java.util.HashMap)3 Fixture (org.apache.sling.resourceresolver.impl.Fixture)3 ServiceReference (org.osgi.framework.ServiceReference)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FeatureEvent (org.apache.karaf.features.FeatureEvent)2 RepositoryEvent (org.apache.karaf.features.RepositoryEvent)2 AdapterFactoryDescriptor (org.apache.sling.adapter.internal.AdapterFactoryDescriptor)2 AdapterFactoryDescriptorMap (org.apache.sling.adapter.internal.AdapterFactoryDescriptorMap)2 ChangeListener (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker.ChangeListener)2 Before (org.junit.Before)2 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)1 Date (java.util.Date)1 Dictionary (java.util.Dictionary)1 EventObject (java.util.EventObject)1