1
|
package eu.etaxonomy.cdm.server.instance;
|
2
|
|
3
|
import java.lang.reflect.InvocationTargetException;
|
4
|
import java.sql.Connection;
|
5
|
import java.sql.SQLException;
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.List;
|
8
|
|
9
|
import javax.naming.NamingException;
|
10
|
import javax.sql.DataSource;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
import org.eclipse.jetty.plus.jndi.Resource;
|
14
|
import org.eclipse.jetty.server.handler.ContextHandler.Context;
|
15
|
import org.eclipse.jetty.util.component.LifeCycle;
|
16
|
import org.eclipse.jetty.util.component.LifeCycle.Listener;
|
17
|
import org.eclipse.jetty.webapp.WebAppContext;
|
18
|
|
19
|
|
20
|
public class CdmInstance implements Listener {
|
21
|
|
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
|
setProblems(null);
|
144
|
}
|
145
|
|
146
|
@SuppressWarnings("unchecked")
|
147
|
@Override
|
148
|
public void lifeCycleStarted(LifeCycle event) {
|
149
|
logger.info("lifeCycleStarted");
|
150
|
|
151
|
List<String> messages = getServletContextAttribute(webAppContext, SharedAttributes.ATTRIBUTE_ERROR_MESSAGES, List.class);
|
152
|
String dataSourceName = getServletContextAttribute(webAppContext, SharedAttributes.ATTRIBUTE_DATASOURCE_NAME, String.class);
|
153
|
|
154
|
if (messages != null && dataSourceName != null) {
|
155
|
// Problems with instance
|
156
|
Status errorStatus = Status.error;
|
157
|
for(String message : messages){
|
158
|
if(message.startsWith("Incompatible version")){
|
159
|
errorStatus = Status.incompatible_version;
|
160
|
break;
|
161
|
}
|
162
|
}
|
163
|
setStatus(errorStatus);
|
164
|
|
165
|
getProblems().addAll(messages);
|
166
|
|
167
|
try {
|
168
|
logger.warn("Stopping context '" + dataSourceName + "' due to errors reported in ServletContext");
|
169
|
webAppContext.stop();
|
170
|
setStatus(errorStatus);
|
171
|
} catch (Exception e) {
|
172
|
logger.error(e);
|
173
|
}
|
174
|
} else {
|
175
|
// Instance is OK
|
176
|
setStatus(Status.started);
|
177
|
}
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
public void lifeCycleFailure(LifeCycle event, Throwable cause) {
|
182
|
logger.error("lifeCycleFailure");
|
183
|
if(!getStatus().equals(Status.removed)){
|
184
|
// never override Status.removed !!!
|
185
|
setStatus(Status.error);
|
186
|
}
|
187
|
getProblems().add(cause.getMessage());
|
188
|
}
|
189
|
|
190
|
public void releaseWebAppContext() {
|
191
|
webAppContext = null;
|
192
|
}
|
193
|
|
194
|
public boolean bindJndiDataSource() {
|
195
|
try {
|
196
|
Class<DataSource> dsCass = (Class<DataSource>) Thread.currentThread().getContextClassLoader().loadClass("com.mchange.v2.c3p0.ComboPooledDataSource");
|
197
|
DataSource datasource = dsCass.newInstance();
|
198
|
dsCass.getMethod("setDriverClass", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getDriverClass()});
|
199
|
dsCass.getMethod("setJdbcUrl", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getDataSourceUrl()});
|
200
|
dsCass.getMethod("setUser", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getUsername()});
|
201
|
dsCass.getMethod("setPassword", new Class[] {String.class}).invoke(datasource, new Object[] {configuration.getPassword()});
|
202
|
|
203
|
Connection connection = null;
|
204
|
String sqlerror = null;
|
205
|
try {
|
206
|
connection = datasource.getConnection();
|
207
|
connection.close();
|
208
|
} catch (SQLException e) {
|
209
|
sqlerror = e.getMessage() + "["+ e.getSQLState() + "]";
|
210
|
getProblems().add(sqlerror);
|
211
|
setStatus(Status.error);
|
212
|
if(connection != null){
|
213
|
try {connection.close();} catch (SQLException e1) { /* IGNORE */ }
|
214
|
}
|
215
|
logger.error(configuration.toString() + " has problem : "+ sqlerror );
|
216
|
}
|
217
|
|
218
|
if(!onError()){
|
219
|
logger.info("binding jndi datasource at " + configuration.getJdbcJndiName() + " with " + configuration.getUsername() +"@"+ configuration.getDataSourceUrl());
|
220
|
jndiDataSource = new Resource(configuration.getJdbcJndiName(), datasource);
|
221
|
return true;
|
222
|
}
|
223
|
|
224
|
} catch (IllegalArgumentException e) {
|
225
|
logger.error(e);
|
226
|
e.printStackTrace();
|
227
|
} catch (SecurityException e) {
|
228
|
logger.error(e);
|
229
|
} catch (ClassNotFoundException e) {
|
230
|
logger.error(e);
|
231
|
} catch (InstantiationException e) {
|
232
|
logger.error(e);
|
233
|
} catch (IllegalAccessException e) {
|
234
|
logger.error(e);
|
235
|
} catch (InvocationTargetException e) {
|
236
|
logger.error(e);
|
237
|
} catch (NoSuchMethodException e) {
|
238
|
logger.error(e);
|
239
|
} catch (NamingException e) {
|
240
|
logger.error(e);
|
241
|
}
|
242
|
return false;
|
243
|
}
|
244
|
|
245
|
public void unbindJndiDataSource() {
|
246
|
if(jndiDataSource != null){
|
247
|
jndiDataSource.release();
|
248
|
jndiDataSource = null;
|
249
|
}
|
250
|
}
|
251
|
|
252
|
}
|