Search in sources :

Example 31 with ServiceReference

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");
    }
}
Also used : SessionFactory(org.apache.karaf.shell.api.console.SessionFactory) SecuredSessionFactoryImpl(org.apache.karaf.shell.impl.console.osgi.secured.SecuredSessionFactoryImpl) ThreadIOImpl(org.apache.felix.gogo.runtime.threadio.ThreadIOImpl) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ServiceReference(org.osgi.framework.ServiceReference) CommandExtender(org.apache.karaf.shell.impl.action.osgi.CommandExtender) ManagerImpl(org.apache.karaf.shell.impl.action.command.ManagerImpl) CommandLoggingFilter(org.apache.karaf.shell.api.console.CommandLoggingFilter)

Example 32 with ServiceReference

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;
}
Also used : Bundle(org.osgi.framework.Bundle) ServiceReference(org.osgi.framework.ServiceReference)

Example 33 with ServiceReference

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);
        }
    }
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 34 with ServiceReference

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;
    }
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) ServiceReference(org.osgi.framework.ServiceReference)

Example 35 with ServiceReference

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;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ArrayList(java.util.ArrayList) ResourceProviderInfo(org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1687 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)229 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)182 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65