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