Making AppLifeCycle binding lazy to allow for multiple xml configuration

scenario.  Cleaning up GraphOutputDot to sort 'undeployed' at top of
graph for readabilities sake.

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1134 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Joakim Erdfelt 2009-12-08 22:17:33 +00:00
parent 07d9656136
commit 5c48784e86
2 changed files with 72 additions and 44 deletions

View File

@ -36,6 +36,8 @@ import org.eclipse.jetty.util.log.Log;
*/ */
public class AppLifeCycle extends Graph public class AppLifeCycle extends Graph
{ {
private static final String ALL_NODES = "*";
public static interface Binding public static interface Binding
{ {
/** /**
@ -68,7 +70,7 @@ public class AppLifeCycle extends Graph
private static final String NODE_STARTED = "started"; private static final String NODE_STARTED = "started";
private static final String NODE_STOPPING = "stopping"; private static final String NODE_STOPPING = "stopping";
private static final String NODE_UNDEPLOYING = "undeploying"; private static final String NODE_UNDEPLOYING = "undeploying";
private Map<Node, List<Binding>> lifecyclebindings = new HashMap<Node, List<Binding>>(); private Map<String, List<Binding>> lifecyclebindings = new HashMap<String, List<Binding>>();
public AppLifeCycle() public AppLifeCycle()
{ {
@ -95,33 +97,15 @@ public class AppLifeCycle extends Graph
{ {
for (String nodeName : binding.getBindingTargets()) for (String nodeName : binding.getBindingTargets())
{ {
if (nodeName.equals("*")) List<Binding> bindings = lifecyclebindings.get(nodeName);
if (bindings == null)
{ {
// Special Case: Bind to all Nodes bindings = new ArrayList<Binding>();
for (Node node : getNodes())
{
bindToNode(node,binding);
}
} }
else bindings.add(binding);
{
// Bind to specific node
Node node = getNodeByName(nodeName);
bindToNode(node,binding);
}
}
}
private void bindToNode(Node node, AppLifeCycle.Binding binding) lifecyclebindings.put(nodeName,bindings);
{
List<Binding> bindings = lifecyclebindings.get(node);
if (bindings == null)
{
bindings = new ArrayList<Binding>();
} }
bindings.add(binding);
lifecyclebindings.put(node,bindings);
} }
/** /**
@ -147,10 +131,20 @@ public class AppLifeCycle extends Graph
* @return Set of Object(s) for specific lifecycle bindings. never null. * @return Set of Object(s) for specific lifecycle bindings. never null.
*/ */
public Set<AppLifeCycle.Binding> getBindings(Node node) public Set<AppLifeCycle.Binding> getBindings(Node node)
{
return getBindings(node.getName());
}
/**
* Get all objects bound to a specific {@link Node}
*
* @return Set of Object(s) for specific lifecycle bindings. never null.
*/
public Set<AppLifeCycle.Binding> getBindings(String nodeName)
{ {
Set<Binding> boundset = new HashSet<Binding>(); Set<Binding> boundset = new HashSet<Binding>();
List<Binding> bindings = lifecyclebindings.get(node); List<Binding> bindings = lifecyclebindings.get(nodeName);
if (bindings == null) if (bindings == null)
{ {
return boundset; return boundset;
@ -161,19 +155,11 @@ public class AppLifeCycle extends Graph
return boundset; return boundset;
} }
public Set<Binding> getBindings(String nodeName)
{
Node node = getNodeByName(nodeName);
return getBindings(node);
}
public void runBindings(Node node, App app, DeploymentManager deploymentManager) throws Throwable public void runBindings(Node node, App app, DeploymentManager deploymentManager) throws Throwable
{ {
List<Binding> bindings = this.lifecyclebindings.get(node); List<Binding> bindings = new ArrayList<Binding>();
if (bindings == null) bindings.addAll(getBindings(ALL_NODES)); // Bindings (special) All Nodes
{ bindings.addAll(getBindings(node)); // Specific Node
return;
}
for (Binding binding : bindings) for (Binding binding : bindings)
{ {

View File

@ -19,6 +19,11 @@ import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.CollationKey;
import java.text.Collator;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.IO;
@ -31,7 +36,41 @@ public class GraphOutputDot
{ {
} }
public static void write(Graph graph,File outputFile) throws IOException private static final String TOPNODE = "undeployed";
/**
* Comparator that makes the 'undeployed' node the first node in the sort list.
*
* This makes the 'undeployed' node show up at the top of the generated graph.
*/
private static class TopNodeSort implements Comparator<Node>
{
private Collator collator = Collator.getInstance();
public int compare(Node o1, Node o2)
{
if (o1.getName().equals(TOPNODE))
{
return -1;
}
if (o2.getName().equals(TOPNODE))
{
return 1;
}
CollationKey key1 = toKey(o1);
CollationKey key2 = toKey(o2);
return key1.compareTo(key2);
}
private CollationKey toKey(Node node)
{
return collator.getCollationKey(node.getName());
}
}
public static void write(Graph graph, File outputFile) throws IOException
{ {
FileWriter writer = null; FileWriter writer = null;
PrintWriter out = null; PrintWriter out = null;
@ -48,7 +87,10 @@ public class GraphOutputDot
writeNodeDefaults(out); writeNodeDefaults(out);
writeEdgeDefaults(out); writeEdgeDefaults(out);
for (Node node : graph.getNodes()) Set<Node> nodes = new TreeSet<Node>(new TopNodeSort());
nodes.addAll(graph.getNodes());
for (Node node : nodes)
{ {
writeNode(out,node); writeNode(out,node);
} }
@ -72,8 +114,8 @@ public class GraphOutputDot
out.println(); out.println();
out.println(" // Edge"); out.println(" // Edge");
out.printf(" \"%s\" -> \"%s\" [%n",toId(edge.getFrom()),toId(edge.getTo())); out.printf(" \"%s\" -> \"%s\" [%n",toId(edge.getFrom()),toId(edge.getTo()));
out.printf(" arrowtail=none,%n"); out.println(" arrowtail=none,");
out.printf(" arrowhead=normal%n"); out.println(" arrowhead=normal");
out.println(" ];"); out.println(" ];");
} }
@ -85,10 +127,10 @@ public class GraphOutputDot
out.printf(" label=\"%s\",%n",node.getName()); out.printf(" label=\"%s\",%n",node.getName());
if (node.getName().endsWith("ed")) if (node.getName().endsWith("ed"))
{ {
out.printf(" color=\"#ddddff\",%n"); out.println(" color=\"#ddddff\",");
out.printf(" style=filled,%n"); out.println(" style=filled,");
} }
out.printf(" shape=box%n"); out.println(" shape=box");
out.println(" ];"); out.println(" ];");
} }