Search in sources :

Example 56 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project bnd by bndtools.

the class OSGiTestCase method assertSvcAvail.

/**
	 * Asserts that at least one service of the specified type is currently
	 * available. If not, an {@link AssertionFailedError} is thrown with the
	 * given message.
	 * 
	 * @param message
	 * @param service The service interface type.
	 * @param filter An additional service filter, which may be {@code
		 * null}.
	 */
protected void assertSvcAvail(String message, Class<?> service, String filter) {
    BundleContext context = getBundleContext();
    ServiceReference[] refs = null;
    try {
        refs = context.getServiceReferences(service.getName(), filter);
    } catch (InvalidSyntaxException e) {
        fail("Invalid filter syntax");
    }
    if (refs == null || refs.length == 0) {
        fail(message);
        return;
    }
    Object svcObj = context.getService(refs[0]);
    if (svcObj == null)
        fail(message);
    try {
        if (!service.isInstance(svcObj))
            fail(message);
    } finally {
        context.ungetService(refs[0]);
    }
}
Also used : InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 57 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project bnd by bndtools.

the class AnalyzerTracker method addingService.

@Override
public TrackingStruct addingService(ServiceReference<ResourceAnalyzer> reference) {
    TrackingStruct struct = new TrackingStruct();
    try {
        String filterStr = (String) reference.getProperty(ResourceAnalyzer.FILTER);
        Filter filter = (filterStr != null) ? FrameworkUtil.createFilter(filterStr) : null;
        ResourceAnalyzer analyzer = context.getService(reference);
        if (analyzer == null)
            return null;
        struct = new TrackingStruct();
        struct.analyzer = analyzer;
        struct.filter = filter;
        struct.valid = true;
        indexer.addAnalyzer(analyzer, filter);
    } catch (InvalidSyntaxException e) {
        struct.valid = false;
        log.log(reference, LogService.LOG_ERROR, "Ignoring ResourceAnalyzer due to invalid filter expression", e);
    }
    return struct;
}
Also used : ResourceAnalyzer(org.osgi.service.indexer.ResourceAnalyzer) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrackingStruct(org.osgi.service.indexer.osgi.AnalyzerTracker.TrackingStruct)

Example 58 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project bnd by bndtools.

the class AnalyzerTracker method modifiedService.

@Override
public void modifiedService(ServiceReference<ResourceAnalyzer> reference, TrackingStruct struct) {
    if (struct.valid) {
        indexer.removeAnalyzer(struct.analyzer, struct.filter);
    }
    try {
        String filterStr = (String) reference.getProperty(ResourceAnalyzer.FILTER);
        Filter filter = (filterStr != null) ? FrameworkUtil.createFilter(filterStr) : null;
        struct = new TrackingStruct();
        struct.filter = filter;
        struct.valid = true;
        indexer.addAnalyzer(struct.analyzer, filter);
    } catch (InvalidSyntaxException e) {
        struct.valid = false;
        log.log(reference, LogService.LOG_ERROR, "Ignoring ResourceAnalyzer due to invalid filter expression", e);
    }
}
Also used : Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) TrackingStruct(org.osgi.service.indexer.osgi.AnalyzerTracker.TrackingStruct)

Example 59 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project rt.equinox.framework by eclipse.

the class LocationAreaSessionTest method doLock.

static void doLock(String testLocationDir, boolean release, boolean succeed) {
    if (testLocationDir == null)
        fail("The testLocationDir is not set");
    ServiceReference[] refs = null;
    try {
        refs = OSGiTestsActivator.getContext().getServiceReferences(Location.class.getName(), "(type=osgi.configuration.area)");
    } catch (InvalidSyntaxException e) {
        fail("failed to create filter", e);
    }
    // this is test code so we are not very careful; just assume there is at lease one service.  Do not copy and paste this code!!!
    Location configLocation = (Location) OSGiTestsActivator.getContext().getService(refs[0]);
    Location testLocation = null;
    try {
        testLocation = configLocation.createLocation(null, new File(testLocationDir).toURL(), false);
        testLocation.setURL(testLocation.getDefault(), false);
        // try locking location
        if (succeed ? testLocation.isLocked() : !testLocation.isLocked())
            fail("location should " + (succeed ? "not " : "") + "be locked");
        if (succeed ? !testLocation.lock() : testLocation.lock())
            fail((succeed ? "Could not" : "Could") + " lock location");
        if (!testLocation.isLocked())
            fail("location should be locked");
    } catch (MalformedURLException e) {
        fail("failed to create the location URL", e);
    } catch (IOException e) {
        fail("failed to lock with IOExcetpion", e);
    } finally {
        if (release && testLocation != null)
            testLocation.release();
        if (!release)
            lockedTestLocation = testLocation;
        OSGiTestsActivator.getContext().ungetService(refs[0]);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) File(java.io.File) ServiceReference(org.osgi.framework.ServiceReference) Location(org.eclipse.osgi.service.datalocation.Location)

Example 60 with InvalidSyntaxException

use of org.osgi.framework.InvalidSyntaxException in project rt.equinox.framework by eclipse.

the class NativeCodeFinder method getNativePaths.

private List<String> getNativePaths() {
    ModuleRevision revision = generation.getRevision();
    ModuleWiring wiring = revision.getWiring();
    if (wiring == null) {
        // unresolved?  should not be possible
        return Collections.emptyList();
    }
    if ((revision.getTypes() & BundleRevision.TYPE_FRAGMENT) != 0) {
        List<ModuleWire> hosts = wiring.getRequiredModuleWires(HostNamespace.HOST_NAMESPACE);
        if (hosts == null) {
            // unresolved or invalid?  should not be possible
            return Collections.emptyList();
        }
        if (!hosts.isEmpty()) {
            // just use the first host wiring
            wiring = hosts.get(0).getProviderWiring();
        }
    }
    List<ModuleWire> nativeCode = wiring.getRequiredModuleWires(NativeNamespace.NATIVE_NAMESPACE);
    if (nativeCode.isEmpty()) {
        return Collections.emptyList();
    }
    // just taking the first paths for the revision, we sorted correctly when transforming to the requirement
    for (ModuleWire moduleWire : nativeCode) {
        if (moduleWire.getRequirement().getRevision().equals(revision)) {
            @SuppressWarnings("unchecked") List<String> result = (List<String>) nativeCode.get(0).getRequirement().getAttributes().get(REQUIREMENT_NATIVE_PATHS_ATTRIBUTE);
            if (result != null)
                return result;
            // this must be a multi-clause Bundle-NativeCode header, need to check for the correct one in the index
            try {
                FilterImpl filter = FilterImpl.newInstance(moduleWire.getRequirement().getDirectives().get(NativeNamespace.REQUIREMENT_FILTER_DIRECTIVE));
                int index = -1;
                Map<String, Object> capabilityAttrs = moduleWire.getCapability().getAttributes();
                for (FilterImpl child : filter.getChildren()) {
                    index++;
                    if (child.matches(capabilityAttrs)) {
                        break;
                    }
                }
                if (index != -1) {
                    @SuppressWarnings("unchecked") List<String> indexResult = (List<String>) nativeCode.get(0).getRequirement().getAttributes().get(REQUIREMENT_NATIVE_PATHS_ATTRIBUTE + '.' + index);
                    if (indexResult != null)
                        return indexResult;
                }
            } catch (InvalidSyntaxException e) {
                throw new RuntimeException(e);
            }
        }
    }
    return Collections.emptyList();
}
Also used : FilterImpl(org.eclipse.osgi.internal.framework.FilterImpl) 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