Search in sources :

Example 76 with Filter

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

the class Storage method listEntryPaths.

/**
 * Returns the names of resources available from a list of bundle files.
 * No duplicate resource names are returned, each name is unique.
 * @param bundleFiles the list of bundle files to search in
 * @param path The path name in which to look.
 * @param filePattern The file name pattern for selecting resource names in
 *        the specified path.
 * @param options The options for listing resource names.
 * @return a list of resource names.  If no resources are found then
 * the empty list is returned.
 * @see BundleWiring#listResources(String, String, int)
 */
public static List<String> listEntryPaths(List<BundleFile> bundleFiles, String path, String filePattern, int options) {
    // Use LinkedHashSet for optimized performance of contains() plus
    // ordering guarantees.
    LinkedHashSet<String> pathList = new LinkedHashSet<>();
    Filter patternFilter = null;
    Hashtable<String, String> patternProps = null;
    if (filePattern != null) {
        // Avoid pattern matching and use BundleFile.getEntry() if recursion was not requested.
        if ((options & BundleWiring.FINDENTRIES_RECURSE) == 0 && filePattern.indexOf('*') == -1 && filePattern.indexOf('\\') == -1) {
            if (path.length() == 0)
                path = filePattern;
            else
                path += path.charAt(path.length() - 1) == '/' ? filePattern : '/' + filePattern;
            for (BundleFile bundleFile : bundleFiles) {
                if (bundleFile.getEntry(path) != null && !pathList.contains(path))
                    pathList.add(path);
            }
            return new ArrayList<>(pathList);
        }
        // For when the file pattern includes a wildcard.
        try {
            // create a file pattern filter with 'filename' as the key
            // $NON-NLS-1$ //$NON-NLS-2$
            patternFilter = FilterImpl.newInstance("(filename=" + sanitizeFilterInput(filePattern) + ")");
            // create a single hashtable to be shared during the recursive search
            patternProps = new Hashtable<>(2);
        } catch (InvalidSyntaxException e) {
            // eventPublisher.publishFrameworkEvent(FrameworkEvent.ERROR, b, e);
            return new ArrayList<>(pathList);
        }
    }
    // find the entry paths for the datas
    for (BundleFile bundleFile : bundleFiles) {
        listEntryPaths(bundleFile, path, patternFilter, patternProps, options, pathList);
    }
    return new ArrayList<>(pathList);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Filter(org.osgi.framework.Filter) ArrayList(java.util.ArrayList) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) NestedDirBundleFile(org.eclipse.osgi.storage.bundlefile.NestedDirBundleFile) ZipBundleFile(org.eclipse.osgi.storage.bundlefile.ZipBundleFile) DirBundleFile(org.eclipse.osgi.storage.bundlefile.DirBundleFile) BundleFile(org.eclipse.osgi.storage.bundlefile.BundleFile)

Example 77 with Filter

use of org.osgi.framework.Filter in project fabric8 by jboss-fuse.

the class OsgiUtils method waitForSerice.

public static void waitForSerice(Class type, String filter, long timeout) {
    BundleContext bundleContext = getBundleContext();
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        if (tracker.waitForService(timeout) == null) {
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } finally {
        tracker.close();
    }
}
Also used : ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleContext(org.osgi.framework.BundleContext)

Example 78 with Filter

use of org.osgi.framework.Filter in project eclipse-integration-commons by spring-projects.

the class DescriptorMatcher method match.

@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean match(Descriptor descriptor) {
    if (version != null && descriptor.getRequires() != null) {
        try {
            VersionRange versionRange = new VersionRange(descriptor.getRequires());
            if (!versionRange.isIncluded(version)) {
                return false;
            }
        } catch (IllegalArgumentException e) {
        // ignore
        }
    }
    if (descriptor.getFilter() != null) {
        try {
            Filter filter = FrameworkUtil.createFilter(descriptor.getFilter());
            // TODO e3.7 remove cast and use expected typesObject
            return filter.match((Dictionary) environment);
        } catch (IllegalArgumentException e) {
        // ignore
        } catch (InvalidSyntaxException e) {
        // ignore
        }
    }
    if (descriptor.isLocal()) {
        File baseDirectory = installDirectory == null ? manager.getInstallDirectory() : installDirectory;
        File directory = new File(baseDirectory, descriptor.getId() + "-" + descriptor.getVersion());
        if (!directory.exists()) {
            return false;
        }
    }
    return true;
}
Also used : Filter(org.osgi.framework.Filter) VersionRange(org.eclipse.osgi.service.resolver.VersionRange) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) File(java.io.File)

Example 79 with Filter

use of org.osgi.framework.Filter in project acs-aem-commons by Adobe-Consulting-Services.

the class CustomPollingImporterListServlet method activate.

@Activate
protected void activate(ComponentContext ctx) throws InvalidSyntaxException {
    final BundleContext bundleContext = ctx.getBundleContext();
    StringBuilder builder = new StringBuilder();
    builder.append("(&(");
    builder.append(Constants.OBJECTCLASS).append("=").append(Importer.SERVICE_NAME).append(")");
    builder.append("(displayName=*))");
    Filter filter = bundleContext.createFilter(builder.toString());
    this.tracker = new ServiceTracker(bundleContext, filter, null);
    this.tracker.open();
}
Also used : Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) BundleContext(org.osgi.framework.BundleContext) Activate(org.apache.felix.scr.annotations.Activate)

Example 80 with Filter

use of org.osgi.framework.Filter in project ecf by eclipse.

the class EventHandlerWrapper method handleEvent.

/**
 * Dispatch event to handler. Perform final tests before actually calling
 * the handler.
 *
 * @param event
 *            The event to dispatch
 * @param perm
 *            The permission to be checked
 */
public void handleEvent(Event event, Permission perm) {
    Bundle bundle = reference.getBundle();
    // is service unregistered?
    if (bundle == null) {
        return;
    }
    // filter match
    Filter eventFilter = getFilter();
    if ((eventFilter != null) && !event.matches(eventFilter)) {
        return;
    }
    // permission check
    if ((perm != null) && (!bundle.hasPermission(perm))) {
        return;
    }
    // get handler service
    EventHandler handlerService = getHandler();
    if (handlerService == null) {
        return;
    }
    try {
        handlerService.handleEvent(event);
    } catch (Throwable t) {
        // log/handle any Throwable thrown by the listener
        log.log(LogService.LOG_ERROR, "Exception while dispatching event " + event + " to handler " + handlerService, t);
    }
}
Also used : Filter(org.osgi.framework.Filter) Bundle(org.osgi.framework.Bundle) EventHandler(org.osgi.service.event.EventHandler)

Aggregations

Filter (org.osgi.framework.Filter)167 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)63 ServiceTracker (org.osgi.util.tracker.ServiceTracker)42 ArrayList (java.util.ArrayList)41 ServiceReference (org.osgi.framework.ServiceReference)36 BundleContext (org.osgi.framework.BundleContext)27 Hashtable (java.util.Hashtable)25 List (java.util.List)23 Bundle (org.osgi.framework.Bundle)21 Dictionary (java.util.Dictionary)20 HashMap (java.util.HashMap)17 Test (org.junit.Test)17 Map (java.util.Map)15 IOException (java.io.IOException)9 Iterator (java.util.Iterator)9 Properties (java.util.Properties)8 SharePolicy (org.apache.aries.subsystem.scope.SharePolicy)7 Configuration (org.osgi.service.cm.Configuration)7 URL (java.net.URL)6 Collection (java.util.Collection)6