From f19f07d6e4c72f241057ecfc32d80be21201bbf7 Mon Sep 17 00:00:00 2001 From: Conor MacNeill Date: Fri, 31 May 2002 15:15:47 +0000 Subject: [PATCH] Various cleanups git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272820 13f79535-47bb-0310-9956-ffa450edef68 --- proposal/mutant/build/ant1compat.xml | 2 + proposal/mutant/build/docs.xml | 12 +-- proposal/mutant/docs/desc.html | 1 + .../ant/antcore/antlib/AntLibManager.java | 28 +++++- .../antcore/execution/ComponentManager.java | 92 ++++++++++--------- .../antcore/execution/ExecutionContext.java | 2 +- .../apache/ant/antcore/execution/Frame.java | 18 ++-- .../org/apache/tools/ant/Ant1Factory.java | 4 +- .../ant/antlib/monitor/MonitorAspect.java | 90 +++++------------- .../ant/antlib/monitor/MonitorRecord.java | 46 +++++++--- .../apache/ant/common/antlib/AntContext.java | 9 ++ .../apache/ant/common/event/BuildEvent.java | 4 + .../ant/common/logger/DefaultLogger.java | 3 +- 13 files changed, 164 insertions(+), 147 deletions(-) diff --git a/proposal/mutant/build/ant1compat.xml b/proposal/mutant/build/ant1compat.xml index 79867529f..039e4f2d6 100644 --- a/proposal/mutant/build/ant1compat.xml +++ b/proposal/mutant/build/ant1compat.xml @@ -96,6 +96,8 @@ + + diff --git a/proposal/mutant/build/docs.xml b/proposal/mutant/build/docs.xml index ca7cf5149..540d88436 100644 --- a/proposal/mutant/build/docs.xml +++ b/proposal/mutant/build/docs.xml @@ -1,4 +1,4 @@ - + @@ -15,8 +15,8 @@ - - + @@ -24,7 +24,7 @@ - AnakiaTask is not present! Please check to make sure that + AnakiaTask is not present! Please check to make sure that velocity.jar is in your classpath. @@ -33,7 +33,7 @@ - + - + diff --git a/proposal/mutant/docs/desc.html b/proposal/mutant/docs/desc.html index b29576365..49aba6f42 100644 --- a/proposal/mutant/docs/desc.html +++ b/proposal/mutant/docs/desc.html @@ -9,6 +9,7 @@ + The Jakarta Site - Mutant Design Notes diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/antlib/AntLibManager.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/antlib/AntLibManager.java index 5f9c156b4..1b27b7607 100644 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/antlib/AntLibManager.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/antlib/AntLibManager.java @@ -53,6 +53,7 @@ */ package org.apache.ant.antcore.antlib; import java.io.FileNotFoundException; +import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -248,7 +249,6 @@ public class AntLibManager { */ public void addLibPath(String libraryId, URL libPath) throws AntLibException { - System.out.println("Adding path " + libPath + " for " + libraryId); if (!libPath.getProtocol().equals("file") && !remoteAllowed) { throw new AntLibException("Remote libpaths are not" @@ -260,11 +260,31 @@ public class AntLibManager { libPaths = new ArrayList(); libPathsMap.put(libraryId, libPaths); } - libPaths.add(libPath); + + List newPaths = new ArrayList(); + newPaths.add(libPath); + if (libPath.getProtocol().equals("file")) { + File dir = new File(libPath.getFile()); + if (dir.isDirectory()) { + try { + URL[] pathURLs = LoaderUtils.getLocationURLs(libPath, + null, ANTLIB_EXTENSIONS); + for (int i = 0; i < pathURLs.length; ++i) { + newPaths.add(pathURLs[i]); + } + } catch (MalformedURLException e) { + // ignore and just use what we were given + } + } + } AntLibrary antLibrary = getLibrary(libraryId); - if (antLibrary != null) { - antLibrary.addLibraryURL(libPath); + for (Iterator i = newPaths.iterator(); i.hasNext();) { + URL newPath = (URL) i.next(); + libPaths.add(newPath); + if (antLibrary != null) { + antLibrary.addLibraryURL(newPath); + } } } diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ComponentManager.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ComponentManager.java index 179679e60..422cbd7e1 100644 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ComponentManager.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ComponentManager.java @@ -435,37 +435,18 @@ public class ComponentManager implements ComponentService { private Object createComponent(String componentName, BuildElement model) throws AntException { - Object component = null; - if (model != null) { - for (Iterator i = aspects.iterator(); i.hasNext();) { - Aspect aspect = (Aspect) i.next(); - component = aspect.preCreateComponent(component, model); - } + ImportInfo importInfo = getImport(componentName); + if (importInfo == null) { + throw new ExecutionException("There is no definition of the <" + + componentName + "> component"); } + String className = importInfo.getClassName(); - if (component == null) { - ImportInfo importInfo = getImport(componentName); - if (importInfo == null) { - throw new ExecutionException("There is no definition of the <" - + componentName + "> component"); - } - String className = importInfo.getClassName(); - - ComponentLibrary componentLibrary + ComponentLibrary componentLibrary = importInfo.getComponentLibrary(); - component = createComponentFromDef(componentName, componentLibrary, - importInfo.getDefinition(), model); - } - - if (model != null) { - for (Iterator i = aspects.iterator(); i.hasNext();) { - Aspect aspect = (Aspect) i.next(); - component = aspect.postCreateComponent(component, model); - } - } - - return component; + return createComponentFromDef(componentName, componentLibrary, + importInfo.getDefinition(), model); } /** @@ -486,22 +467,36 @@ public class ComponentManager implements ComponentService { throws AntException { Location location = Location.UNKNOWN_LOCATION; - if (model != null) { - location = model.getLocation(); - } - - boolean isTask - = libDefinition.getDefinitionType() == AntLibrary.TASKDEF; - String localName = libDefinition.getDefinitionName(); - String className = libDefinition.getClassName(); + String className = null; try { - ClassLoader componentLoader = componentLibrary.getClassLoader(); - Class componentClass - = Class.forName(className, true, componentLoader); + boolean isTask + = libDefinition.getDefinitionType() == AntLibrary.TASKDEF; + + + Object component = null; + if (model != null) { + location = model.getLocation(); + for (Iterator i = aspects.iterator(); i.hasNext();) { + Aspect aspect = (Aspect) i.next(); + component = aspect.preCreateComponent(component, model); + } + } + AntLibFactory libFactory = getLibFactory(componentLibrary); - // create the component using the factory - Object component - = libFactory.createComponent(componentClass, localName); + ClassLoader componentLoader = null; + if (component == null) { + String localName = libDefinition.getDefinitionName(); + className = libDefinition.getClassName(); + componentLoader = componentLibrary.getClassLoader(); + Class componentClass + = Class.forName(className, true, componentLoader); + // create the component using the factory + component + = libFactory.createComponent(componentClass, localName); + } else { + className = component.getClass().getName(); + componentLoader = component.getClass().getClassLoader(); + } // wrap the component in an adapter if required. ExecutionComponent execComponent = null; @@ -522,10 +517,13 @@ public class ComponentManager implements ComponentService { // if the component is an execution component create a context and // initialise the component with it. if (execComponent != null) { - ExecutionContext context - = new ExecutionContext(frame, execComponent, model); - context.setClassLoader(componentLoader); - execComponent.init(context, componentName); + // give it a context unless it already has one + if (execComponent.getAntContext() == null) { + ExecutionContext context + = new ExecutionContext(frame, execComponent, model); + context.setClassLoader(componentLoader); + execComponent.init(context, componentName); + } } // if we have a model, use it to configure the component. Otherwise @@ -537,6 +535,10 @@ public class ComponentManager implements ComponentService { if (execComponent != null) { execComponent.validateComponent(); } + for (Iterator i = aspects.iterator(); i.hasNext();) { + Aspect aspect = (Aspect) i.next(); + component = aspect.postCreateComponent(component, model); + } } // reset the loader diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionContext.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionContext.java index 80b5213f2..e225809be 100755 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionContext.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/ExecutionContext.java @@ -175,7 +175,7 @@ public class ExecutionContext implements AntContext { * * @return the build model or null if there is no build model. */ - protected BuildElement getModel() { + public BuildElement getModel() { return model; } } diff --git a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/Frame.java b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/Frame.java index 27d25c962..c3c89661c 100644 --- a/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/Frame.java +++ b/proposal/mutant/src/java/antcore/org/apache/ant/antcore/execution/Frame.java @@ -973,14 +973,15 @@ public class Frame implements DemuxOutputReceiver { failureCause = aspect.postExecuteTask(aspectContext, failureCause); } + eventSupport.fireTaskFinished(task, failureCause); if (aspectContexts.size() != 0) { aspectContextsMap.remove(task); } if (failureCause != null) { - if (failureCause instanceof ExecutionException) { - throw (ExecutionException) failureCause; + if (failureCause instanceof AntException) { + throw (AntException) failureCause; } throw new ExecutionException(failureCause); } @@ -1156,7 +1157,8 @@ public class Frame implements DemuxOutputReceiver { // load system ant lib URL systemLibs = new URL(initConfig.getLibraryURL(), "syslibs/"); - componentManager.loadLib(systemLibs, true); + componentManager.loadLib(systemLibs, false); + importStandardComponents(); executeTasks(config.getGlobalTasks()); @@ -1164,12 +1166,12 @@ public class Frame implements DemuxOutputReceiver { URL antLibs = new URL(initConfig.getLibraryURL(), "antlibs/"); componentManager.loadLib(antLibs, false); - } catch (MalformedURLException e) { - throw new ExecutionException("Unable to initialize antlibs", e); - } - - try { runBuild(targets); + } catch (MalformedURLException e) { + ExecutionException ee = + new ExecutionException("Unable to initialize antlibs", e); + buildFailureCause = ee; + throw ee; } catch (RuntimeException e) { buildFailureCause = e; throw e; diff --git a/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Ant1Factory.java b/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Ant1Factory.java index 195d15a19..7a0d0dfc0 100644 --- a/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Ant1Factory.java +++ b/proposal/mutant/src/java/antlibs/ant1compat/org/apache/tools/ant/Ant1Factory.java @@ -57,7 +57,7 @@ import org.apache.ant.common.antlib.StandardLibFactory; import org.apache.ant.common.service.EventService; import org.apache.ant.common.util.AntException; import org.apache.ant.init.LoaderUtils; - +import java.lang.reflect.InvocationTargetException; /** * The factory object for the Ant1 compatability Ant library * @@ -132,7 +132,7 @@ public class Ant1Factory extends StandardLibFactory { ((ProjectComponent) component).setProject(project); } return component; - } catch (java.lang.reflect.InvocationTargetException ite) { + } catch (InvocationTargetException ite) { Throwable t = ite.getTargetException(); String msg = "Could not create component of type: " + componentClass.getName() + " due to " + t; diff --git a/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorAspect.java b/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorAspect.java index c01c2783f..dcef233de 100644 --- a/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorAspect.java +++ b/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorAspect.java @@ -55,6 +55,7 @@ package org.apache.ant.antlib.monitor; import org.apache.ant.common.antlib.AbstractAspect; import org.apache.ant.common.antlib.Task; +import org.apache.ant.common.antlib.AntContext; import org.apache.ant.common.model.BuildElement; import org.apache.ant.common.model.AspectValueCollection; import org.apache.ant.common.util.AntException; @@ -71,70 +72,25 @@ import java.io.IOException; * @author Conor MacNeill */ public class MonitorAspect extends AbstractAspect { + /** The log into which monitoring info is written */ private static PrintStream monitorLog; - private static long lastMessageTime; - public MonitorAspect() { - if (monitorLog == null) { - try { - monitorLog - = new PrintStream(new FileOutputStream("monitor.log")); - monitorLog.println("Logging started at " + new Date()); - lastMessageTime = System.currentTimeMillis(); - } catch (IOException e) { - log("Unable to open monitor log", MessageLevel.MSG_WARN); - } - } - } - - private void monitor(String message) { - Runtime rt = Runtime.getRuntime(); - synchronized (monitorLog) { - long now = System.currentTimeMillis(); - long diff = now - lastMessageTime; - lastMessageTime = now; - long freeMem = rt.freeMemory(); - long usedMem = rt.totalMemory() - freeMem; - monitorLog.println("+" + diff + " (" + usedMem + "/" - + freeMem + "): " + message); - } - } - - /** - * This join point is activated before a component is to be created. - * The aspect can return an object to be used rather than the core creating - * the object. - * - * @param component the component that has been created. This will be null - * unless another aspect has created the component - * @param model the Build model that applies to the component - * - * @return a component to use. - * @exception AntException if the aspect cannot process the component. - */ - public Object preCreateComponent(Object component, BuildElement model) - throws AntException { - monitor("Creating component " + "from <" + model.getType() + ">"); - return component; - } /** - * This join point is activated after a component has been created and - * configured. If the aspect wishes, an object can be returned in place - * of the one created by Ant. - * - * @param component the component that has been created. - * @param model the Build model used to create the component. - * - * @return a replacement for the component if desired. If null is returned - * the current component is used. - * @exception AntException if the aspect cannot process the component. + * Create a monitoring aspect instance. There will be one instance per + * active frame but they all share the same log output. */ - public Object postCreateComponent(Object component, BuildElement model) - throws AntException { - monitor("Created component " - + component.getClass().getName() - + " from <" + model.getType() + ">"); - return component; + public MonitorAspect() { + synchronized (MonitorAspect.class) { + if (monitorLog == null) { + try { + monitorLog + = new PrintStream(new FileOutputStream("monitor.log")); + monitorLog.println("Logging started at " + new Date()); + } catch (IOException e) { + log("Unable to open monitor log", MessageLevel.MSG_WARN); + } + } + } } /** @@ -152,9 +108,12 @@ public class MonitorAspect extends AbstractAspect { */ public Object preExecuteTask(Task task, AspectValueCollection aspectValues) throws AntException { - String taskName = task.getClass().getName(); - MonitorRecord record = new MonitorRecord(taskName); - return record; + System.gc(); + AntContext taskContext = task.getAntContext(); + BuildElement model = taskContext.getModel(); + String name = (model == null) ? task.getClass().getName() + : model.getType(); + return new MonitorRecord(name, taskContext.getLocation()); } /** @@ -167,10 +126,9 @@ public class MonitorAspect extends AbstractAspect { * @return a new failure reason or null if the task is not to fail. */ public Throwable postExecuteTask(Object context, Throwable failureCause) { - MonitorRecord record = (MonitorRecord)context; - record.print(monitorLog); + MonitorRecord record = (MonitorRecord) context; System.gc(); + record.print(monitorLog); return failureCause; } - } diff --git a/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorRecord.java b/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorRecord.java index 0b30d0740..bda03dadf 100644 --- a/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorRecord.java +++ b/proposal/mutant/src/java/antlibs/monitor/org/apache/ant/antlib/monitor/MonitorRecord.java @@ -53,16 +53,8 @@ */ package org.apache.ant.antlib.monitor; -import org.apache.ant.common.antlib.AbstractAspect; -import org.apache.ant.common.antlib.Task; -import org.apache.ant.common.model.BuildElement; -import org.apache.ant.common.model.AspectValueCollection; -import org.apache.ant.common.util.AntException; -import org.apache.ant.common.event.MessageLevel; -import java.util.Date; +import org.apache.ant.common.util.Location; import java.io.PrintStream; -import java.io.FileOutputStream; -import java.io.IOException; /** * A record of some performance values at a particular time @@ -70,25 +62,51 @@ import java.io.IOException; * @author Conor MacNeill */ public class MonitorRecord { - private String recordName; + /** The element name being monitored */ + private String elementName; + + /** The location in the build file */ + private Location location; + + /** System clock when task started */ private long startMillis; + + /** Starting memory when task started */ private long startMemory; + /** + * Get the current used memory from the runtime + * + * @return the amount of memory in use. + */ private long getMemoryUsage() { Runtime rt = Runtime.getRuntime(); return rt.totalMemory() - rt.freeMemory(); } - public MonitorRecord(String recordName) { - this.recordName = recordName; + /** + * Create a monitoring record. + * + * @param elementName the name of the element. + * @param location the build file location of the element. + */ + public MonitorRecord(String elementName, Location location) { + this.elementName = elementName; + this.location = location; startMillis = System.currentTimeMillis(); startMemory = getMemoryUsage(); } + /** + * Print a summary of the monitor + * + * @param stream the stream onto which the summary is printed. + */ public void print(PrintStream stream) { long timeDiff = System.currentTimeMillis() - startMillis; long memDiff = getMemoryUsage() - startMemory; - stream.println(recordName + " took " + timeDiff - + " milliseconds and memory used changed by " + memDiff); + stream.println(location + elementName + " took " + timeDiff + + " milliseconds, memory used at start " + startMemory + + " which changed by " + memDiff); } } diff --git a/proposal/mutant/src/java/common/org/apache/ant/common/antlib/AntContext.java b/proposal/mutant/src/java/common/org/apache/ant/common/antlib/AntContext.java index 5f1c0ddd5..dc62c05bd 100644 --- a/proposal/mutant/src/java/common/org/apache/ant/common/antlib/AntContext.java +++ b/proposal/mutant/src/java/common/org/apache/ant/common/antlib/AntContext.java @@ -54,6 +54,7 @@ package org.apache.ant.common.antlib; import org.apache.ant.common.util.AntException; import org.apache.ant.common.util.Location; +import org.apache.ant.common.model.BuildElement; /** * The AntContext is the interface through which the Ant container and the @@ -91,5 +92,13 @@ public interface AntContext { * @return the location which may be the unknown location */ Location getLocation(); + + /** + * Get the build model associated with this context if any + * + * @return the build model which may be null if there is no associated + * model + */ + BuildElement getModel(); } diff --git a/proposal/mutant/src/java/common/org/apache/ant/common/event/BuildEvent.java b/proposal/mutant/src/java/common/org/apache/ant/common/event/BuildEvent.java index 6d4fafa5b..15758eab8 100644 --- a/proposal/mutant/src/java/common/org/apache/ant/common/event/BuildEvent.java +++ b/proposal/mutant/src/java/common/org/apache/ant/common/event/BuildEvent.java @@ -80,6 +80,10 @@ public class BuildEvent extends EventObject { public static final int TASK_FINISHED = 6; /** message event type */ public static final int MESSAGE = 7; + /** Project started event type */ + public static final int PROJECT_STARTED = 8; + /** Project finished event type */ + public static final int PROJECT_FINISHED = 9; /** The type of this event */ private int eventType; diff --git a/proposal/mutant/src/java/common/org/apache/ant/common/logger/DefaultLogger.java b/proposal/mutant/src/java/common/org/apache/ant/common/logger/DefaultLogger.java index 64283397d..4dbd387cd 100755 --- a/proposal/mutant/src/java/common/org/apache/ant/common/logger/DefaultLogger.java +++ b/proposal/mutant/src/java/common/org/apache/ant/common/logger/DefaultLogger.java @@ -57,6 +57,7 @@ import java.io.PrintStream; import org.apache.ant.common.antlib.ExecutionComponent; import org.apache.ant.common.antlib.Task; import org.apache.ant.common.event.BuildEvent; +import org.apache.ant.common.event.BuildListenerAdapter; import org.apache.ant.common.event.MessageLevel; import org.apache.ant.common.model.Target; import org.apache.ant.common.util.AntException; @@ -69,7 +70,7 @@ import org.apache.ant.common.util.Location; * @author Conor MacNeill * @created 15 January 2002 */ -public class DefaultLogger implements BuildLogger { +public class DefaultLogger extends BuildListenerAdapter implements BuildLogger { /** Standard field separator */ private static String lSep = System.getProperty("line.separator");