Igor Baidiuk avatar Igor Baidiuk committed 0fbcfb2

Initial rev. of events lib, actually need to write excessive testing in build.xml

Comments (0)

Files changed (5)

+<project name="ant-events" default="build" basedir="." >
+	<target name="build" >
+		<mkdir dir="classes" />
+		<javac
+			srcdir="src"
+			destdir="classes"
+			target="1.4"
+			source="1.4"
+			debug="on"
+			includeantruntime="true"
+		/>
+		<copy todir="classes" >
+			<fileset dir="src" includes="**/*.xml" />
+		</copy>
+		<mkdir dir="bin" />
+		<jar
+			destfile="bin/ant-events.jar"
+			basedir="classes"
+		/>
+	</target>
+	
+	<target name="clean" >
+		<delete dir="classes" quiet="true" />
+		<delete dir="bin" quiet="true" />
+	</target>
+	
+	<target name="test" depends="build" >
+		<typedef classpath="${basedir}/bin/ant-events.jar" resource="org/antcontrib/events/antlib.xml" />
+		
+		<event name="foo" >
+			<echo message="event foo" />
+		</event>
+		
+		<fire-event name="foo" />
+	</target>
+</project>

src/org/antcontrib/events/Event.java

+package org.antcontrib.events;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.antcontrib.events.EventList;
+
+import org.apache.tools.ant.BuildException;
+
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.TaskContainer;
+import org.apache.tools.ant.property.LocalProperties;
+
+public class Event extends Task implements TaskContainer {
+
+	protected String name = null;
+	protected int priority = 0;
+	
+	protected List tasks = new ArrayList();
+	
+	public void addTask(Task task)
+	{
+		tasks.add(task);
+	}
+	
+	public void setName (String name)
+	{
+		this.name = name;
+	}
+	
+	public String getName ()
+	{
+		return name;
+	}
+	
+	public void setPriority(int priority)
+	{
+		this.priority = priority;
+	}
+	
+	public int getPriority()
+	{
+		return priority;
+	}
+	
+	public void execute()
+	{
+		if (name == null || name.isEmpty())
+			throw new BuildException("Event name must be a nonempty string");
+		// TODO: implement context capture
+		// Add this task to respective event list
+		EventList.get(getProject(), name).addEvent(this);
+	}
+	
+	public void performTasks()
+	{
+		// TODO: implement closure enforcement
+		LocalProperties lp = LocalProperties.get(getProject());
+		lp.enterScope();
+		try
+		{
+			// Perform child tasks
+			for (int i = 0; i < tasks.size(); ++i)
+				((Task)tasks.get(i)).perform();
+		}
+		finally
+		{
+			lp.exitScope();
+		}
+	}
+}

src/org/antcontrib/events/EventList.java

+package org.antcontrib.events;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Iterator;
+import java.util.Comparator;
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import org.antcontrib.events.Event;
+
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.BuildException;
+
+public class EventList
+{
+	private static final String REFID_EVENTLISTMAP = "antcontrib.eventListMap";
+	
+	private final List events = new ArrayList();
+	
+	public static synchronized EventList get(Project project, String name)
+	{
+        Map m = (Map) project.getReference(REFID_EVENTLISTMAP);
+        if (m == null)
+        {
+            m = new HashMap();
+            project.addReference(REFID_EVENTLISTMAP, m);
+        }
+        EventList l = (EventList) m.get(name);
+        if (l == null) 
+        {
+        	l = new EventList();
+        	m.put(name, l);
+        }
+        return l;
+	}
+	
+	private EventList()
+	{ }
+	
+	public void addEvent(Event e)
+	{
+		events.add(e);
+	}
+	
+	public void execute(String order)
+	{
+		List l = new ArrayList();
+		l.addAll(events);
+		
+		if (order == "ascending")
+			Collections.sort(l, new Comparator()
+				{
+					public int compare(Object left, Object right)
+					{
+						return ((Event)right).getPriority() - ((Event)left).getPriority();
+					}
+				}
+			);
+		else if (order == "descending")
+			Collections.sort(l, new Comparator()
+				{
+					public int compare(Object left, Object right)
+					{
+						return ((Event)left).getPriority() - ((Event)right).getPriority();
+					}
+				}
+			);
+		else if (order != null && !order.isEmpty() && order != "unordered")
+			throw new BuildException("Order must be either 'unordered', 'ascending' or 'descending'");
+		
+		for (Iterator i = l.iterator(); i.hasNext(); )
+			((Event)i.next()).performTasks();
+	}  
+}

src/org/antcontrib/events/FireEvent.java

+package org.antcontrib.events;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+
+import org.antcontrib.events.EventList;
+
+public class FireEvent extends Task
+{
+	protected String name;
+	protected String order;
+	
+	public String getName()
+	{
+		return name;
+	}
+	
+	public void setName(String name)
+	{
+		this.name = name;
+	}
+	
+	public String getOrder()
+	{
+		return order;
+	}
+	
+	public void setOrder(String order)
+	{
+		this.order = order;
+	}
+	
+	public void execute()
+	{
+		if (name == null || name.isEmpty())
+			throw new BuildException("Event list name must be a nonempty string");
+		
+		EventList.get(getProject(), name).execute(order);
+	}
+}

src/org/antcontrib/events/antlib.xml

+<antlib>
+	<typedef name="event"      classname="org.antcontrib.events.Event" />
+	<typedef name="fire-event" classname="org.antcontrib.events.FireEvent" />
+</antlib>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.