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