Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
 package com.sun.jersey.core.spi.scanning;
 import java.util.Map;

A scanner that recursively scans URI-based resources present in a set of package names, and sub-package names of that set.

The URIs for a package name are obtained, by default, by invoking with the parameter that is the package name with "." replaced by "/". The obtaining of the resources with a given name may be overridden by registering an implementation of PackageNamesScanner.ResourcesProvider using the method .

Each URI is then scanned using a registered com.sun.jersey.core.spi.scanning.uri.UriSchemeScanner that supports the URI scheme.

The following are registered by default. The com.sun.jersey.core.spi.scanning.uri.FileSchemeScanner for "file" URI schemes. The com.sun.jersey.core.spi.scanning.uri.JarZipSchemeScanner for "jar" or "zip" URI schemes to jar resources. The com.sun.jersey.core.spi.scanning.uri.VfsSchemeScanner for the JBoss-based "vfsfile" and "vfszip" URI schemes.

Further schemes may be registered by registering an implementation of com.sun.jersey.core.spi.scanning.uri.UriSchemeScanner in the META-INF/services file whose name is the the fully qualified class name of com.sun.jersey.core.spi.scanning.uri.UriSchemeScanner.

If a URI scheme is not supported a ScannerException will be thrown and package scanning deployment will fail.

 public class PackageNamesScanner implements Scanner {
     private final String[] packages;
     private final ClassLoader classloader;
     private final Map<StringUriSchemeScannerscanners;

Scan from a set of packages using the context class loader.

packages an array of package names.
    public PackageNamesScanner(final String[] packages) {
        this(ReflectionHelper.getContextClassLoader(), packages);

Scan from a set of packages using declared class loader.

classloader the class loader to load classes from.
packages an array of package names.
    public PackageNamesScanner(final ClassLoader classloaderfinal String[] packages) {
        this. = packages;
        this. = classloader;
        this. = new HashMap<StringUriSchemeScanner>();
        add(new JarZipSchemeScanner());
        add(new FileSchemeScanner());
        add(new VfsSchemeScanner());
        add(new BundleSchemeScanner());
        for (UriSchemeScanner s : ServiceFinder.find(UriSchemeScanner.class)) {
    private void add(final UriSchemeScanner ss) {
        for (final String s : ss.getSchemes()) {
            .put(s.toLowerCase(), ss);
    public void scan(final ScannerListener cfl) {
        for (final String p : ) {
            try {
                final Enumeration<URLurls = ResourcesProvider.getInstance().
                        getResources(p.replace('.''/'), );
                while (urls.hasMoreElements()) {
                    try {
                        scan(toURI(urls.nextElement()), cfl);
                    } catch (URISyntaxException ex) {
                        throw new ScannerException("Error when converting a URL to a URI"ex);
            } catch (IOException ex) {
                throw new ScannerException("IO error when package scanning jar"ex);

Find resources with a given name and class loader.
    public static abstract class ResourcesProvider {
        private static volatile ResourcesProvider provider;
        private static ResourcesProvider getInstance() {
            // Double-check idiom for lazy initialization
            ResourcesProvider result = ;
            if (result == null) { // first check without locking
                synchronized (ResourcesProvider.class) {
                    result = ;
                    if (result == null) { // second check with locking
                         = result = new ResourcesProvider() {
                            public Enumeration<URLgetResources(String nameClassLoader cl)
                                    throws IOException {
                                return cl.getResources(name);
            return result;
        private static void setInstance(ResourcesProvider providerthrows SecurityException {
            SecurityManager security = System.getSecurityManager();
            if (security != null) {
                ReflectPermission rp = new ReflectPermission("suppressAccessChecks");
            synchronized (ResourcesProvider.class) {
                . = provider;

Find all resources with the given name using a class loader.

cl the class loader use to find the resources
name the resource name
An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources that the class loader doesn't have access to will not be in the enumeration.
Throws: if I/O errors occur
        public abstract Enumeration<URLgetResources(String nameClassLoader clthrows IOException;

Set the PackageNamesScanner.ResourcesProvider implementation to find resources.

This method should be invoked before any package scanning is performed otherwise the functionality method will be utilized.

provider the resources provider.
java.lang.SecurityException if the resources provider cannot be set.
    public static void setResourcesProvider(ResourcesProvider providerthrows SecurityException {
    private void scan(final URI ufinal ScannerListener cfl) {
        final UriSchemeScanner ss = .get(u.getScheme().toLowerCase());
        if (ss != null) {
        } else {
            throw new ScannerException("The URI scheme " + u.getScheme() +
                    " of the URI " + u +
                    " is not supported. Package scanning deployment is not" +
                    " supported for such URIs." +
                    "\nTry using a different deployment mechanism such as" +
                    " explicitly declaring root resource and provider classes" +
                    " using an extension of");
    private URI toURI(URL urlthrows URISyntaxException {
        try {
            return url.toURI();
        } catch (URISyntaxException e) {
            // Work around bug where some URLs are incorrectly encoded.
            // This can occur when certain class loaders are utilized
            // to obtain URLs for resources.
            return URI.create(toExternalForm(url));
    private String toExternalForm(URL u) {
        // pre-compute length of StringBuffer
        int len = u.getProtocol().length() + 1;
        if (u.getAuthority() != null && u.getAuthority().length() > 0) {
            len += 2 + u.getAuthority().length();
        if (u.getPath() != null) {
            len += u.getPath().length();
        if (u.getQuery() != null) {
            len += 1 + u.getQuery().length();
        if (u.getRef() != null) {
            len += 1 + u.getRef().length();
        StringBuilder result = new StringBuilder(len);
        if (u.getAuthority() != null && u.getAuthority().length() > 0) {
        if (u.getPath() != null) {
            result.append(UriComponent.contextualEncode(u.getPath(), .));
        if (u.getQuery() != null) {
            result.append(UriComponent.contextualEncode(u.getQuery(), .));
        if (u.getRef() != null) {
        return result.toString();
New to GrepCode? Check out our FAQ X