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
61
62 void processBinding(Node node, App app) throws Exception;
63 }
64
65
66 public static final String UNDEPLOYED = "undeployed";
67 public static final String DEPLOYING = "deploying";
68 public static final String DEPLOYED = "deployed";
69 public static final String STARTING = "starting";
70 public static final String STARTED = "started";
71 public static final String STOPPING = "stopping";
72 public static final String UNDEPLOYING = "undeploying";
73
74
75 private Map<String, List<Binding>> lifecyclebindings = new HashMap<String, List<Binding>>();
76
77 public AppLifeCycle()
78 {
79
80
81
82 addEdge(UNDEPLOYED,DEPLOYING);
83 addEdge(DEPLOYING,DEPLOYED);
84
85
86 addEdge(DEPLOYED,STARTING);
87 addEdge(STARTING,STARTED);
88
89
90 addEdge(STARTED,STOPPING);
91 addEdge(STOPPING,DEPLOYED);
92
93
94 addEdge(DEPLOYED,UNDEPLOYING);
95 addEdge(UNDEPLOYING,UNDEPLOYED);
96 }
97
98 public void addBinding(AppLifeCycle.Binding binding)
99 {
100 for (String nodeName : binding.getBindingTargets())
101 {
102 List<Binding> bindings = lifecyclebindings.get(nodeName);
103 if (bindings == null)
104 {
105 bindings = new ArrayList<Binding>();
106 }
107 bindings.add(binding);
108
109 lifecyclebindings.put(nodeName,bindings);
110 }
111 }
112
113 public void removeBinding(AppLifeCycle.Binding binding)
114 {
115 for (String nodeName : binding.getBindingTargets())
116 {
117 List<Binding> bindings = lifecyclebindings.get(nodeName);
118 if (bindings != null)
119 bindings.remove(binding);
120 }
121 }
122
123
124
125
126
127
128 public Set<AppLifeCycle.Binding> getBindings()
129 {
130 Set<Binding> boundset = new HashSet<Binding>();
131
132 for (List<Binding> bindings : lifecyclebindings.values())
133 {
134 boundset.addAll(bindings);
135 }
136
137 return boundset;
138 }
139
140
141
142
143
144
145 public Set<AppLifeCycle.Binding> getBindings(Node node)
146 {
147 return getBindings(node.getName());
148 }
149
150
151
152
153
154
155 public Set<AppLifeCycle.Binding> getBindings(String nodeName)
156 {
157 Set<Binding> boundset = new HashSet<Binding>();
158
159
160 List<Binding> bindings = lifecyclebindings.get(nodeName);
161 if (bindings != null)
162 {
163 boundset.addAll(bindings);
164 }
165
166
167 bindings = lifecyclebindings.get(ALL_NODES);
168 if (bindings != null)
169 {
170 boundset.addAll(bindings);
171 }
172
173 return boundset;
174 }
175
176 public void runBindings(Node node, App app, DeploymentManager deploymentManager) throws Throwable
177 {
178 for (Binding binding : getBindings(node))
179 {
180 if (Log.isDebugEnabled())
181 Log.debug("Calling " + binding.getClass().getName()+" for "+app);
182 binding.processBinding(node,app);
183 }
184 }
185 }