Revision 24841ac2
Added by Andreas Kohlbecker almost 11 years ago
src/main/java/eu/etaxonomy/cdm/server/instance/CdmInstance.java | ||
---|---|---|
1 | 1 |
package eu.etaxonomy.cdm.server.instance; |
2 | 2 |
|
3 |
import java.lang.reflect.InvocationTargetException; |
|
4 |
import java.sql.Connection; |
|
5 |
import java.sql.SQLException; |
|
3 | 6 |
import java.util.ArrayList; |
4 | 7 |
import java.util.List; |
5 | 8 |
|
9 |
import javax.naming.NamingException; |
|
10 |
import javax.sql.DataSource; |
|
11 |
|
|
6 | 12 |
import org.apache.log4j.Logger; |
13 |
import org.eclipse.jetty.plus.jndi.Resource; |
|
7 | 14 |
import org.eclipse.jetty.server.handler.ContextHandler.Context; |
8 | 15 |
import org.eclipse.jetty.util.component.LifeCycle; |
9 | 16 |
import org.eclipse.jetty.util.component.LifeCycle.Listener; |
10 | 17 |
import org.eclipse.jetty.webapp.WebAppContext; |
11 | 18 |
|
12 |
import eu.etaxonomy.cdm.server.Bootloader; |
|
13 | 19 |
|
14 | 20 |
public class CdmInstance implements Listener { |
15 | 21 |
|
16 |
private static final Logger logger = Logger.getLogger(InstanceManager.class); |
|
17 |
|
|
18 |
private WebAppContext webAppContext = null; |
|
19 |
|
|
20 |
private final Configuration configuration; |
|
21 |
|
|
22 |
private List<String> problems; |
|
23 |
private Status status = Status.uninitialized; |
|
24 |
|
|
25 |
public CdmInstance(Configuration configuration) { |
|
26 |
this.configuration = configuration; |
|
27 |
} |
|
28 |
|
|
29 |
public List<String> getProblems() { |
|
30 |
if (problems == null) { |
|
31 |
problems = new ArrayList<String>(); |
|
32 |
} |
|
33 |
return problems; |
|
34 |
} |
|
35 |
|
|
36 |
public void setProblems(List<String> problems) { |
|
37 |
this.problems = problems; |
|
38 |
} |
|
39 |
|
|
40 |
public boolean hasProblems() { |
|
41 |
return getProblems().size() > 0; |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* @param status |
|
46 |
* the status to set |
|
47 |
*/ |
|
48 |
public void setStatus(Status status) { |
|
49 |
this.status = status; |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* @return the status |
|
54 |
*/ |
|
55 |
public Status getStatus() { |
|
56 |
return status; |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* @return the enabled |
|
61 |
*/ |
|
62 |
public boolean isEnabled() { |
|
63 |
return !status.equals(Status.disabled); |
|
64 |
} |
|
65 |
|
|
66 |
/** |
|
67 |
* @return the configuration |
|
68 |
*/ |
|
69 |
public Configuration getConfiguration() { |
|
70 |
return configuration; |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* @return the webAppContext |
|
75 |
*/ |
|
76 |
public WebAppContext getWebAppContext() { |
|
77 |
return webAppContext; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* @return the webAppContext |
|
82 |
*/ |
|
83 |
public void setWebAppContext(WebAppContext webAppContext) { |
|
84 |
this.webAppContext = webAppContext; |
|
85 |
} |
|
86 |
|
|
87 |
/** |
|
88 |
* @param <T> |
|
89 |
* @param webAppContext |
|
90 |
* @param attributeName |
|
91 |
* @param type |
|
92 |
* @return |
|
93 |
*/ |
|
94 |
@SuppressWarnings("unchecked") |
|
95 |
private <T> T getServletContextAttribute(WebAppContext webAppContext, String attributeName, Class<T> type) { |
|
96 |
|
|
97 |
Context servletContext = webAppContext.getServletContext(); |
|
98 |
Object value = servletContext.getAttribute(attributeName); |
|
99 |
if (value != null && type.isAssignableFrom(value.getClass())) { |
|
100 |
|
|
101 |
} |
|
102 |
return (T) value; |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public void lifeCycleStopping(LifeCycle event) { |
|
107 |
logger.info("lifeCycleStopping"); |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public void lifeCycleStopped(LifeCycle event) { |
|
112 |
logger.info("lifeCycleStopped"); |
|
113 |
|
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public void lifeCycleStarting(LifeCycle event) { |
|
118 |
logger.info("lifeCycleStarting"); |
|
119 |
} |
|
120 |
|
|
121 |
@SuppressWarnings("unchecked") |
|
122 |
@Override |
|
123 |
public void lifeCycleStarted(LifeCycle event) { |
|
124 |
logger.info("lifeCycleStarted"); |
|
125 |
|
|
126 |
List<String> messages = getServletContextAttribute(webAppContext, Bootloader.ATTRIBUTE_ERROR_MESSAGES, List.class); |
|
127 |
String dataSourceName = getServletContextAttribute(webAppContext, Bootloader.ATTRIBUTE_DATASOURCE_NAME, String.class); |
|
128 |
|
|
129 |
if (messages != null && dataSourceName != null) { |
|
130 |
|
|
131 |
getProblems().addAll(messages); |
|
132 |
setStatus(Status.error); |
|
133 |
try { |
|
134 |
logger.warn("Stopping context '" + dataSourceName + "' due to errors reported in ServletContext"); |
|
135 |
webAppContext.stop(); |
|
136 |
} catch (Exception e) { |
|
137 |
logger.error(e); |
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
@Override |
|
143 |
public void lifeCycleFailure(LifeCycle event, Throwable cause) { |
|
144 |
logger.error("lifeCycleFailure"); |
|
145 |
} |
|
22 |
private static final Logger logger = Logger.getLogger(InstanceManager.class); |
|
23 |
|
|
24 |
private WebAppContext webAppContext = null; |
|
25 |
|
|
26 |
private Configuration configuration; |
|
27 |
|
|
28 |
private List<String> problems; |
|
29 |
private Status status = Status.uninitialized; |
|
30 |
|
|
31 |
private Resource jndiDataSource; |
|
32 |
|
|
33 |
public CdmInstance(Configuration configuration) { |
|
34 |
this.configuration = configuration; |
|
35 |
} |
|
36 |
|
|
37 |
public List<String> getProblems() { |
|
38 |
if (problems == null) { |
|
39 |
problems = new ArrayList<String>(); |
|
40 |
} |
|
41 |
return problems; |
|
42 |
} |
|
43 |
|
|
44 |
public void setProblems(List<String> problems) { |
|
45 |
this.problems = problems; |
|
46 |
} |
|
47 |
|
|
48 |
public boolean onError() { |
|
49 |
return status.equals(Status.error); |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* @param status |
|
54 |
* the status to set |
|
55 |
*/ |
|
56 |
public void setStatus(Status status) { |
|
57 |
this.status = status; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* @return the status |
|
62 |
*/ |
|
63 |
public Status getStatus() { |
|
64 |
return status; |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* @return true if status is not {@link Status#disabled} |
|
69 |
*/ |
|
70 |
public boolean isEnabled() { |
|
71 |
return !status.equals(Status.disabled); |
|
72 |
} |
|
73 |
|
|
74 |
/** |
|
75 |
* @return the configuration |
|
76 |
*/ |
|
77 |
public Configuration getConfiguration() { |
|
78 |
return configuration; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* @return the configuration |
|
83 |
*/ |
|
84 |
public void setConfiguration(Configuration configuration) { |
|
85 |
this.configuration = configuration; |
|
86 |
} |
|
87 |
|
|
88 |
/** |
|
89 |
* @return the webAppContext |
|
90 |
*/ |
|
91 |
public WebAppContext getWebAppContext() { |
|
92 |
return webAppContext; |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* @return the webAppContext |
|
97 |
*/ |
|
98 |
public void setWebAppContext(WebAppContext webAppContext) { |
|
99 |
this.webAppContext = webAppContext; |
|
100 |
} |
|
101 |
|
|
102 |
/** |
|
103 |
* @param <T> |
|
104 |
* @param webAppContext |
|
105 |
* @param attributeName |
|
106 |
* @param type |
|
107 |
* @return |
|
108 |
*/ |
|
109 |
@SuppressWarnings("unchecked") |
|
110 |
private <T> T getServletContextAttribute(WebAppContext webAppContext, String attributeName, Class<T> type) { |
|
111 |
|
|
112 |
Context servletContext = webAppContext.getServletContext(); |
|
113 |
Object value = servletContext.getAttribute(attributeName); |
|
114 |
if (value != null && type.isAssignableFrom(value.getClass())) { |
|
115 |
|
|
116 |
} |
|
117 |
return (T) value; |
|
118 |
} |
|
119 |
|
|
120 |
@Override |
|
121 |
public void lifeCycleStopping(LifeCycle event) { |
|
122 |
logger.info("lifeCycleStopping"); |
|
123 |
if(!getStatus().equals(Status.removed)){ |
|
124 |
// never override Status.removed !!! |
|
125 |
setStatus(Status.stopping); |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
public void lifeCycleStopped(LifeCycle event) { |
|
131 |
logger.info("lifeCycleStopped"); |
|
132 |
if(!getStatus().equals(Status.removed)){ |
|
133 |
// never override Status.removed !!! |
|
134 |
setStatus(Status.stopped); |
|
135 |
} |
|
136 |
|
|
137 |
} |
|
138 |
|
|
139 |
@Override |
|
140 |
public void lifeCycleStarting(LifeCycle event) { |
|
141 |
logger.info("lifeCycleStarting"); |
|
142 |
setStatus(Status.starting); |
|
143 |
} |
|
144 |
|
|
145 |
@SuppressWarnings("unchecked") |
|
146 |
@Override |
|
147 |
public void lifeCycleStarted(LifeCycle event) { |
|
148 |
logger.info("lifeCycleStarted"); |
|
149 |
|
|
150 |
List<String> messages = getServletContextAttribute(webAppContext, SharedAttributes.ATTRIBUTE_ERROR_MESSAGES, List.class); |
|
151 |
String dataSourceName = getServletContextAttribute(webAppContext, SharedAttributes.ATTRIBUTE_DATASOURCE_NAME, String.class); |
|
152 |
|
|
153 |
if (messages != null && dataSourceName != null) { |
|
154 |
|
|
155 |
getProblems().addAll(messages); |
|
156 |
setStatus(Status.error); |
|
157 |
try { |
|
158 |
logger.warn("Stopping context '" + dataSourceName + "' due to errors reported in ServletContext"); |
|
159 |
webAppContext.stop(); |
|
160 |
} catch (Exception e) { |
|
161 |
logger.error(e); |
|
162 |
} |
|
163 |
} |
|
164 |
setStatus(Status.started); |
|
165 |
} |
|
166 |
|
|
167 |
@Override |
|
168 |
public void lifeCycleFailure(LifeCycle event, Throwable cause) { |
|
169 |
logger.error("lifeCycleFailure"); |
|
170 |
if(!getStatus().equals(Status.removed)){ |
|
171 |
// never override Status.removed !!! |
|
172 |
setStatus(Status.error); |
|
173 |
} |
|
174 |
getProblems().add(cause.getMessage()); |
|
175 |
} |
|
176 |
|
|
177 |
public void releaseWebAppContext() { |
|
178 |
webAppContext = null; |
|
179 |
} |
|
180 |
|
|
181 |
public boolean bindJndiDataSource() { |
|
182 |
try { |
|
183 |
Class<DataSource> dsCass = (Class<DataSource>) Thread.currentThread().getContextClassLoader().loadClass("com.mchange.v2.c3p0.ComboPooledDataSource"); |
|
184 |
DataSource datasource = dsCass.newInstance(); |
|
185 |
dsCass.getMethod("setDriverClass", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getDriverClass()}); |
|
186 |
dsCass.getMethod("setJdbcUrl", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getDataSourceUrl()}); |
|
187 |
dsCass.getMethod("setUser", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getUsername()}); |
|
188 |
dsCass.getMethod("setPassword", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getPassword()}); |
|
189 |
|
|
190 |
Connection connection = null; |
|
191 |
String sqlerror = null; |
|
192 |
try { |
|
193 |
connection = datasource.getConnection(); |
|
194 |
connection.close(); |
|
195 |
} catch (SQLException e) { |
|
196 |
sqlerror = e.getMessage() + "["+ e.getSQLState() + "]"; |
|
197 |
getProblems().add(sqlerror); |
|
198 |
setStatus(Status.error); |
|
199 |
if(connection != null){ |
|
200 |
try {connection.close();} catch (SQLException e1) { /* IGNORE */ } |
|
201 |
} |
|
202 |
logger.error(configuration.toString() + " has problem : "+ sqlerror ); |
|
203 |
} |
|
204 |
|
|
205 |
if(!onError()){ |
|
206 |
logger.info("binding jndi datasource at " + configuration.getJdbcJndiName() + " with " + configuration.getUsername() +"@"+ configuration.getDataSourceUrl()); |
|
207 |
jndiDataSource = new Resource(configuration.getJdbcJndiName(), datasource); |
|
208 |
return true; |
|
209 |
} |
|
210 |
|
|
211 |
} catch (IllegalArgumentException e) { |
|
212 |
logger.error(e); |
|
213 |
e.printStackTrace(); |
|
214 |
} catch (SecurityException e) { |
|
215 |
logger.error(e); |
|
216 |
} catch (ClassNotFoundException e) { |
|
217 |
logger.error(e); |
|
218 |
} catch (InstantiationException e) { |
|
219 |
logger.error(e); |
|
220 |
} catch (IllegalAccessException e) { |
|
221 |
logger.error(e); |
|
222 |
} catch (InvocationTargetException e) { |
|
223 |
logger.error(e); |
|
224 |
} catch (NoSuchMethodException e) { |
|
225 |
logger.error(e); |
|
226 |
} catch (NamingException e) { |
|
227 |
logger.error(e); |
|
228 |
} |
|
229 |
return false; |
|
230 |
} |
|
231 |
|
|
232 |
public void unbindJndiDataSource() { |
|
233 |
if(jndiDataSource != null){ |
|
234 |
jndiDataSource.release(); |
|
235 |
jndiDataSource = null; |
|
236 |
} |
|
237 |
} |
|
146 | 238 |
|
147 | 239 |
} |
Also available in: Unified diff
start, stop of instances and reload of configuration implemented, see #3471 (cdmlib-remote-webapp instances can be stopped / started individually)