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));
}
}
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;
}
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();
}
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);
}
}
}
}
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();
}
Aggregations