use of org.osgi.framework.ServiceReference in project karaf by apache.
the class Activator method start.
@Override
public void start(final BundleContext context) throws Exception {
threadIO = new ThreadIOImpl();
threadIO.start();
sessionFactory = new SecuredSessionFactoryImpl(context, threadIO);
sessionFactory.getCommandProcessor().addConverter(new Converters(context));
sessionFactory.getCommandProcessor().addConstant(".context", context.getBundle(0).getBundleContext());
final CopyOnWriteArraySet<CommandLoggingFilter> listeners = new CopyOnWriteArraySet<>();
filterTracker = new ServiceTracker<>(context, CommandLoggingFilter.class, new ServiceTrackerCustomizer<CommandLoggingFilter, CommandLoggingFilter>() {
@Override
public CommandLoggingFilter addingService(ServiceReference<CommandLoggingFilter> reference) {
CommandLoggingFilter service = context.getService(reference);
listeners.add(service);
return service;
}
@Override
public void modifiedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
}
@Override
public void removedService(ServiceReference<CommandLoggingFilter> reference, CommandLoggingFilter service) {
listeners.remove(service);
context.ungetService(reference);
}
});
filterTracker.open();
LoggingCommandSessionListener loggingCommandSessionListener = new LoggingCommandSessionListener();
loggingCommandSessionListener.setFilters(listeners);
sessionFactory.getCommandProcessor().addListener(loggingCommandSessionListener);
try {
EventAdminListener listener = new EventAdminListener(context);
sessionFactory.getCommandProcessor().addListener(listener);
eventAdminListener = listener;
} catch (NoClassDefFoundError error) {
// Ignore the listener if EventAdmin package isn't present
}
sessionFactory.register(new ManagerImpl(sessionFactory, sessionFactory));
sessionFactoryRegistration = context.registerService(SessionFactory.class, sessionFactory, null);
actionExtender = new CommandExtender(sessionFactory);
actionExtender.start(context);
commandTracker = new CommandTracker(sessionFactory, context);
commandTracker.open();
if (Boolean.parseBoolean(context.getProperty(START_CONSOLE))) {
localConsoleManager = new LocalConsoleManager(context, sessionFactory);
localConsoleManager.start();
} else {
LOGGER.info("Not starting local console. To activate set " + START_CONSOLE + "=true");
}
}
use of org.osgi.framework.ServiceReference in project karaf by apache.
the class Capabilities method printServiceCapabilities.
static boolean printServiceCapabilities(Bundle b) {
boolean matches = false;
try {
ServiceReference<?>[] refs = b.getRegisteredServices();
if ((refs != null) && (refs.length > 0)) {
matches = true;
// Print properties for each service.
for (ServiceReference<?> ref : refs) {
// Print object class with "namespace".
System.out.println(NONSTANDARD_SERVICE_NAMESPACE + "; " + ShellUtil.getValueString(ref.getProperty("objectClass")) + " with properties:");
// Print service properties.
String[] keys = ref.getPropertyKeys();
for (String key : keys) {
if (!key.equalsIgnoreCase(Constants.OBJECTCLASS)) {
Object v = ref.getProperty(key);
System.out.println(" " + key + " = " + ShellUtil.getValueString(v));
}
}
Bundle[] users = ref.getUsingBundles();
if ((users != null) && (users.length > 0)) {
System.out.println(" Used by:");
for (Bundle user : users) {
System.out.println(" " + user);
}
}
}
}
} catch (Exception ex) {
System.err.println(ex.toString());
}
return matches;
}
use of org.osgi.framework.ServiceReference in project karaf by apache.
the class MultiServiceTracker method close.
@SuppressWarnings("rawtypes")
public void close() {
if (open.compareAndSet(true, false)) {
ctx.removeServiceListener(listener);
List<ServiceReference> oldRefs;
synchronized (refs) {
oldRefs = new ArrayList<>(refs.keySet());
refs.clear();
}
for (ServiceReference ref : oldRefs) {
ctx.ungetService(ref);
}
}
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class FactoryPreconditions method checkPreconditions.
public boolean checkPreconditions(final String unavailableName, final String unavailableServicePid) {
synchronized (this) {
final List<RequiredProvider> localRequiredProviders = this.requiredProviders;
final ResourceProviderTracker localTracker = this.tracker;
boolean canRegister = localTracker != null;
if (localRequiredProviders != null && localTracker != null) {
for (final RequiredProvider rp : localRequiredProviders) {
canRegister = false;
for (final ResourceProviderHandler h : localTracker.getResourceProviderStorage().getAllHandlers()) {
final ServiceReference ref = h.getInfo().getServiceReference();
final Object servicePid = ref.getProperty(Constants.SERVICE_PID);
if (unavailableServicePid != null && unavailableServicePid.equals(servicePid)) {
// ignore this service
continue;
}
if (unavailableName != null && unavailableName.equals(h.getInfo().getName())) {
// ignore this service
continue;
}
if (rp.name != null && rp.name.equals(h.getInfo().getName())) {
canRegister = true;
break;
} else if (rp.filter != null && rp.filter.match(ref)) {
canRegister = true;
break;
} else if (rp.pid != null && rp.pid.equals(servicePid)) {
canRegister = true;
break;
} else if (rp.pid != null && rp.pid.equals(ref.getProperty(LegacyResourceProviderWhiteboard.ORIGINAL_SERVICE_PID))) {
canRegister = true;
break;
}
}
if (!canRegister) {
break;
}
}
}
return canRegister;
}
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class FactoryPreconditionsTest method getResourceProviderHandlersWithNames.
private List<ResourceProviderHandler> getResourceProviderHandlersWithNames(String[] names) {
final List<ResourceProviderHandler> result = new ArrayList<ResourceProviderHandler>();
for (final String n : names) {
final ResourceProviderHandler handler = Mockito.mock(ResourceProviderHandler.class);
final ResourceProviderInfo info = Mockito.mock(ResourceProviderInfo.class);
Mockito.when(info.getName()).thenReturn(n);
final ServiceReference ref = Mockito.mock(ServiceReference.class);
Mockito.when(handler.getInfo()).thenReturn(info);
Mockito.when(info.getServiceReference()).thenReturn(ref);
result.add(handler);
}
return result;
}
Aggregations