Search in sources :

Example 81 with InvalidSyntaxException

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

the class RepositoryServiceRepository method findProviders.

@SuppressWarnings("unchecked")
public Collection<Capability> findProviders(Requirement requirement) {
    Set<Capability> result = new HashSet<Capability>();
    ServiceReference<?>[] references;
    try {
        references = context.getAllServiceReferences("org.osgi.service.repository.Repository", null);
        if (references == null)
            return result;
    } catch (InvalidSyntaxException e) {
        throw new IllegalStateException(e);
    }
    for (ServiceReference<?> reference : references) {
        Object repository = context.getService(reference);
        if (repository == null)
            continue;
        try {
            // Reflection is used here to allow the service to work with a mixture of
            // Repository services implementing different versions of the API.
            Class<?> clazz = repository.getClass();
            Class<?> repoInterface = null;
            while (clazz != null && repoInterface == null) {
                for (Class<?> intf : clazz.getInterfaces()) {
                    if (Repository.class.getName().equals(intf.getName())) {
                        // Compare interfaces by name so that we can work with different versions of the
                        // interface.
                        repoInterface = intf;
                        break;
                    }
                }
                clazz = clazz.getSuperclass();
            }
            if (repoInterface == null) {
                continue;
            }
            Map<Requirement, Collection<Capability>> map;
            try {
                Method method = repoInterface.getMethod("findProviders", Collection.class);
                map = (Map<Requirement, Collection<Capability>>) method.invoke(repository, Collections.singleton(requirement));
            } catch (Exception e) {
                throw new SubsystemException(e);
            }
            Collection<Capability> capabilities = map.get(requirement);
            if (capabilities == null)
                continue;
            result.addAll(capabilities);
        } finally {
            context.ungetService(reference);
        }
    }
    return result;
}
Also used : Capability(org.osgi.resource.Capability) SubsystemException(org.osgi.service.subsystem.SubsystemException) Method(java.lang.reflect.Method) SubsystemException(org.osgi.service.subsystem.SubsystemException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) HashSet(java.util.HashSet)

Example 82 with InvalidSyntaxException

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

the class SubsystemResource method getRegion.

public synchronized Region getRegion() throws BundleException, IOException, InvalidSyntaxException, URISyntaxException {
    if (region == null) {
        region = createRegion(getId());
        Coordination coordination = Activator.getInstance().getCoordinator().peek();
        coordination.addParticipant(new Participant() {

            @Override
            public void ended(Coordination arg0) throws Exception {
            // Nothing.
            }

            @Override
            public void failed(Coordination arg0) throws Exception {
                if (isScoped())
                    region.getRegionDigraph().removeRegion(region);
            }
        });
        if (!isApplication()) {
            setImportIsolationPolicy();
        }
    }
    return region;
}
Also used : Coordination(org.osgi.service.coordinator.Coordination) Participant(org.osgi.service.coordinator.Participant) URISyntaxException(java.net.URISyntaxException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ResolutionException(org.osgi.service.resolver.ResolutionException) SubsystemException(org.osgi.service.subsystem.SubsystemException) IOException(java.io.IOException)

Example 83 with InvalidSyntaxException

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

the class RichBundleContext method getService.

public <T> T getService(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(delegate, osgiFilter, null);
        tracker.open();
        Object svc = type.cast(tracker.waitForService(timeout));
        if (svc == null) {
            System.out.println("Could not obtain a service in time, service-ref=" + tracker.getServiceReference() + ", time=" + System.currentTimeMillis());
            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 : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 84 with InvalidSyntaxException

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

the class AbstractJPAManagedServiceFactory method getConfigurationDrivenResource.

@Override
protected LifecycleAware getConfigurationDrivenResource(BundleContext context, String pid, Map<String, Object> properties) throws Exception {
    Properties jdbcProps = getJdbcProps(pid, properties);
    Map<String, Object> jpaProps = getJPAProps(pid, properties);
    try {
        LifecycleAware worker;
        if (properties.containsKey(OSGI_JDBC_DRIVER_CLASS) || properties.containsKey(DSF_TARGET_FILTER)) {
            worker = dataSourceTracking(context, pid, properties, jdbcProps, jpaProps);
        } else {
            if (!jdbcProps.isEmpty()) {
                LOG.warn("The configuration {} contains raw JDBC configuration, but no osgi.jdbc.driver.class or aries.dsf.target.filter properties. No DataSourceFactory will be used byt this bundle, so the JPA provider must be able to directly create the datasource, and these configuration properties will likely be ignored. {}", pid, jdbcProps.stringPropertyNames());
            }
            worker = emfTracking(context, pid, properties, jpaProps);
        }
        return worker;
    } catch (InvalidSyntaxException e) {
        LOG.error("The configuration {} contained an invalid target filter {}", pid, e.getFilter());
        throw new ConfigurationException(DSF_TARGET_FILTER, "The target filter was invalid", e);
    }
}
Also used : LifecycleAware(org.apache.aries.tx.control.resource.common.impl.LifecycleAware) ConfigurationException(org.osgi.service.cm.ConfigurationException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) Properties(java.util.Properties)

Example 85 with InvalidSyntaxException

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

the class SingleServiceTracker method findMatchingReference.

private void findMatchingReference(ServiceReference original) {
    boolean clear = true;
    ServiceReference ref;
    if (isCustomFilter) {
        try {
            ServiceReference[] refs = ctx.getServiceReferences(className, filterString);
            if (refs == null || refs.length == 0) {
                ref = null;
            } else {
                ref = refs[0];
            }
        } catch (InvalidSyntaxException e) {
            //This can't happen, we'd have blown up in the constructor
            ref = null;
        }
    } else {
        ref = ctx.getServiceReference(className);
    }
    if (ref != null) {
        T service = (T) ctx.getService(ref);
        if (service != null) {
            clear = false;
            // We do the unget out of the lock so we don't exit this class while holding a lock.
            if (!!!update(original, ref, service)) {
                ctx.ungetService(ref);
            }
        }
    } else if (original == null) {
        clear = false;
    }
    if (clear) {
        update(original, null, null);
    }
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

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