### Eclipse Workspace Patch 1.0 #P weld-se Index: src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java =================================================================== --- src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java (revision 5961) +++ src/main/java/org/jboss/weld/environment/se/discovery/URLScanner.java (working copy) @@ -28,10 +28,13 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipException; import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sun.misc.CompoundEnumeration; + /** * Implementation of {@link Scanner} which can scan a {@link URLClassLoader} * @@ -41,159 +44,167 @@ * @author Pete Muir * */ -public class URLScanner extends AbstractScanner -{ - private static final Logger log = LoggerFactory.getLogger(URLScanner.class); +public class URLScanner extends AbstractScanner { + private static final Logger log = LoggerFactory.getLogger(URLScanner.class); + + public URLScanner(ClassLoader classLoader, SEWeldDiscovery webBeanDiscovery) { + super(classLoader, webBeanDiscovery); + } + + public void scanDirectories(File[] directories) { + for (File directory : directories) { + handleDirectory(directory, null); + } + } + + public void scanResources(String[] resources) { + Set paths = new HashSet(); + + for (String resourceName : resources) { + try { + Enumeration urlEnum = getClassLoader().getResources(resourceName); + while (urlEnum.hasMoreElements()) { + + URL url = urlEnum.nextElement(); + + String urlPath = url.getFile(); + System.out.println(urlPath); + urlPath = URLDecoder.decode(urlPath, "UTF-8"); + + if (urlPath.startsWith("lib") || urlPath.startsWith("main")) { + urlPath = urlPath.substring(0, urlPath.indexOf('/', 6)); + + CompoundEnumeration ce = (CompoundEnumeration) getClassLoader().getResources(urlPath); + while (ce.hasMoreElements()) { + URL entry = (URL) ce.nextElement(); + System.out.println(entry); - public URLScanner(ClassLoader classLoader, SEWeldDiscovery webBeanDiscovery) - { - super(classLoader, webBeanDiscovery); - } + String oneJarPath = entry.getFile().substring(entry.getFile().indexOf('/'), entry.getFile().indexOf('!')); - public void scanDirectories(File[] directories) - { - for (File directory : directories) - { - handleDirectory(directory, null); - } - } + File file = new File(oneJarPath); - public void scanResources(String[] resources) - { - Set paths = new HashSet(); + ZipFile zip = new ZipFile(file); + Enumeration entries = zip.entries(); - for (String resourceName : resources) - { - try - { - Enumeration urlEnum = getClassLoader().getResources(resourceName); + while (entries.hasMoreElements()) { + ZipEntry zipEntry = entries.nextElement(); - while (urlEnum.hasMoreElements()) - { - String urlPath = urlEnum.nextElement().getFile(); - urlPath = URLDecoder.decode(urlPath, "UTF-8"); + String name = zipEntry.getName(); + + System.out.println(name); + if (name.equals(urlPath)) { + if (urlPath.startsWith("lib") || urlPath.startsWith("main")) { + ZipEntry ze = null; + ZipInputStream zin = new ZipInputStream(zip.getInputStream(zipEntry)); + while ((ze = zin.getNextEntry()) != null) { + System.out.println("handling " + ze.getName()); + handle(ze.getName(), getClassLoader().getResource(ze.getName())); + zin.closeEntry(); + } + zin.close(); + } + } + } + } + } else { + if (urlPath.startsWith("file:")) { + urlPath = urlPath.substring(5); + } - if (urlPath.startsWith("file:")) - { - urlPath = urlPath.substring(5); - } + if (urlPath.indexOf('!') > 0) { + urlPath = urlPath.substring(0, urlPath.indexOf('!')); + } else { + File dirOrArchive = new File(urlPath); - if (urlPath.indexOf('!') > 0) - { - urlPath = urlPath.substring(0, urlPath.indexOf('!')); - } - else - { - File dirOrArchive = new File(urlPath); + if ((resourceName != null) && (resourceName.lastIndexOf('/') > 0)) { + // for META-INF/components.xml + dirOrArchive = dirOrArchive.getParentFile(); + } - if ((resourceName != null) && (resourceName.lastIndexOf('/') > 0)) - { - // for META-INF/components.xml - dirOrArchive = dirOrArchive.getParentFile(); - } + urlPath = dirOrArchive.getParent(); + } - urlPath = dirOrArchive.getParent(); - } + paths.add(urlPath); + + } - paths.add(urlPath); - } - } - catch (IOException ioe) - { - log.warn("could not read: " + resourceName, ioe); - } - } + } + } catch (IOException ioe) { + log.warn("could not read: " + resourceName, ioe); + } + } - handle(paths); - } + handle(paths); + } - protected void handle(Set paths) - { - for (String urlPath : paths) - { - try - { - log.trace("scanning: " + urlPath); + protected void handle(Set paths) { + for (String urlPath : paths) { + try { + System.out.println("scanning: " + urlPath); + log.trace("scanning: " + urlPath); - File file = new File(urlPath); + File file = new File(urlPath); - if (file.isDirectory()) - { - handleDirectory(file, null); - } - else - { - handleArchiveByFile(file); - } - } - catch (IOException ioe) - { - log.warn("could not read entries", ioe); - } - } - } + System.out.println("dir = " + file.isDirectory()); + System.out.println("file = " + file.isFile()); - private void handleArchiveByFile(File file) throws IOException - { - try - { - log.trace("archive: " + file); + if (!file.isFile()) { + handleDirectory(file, null); + } else { + handleArchiveByFile(file); + } + } catch (IOException ioe) { + log.warn("could not read entries", ioe); + } + } + } - ZipFile zip = new ZipFile(file); - Enumeration entries = zip.entries(); + private void handleArchiveByFile(File file) throws IOException { + try { + System.out.println("archive: " + file); + log.trace("archive: " + file); - while (entries.hasMoreElements()) - { - ZipEntry entry = entries.nextElement(); - String name = entry.getName(); - handle(name, getClassLoader().getResource(name)); - } - } - catch (ZipException e) - { - throw new RuntimeException("Error handling file " + file, e); - } - } + ZipFile zip = new ZipFile(file); + Enumeration entries = zip.entries(); - private void handleDirectory(File file, String path) - { - handleDirectory(file, path, new File[0]); - } + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + String name = entry.getName(); + handle(name, getClassLoader().getResource(name)); + } + } catch (ZipException e) { + throw new RuntimeException("Error handling file " + file, e); + } + } - private void handleDirectory(File file, String path, File[] excludedDirectories) - { - for (File excludedDirectory : excludedDirectories) - { - if (file.equals(excludedDirectory)) - { - log.trace("skipping excluded directory: " + file); + private void handleDirectory(File file, String path) { + handleDirectory(file, path, new File[0]); + } - return; - } - } + private void handleDirectory(File file, String path, File[] excludedDirectories) { + for (File excludedDirectory : excludedDirectories) { + if (file.equals(excludedDirectory)) { + log.trace("skipping excluded directory: " + file); - log.trace("handling directory: " + file); + return; + } + } - for (File child : file.listFiles()) - { - String newPath = (path == null) ? child.getName() : (path + '/' + child.getName()); + log.trace("handling directory: " + file); - if (child.isDirectory()) - { - handleDirectory(child, newPath, excludedDirectories); - } - else - { - try - { - handle(newPath, child.toURI().toURL()); - } - catch (MalformedURLException e) - { - log.error("Error loading file " + newPath); - } - } - } - } + for (File child : file.listFiles()) { + String newPath = (path == null) ? child.getName() : (path + '/' + child.getName()); + + if (child.isDirectory()) { + handleDirectory(child, newPath, excludedDirectories); + } else { + try { + handle(newPath, child.toURI().toURL()); + } catch (MalformedURLException e) { + log.error("Error loading file " + newPath); + } + } + } + } }