Search in sources :

Example 71 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project karaf by apache.

the class KarafTestSupport method getOsgiService.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        // Note that the tracker is not closed to keep the reference
        // This is buggy, as the service reference may change i think
        Object svc = type.cast(tracker.waitForService(timeout));
        if (svc == null) {
            Dictionary dic = bundleContext.getBundle().getHeaders();
            System.err.println("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                System.err.println("ServiceReference: " + ref);
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref);
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return type.cast(svc);
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : Dictionary(java.util.Dictionary) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 72 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project karaf by apache.

the class SecuredCommandConfigTransformer method refreshTheAffectedShellCommandBundle.

private void refreshTheAffectedShellCommandBundle(ConfigurationEvent event, Configuration config) {
    if (!config.getPid().startsWith(PROXY_COMMAND_ACL_PID_PREFIX)) {
        // not a command scope configuration file
        return;
    }
    String filter = "";
    String scopeName = config.getPid().substring(PROXY_COMMAND_ACL_PID_PREFIX.length());
    if (scopeName.indexOf('.') >= 0) {
        // scopes don't contains dots, not a command scope
        return;
    }
    scopeName = scopeName.trim();
    for (Entry<String, String> entry : loadScopeBundleMaps().entrySet()) {
        if (entry.getKey().equals(scopeName)) {
            filter = "(" + "osgi.blueprint.container.symbolicname" + "=" + entry.getValue() + ")";
            break;
        }
    }
    if (filter.length() == 0) {
        return;
    }
    BundleContext bundleContext = event.getReference().getBundle().getBundleContext();
    try {
        ServiceReference<?>[] sr = bundleContext.getServiceReferences("org.osgi.service.blueprint.container.BlueprintContainer", filter);
        if (sr == null) {
            LOGGER.error("can't find the command bundle for scope " + scopeName);
            return;
        }
        LOGGER.debug("the refreshed bundle is " + sr[0].getBundle().getSymbolicName());
        ServiceReference ref = bundleContext.getServiceReference(PackageAdmin.class.getName());
        if (ref == null) {
            LOGGER.error("PackageAdmin service is unavailable.");
            return;
        }
        try {
            PackageAdmin pa = (PackageAdmin) bundleContext.getService(ref);
            if (pa == null) {
                LOGGER.error("PackageAdmin service is unavailable.");
                return;
            }
            pa.refreshPackages(new Bundle[] { sr[0].getBundle() });
        } finally {
            bundleContext.ungetService(ref);
        }
    } catch (InvalidSyntaxException ex) {
        LOGGER.error("Problem refresh the affected shell command bundle", ex);
    }
}
Also used : PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 73 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project jackrabbit-oak by apache.

the class SegmentBlobTrackerRegistrationTest method unregisterNodeStoreService.

@Override
protected void unregisterNodeStoreService() {
    ServiceReference[] serviceReferences;
    try {
        serviceReferences = context.bundleContext().getServiceReferences(SegmentNodeStoreService.class.getName(), null);
    } catch (InvalidSyntaxException e) {
        throw new IllegalStateException("Unable to read references to SegmentNodeStoreService", e);
    }
    for (ServiceReference serviceReference : serviceReferences) {
        Object service = context.bundleContext().getService(serviceReference);
        if (service == null) {
            continue;
        }
        deactivate(service, serviceReference.getBundle().getBundleContext());
    }
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 74 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project aries by apache.

the class BundleRepositoryManagerImpl method getBundleRepositoryCollection.

public Collection<BundleRepository> getBundleRepositoryCollection(String appName, String appVersion) {
    LOGGER.debug(LOG_ENTRY, "getBundleRepositoryCollection", new Object[] { appName, appVersion });
    ServiceCollection<BundleRepository> providers = new ArrayServiceList<BundleRepository>(bc);
    String appScope = appName + "_" + appVersion;
    String filter = "(|(" + BundleRepository.REPOSITORY_SCOPE + "=" + BundleRepository.GLOBAL_SCOPE + ")(" + BundleRepository.REPOSITORY_SCOPE + "=" + appScope + "))";
    try {
        ServiceReference[] refs = bc.getServiceReferences(BundleRepository.class.getName(), filter);
        if (refs != null) {
            for (ServiceReference ref : refs) {
                providers.addService(ref);
            }
        }
    } catch (InvalidSyntaxException e) {
        LOGGER.error(LOG_EXCEPTION, e);
    }
    LOGGER.debug(LOG_EXIT, "getBundleRepositoryCollection");
    return providers;
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ArrayServiceList(org.apache.aries.application.utils.service.ArrayServiceList) BundleRepository(org.apache.aries.application.management.spi.repository.BundleRepository) ServiceReference(org.osgi.framework.ServiceReference)

Example 75 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project aries by apache.

the class DeploymentMetadataImpl method getFilters.

private Collection<Filter> getFilters(String filterString) throws InvalidAttributeException {
    Collection<Filter> filters = new ArrayList<Filter>();
    List<String> fs = ManifestProcessor.split(filterString, ",");
    if ((fs != null) && (!!!fs.isEmpty())) {
        for (String filter : fs) {
            try {
                filters.add(FrameworkUtil.createFilter(FilterUtils.removeMandatoryFilterToken(filter)));
            } catch (InvalidSyntaxException ise) {
                InvalidAttributeException iae = new InvalidAttributeException(ise);
                throw iae;
            }
        }
    }
    return filters;
}
Also used : InvalidAttributeException(org.apache.aries.application.InvalidAttributeException) Filter(org.osgi.framework.Filter) ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Aggregations

InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)124 ServiceReference (org.osgi.framework.ServiceReference)59 Filter (org.osgi.framework.Filter)29 ArrayList (java.util.ArrayList)27 IOException (java.io.IOException)23 BundleContext (org.osgi.framework.BundleContext)21 HashMap (java.util.HashMap)17 ServiceTracker (org.osgi.util.tracker.ServiceTracker)14 BundleException (org.osgi.framework.BundleException)12 Configuration (org.osgi.service.cm.Configuration)12 Map (java.util.Map)11 Dictionary (java.util.Dictionary)9 Hashtable (java.util.Hashtable)9 Test (org.junit.Test)9 List (java.util.List)8 File (java.io.File)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 URL (java.net.URL)6 LinkedHashMap (java.util.LinkedHashMap)6 FilterImpl (org.eclipse.osgi.internal.framework.FilterImpl)6