Java Resource File getBundlesContainingResource(BundleContext bundleContext, String resourcePattern)

Here you can find the source of getBundlesContainingResource(BundleContext bundleContext, String resourcePattern)

Description

Returns all bundles that contain a class

License

Open Source License

Parameter

Parameter Description
bundleContext Bundle context for interaction with the OSGi framework
resourcePattern Search pattern in the form <package>/<class name>. The pattern can contain wildcards

Return

Map between the bundle (key) and the URL(s) of the resources (value)

Declaration

public static Map<Bundle, List<String>> getBundlesContainingResource(BundleContext bundleContext,
        String resourcePattern) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 SAP AG/* www  . j a  v a2  s.c om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Hristo Iliev, SAP AG - initial contribution
 *******************************************************************************/

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import java.net.URL;
import java.util.*;

public class Main {
    /**
     * Returns all bundles that contain a class
     *
     * @param bundleContext   Bundle context for interaction with the OSGi framework
     * @param resourcePattern Search pattern in the form &lt;package&gt;/&lt;class name&gt;. The pattern can contain wildcards
     * @return Map between the bundle (key) and the URL(s) of the resources (value)
     */
    public static Map<Bundle, List<String>> getBundlesContainingResource(BundleContext bundleContext,
            String resourcePattern) {
        Map<Bundle, List<String>> result = new HashMap<Bundle, List<String>>();

        Bundle[] bundles = bundleContext.getBundles();
        for (Bundle bundle : bundles) {
            List<String> entries = findEntries(bundle, resourcePattern);
            if (entries != null && entries.size() != 0) {
                result.put(bundle, entries);
            }
        }

        return result;
    }

    /**
     * Returns a list with bundle entries matching a resource pattern
     *
     * @param bundle Bundle to scan for entries
     * @param resourcePattern Pattern used for matching
     * @return List with found entries
     */
    private static List<String> findEntries(Bundle bundle, String resourcePattern) {
        HashSet<String> urls = new HashSet<String>();

        int index = resourcePattern.lastIndexOf("/");
        if (index != -1) {
            String resourcePath = resourcePattern.substring(0, index);
            String resourceEntity = resourcePattern.substring(index + 1);
            // Search the whole bundle for entity starting from the root. We need this since "the pattern is only
            // matched against the last element of the entry path" as stated in findEntries JavaDoc. This means that
            // web bundle that packages a class in WEB-INF/classes will not be found by findEntries since the path is
            // prepended with WEB-INF/classes. Therefore we search for a class everywhere in the bundle and then
            // filter the result.
            addURLs(urls, bundle.findEntries("/", resourceEntity, true), resourcePath);
        }

        // Search the root of the bundle for entity matching the specified pattern
        addURLs(urls, bundle.findEntries("/", resourcePattern, true), null);
        return new ArrayList<String>(urls);
    }

    /**
     * Adds all found resources eliminating the duplicates or the ones that do not contain the requested path
     *
     * @param urls      Result set with URLs as string
     * @param foundURLs Enumeration to scan
     * @param path      Expected path of the entities. The entities are not put in the result set unless they contain
     *                  this path.
     */
    private static void addURLs(HashSet<String> urls, Enumeration<URL> foundURLs, String path) {
        if (foundURLs != null) {
            while (foundURLs.hasMoreElements()) {
                String url = foundURLs.nextElement().getFile();
                if (path != null && !url.contains(path)) {
                    continue;
                }
                urls.add(url);
            }
        }
    }
}

Related

  1. copyDecompressed(String resource, File output)
  2. copyResource(String name)
  3. expendResource(String fileName)
  4. getAllResources(Class clazz, String resource)
  5. getBaseLineFolder(String resourceFile)
  6. getBytes(Class contextClass, String resourceName)
  7. getClassResource(Class clazz)
  8. getContentAsString(String resource)
  9. getDefaultWebXmlResourceLocation()