Skip to content

Commit

Permalink
Avoiding costly new URL() for performance reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 5, 2015
1 parent cee9e53 commit 51cb67f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
Expand Down Expand Up @@ -39,7 +37,7 @@ public ModularFileSystemURLHandler(ResourceLoader resourceLoader)
this.resourceLoader = resourceLoader;
}

public void handle(Collection<String> paths, List<String> discoveredClasses, List<URL> discoveredBeansXmlUrls)
public void handle(Collection<String> paths, List<String> discoveredClasses, List<String> discoveredBeansXmlUrls)
{
for (String urlPath : paths)
{
Expand Down Expand Up @@ -73,24 +71,24 @@ public void handle(Collection<String> paths, List<String> discoveredClasses, Lis
}
}

private void handleArchiveByFile(File file, List<String> discoveredClasses, List<URL> discoveredBeansXmlUrls)
private void handleArchiveByFile(File file, List<String> discoveredClasses, List<String> discoveredBeansXmlUrls)
throws IOException
{
try
{
log.log(Level.FINEST, "Archive: " + file);

String archiveUrl = "jar:" + file.toURI().toURL().toExternalForm() + "!/";
ZipFile zip = new ZipFile(file);
Enumeration<? extends ZipEntry> entries = zip.entries();

while (entries.hasMoreElements())
try (ZipFile zip = new ZipFile(file))
{
ZipEntry entry = entries.nextElement();
String name = entry.getName();
handle(name, new URL(archiveUrl + name), discoveredClasses, discoveredBeansXmlUrls);
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = entries.nextElement();
String name = entry.getName();
handle(name, archiveUrl + name, discoveredClasses, discoveredBeansXmlUrls);
}
}
zip.close();
}
catch (ZipException e)
{
Expand All @@ -99,13 +97,13 @@ private void handleArchiveByFile(File file, List<String> discoveredClasses, List
}

protected void handleDirectory(File file, String path, List<String> discoveredClasses,
List<URL> discoveredBeansXmlUrls)
List<String> discoveredBeansXmlUrls)
{
handleDirectory(file, path, new File[0], discoveredClasses, discoveredBeansXmlUrls);
}

private void handleDirectory(File file, String path, File[] excludedDirectories, List<String> discoveredClasses,
List<URL> discoveredBeansXmlUrls)
List<String> discoveredBeansXmlUrls)
{
for (File excludedDirectory : excludedDirectories)
{
Expand All @@ -129,19 +127,12 @@ private void handleDirectory(File file, String path, File[] excludedDirectories,
}
else
{
try
{
handle(newPath, child.toURI().toURL(), discoveredClasses, discoveredBeansXmlUrls);
}
catch (MalformedURLException e)
{
log.log(Level.SEVERE, "Error loading file: " + newPath, e);
}
handle(newPath, child.getAbsolutePath(), discoveredClasses, discoveredBeansXmlUrls);
}
}
}

protected void handle(String name, URL url, List<String> discoveredClasses, List<URL> discoveredBeansXmlUrls)
protected void handle(String name, String url, List<String> discoveredClasses, List<String> discoveredBeansXmlUrls)
{
if (name.endsWith(".class"))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ModularURLScanner(ResourceLoader resourceLoader, String... resources)
public ModuleScanResult scan()
{
List<String> discoveredClasses = new ArrayList<>();
List<URL> discoveredResourceUrls = new ArrayList<>();
List<String> discoveredResourceUrls = new ArrayList<>();
Collection<String> paths = new ArrayList<>();
for (String resourceName : resources)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
*/
package org.jboss.forge.furnace.container.cdi.weld;

import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.jboss.weld.resources.spi.ResourceLoader;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class ModuleScanResult
{
private final ResourceLoader loader;
private final List<URL> resourceUrls;
private final List<String> resourceUrls;
private final Collection<String> classes;

public ModuleScanResult(ResourceLoader loader, List<URL> discoveredResourceUrls, Collection<String> discoveredClasses)
public ModuleScanResult(ResourceLoader loader, List<String> discoveredResourceUrls,
Collection<String> discoveredClasses)
{
this.loader = loader;
this.resourceUrls = discoveredResourceUrls;
Expand All @@ -34,7 +34,7 @@ public Collection<String> getDiscoveredClasses()
return classes;
}

public List<URL> getDiscoveredResourceUrls()
public List<String> getDiscoveredResourceUrls()
{
return resourceUrls;
}
Expand All @@ -50,10 +50,10 @@ public String toString()
StringBuilder result = new StringBuilder();
if (resourceUrls != null)
{
Iterator<URL> iterator = resourceUrls.iterator();
Iterator<String> iterator = resourceUrls.iterator();
while (iterator.hasNext())
{
String url = iterator.next().toString();
String url = iterator.next();
result.append(url).append("\n");
}
}
Expand Down

0 comments on commit 51cb67f

Please sign in to comment.