Skip to content

Commit

Permalink
ToolLoader ceylon/ceylon-runtime#53: save tool models
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed May 6, 2014
1 parent 9e9ef2b commit 1a31c15
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/com/redhat/ceylon/common/tool/ToolLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -30,6 +29,8 @@ public abstract class ToolLoader {
protected static final String SCRIPT_PREFIX = "SCRIPT:";

protected final ClassLoader loader;

private Map<String, ToolModel<? extends Tool>> toolModels = new HashMap<String, ToolModel<? extends Tool>>();

public ToolLoader() {
this(ToolLoader.class.getClassLoader());
Expand Down Expand Up @@ -75,7 +76,17 @@ protected String getToolClassName(final String toolName) {
* Returns a ToolModel given the name of the tool, or null if no such tool is
* know to this tool loader.
*/
public <T extends Tool> ToolModel<T> loadToolModel(String toolName) {
public synchronized <T extends Tool> ToolModel<T> loadToolModel(String toolName) {
@SuppressWarnings("unchecked")
ToolModel<T> loadedModel = (ToolModel<T>) toolModels.get(toolName);
if(loadedModel == null){
loadedModel = loadToolModelMemoised(toolName);
toolModels.put(toolName, loadedModel);
}
return loadedModel;
}

private <T extends Tool> ToolModel<T> loadToolModelMemoised(String toolName) {
String className = getToolClassName(toolName);
if(className != null && className.startsWith(SCRIPT_PREFIX)){
return loadScriptTool(className, toolName);
Expand Down

0 comments on commit 1a31c15

Please sign in to comment.