-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de55343
commit d35c2f9
Showing
1 changed file
with
31 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package org.jboss.forge.furnace.container.cdi.impl; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.LinkedHashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.logging.Logger; | ||
|
@@ -24,7 +28,7 @@ | |
|
||
public class ServiceRegistryImpl implements ServiceRegistry | ||
{ | ||
private final Set<Class<?>> services; | ||
private final Class<?>[] services; | ||
|
||
private final BeanManager manager; | ||
|
||
|
@@ -34,6 +38,12 @@ public class ServiceRegistryImpl implements ServiceRegistry | |
|
||
private final LockManager lock; | ||
|
||
private Set<Class<?>> servicesSet; | ||
|
||
private ClassLoader addonClassLoader; | ||
|
||
private Map<String, Class<?>> classCache = new WeakHashMap<String, Class<?>>(); | ||
|
||
public ServiceRegistryImpl(LockManager lock, Addon addon, BeanManager manager, | ||
Set<Class<?>> services) | ||
{ | ||
|
@@ -43,7 +53,11 @@ public ServiceRegistryImpl(LockManager lock, Addon addon, BeanManager manager, | |
// Copy set to avoid any reference pointers | ||
Set<Class<?>> copy = new LinkedHashSet<Class<?>>(); | ||
copy.addAll(services); | ||
this.services = Collections.unmodifiableSet(copy); | ||
this.services = new ArrayList<Class<?>>(copy).toArray(new Class<?>[copy.size()]); | ||
this.servicesSet = Collections.unmodifiableSet(new LinkedHashSet<Class<?>>(Arrays.asList(this.services))); | ||
|
||
// Extracted for performance optimization | ||
this.addonClassLoader = addon.getClassLoader(); | ||
} | ||
|
||
@Override | ||
|
@@ -197,8 +211,9 @@ public <T> Set<ExportedInstance<T>> getExportedInstances(Class<T> requestedType) | |
return result; | ||
} | ||
|
||
for (Class<?> type : services) | ||
for (int i = 0; i < services.length; i++) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lincolnthree
Author
Member
|
||
{ | ||
Class<?> type = services[i]; | ||
if (requestedLoadedType.isAssignableFrom(type)) | ||
{ | ||
Set<Bean<?>> beans = manager.getBeans(type, getQualifiersFrom(type)); | ||
|
@@ -222,7 +237,7 @@ public <T> Set<ExportedInstance<T>> getExportedInstances(Class<T> requestedType) | |
@Override | ||
public Set<Class<?>> getExportedTypes() | ||
{ | ||
return services; | ||
return servicesSet; | ||
} | ||
|
||
@Override | ||
|
@@ -239,13 +254,13 @@ public <T> Set<Class<T>> getExportedTypes(Class<T> type) | |
} | ||
|
||
/** | ||
* Ensures that the returned class is loaded from the {@link Addon} | ||
* Ensures that the returned class is loaded from this {@link Addon} | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
private <T> Class<T> loadAddonClass(Class<T> actualType) throws ClassNotFoundException | ||
{ | ||
final Class<T> type; | ||
if (actualType.getClassLoader() == addon.getClassLoader()) | ||
if (actualType.getClassLoader() == addonClassLoader) | ||
{ | ||
type = actualType; | ||
} | ||
|
@@ -258,7 +273,16 @@ private <T> Class<T> loadAddonClass(Class<T> actualType) throws ClassNotFoundExc | |
|
||
private Class<?> loadAddonClass(String className) throws ClassNotFoundException | ||
{ | ||
return Class.forName(className, true, addon.getClassLoader()); | ||
Class<?> cached = classCache.get(className); | ||
if (cached == null) | ||
{ | ||
Class<?> result = Class.forName(className, false, addonClassLoader); | ||
// potentially not thread-safe | ||
classCache.put(className, result); | ||
cached = result; | ||
} | ||
|
||
return cached; | ||
} | ||
|
||
@Override | ||
|
Enhanced-for works with arrays also ;)