Skip to content

Commit

Permalink
TRUNK-2694 Fix Performance hog: Maps and sets of URLs
Browse files Browse the repository at this point in the history
(cherry picked from commit 358de98)

manually added missing imports
  • Loading branch information
rkorytkowski authored and djazayeri committed Jan 3, 2014
1 parent ea92dd0 commit 72bbfe0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
25 changes: 18 additions & 7 deletions api/src/main/java/org/openmrs/module/ModuleClassLoader.java
Expand Up @@ -21,6 +21,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLStreamHandlerFactory;
Expand Down Expand Up @@ -61,7 +63,7 @@ public class ModuleClassLoader extends URLClassLoader {

private Module[] awareOfModules;

private Map<URL, File> libraryCache;
private Map<URI, File> libraryCache;

private boolean probeParentLoaderLast = true;

Expand All @@ -86,7 +88,7 @@ protected ModuleClassLoader(final Module module, final List<URL> urls, final Cla
collectRequiredModuleImports();
collectAwareOfModuleImports();
collectFilters();
libraryCache = new WeakHashMap<URL, File>();
libraryCache = new WeakHashMap<URI, File>();
}

/**
Expand Down Expand Up @@ -364,7 +366,7 @@ protected void modulesSetChanged() {
// repopulate resource URLs
//resourceLoader = ModuleResourceLoader.get(getModule());
collectFilters();
for (Iterator<Map.Entry<URL, File>> it = libraryCache.entrySet().iterator(); it.hasNext();) {
for (Iterator<Map.Entry<URI, File>> it = libraryCache.entrySet().iterator(); it.hasNext();) {
if (it.next().getValue() == null) {
it.remove();
}
Expand Down Expand Up @@ -688,8 +690,17 @@ protected String findLibrary(final String name) {
*/
protected File cacheLibrary(final URL libUrl, final String libname) {
File cacheFolder = OpenmrsClassLoader.getLibCacheFolder();
if (libraryCache.containsKey(libUrl)) {
return libraryCache.get(libUrl);

URI libUri;
try {
libUri = libUrl.toURI();
}
catch (URISyntaxException e) {
throw new IllegalArgumentException(libUrl.getPath() + " is not a valid URI", e);
}

if (libraryCache.containsKey(libUri)) {
return libraryCache.get(libUri);
}

File result = null;
Expand Down Expand Up @@ -736,7 +747,7 @@ protected File cacheLibrary(final URL libUrl, final String libname) {
}

// save a link to the cached file
libraryCache.put(libUrl, result);
libraryCache.put(libUri, result);

if (log.isDebugEnabled()) {
log.debug("library " + libname + " successfully cached from URL " + libUrl + " and saved to local file "
Expand All @@ -746,7 +757,7 @@ protected File cacheLibrary(final URL libUrl, final String libname) {
}
catch (IOException ioe) {
log.error("can't cache library " + libname + " from URL " + libUrl, ioe);
libraryCache.put(libUrl, null);
libraryCache.put(libUri, null);
result = null;
}

Expand Down
56 changes: 48 additions & 8 deletions api/src/main/java/org/openmrs/util/OpenmrsClassLoader.java
Expand Up @@ -19,15 +19,19 @@
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
Expand Down Expand Up @@ -202,21 +206,41 @@ public URL findResource(final String name) {
*/
@Override
public Enumeration<URL> findResources(final String name) throws IOException {
Set<URL> results = new HashSet<URL>();
Set<URI> results = new HashSet<URI>();
for (ModuleClassLoader classLoader : ModuleFactory.getModuleClassLoaders()) {
Enumeration<URL> urls = classLoader.findResources(name);
while (urls.hasMoreElements()) {
URL result = urls.nextElement();
if (result != null)
results.add(result);
try {
results.add(result.toURI());
}
catch (URISyntaxException e) {
throwInvalidURI(result, e);
}
}
}

for (Enumeration<URL> en = super.findResources(name); en.hasMoreElements();) {
results.add(en.nextElement());
URL url = en.nextElement();
try {
results.add(url.toURI());
}
catch (URISyntaxException e) {
throwInvalidURI(url, e);
}
}

List<URL> resources = new ArrayList<URL>(results.size());
for (URI result : results) {
resources.add(result.toURL());
}

return Collections.enumeration(results);
return Collections.enumeration(resources);
}

private void throwInvalidURI(URL url, Exception e) throws IOException {
throw new IOException(url.getPath() + " is not a valid URI", e);
}

/**
Expand All @@ -242,21 +266,37 @@ public InputStream getResourceAsStream(String file) {
*/
@Override
public Enumeration<URL> getResources(String packageName) throws IOException {
Set<URL> results = new HashSet<URL>();
Set<URI> results = new HashSet<URI>();
for (ModuleClassLoader classLoader : ModuleFactory.getModuleClassLoaders()) {
Enumeration<URL> urls = classLoader.getResources(packageName);
while (urls.hasMoreElements()) {
URL result = urls.nextElement();
if (result != null)
results.add(result);
try {
results.add(result.toURI());
}
catch (URISyntaxException e) {
throwInvalidURI(result, e);
}
}
}

for (Enumeration<URL> en = super.getResources(packageName); en.hasMoreElements();) {
results.add(en.nextElement());
URL url = en.nextElement();
try {
results.add(url.toURI());
}
catch (URISyntaxException e) {
throwInvalidURI(url, e);
}
}

List<URL> resources = new ArrayList<URL>(results.size());
for (URI result : results) {
resources.add(result.toURL());
}

return Collections.enumeration(results);
return Collections.enumeration(resources);
}

/**
Expand Down

0 comments on commit 72bbfe0

Please sign in to comment.