1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.eclipse.jetty.deploy;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.eclipse.jetty.deploy.graph.Graph;
26 import org.eclipse.jetty.deploy.graph.Node;
27 import org.eclipse.jetty.util.log.Log;
28
29
30
31
32
33
34
35
36
37 public class AppLifeCycle extends Graph
38 {
39 private static final String ALL_NODES = "*";
40
41 public static interface Binding
42 {
43
44
45
46
47
48 String[] getBindingTargets();
49
50
51
52
53
54
55
56
57
58
59
60 void processBinding(Node node, App app) throws Exception;
61 }
62
63
64 public static final String UNDEPLOYED = "undeployed";
65 public static final String DEPLOYING = "deploying";
66 public static final String DEPLOYED = "deployed";
67 public static final String STARTING = "starting";
68 public static final String STARTED = "started";
69 public static final String STOPPING = "stopping";
70 public static final String UNDEPLOYING = "undeploying";
71
72
73 private Map<String, List<Binding>> lifecyclebindings = new HashMap<String, List<Binding>>();
74
75 public AppLifeCycle()
76 {
77
78
79
80 addEdge(UNDEPLOYED,DEPLOYING);
81 addEdge(DEPLOYING,DEPLOYED);
82
83
84 addEdge(DEPLOYED,STARTING);
85 addEdge(STARTING,STARTED);
86
87
88 addEdge(STARTED,STOPPING);
89 addEdge(STOPPING,DEPLOYED);
90
91
92 addEdge(DEPLOYED,UNDEPLOYING);
93 addEdge(UNDEPLOYING,UNDEPLOYED);
94 }
95
96 public void addBinding(AppLifeCycle.Binding binding)
97 {
98 for (String nodeName : binding.getBindingTargets())
99 {
100 List<Binding> bindings = lifecyclebindings.get(nodeName);
101 if (bindings == null)
102 {
103 bindings = new ArrayList<Binding>();
104 }
105 bindings.add(binding);
106
107 lifecyclebindings.put(nodeName,bindings);
108 }
109 }
110
111 public void removeBinding(AppLifeCycle.Binding binding)
112 {
113 for (String nodeName : binding.getBindingTargets())
114 {
115 List<Binding> bindings = lifecyclebindings.get(nodeName);
116 if (bindings != null)
117 bindings.remove(binding);
118 }
119 }
120
121
122
123
124
125
126 public Set<AppLifeCycle.Binding> getBindings()
127 {
128 Set<Binding> boundset = new HashSet<Binding>();
129
130 for (List<Binding> bindings : lifecyclebindings.values())
131 {
132 boundset.addAll(bindings);
133 }
134
135 return boundset;
136 }
137
138
139
140
141
142
143 public Set<AppLifeCycle.Binding> getBindings(Node node)
144 {
145 return getBindings(node.getName());
146 }
147
148
149
150
151
152
153 public Set<AppLifeCycle.Binding> getBindings(String nodeName)
154 {
155 Set<Binding> boundset = new HashSet<Binding>();
156
157
158 List<Binding> bindings = lifecyclebindings.get(nodeName);
159 if (bindings != null)
160 {
161 boundset.addAll(bindings);
162 }
163
164
165 bindings = lifecyclebindings.get(ALL_NODES);
166 if (bindings != null)
167 {
168 boundset.addAll(bindings);
169 }
170
171 return boundset;
172 }
173
174 public void runBindings(Node node, App app, DeploymentManager deploymentManager) throws Throwable
175 {
176 for (Binding binding : getBindings(node))
177 {
178 if (Log.isDebugEnabled())
179 Log.debug("Calling " + binding.getClass().getName()+" for "+app);
180 binding.processBinding(node,app);
181 }
182 }
183 }