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