Search in sources :

Example 11 with Event

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

the class AdapterManagerImpl method registerAdapterFactory.

/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void registerAdapterFactory(final ComponentContext context, final ServiceReference<AdapterFactory> reference) {
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    final boolean allowedInPrivatePackage = PropertiesUtil.toBoolean(reference.getProperty(ALLOWED_IN_PRIVATE), false);
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    for (String clazz : adaptables) {
        if (!allowedInPrivatePackage && !checkPackage(packageAdmin, clazz)) {
            log.warn("Adaptable class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
        }
    }
    for (String clazz : adapters) {
        if (!allowedInPrivatePackage && !checkPackage(packageAdmin, clazz)) {
            log.warn("Adapter class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
        }
    }
    final AdapterFactoryDescriptor factoryDesc = new AdapterFactoryDescriptor(context, reference, adapters);
    for (final String adaptable : adaptables) {
        AdapterFactoryDescriptorMap adfMap = null;
        synchronized (this.descriptors) {
            adfMap = descriptors.get(adaptable);
            if (adfMap == null) {
                adfMap = new AdapterFactoryDescriptorMap();
                descriptors.put(adaptable, adfMap);
            }
        }
        synchronized (adfMap) {
            adfMap.put(reference, factoryDesc);
        }
    }
    // clear the factory cache to force rebuild on next access
    this.factoryCache.clear();
    // register adaption
    final Dictionary<String, Object> props = new Hashtable<>();
    props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
    props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
    ServiceRegistration<Adaption> adaptionRegistration = this.context.getBundleContext().registerService(Adaption.class, AdaptionImpl.INSTANCE, props);
    if (log.isDebugEnabled()) {
        log.debug("Registered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
    }
    factoryDesc.setAdaption(adaptionRegistration);
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_ADDED, 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 12 with Event

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

the class DistributedEventSender method readEvent.

/**
     * Read an event from the resource
     * @return The event object or <code>null</code>
     */
private Event readEvent(final Resource eventResource) {
    try {
        final ValueMap vm = ResourceHelper.getValueMap(eventResource);
        final String topic = vm.get(EventConstants.EVENT_TOPIC, String.class);
        if (topic == null) {
            // no topic should never happen as we check the resource type before
            logger.error("Unable to read distributed event from " + eventResource.getPath() + " : no topic property available.");
        } else {
            final Map<String, Object> properties = ResourceHelper.cloneValueMap(vm);
            // only send event if there are no read errors, otherwise discard it
            @SuppressWarnings("unchecked") final List<Exception> readErrorList = (List<Exception>) properties.remove(ResourceHelper.PROPERTY_MARKER_READ_ERROR_LIST);
            if (readErrorList == null) {
                properties.remove(EventConstants.EVENT_TOPIC);
                properties.remove(DEAConstants.PROPERTY_DISTRIBUTE);
                final Object oldRT = properties.remove("event.dea." + ResourceResolver.PROPERTY_RESOURCE_TYPE);
                if (oldRT != null) {
                    properties.put(ResourceResolver.PROPERTY_RESOURCE_TYPE, oldRT);
                } else {
                    properties.remove(ResourceResolver.PROPERTY_RESOURCE_TYPE);
                }
                try {
                    final Event event = new Event(topic, properties);
                    return event;
                } catch (final IllegalArgumentException iae) {
                    // this exception occurs if the topic is not correct (it should never happen,
                    // but you never know)
                    logger.error("Unable to read event: " + iae.getMessage(), iae);
                }
            } else {
                for (final Exception e : readErrorList) {
                    logger.warn("Unable to read distributed event from " + eventResource.getPath(), e);
                }
            }
        }
    } catch (final InstantiationException ie) {
        // something happened with the resource in the meantime
        this.ignoreException(ie);
    }
    return null;
}
Also used : ValueMap(org.apache.sling.api.resource.ValueMap) Event(org.osgi.service.event.Event) List(java.util.List) LoginException(org.apache.sling.api.resource.LoginException)

Example 13 with Event

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

the class DistributingEventHandlerTest method testSendEventPlusAppId.

@org.junit.Test(timeout = 5000)
public void testSendEventPlusAppId() throws Exception {
    this.events.clear();
    final String VALUE = "some value";
    final String topic = TOPIC_PREFIX + "event/test";
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put("a property", "some value");
    // now we check if the application id is handled correctly
    props.put(DEAConstants.PROPERTY_APPLICATION, "foo");
    final Event e = new Event(topic, props);
    this.receiver.handleEvent(e);
    while (this.events.size() == 0) {
        Thread.sleep(5);
    }
    final Event receivedEvent = this.events.get(0);
    assertEquals(topic, receivedEvent.getTopic());
    assertEquals(OTHER_APP_ID, receivedEvent.getProperty(DEAConstants.PROPERTY_APPLICATION));
    assertEquals(VALUE, receivedEvent.getProperty("a property"));
    assertNull(receivedEvent.getProperty(ResourceResolver.PROPERTY_RESOURCE_TYPE));
    this.events.clear();
}
Also used : Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event)

Example 14 with Event

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

the class NewJobSender method onChange.

@Override
public void onChange(final List<ResourceChange> resourceChanges) {
    for (final ResourceChange resourceChange : resourceChanges) {
        logger.debug("Received event {}", resourceChange);
        final String path = resourceChange.getPath();
        final int topicStart = this.configuration.getLocalJobsPath().length() + 1;
        final int topicEnd = path.indexOf('/', topicStart);
        if (topicEnd != -1) {
            final String topic = path.substring(topicStart, topicEnd).replace('.', '/');
            final String jobId = path.substring(topicEnd + 1);
            if (path.indexOf("_", topicEnd + 1) != -1) {
                // only job id and topic are guaranteed
                final Dictionary<String, Object> properties = new Hashtable<>();
                properties.put(NotificationConstants.NOTIFICATION_PROPERTY_JOB_ID, jobId);
                properties.put(NotificationConstants.NOTIFICATION_PROPERTY_JOB_TOPIC, topic);
                // we also set internally the queue name
                final String queueName = this.configuration.getQueueConfigurationManager().getQueueInfo(topic).queueName;
                properties.put(Job.PROPERTY_JOB_QUEUE_NAME, queueName);
                final Event jobEvent = new Event(NotificationConstants.TOPIC_JOB_ADDED, properties);
                // as this is send within handling an event, we do sync call
                this.eventAdmin.sendEvent(jobEvent);
            }
        }
    }
}
Also used : Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event) ResourceChange(org.apache.sling.api.resource.observation.ResourceChange)

Example 15 with Event

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

the class TopicMatchingTest method testDeepMatching.

/**
     * Test deep pattern matching /**
     */
@Test(timeout = DEFAULT_TEST_TIMEOUT)
public void testDeepMatching() throws Exception {
    final Barrier barrier = new Barrier(2);
    this.registerJobExecutor("sling/**", new JobExecutor() {

        @Override
        public JobExecutionResult process(final Job job, final JobExecutionContext context) {
            return context.result().succeeded();
        }
    });
    this.registerEventHandler(NotificationConstants.TOPIC_JOB_FINISHED, new EventHandler() {

        @Override
        public void handleEvent(final Event event) {
            barrier.block();
        }
    });
    this.getJobManager().addJob(TOPIC, null);
    barrier.block();
}
Also used : JobExecutionResult(org.apache.sling.event.jobs.consumer.JobExecutionResult) JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) JobExecutionContext(org.apache.sling.event.jobs.consumer.JobExecutionContext) EventHandler(org.osgi.service.event.EventHandler) Event(org.osgi.service.event.Event) Barrier(org.apache.sling.event.impl.Barrier) Job(org.apache.sling.event.jobs.Job) Test(org.junit.Test)

Aggregations

Event (org.osgi.service.event.Event)142 Test (org.junit.Test)79 HashMap (java.util.HashMap)48 Hashtable (java.util.Hashtable)44 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)23 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)20 EventAdmin (org.osgi.service.event.EventAdmin)19 GeospatialPredicate (ddf.catalog.pubsub.predicate.GeospatialPredicate)18 Predicate (ddf.catalog.pubsub.predicate.Predicate)16 ArrayList (java.util.ArrayList)16 ActivityEvent (org.codice.ddf.activities.ActivityEvent)13 EventHandler (org.osgi.service.event.EventHandler)13 SubscriptionFilterVisitor (ddf.catalog.pubsub.internal.SubscriptionFilterVisitor)11 Date (java.util.Date)11 Job (org.apache.sling.event.jobs.Job)9 Map (java.util.Map)8 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 JobManager (org.apache.sling.event.jobs.JobManager)7 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)7