Revision 25876144
Added by Andreas Kohlbecker almost 13 years ago
src/main/java/eu/etaxonomy/cdm/server/Bootloader.java | ||
---|---|---|
1 | 1 |
// $Id$ |
2 | 2 |
/** |
3 | 3 |
* Copyright (C) 2009 EDIT |
4 |
* European Distributed Institute of Taxonomy
|
|
4 |
* European Distributed Institute of Taxonomy |
|
5 | 5 |
* http://www.e-taxonomy.eu |
6 |
*
|
|
6 |
* |
|
7 | 7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
8 | 8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
9 | 9 |
*/ |
... | ... | |
16 | 16 |
import static eu.etaxonomy.cdm.server.CommandOptions.JMX; |
17 | 17 |
import static eu.etaxonomy.cdm.server.CommandOptions.WEBAPP; |
18 | 18 |
import static eu.etaxonomy.cdm.server.CommandOptions.WIN32SERVICE; |
19 |
import static eu.etaxonomy.cdm.server.CommandOptions.LOG_DIR; |
|
19 | 20 |
|
20 | 21 |
import java.io.File; |
21 | 22 |
import java.io.FileNotFoundException; |
... | ... | |
61 | 62 |
|
62 | 63 |
/** |
63 | 64 |
* A bootstrap class for starting Jetty Runner using an embedded war. |
64 |
*
|
|
65 |
* Recommended start options for the java virtual machine:
|
|
65 |
* |
|
66 |
* Recommended start options for the java virtual machine: |
|
66 | 67 |
* <pre> |
67 | 68 |
* -Xmx1024M |
68 |
*
|
|
69 |
* -XX:PermSize=128m
|
|
69 |
* |
|
70 |
* -XX:PermSize=128m |
|
70 | 71 |
* -XX:MaxPermSize=192m |
71 |
*
|
|
72 |
* -XX:+UseConcMarkSweepGC
|
|
72 |
* |
|
73 |
* -XX:+UseConcMarkSweepGC |
|
73 | 74 |
* -XX:+CMSClassUnloadingEnabled |
74 | 75 |
* -XX:+CMSPermGenSweepingEnabled |
75 | 76 |
* </pre> |
76 |
*
|
|
77 |
* |
|
77 | 78 |
* @version $Revision$ |
78 | 79 |
*/ |
79 | 80 |
public final class Bootloader { |
80 |
/**
|
|
81 |
*
|
|
82 |
*/
|
|
83 |
private static final String VERSION_PROPERTIES_FILE = "version.properties";
|
|
84 |
|
|
85 |
//private static final String DEFAULT_WARFILE = "target/";
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
/**
|
|
90 |
* @author a.kohlbecker
|
|
91 |
* @date 03.02.2011
|
|
92 |
*
|
|
93 |
*/
|
|
94 |
private class WebAppContextListener implements Listener {
|
|
95 |
|
|
96 |
WebAppContext cdmWebappContext;
|
|
97 |
/**
|
|
98 |
* @param cdmWebappContext
|
|
99 |
*/
|
|
100 |
public WebAppContextListener(WebAppContext cdmWebappContext) {
|
|
101 |
this.cdmWebappContext = cdmWebappContext;
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
public void lifeCycleStopping(LifeCycle event) {
|
|
106 |
logger.info("lifeCycleStopping");
|
|
107 |
}
|
|
108 |
|
|
109 |
@Override
|
|
110 |
public void lifeCycleStopped(LifeCycle event) {
|
|
111 |
logger.info("lifeCycleStopped");
|
|
112 |
|
|
113 |
}
|
|
114 |
|
|
115 |
@Override
|
|
116 |
public void lifeCycleStarting(LifeCycle event) {
|
|
117 |
logger.info("lifeCycleStarting");
|
|
118 |
}
|
|
119 |
|
|
120 |
@SuppressWarnings("unchecked")
|
|
121 |
@Override
|
|
122 |
public void lifeCycleStarted(LifeCycle event) {
|
|
123 |
logger.info("lifeCycleStarted");
|
|
124 |
|
|
125 |
List<String> messages = getServletContextAttribute(cdmWebappContext, ATTRIBUTE_ERROR_MESSAGES, List.class);
|
|
126 |
String dataSourceName = getServletContextAttribute(cdmWebappContext, ATTRIBUTE_DATASOURCE_NAME, String.class);
|
|
127 |
|
|
128 |
if(messages != null && dataSourceName != null){
|
|
129 |
CdmInstanceProperties configAndStatus = findConfigAndStatusFor(dataSourceName);
|
|
130 |
configAndStatus.getProblems().addAll(messages);
|
|
131 |
configAndStatus.setStatus(Status.error);
|
|
132 |
try {
|
|
133 |
logger.warn("Stopping context '" + dataSourceName + "' due to errors reported in ServletContext");
|
|
134 |
cdmWebappContext.stop();
|
|
135 |
} catch (Exception e) {
|
|
136 |
logger.error(e);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
|
|
142 |
@Override
|
|
143 |
public void lifeCycleFailure(LifeCycle event, Throwable cause) {
|
|
144 |
logger.error("lifeCycleFailure");
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
private static final Logger logger = Logger.getLogger(Bootloader.class);
|
|
149 |
|
|
150 |
private static final String DATASOURCE_BEANDEF_FILE = "datasources.xml";
|
|
151 |
private static final String REALM_PROPERTIES_FILE = "cdm-server-realm.properties";
|
|
152 |
|
|
153 |
private static final String USERHOME_CDM_LIBRARY_PATH = System.getProperty("user.home")+File.separator+".cdmLibrary"+File.separator;
|
|
154 |
private static final String TMP_PATH = USERHOME_CDM_LIBRARY_PATH + "server" + File.separator;
|
|
155 |
private static final String LOG_PATH = USERHOME_CDM_LIBRARY_PATH + "log" + File.separator;
|
|
156 |
|
|
157 |
private static final String APPLICATION_NAME = "CDM Server";
|
|
81 |
/**
|
|
82 |
*
|
|
83 |
*/
|
|
84 |
private static final String VERSION_PROPERTIES_FILE = "version.properties";
|
|
85 |
|
|
86 |
//private static final String DEFAULT_WARFILE = "target/";
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
/**
|
|
91 |
* @author a.kohlbecker
|
|
92 |
* @date 03.02.2011
|
|
93 |
*
|
|
94 |
*/
|
|
95 |
private class WebAppContextListener implements Listener {
|
|
96 |
|
|
97 |
WebAppContext cdmWebappContext;
|
|
98 |
/**
|
|
99 |
* @param cdmWebappContext
|
|
100 |
*/
|
|
101 |
public WebAppContextListener(WebAppContext cdmWebappContext) {
|
|
102 |
this.cdmWebappContext = cdmWebappContext;
|
|
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(cdmWebappContext, ATTRIBUTE_ERROR_MESSAGES, List.class);
|
|
127 |
String dataSourceName = getServletContextAttribute(cdmWebappContext, ATTRIBUTE_DATASOURCE_NAME, String.class);
|
|
128 |
|
|
129 |
if(messages != null && dataSourceName != null){
|
|
130 |
CdmInstanceProperties configAndStatus = findConfigAndStatusFor(dataSourceName);
|
|
131 |
configAndStatus.getProblems().addAll(messages);
|
|
132 |
configAndStatus.setStatus(Status.error);
|
|
133 |
try {
|
|
134 |
logger.warn("Stopping context '" + dataSourceName + "' due to errors reported in ServletContext");
|
|
135 |
cdmWebappContext.stop();
|
|
136 |
} catch (Exception e) {
|
|
137 |
logger.error(e);
|
|
138 |
}
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
|
|
143 |
@Override
|
|
144 |
public void lifeCycleFailure(LifeCycle event, Throwable cause) {
|
|
145 |
logger.error("lifeCycleFailure");
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
private static final Logger logger = Logger.getLogger(Bootloader.class);
|
|
150 |
|
|
151 |
private static final String DATASOURCE_BEANDEF_FILE = "datasources.xml";
|
|
152 |
private static final String REALM_PROPERTIES_FILE = "cdm-server-realm.properties";
|
|
153 |
|
|
154 |
private static final String USERHOME_CDM_LIBRARY_PATH = System.getProperty("user.home")+File.separator+".cdmLibrary"+File.separator;
|
|
155 |
private static final String TMP_PATH = USERHOME_CDM_LIBRARY_PATH + "server" + File.separator;
|
|
156 |
private static final String LOG_PATH = USERHOME_CDM_LIBRARY_PATH + "log" + File.separator;
|
|
157 |
|
|
158 |
private static final String APPLICATION_NAME = "CDM Server";
|
|
158 | 159 |
private static final String WAR_POSTFIX = ".war"; |
159 |
|
|
160 |
|
|
160 | 161 |
private static final String CDMLIB_REMOTE_WEBAPP = "cdmlib-remote-webapp"; |
161 | 162 |
private static final String CDMLIB_REMOTE_WEBAPP_VERSION = "cdmlib-remote-webapp.version"; |
162 | 163 |
|
163 | 164 |
private static final String DEFAULT_WEBAPP_WAR_NAME = "default-webapp"; |
164 | 165 |
private static final File DEFAULT_WEBAPP_TEMP_FOLDER = new File(TMP_PATH + DEFAULT_WEBAPP_WAR_NAME); |
165 | 166 |
private static final File CDM_WEBAPP_TEMP_FOLDER = new File(TMP_PATH + CDMLIB_REMOTE_WEBAPP); |
166 |
|
|
167 |
|
|
167 | 168 |
private static final String ATTRIBUTE_JDBC_JNDI_NAME = "cdm.jdbcJndiName"; |
168 | 169 |
private static final String ATTRIBUTE_DATASOURCE_NAME = "cdm.datasource"; |
169 | 170 |
private static final String ATTRIBUTE_CDM_LOGFILE = "cdm.logfile"; |
... | ... | |
172 | 173 |
*/ |
173 | 174 |
private static final String ATTRIBUTE_ERROR_MESSAGES = "cdm.errorMessages"; |
174 | 175 |
|
175 |
|
|
176 |
|
|
176 | 177 |
// memory requirements |
177 | 178 |
private static final long MB = 1024 * 1024; |
178 | 179 |
private static final long PERM_GEN_SPACE_PER_INSTANCE = 64 * MB; |
179 | 180 |
private static final long HEAP_PER_INSTANCE = 150 * MB; |
180 |
|
|
181 |
|
|
181 | 182 |
private static final int KB = 1024; |
182 |
|
|
183 |
|
|
183 |
|
|
184 |
|
|
184 | 185 |
private Set<CdmInstanceProperties> configAndStatusSet = null; |
185 |
|
|
186 |
|
|
186 | 187 |
public Set<CdmInstanceProperties> getConfigAndStatus() { |
187 |
return configAndStatusSet;
|
|
188 |
}
|
|
188 |
return configAndStatusSet;
|
|
189 |
}
|
|
189 | 190 |
|
190 |
private File cdmRemoteWebAppFile = null;
|
|
191 |
private File cdmRemoteWebAppFile = null;
|
|
191 | 192 |
private File defaultWebAppFile = null; |
192 |
|
|
193 |
|
|
194 |
private String logPath = null; |
|
195 |
|
|
193 | 196 |
private Server server = null; |
194 | 197 |
private ContextHandlerCollection contexts = new ContextHandlerCollection(); |
195 |
|
|
198 |
|
|
196 | 199 |
private CommandLine cmdLine; |
197 |
|
|
200 |
|
|
198 | 201 |
/* thread save singleton implementation */ |
199 | 202 |
|
200 |
private static Bootloader instance = new Bootloader();
|
|
203 |
private static Bootloader instance = new Bootloader();
|
|
201 | 204 |
|
202 | 205 |
private Bootloader() {} |
203 |
|
|
206 |
|
|
207 |
/** |
|
208 |
* @return the thread save singleton instance of the Bootloader |
|
209 |
*/ |
|
204 | 210 |
public synchronized static Bootloader getBootloader(){ |
205 |
return instance;
|
|
211 |
return instance;
|
|
206 | 212 |
} |
207 |
|
|
213 |
|
|
208 | 214 |
/* end of singleton implementation */ |
209 |
|
|
215 |
|
|
210 | 216 |
private Set<CdmInstanceProperties> loadDataSources(){ |
211 |
if(configAndStatusSet == null){
|
|
212 |
File datasourcesFile = new File(USERHOME_CDM_LIBRARY_PATH, DATASOURCE_BEANDEF_FILE);
|
|
213 |
configAndStatusSet = DataSourcePropertyParser.parseDataSourceConfigs(datasourcesFile);
|
|
214 |
logger.info("cdm server instance names loaded: "+ configAndStatusSet.toString());
|
|
215 |
}
|
|
216 |
return configAndStatusSet;
|
|
217 |
if(configAndStatusSet == null){
|
|
218 |
File datasourcesFile = new File(USERHOME_CDM_LIBRARY_PATH, DATASOURCE_BEANDEF_FILE);
|
|
219 |
configAndStatusSet = DataSourcePropertyParser.parseDataSourceConfigs(datasourcesFile);
|
|
220 |
logger.info("cdm server instance names loaded: "+ configAndStatusSet.toString());
|
|
221 |
}
|
|
222 |
return configAndStatusSet;
|
|
217 | 223 |
} |
218 | 224 |
|
219 | 225 |
public int writeStreamTo(final InputStream input, final OutputStream output, int bufferSize) throws IOException { |
... | ... | |
229 | 235 |
return answer; |
230 | 236 |
} |
231 | 237 |
|
232 |
private boolean bindJndiDataSource(CdmInstanceProperties conf) {
|
|
233 |
try {
|
|
234 |
Class<DataSource> dsCass = (Class<DataSource>) Thread.currentThread().getContextClassLoader().loadClass("com.mchange.v2.c3p0.ComboPooledDataSource");
|
|
235 |
DataSource datasource = dsCass.newInstance();
|
|
236 |
dsCass.getMethod("setDriverClass", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getDriverClass()});
|
|
237 |
dsCass.getMethod("setJdbcUrl", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getUrl()});
|
|
238 |
dsCass.getMethod("setUser", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getUsername()});
|
|
239 |
dsCass.getMethod("setPassword", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getPassword()});
|
|
240 |
|
|
241 |
Connection connection = null;
|
|
242 |
String sqlerror = null;
|
|
243 |
try {
|
|
244 |
connection = datasource.getConnection();
|
|
245 |
connection.close();
|
|
246 |
} catch (SQLException e) {
|
|
247 |
sqlerror = e.getMessage() + "["+ e.getSQLState() + "]";
|
|
248 |
conf.getProblems().add(sqlerror);
|
|
249 |
if(connection != null){
|
|
250 |
try {connection.close();} catch (SQLException e1) { /* IGNORE */ }
|
|
251 |
}
|
|
252 |
logger.error(conf.toString() + " has problem : "+ sqlerror );
|
|
253 |
}
|
|
254 |
|
|
255 |
if(!conf.hasProblems()){
|
|
256 |
logger.info("binding jndi datasource at " + conf.getJdbcJndiName() + " with "+conf.getUsername() +"@"+ conf.getUrl());
|
|
257 |
org.eclipse.jetty.plus.jndi.Resource jdbcResource = new org.eclipse.jetty.plus.jndi.Resource(conf.getJdbcJndiName(), datasource);
|
|
258 |
return true;
|
|
259 |
}
|
|
260 |
|
|
261 |
} catch (IllegalArgumentException e) {
|
|
262 |
logger.error(e);
|
|
263 |
e.printStackTrace();
|
|
264 |
} catch (SecurityException e) {
|
|
265 |
logger.error(e);
|
|
266 |
} catch (ClassNotFoundException e) {
|
|
267 |
logger.error(e);
|
|
268 |
} catch (InstantiationException e) {
|
|
269 |
logger.error(e);
|
|
270 |
} catch (IllegalAccessException e) {
|
|
271 |
logger.error(e);
|
|
272 |
} catch (InvocationTargetException e) {
|
|
273 |
logger.error(e);
|
|
274 |
} catch (NoSuchMethodException e) {
|
|
275 |
logger.error(e);
|
|
276 |
} catch (NamingException e) {
|
|
277 |
logger.error(e);
|
|
278 |
}
|
|
279 |
return false;
|
|
280 |
}
|
|
281 |
|
|
282 |
private void parseCommandOptions(String[] args) throws ParseException {
|
|
283 |
CommandLineParser parser = new GnuParser();
|
|
284 |
cmdLine = parser.parse( CommandOptions.getOptions(), args );
|
|
285 |
|
|
286 |
// print the help message
|
|
287 |
if(cmdLine.hasOption(HELP.getOpt())){
|
|
288 |
HelpFormatter formatter = new HelpFormatter();
|
|
289 |
formatter.setWidth(200);
|
|
290 |
formatter.printHelp( "java .. ", CommandOptions.getOptions() );
|
|
291 |
System.exit(0);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
|
|
295 |
|
|
296 |
private File extractWar(String warName) throws IOException, FileNotFoundException {
|
|
297 |
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
298 |
String warFileName = warName + WAR_POSTFIX;
|
|
299 |
|
|
300 |
// 1. find in classpath
|
|
301 |
URL resource = classLoader.getResource(warFileName);
|
|
302 |
if (resource == null) {
|
|
303 |
logger.error("Could not find the " + warFileName + " on classpath!");
|
|
304 |
|
|
305 |
File pomxml = new File("pom.xml");
|
|
306 |
if(pomxml.exists()){
|
|
307 |
// 2. try finding in target folder of maven project
|
|
308 |
File warFile = new File("target" + File.separator + warFileName);
|
|
309 |
logger.debug("looging for war file at " + warFile.getAbsolutePath());
|
|
310 |
if (warFile.canRead()) {
|
|
311 |
resource = warFile.toURI().toURL();
|
|
312 |
} else {
|
|
313 |
logger.error("Also could not find the " + warFileName + " in maven project, try excuting 'mvn install'");
|
|
314 |
}
|
|
315 |
}
|
|
316 |
}
|
|
317 |
|
|
318 |
if (resource == null) {
|
|
319 |
// no way finding the war file :-(
|
|
320 |
System.exit(1);
|
|
321 |
}
|
|
322 |
|
|
323 |
|
|
324 |
File warFile = new File(TMP_PATH, warName + "-" + WAR_POSTFIX);
|
|
325 |
logger.info("Extracting " + warFileName + " to " + warFile + " ...");
|
|
326 |
|
|
327 |
writeStreamTo(resource.openStream(), new FileOutputStream(warFile), 8 * KB);
|
|
328 |
|
|
329 |
logger.info("Extracted " + warFileName);
|
|
330 |
return warFile;
|
|
331 |
}
|
|
332 |
|
|
333 |
|
|
334 |
/**
|
|
335 |
* MAIN METHOD
|
|
336 |
*
|
|
337 |
* @param args
|
|
338 |
* @throws Exception
|
|
339 |
*/
|
|
340 |
public static void main(String[] args) throws Exception {
|
|
341 |
|
|
342 |
Bootloader bootloader = Bootloader.getBootloader();
|
|
343 |
|
|
344 |
bootloader.parseCommandOptions(args);
|
|
345 |
|
|
346 |
bootloader.startServer();
|
|
238 |
private boolean bindJndiDataSource(CdmInstanceProperties conf) {
|
|
239 |
try {
|
|
240 |
Class<DataSource> dsCass = (Class<DataSource>) Thread.currentThread().getContextClassLoader().loadClass("com.mchange.v2.c3p0.ComboPooledDataSource");
|
|
241 |
DataSource datasource = dsCass.newInstance();
|
|
242 |
dsCass.getMethod("setDriverClass", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getDriverClass()});
|
|
243 |
dsCass.getMethod("setJdbcUrl", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getUrl()});
|
|
244 |
dsCass.getMethod("setUser", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getUsername()});
|
|
245 |
dsCass.getMethod("setPassword", new Class[] {String.class}).invoke(datasource, new Object[] {conf.getPassword()});
|
|
246 |
|
|
247 |
Connection connection = null;
|
|
248 |
String sqlerror = null;
|
|
249 |
try {
|
|
250 |
connection = datasource.getConnection();
|
|
251 |
connection.close();
|
|
252 |
} catch (SQLException e) {
|
|
253 |
sqlerror = e.getMessage() + "["+ e.getSQLState() + "]";
|
|
254 |
conf.getProblems().add(sqlerror);
|
|
255 |
if(connection != null){
|
|
256 |
try {connection.close();} catch (SQLException e1) { /* IGNORE */ }
|
|
257 |
}
|
|
258 |
logger.error(conf.toString() + " has problem : "+ sqlerror );
|
|
259 |
}
|
|
260 |
|
|
261 |
if(!conf.hasProblems()){
|
|
262 |
logger.info("binding jndi datasource at " + conf.getJdbcJndiName() + " with "+conf.getUsername() +"@"+ conf.getUrl());
|
|
263 |
org.eclipse.jetty.plus.jndi.Resource jdbcResource = new org.eclipse.jetty.plus.jndi.Resource(conf.getJdbcJndiName(), datasource);
|
|
264 |
return true;
|
|
265 |
}
|
|
266 |
|
|
267 |
} catch (IllegalArgumentException e) {
|
|
268 |
logger.error(e);
|
|
269 |
e.printStackTrace();
|
|
270 |
} catch (SecurityException e) {
|
|
271 |
logger.error(e);
|
|
272 |
} catch (ClassNotFoundException e) {
|
|
273 |
logger.error(e);
|
|
274 |
} catch (InstantiationException e) {
|
|
275 |
logger.error(e);
|
|
276 |
} catch (IllegalAccessException e) {
|
|
277 |
logger.error(e);
|
|
278 |
} catch (InvocationTargetException e) {
|
|
279 |
logger.error(e);
|
|
280 |
} catch (NoSuchMethodException e) {
|
|
281 |
logger.error(e);
|
|
282 |
} catch (NamingException e) {
|
|
283 |
logger.error(e);
|
|
284 |
}
|
|
285 |
return false;
|
|
286 |
}
|
|
287 |
|
|
288 |
public void parseCommandOptions(String[] args) throws ParseException {
|
|
289 |
CommandLineParser parser = new GnuParser();
|
|
290 |
cmdLine = parser.parse( CommandOptions.getOptions(), args );
|
|
291 |
|
|
292 |
// print the help message
|
|
293 |
if(cmdLine.hasOption(HELP.getOpt())){
|
|
294 |
HelpFormatter formatter = new HelpFormatter();
|
|
295 |
formatter.setWidth(200);
|
|
296 |
formatter.printHelp( "java .. ", CommandOptions.getOptions() );
|
|
297 |
System.exit(0);
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
|
|
302 |
private File extractWar(String warName) throws IOException, FileNotFoundException {
|
|
303 |
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
|
|
304 |
String warFileName = warName + WAR_POSTFIX;
|
|
305 |
|
|
306 |
// 1. find in classpath
|
|
307 |
URL resource = classLoader.getResource(warFileName);
|
|
308 |
if (resource == null) {
|
|
309 |
logger.error("Could not find the " + warFileName + " on classpath!");
|
|
310 |
|
|
311 |
File pomxml = new File("pom.xml");
|
|
312 |
if(pomxml.exists()){
|
|
313 |
// 2. try finding in target folder of maven project
|
|
314 |
File warFile = new File("target" + File.separator + warFileName);
|
|
315 |
logger.debug("looging for war file at " + warFile.getAbsolutePath());
|
|
316 |
if (warFile.canRead()) {
|
|
317 |
resource = warFile.toURI().toURL();
|
|
318 |
} else {
|
|
319 |
logger.error("Also could not find the " + warFileName + " in maven project, try excuting 'mvn install'");
|
|
320 |
}
|
|
321 |
}
|
|
322 |
}
|
|
323 |
|
|
324 |
if (resource == null) {
|
|
325 |
// no way finding the war file :-(
|
|
326 |
System.exit(1);
|
|
327 |
}
|
|
328 |
|
|
329 |
|
|
330 |
File warFile = new File(TMP_PATH, warName + "-" + WAR_POSTFIX);
|
|
331 |
logger.info("Extracting " + warFileName + " to " + warFile + " ...");
|
|
332 |
|
|
333 |
writeStreamTo(resource.openStream(), new FileOutputStream(warFile), 8 * KB);
|
|
334 |
|
|
335 |
logger.info("Extracted " + warFileName);
|
|
336 |
return warFile;
|
|
337 |
}
|
|
338 |
|
|
339 |
|
|
340 |
/**
|
|
341 |
* MAIN METHOD
|
|
342 |
*
|
|
343 |
* @param args
|
|
344 |
* @throws Exception
|
|
345 |
*/
|
|
346 |
public static void main(String[] args) throws Exception {
|
|
347 |
|
|
348 |
Bootloader bootloader = Bootloader.getBootloader();
|
|
349 |
|
|
350 |
bootloader.parseCommandOptions(args);
|
|
351 |
|
|
352 |
bootloader.startServer();
|
|
347 | 353 |
} |
348 | 354 |
|
349 |
private void startServer() throws IOException, |
|
350 |
FileNotFoundException, Exception, InterruptedException { |
|
351 |
|
|
352 |
|
|
353 |
//assure LOG_PATH exists |
|
354 |
File logPath = new File(LOG_PATH); |
|
355 |
if(!logPath.exists()){ |
|
356 |
FileUtils.forceMkdir(new File(LOG_PATH)); |
|
357 |
} |
|
358 |
|
|
359 |
//append logger |
|
360 |
configureFileLogger(); |
|
361 |
|
|
362 |
logger.info("Starting "+APPLICATION_NAME); |
|
363 |
logger.info("Using " + System.getProperty("user.home") + " as home directory. Can be specified by -Duser.home=<FOLDER>"); |
|
364 |
|
|
365 |
//assure TMP_PATH exists and clean it up |
|
366 |
File tempDir = new File(TMP_PATH); |
|
367 |
if(!tempDir.exists() && !tempDir.mkdirs()){ |
|
368 |
logger.error("Error creating temporary directory for webapplications " + tempDir.getAbsolutePath()); |
|
369 |
System.exit(-1); |
|
370 |
} else { |
|
371 |
if(FileUtils.deleteQuietly(tempDir)){ |
|
372 |
tempDir.mkdirs(); |
|
373 |
logger.info("Old webapplications successfully cleared"); |
|
374 |
} |
|
375 |
} |
|
376 |
tempDir = null; |
|
377 |
|
|
378 |
|
|
379 |
// WARFILE |
|
380 |
if(cmdLine.hasOption(WEBAPP.getOpt())){ |
|
381 |
cdmRemoteWebAppFile = new File(cmdLine.getOptionValue(WEBAPP.getOpt())); |
|
382 |
if(cdmRemoteWebAppFile.isDirectory()){ |
|
383 |
logger.info("using user defined web application folder: " + cdmRemoteWebAppFile.getAbsolutePath()); |
|
384 |
} else { |
|
385 |
logger.info("using user defined warfile: " + cdmRemoteWebAppFile.getAbsolutePath()); |
|
386 |
} |
|
387 |
if(isRunningFromCdmRemoteWebAppSource()){ |
|
388 |
//TODO check if all local paths are valid !!!! |
|
389 |
defaultWebAppFile = new File("./src/main/webapp"); |
|
390 |
|
|
391 |
} else { |
|
392 |
defaultWebAppFile = extractWar(DEFAULT_WEBAPP_WAR_NAME); |
|
393 |
} |
|
394 |
} else { |
|
395 |
// read version number |
|
396 |
String version = readCdmRemoteVersion(); |
|
397 |
|
|
398 |
cdmRemoteWebAppFile = extractWar(CDMLIB_REMOTE_WEBAPP + "-" + version); |
|
399 |
defaultWebAppFile = extractWar(DEFAULT_WEBAPP_WAR_NAME); |
|
400 |
} |
|
401 |
|
|
402 |
// HTTP Port |
|
403 |
int httpPort = 8080; |
|
404 |
if(cmdLine.hasOption(HTTP_PORT.getOpt())){ |
|
405 |
try { |
|
406 |
httpPort = Integer.parseInt(cmdLine.getOptionValue(HTTP_PORT.getOpt())); |
|
407 |
logger.info(HTTP_PORT.getOpt()+" set to "+cmdLine.getOptionValue(HTTP_PORT.getOpt())); |
|
408 |
} catch (NumberFormatException e) { |
|
409 |
logger.error("Supplied portnumber is not an integer"); |
|
410 |
System.exit(-1); |
|
411 |
} |
|
412 |
} |
|
413 |
|
|
414 |
if(cmdLine.hasOption(DATASOURCES_FILE.getOpt())){ |
|
415 |
logger.error(DATASOURCES_FILE.getOpt() + " NOT JET IMPLEMENTED!!!"); |
|
416 |
} |
|
417 |
|
|
418 |
loadDataSources(); |
|
419 |
|
|
420 |
verifyMemoryRequirements(); |
|
421 |
|
|
422 |
|
|
423 |
server = new Server(httpPort); |
|
424 |
|
|
425 |
// JMX support |
|
426 |
if(cmdLine.hasOption(JMX.getOpt())){ |
|
427 |
logger.info("adding JMX support ..."); |
|
428 |
MBeanContainer mBeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); |
|
429 |
server.getContainer().addEventListener(mBeanContainer); |
|
430 |
mBeanContainer.addBean(Log.getLog()); |
|
431 |
mBeanContainer.start(); |
|
432 |
} |
|
433 |
|
|
434 |
if(cmdLine.hasOption(WIN32SERVICE.getOpt())){ |
|
435 |
Win32Service win32Service = new Win32Service(); |
|
436 |
win32Service.setServer(server); |
|
437 |
server.setStopAtShutdown(true); |
|
438 |
server.addBean(win32Service); |
|
439 |
} |
|
440 |
|
|
441 |
// add servelet contexts |
|
442 |
|
|
443 |
|
|
444 |
// |
|
445 |
// 1. default context |
|
446 |
// |
|
447 |
logger.info("preparing default WebAppContext"); |
|
448 |
WebAppContext defaultWebappContext = new WebAppContext(); |
|
449 |
|
|
450 |
setWebApp(defaultWebappContext, defaultWebAppFile); |
|
355 |
|
|
356 |
|
|
357 |
public void startServer() throws IOException, |
|
358 |
FileNotFoundException, Exception, InterruptedException { |
|
359 |
|
|
360 |
if(cmdLine.hasOption(LOG_DIR.getOpt())){ |
|
361 |
logPath = cmdLine.getOptionValue(LOG_DIR.getOpt()); |
|
362 |
} else { |
|
363 |
logPath = LOG_PATH; |
|
364 |
} |
|
365 |
|
|
366 |
|
|
367 |
//assure LOG_PATH exists |
|
368 |
File logPathFile = new File(logPath); |
|
369 |
if(!logPathFile.exists()){ |
|
370 |
FileUtils.forceMkdir(new File(logPath)); |
|
371 |
} |
|
372 |
|
|
373 |
//append logger |
|
374 |
configureFileLogger(); |
|
375 |
|
|
376 |
logger.info("Starting "+APPLICATION_NAME); |
|
377 |
logger.info("Using " + System.getProperty("user.home") + " as home directory. Can be specified by -Duser.home=<FOLDER>"); |
|
378 |
|
|
379 |
//assure TMP_PATH exists and clean it up |
|
380 |
File tempDir = new File(TMP_PATH); |
|
381 |
if(!tempDir.exists() && !tempDir.mkdirs()){ |
|
382 |
logger.error("Error creating temporary directory for webapplications " + tempDir.getAbsolutePath()); |
|
383 |
System.exit(-1); |
|
384 |
} else { |
|
385 |
if(FileUtils.deleteQuietly(tempDir)){ |
|
386 |
tempDir.mkdirs(); |
|
387 |
logger.info("Old webapplications successfully cleared"); |
|
388 |
} |
|
389 |
} |
|
390 |
tempDir = null; |
|
391 |
|
|
392 |
|
|
393 |
// WARFILE |
|
394 |
if(cmdLine.hasOption(WEBAPP.getOpt())){ |
|
395 |
cdmRemoteWebAppFile = new File(cmdLine.getOptionValue(WEBAPP.getOpt())); |
|
396 |
if(cdmRemoteWebAppFile.isDirectory()){ |
|
397 |
logger.info("using user defined web application folder: " + cdmRemoteWebAppFile.getAbsolutePath()); |
|
398 |
} else { |
|
399 |
logger.info("using user defined warfile: " + cdmRemoteWebAppFile.getAbsolutePath()); |
|
400 |
} |
|
401 |
if(isRunningFromCdmRemoteWebAppSource()){ |
|
402 |
//TODO check if all local paths are valid !!!! |
|
403 |
defaultWebAppFile = new File("./src/main/webapp"); |
|
404 |
|
|
405 |
} else { |
|
406 |
defaultWebAppFile = extractWar(DEFAULT_WEBAPP_WAR_NAME); |
|
407 |
} |
|
408 |
} else { |
|
409 |
// read version number |
|
410 |
String version = readCdmRemoteVersion(); |
|
411 |
|
|
412 |
cdmRemoteWebAppFile = extractWar(CDMLIB_REMOTE_WEBAPP + "-" + version); |
|
413 |
defaultWebAppFile = extractWar(DEFAULT_WEBAPP_WAR_NAME); |
|
414 |
} |
|
415 |
|
|
416 |
// HTTP Port |
|
417 |
int httpPort = 8080; |
|
418 |
if(cmdLine.hasOption(HTTP_PORT.getOpt())){ |
|
419 |
try { |
|
420 |
httpPort = Integer.parseInt(cmdLine.getOptionValue(HTTP_PORT.getOpt())); |
|
421 |
logger.info(HTTP_PORT.getOpt()+" set to "+cmdLine.getOptionValue(HTTP_PORT.getOpt())); |
|
422 |
} catch (NumberFormatException e) { |
|
423 |
logger.error("Supplied portnumber is not an integer"); |
|
424 |
System.exit(-1); |
|
425 |
} |
|
426 |
} |
|
427 |
|
|
428 |
if(cmdLine.hasOption(DATASOURCES_FILE.getOpt())){ |
|
429 |
logger.error(DATASOURCES_FILE.getOpt() + " NOT JET IMPLEMENTED!!!"); |
|
430 |
} |
|
431 |
|
|
432 |
loadDataSources(); |
|
433 |
|
|
434 |
verifyMemoryRequirements(); |
|
435 |
|
|
436 |
|
|
437 |
server = new Server(httpPort); |
|
438 |
|
|
439 |
// JMX support |
|
440 |
if(cmdLine.hasOption(JMX.getOpt())){ |
|
441 |
logger.info("adding JMX support ..."); |
|
442 |
MBeanContainer mBeanContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer()); |
|
443 |
server.getContainer().addEventListener(mBeanContainer); |
|
444 |
mBeanContainer.addBean(Log.getLog()); |
|
445 |
mBeanContainer.start(); |
|
446 |
} |
|
447 |
|
|
448 |
if(cmdLine.hasOption(WIN32SERVICE.getOpt())){ |
|
449 |
Win32Service win32Service = new Win32Service(); |
|
450 |
win32Service.setServer(server); |
|
451 |
server.setStopAtShutdown(true); |
|
452 |
server.addBean(win32Service); |
|
453 |
} |
|
454 |
|
|
455 |
// add servelet contexts |
|
456 |
|
|
457 |
|
|
458 |
// |
|
459 |
// 1. default context |
|
460 |
// |
|
461 |
logger.info("preparing default WebAppContext"); |
|
462 |
WebAppContext defaultWebappContext = new WebAppContext(); |
|
463 |
|
|
464 |
setWebApp(defaultWebappContext, defaultWebAppFile); |
|
451 | 465 |
defaultWebappContext.setContextPath("/"); |
452 | 466 |
defaultWebappContext.setTempDirectory(DEFAULT_WEBAPP_TEMP_FOLDER); |
453 |
|
|
454 |
// configure security context
|
|
467 |
|
|
468 |
// configure security context
|
|
455 | 469 |
// see for reference * http://docs.codehaus.org/display/JETTY/Realms |
456 | 470 |
// * http://wiki.eclipse.org/Jetty/Starting/Porting_to_Jetty_7 |
457 | 471 |
HashLoginService loginService = new HashLoginService(); |
458 | 472 |
loginService.setConfig(USERHOME_CDM_LIBRARY_PATH + REALM_PROPERTIES_FILE); |
459 | 473 |
defaultWebappContext.getSecurityHandler().setLoginService(loginService); |
460 |
|
|
474 |
|
|
461 | 475 |
// Important: |
462 |
// the defaultWebappContext MUST USE the super classloader
|
|
476 |
// the defaultWebappContext MUST USE the super classloader |
|
463 | 477 |
// otherwise the status page (index.jsp) might not work |
464 | 478 |
defaultWebappContext.setClassLoader(this.getClass().getClassLoader()); |
465 |
contexts.addHandler(defaultWebappContext);
|
|
466 |
|
|
467 |
//
|
|
468 |
// 2. cdm server contexts
|
|
469 |
//
|
|
470 |
server.addLifeCycleListener(new LifeCycle.Listener(){
|
|
471 |
|
|
472 |
@Override
|
|
473 |
public void lifeCycleFailure(LifeCycle event, Throwable cause) {
|
|
474 |
logger.error("Jetty LifeCycleFailure", cause);
|
|
475 |
}
|
|
476 |
|
|
477 |
@Override
|
|
478 |
public void lifeCycleStarted(LifeCycle event) {
|
|
479 |
logger.info("cdmserver has started, now adding CDM server contexts");
|
|
480 |
try {
|
|
481 |
addCdmServerContexts(true);
|
|
482 |
} catch (IOException e1) {
|
|
483 |
logger.error(e1);
|
|
484 |
}
|
|
485 |
}
|
|
486 |
|
|
487 |
@Override
|
|
488 |
public void lifeCycleStarting(LifeCycle event) {
|
|
489 |
}
|
|
490 |
|
|
491 |
@Override
|
|
492 |
public void lifeCycleStopped(LifeCycle event) {
|
|
493 |
}
|
|
494 |
|
|
495 |
@Override
|
|
496 |
public void lifeCycleStopping(LifeCycle event) {
|
|
497 |
}
|
|
498 |
|
|
499 |
});
|
|
500 |
|
|
501 |
|
|
479 |
contexts.addHandler(defaultWebappContext);
|
|
480 |
|
|
481 |
//
|
|
482 |
// 2. cdm server contexts
|
|
483 |
//
|
|
484 |
server.addLifeCycleListener(new LifeCycle.Listener(){
|
|
485 |
|
|
486 |
@Override
|
|
487 |
public void lifeCycleFailure(LifeCycle event, Throwable cause) {
|
|
488 |
logger.error("Jetty LifeCycleFailure", cause);
|
|
489 |
}
|
|
490 |
|
|
491 |
@Override
|
|
492 |
public void lifeCycleStarted(LifeCycle event) {
|
|
493 |
logger.info("cdmserver has started, now adding CDM server contexts");
|
|
494 |
try {
|
|
495 |
addCdmServerContexts(true);
|
|
496 |
} catch (IOException e1) {
|
|
497 |
logger.error(e1);
|
|
498 |
}
|
|
499 |
}
|
|
500 |
|
|
501 |
@Override
|
|
502 |
public void lifeCycleStarting(LifeCycle event) {
|
|
503 |
}
|
|
504 |
|
|
505 |
@Override
|
|
506 |
public void lifeCycleStopped(LifeCycle event) {
|
|
507 |
}
|
|
508 |
|
|
509 |
@Override
|
|
510 |
public void lifeCycleStopping(LifeCycle event) {
|
|
511 |
}
|
|
512 |
|
|
513 |
});
|
|
514 |
|
|
515 |
|
|
502 | 516 |
logger.info("setting contexts ..."); |
503 | 517 |
server.setHandler(contexts); |
504 | 518 |
logger.info("starting jetty ..."); |
505 | 519 |
// try { |
506 |
|
|
507 |
server.start();
|
|
508 |
|
|
520 |
|
|
521 |
server.start();
|
|
522 |
|
|
509 | 523 |
// } catch(org.springframework.beans.BeansException e){ |
510 | 524 |
// Throwable rootCause = null; |
511 | 525 |
// while(e.getCause() != null){ |
512 | 526 |
// rootCause = e.getCause(); |
513 | 527 |
// } |
514 | 528 |
// if(rootCause != null && rootCause.getClass().getSimpleName().equals("InvalidCdmVersionException")){ |
515 |
//
|
|
529 |
// |
|
516 | 530 |
// logger.error("rootCause ----------->" + rootCause.getMessage()); |
517 | 531 |
//// for(CdmInstanceProperties props : configAndStatus){ |
518 | 532 |
//// if(props.getDataSourceName()) |
519 | 533 |
//// } |
520 | 534 |
// } |
521 | 535 |
// } |
522 |
|
|
536 |
|
|
523 | 537 |
if(cmdLine.hasOption(WIN32SERVICE.getOpt())){ |
524 |
logger.info("jetty has started as win32 service");
|
|
538 |
logger.info("jetty has started as win32 service");
|
|
525 | 539 |
} else { |
526 |
server.join();
|
|
527 |
logger.info(APPLICATION_NAME+" stopped.");
|
|
528 |
System.exit(0);
|
|
540 |
server.join();
|
|
541 |
logger.info(APPLICATION_NAME+" stopped.");
|
|
542 |
System.exit(0);
|
|
529 | 543 |
} |
530 |
}
|
|
531 |
|
|
532 |
public String readCdmRemoteVersion() throws IOException {
|
|
533 |
InputStream versionInStream = Bootloader.class.getClassLoader().getResourceAsStream(VERSION_PROPERTIES_FILE);
|
|
534 |
Properties versionProperties = new Properties();
|
|
535 |
versionProperties.load(versionInStream);
|
|
536 |
String version = versionProperties.getProperty(CDMLIB_REMOTE_WEBAPP_VERSION);
|
|
537 |
return version;
|
|
538 |
}
|
|
539 |
|
|
540 |
/**
|
|
541 |
*
|
|
542 |
*/
|
|
543 |
private void verifyMemoryRequirements() {
|
|
544 |
|
|
545 |
verifyMemoryRequirement("PermGenSpace", PERM_GEN_SPACE_PER_INSTANCE, JvmManager.getPermGenSpaceUsage().getMax());
|
|
546 |
verifyMemoryRequirement("HeapSpace", HEAP_PER_INSTANCE, JvmManager.getHeapMemoryUsage().getMax());
|
|
547 |
|
|
548 |
}
|
|
549 |
|
|
550 |
private void verifyMemoryRequirement(String memoryName, long requiredSpacePerIntance, long availableSpace) {
|
|
551 |
|
|
552 |
|
|
553 |
long requiredSpace = configAndStatusSet.size() * requiredSpacePerIntance;
|
|
554 |
|
|
555 |
if(requiredSpace > availableSpace){
|
|
556 |
|
|
557 |
String message = memoryName + " ("
|
|
558 |
+ (availableSpace / MB)
|
|
559 |
+ "MB) insufficient for "
|
|
560 |
+ configAndStatusSet.size()
|
|
561 |
+ " instances. Increase " + memoryName + " to "
|
|
562 |
+ (requiredSpace / MB)
|
|
563 |
+ "MB";
|
|
564 |
;
|
|
565 |
logger.error(message + " => disabling some instances!!!");
|
|
566 |
|
|
567 |
// disabling some instances
|
|
568 |
int i=0;
|
|
569 |
for(CdmInstanceProperties instanceProps : configAndStatusSet){
|
|
570 |
i++;
|
|
571 |
if(i * requiredSpacePerIntance > availableSpace){
|
|
572 |
instanceProps.setStatus(Status.disabled);
|
|
573 |
instanceProps.getProblems().add("Disbled due to: " + message);
|
|
574 |
}
|
|
575 |
}
|
|
576 |
}
|
|
577 |
}
|
|
578 |
|
|
579 |
/**
|
|
580 |
* Configures and adds a {@link RollingFileAppender} to the root logger
|
|
581 |
*
|
|
582 |
* The log files of the cdm-remote instances are configured by the
|
|
583 |
* {@link eu.etaxonomy.cdm.remote.config.LoggingConfigurer}
|
|
584 |
*/
|
|
585 |
private void configureFileLogger() {
|
|
586 |
|
|
587 |
PatternLayout layout = new PatternLayout("%d %p [%c] - %m%n");
|
|
588 |
try {
|
|
589 |
String logFile = LOG_PATH + File.separator + "cdmserver.log";
|
|
590 |
RollingFileAppender appender = new RollingFileAppender(layout, logFile);
|
|
591 |
appender.setMaxBackupIndex(3);
|
|
592 |
appender.setMaxFileSize("2MB");
|
|
593 |
Logger.getRootLogger().addAppender(appender);
|
|
594 |
logger.info("logging to :" + logFile);
|
|
595 |
} catch (IOException e) {
|
|
596 |
logger.error("Creating RollingFileAppender failed:", e);
|
|
597 |
}
|
|
598 |
}
|
|
599 |
|
|
600 |
private void addCdmServerContexts(boolean austostart) throws IOException {
|
|
601 |
|
|
602 |
for(CdmInstanceProperties conf : configAndStatusSet){
|
|
603 |
|
|
604 |
if(!conf.isEnabled()){
|
|
605 |
logger.info(conf.getDataSourceName() + " is disabled => skipping");
|
|
606 |
continue;
|
|
607 |
}
|
|
608 |
conf.setStatus(CdmInstanceProperties.Status.initializing);
|
|
609 |
logger.info("preparing WebAppContext for '"+ conf.getDataSourceName() + "'");
|
|
610 |
WebAppContext cdmWebappContext = new WebAppContext();
|
|
611 |
|
|
612 |
cdmWebappContext.setContextPath("/"+conf.getDataSourceName());
|
|
613 |
cdmWebappContext.setTempDirectory(CDM_WEBAPP_TEMP_FOLDER);
|
|
614 |
|
|
544 |
}
|
|
545 |
|
|
546 |
public String readCdmRemoteVersion() throws IOException {
|
|
547 |
InputStream versionInStream = Bootloader.class.getClassLoader().getResourceAsStream(VERSION_PROPERTIES_FILE);
|
|
548 |
Properties versionProperties = new Properties();
|
|
549 |
versionProperties.load(versionInStream);
|
|
550 |
String version = versionProperties.getProperty(CDMLIB_REMOTE_WEBAPP_VERSION);
|
|
551 |
return version;
|
|
552 |
}
|
|
553 |
|
|
554 |
/**
|
|
555 |
*
|
|
556 |
*/
|
|
557 |
private void verifyMemoryRequirements() {
|
|
558 |
|
|
559 |
verifyMemoryRequirement("PermGenSpace", PERM_GEN_SPACE_PER_INSTANCE, JvmManager.getPermGenSpaceUsage().getMax());
|
|
560 |
verifyMemoryRequirement("HeapSpace", HEAP_PER_INSTANCE, JvmManager.getHeapMemoryUsage().getMax());
|
|
561 |
|
|
562 |
}
|
|
563 |
|
|
564 |
private void verifyMemoryRequirement(String memoryName, long requiredSpacePerIntance, long availableSpace) {
|
|
565 |
|
|
566 |
|
|
567 |
long requiredSpace = configAndStatusSet.size() * requiredSpacePerIntance;
|
|
568 |
|
|
569 |
if(requiredSpace > availableSpace){
|
|
570 |
|
|
571 |
String message = memoryName + " ("
|
|
572 |
+ (availableSpace / MB)
|
|
573 |
+ "MB) insufficient for "
|
|
574 |
+ configAndStatusSet.size()
|
|
575 |
+ " instances. Increase " + memoryName + " to "
|
|
576 |
+ (requiredSpace / MB)
|
|
577 |
+ "MB";
|
|
578 |
;
|
|
579 |
logger.error(message + " => disabling some instances!!!");
|
|
580 |
|
|
581 |
// disabling some instances
|
|
582 |
int i=0;
|
|
583 |
for(CdmInstanceProperties instanceProps : configAndStatusSet){
|
|
584 |
i++;
|
|
585 |
if(i * requiredSpacePerIntance > availableSpace){
|
|
586 |
instanceProps.setStatus(Status.disabled);
|
|
587 |
instanceProps.getProblems().add("Disbled due to: " + message);
|
|
588 |
}
|
|
589 |
}
|
|
590 |
}
|
|
591 |
}
|
|
592 |
|
|
593 |
/**
|
|
594 |
* Configures and adds a {@link RollingFileAppender} to the root logger
|
|
595 |
*
|
|
596 |
* The log files of the cdm-remote instances are configured by the
|
|
597 |
* {@link eu.etaxonomy.cdm.remote.config.LoggingConfigurer}
|
|
598 |
*/
|
|
599 |
private void configureFileLogger() {
|
|
600 |
|
|
601 |
PatternLayout layout = new PatternLayout("%d %p [%c] - %m%n");
|
|
602 |
try {
|
|
603 |
String logFile = logPath + File.separator + "cdmserver.log";
|
|
604 |
RollingFileAppender appender = new RollingFileAppender(layout, logFile);
|
|
605 |
appender.setMaxBackupIndex(3);
|
|
606 |
appender.setMaxFileSize("2MB");
|
|
607 |
Logger.getRootLogger().addAppender(appender);
|
|
608 |
logger.info("logging to :" + logFile);
|
|
609 |
} catch (IOException e) {
|
|
610 |
logger.error("Creating RollingFileAppender failed:", e);
|
|
611 |
}
|
|
612 |
}
|
|
613 |
|
|
614 |
private void addCdmServerContexts(boolean austostart) throws IOException {
|
|
615 |
|
|
616 |
for(CdmInstanceProperties conf : configAndStatusSet){
|
|
617 |
|
|
618 |
if(!conf.isEnabled()){
|
|
619 |
logger.info(conf.getDataSourceName() + " is disabled => skipping");
|
|
620 |
continue;
|
|
621 |
}
|
|
622 |
conf.setStatus(CdmInstanceProperties.Status.initializing);
|
|
623 |
logger.info("preparing WebAppContext for '"+ conf.getDataSourceName() + "'");
|
|
624 |
WebAppContext cdmWebappContext = new WebAppContext();
|
|
625 |
|
|
626 |
cdmWebappContext.setContextPath("/"+conf.getDataSourceName());
|
|
627 |
cdmWebappContext.setTempDirectory(CDM_WEBAPP_TEMP_FOLDER);
|
|
628 |
|
|
615 | 629 |
if(!bindJndiDataSource(conf)){ |
616 |
// a problem with the datasource occurred skip this webapp
|
|
617 |
cdmWebappContext = null;
|
|
618 |
logger.error("a problem with the datasource occurred -> skipping /" + conf.getDataSourceName());
|
|
619 |
conf.setStatus(CdmInstanceProperties.Status.error);
|
|
620 |
continue;
|
|
630 |
// a problem with the datasource occurred skip this webapp
|
|
631 |
cdmWebappContext = null;
|
|
632 |
logger.error("a problem with the datasource occurred -> skipping /" + conf.getDataSourceName());
|
|
633 |
conf.setStatus(CdmInstanceProperties.Status.error);
|
|
634 |
continue;
|
|
621 | 635 |
} |
622 |
|
|
636 |
|
|
623 | 637 |
cdmWebappContext.setAttribute(ATTRIBUTE_DATASOURCE_NAME, conf.getDataSourceName()); |
624 | 638 |
cdmWebappContext.setAttribute(ATTRIBUTE_JDBC_JNDI_NAME, conf.getJdbcJndiName()); |
625 |
setWebApp(cdmWebappContext, cdmRemoteWebAppFile);
|
|
626 |
|
|
627 |
cdmWebappContext.setAttribute(ATTRIBUTE_CDM_LOGFILE,
|
|
628 |
LOG_PATH + File.separator + "cdm-"
|
|
629 |
+ conf.getDataSourceName() + ".log");
|
|
630 |
|
|
631 |
if(cdmRemoteWebAppFile.isDirectory() && isRunningFromCdmRemoteWebAppSource()){
|
|
632 |
|
|
633 |
/*
|
|
634 |
* when running the webapp from {projectpath} src/main/webapp we
|
|
635 |
* must assure that each web application is using it's own
|
|
636 |
* classloader thus we tell the WebAppClassLoader where the
|
|
637 |
* dependencies of the webapplication can be found. Otherwise
|
|
638 |
* the system classloader would load these resources.
|
|
639 |
*/
|
|
640 |
logger.info("Running webapp from source folder, thus adding java.class.path to WebAppClassLoader");
|
|
641 |
|
|
642 |
WebAppClassLoader classLoader = new WebAppClassLoader(cdmWebappContext);
|
|
643 |
|
|
644 |
String classPath = System.getProperty("java.class.path");
|
|
645 |
classLoader.addClassPath(classPath);
|
|
646 |
cdmWebappContext.setClassLoader(classLoader);
|
|
647 |
}
|
|
648 |
|
|
649 |
cdmWebappContext.addLifeCycleListener(new WebAppContextListener(cdmWebappContext));
|
|
650 |
contexts.addHandler(cdmWebappContext);
|
|
651 |
|
|
652 |
if(austostart){
|
|
653 |
try {
|
|
654 |
conf.setStatus(CdmInstanceProperties.Status.starting);
|
|
655 |
cdmWebappContext.start();
|
|
656 |
if(!conf.getStatus().equals(Status.error)){
|
|
657 |
conf.setStatus(CdmInstanceProperties.Status.started);
|
|
658 |
}
|
|
659 |
} catch (Exception e) {
|
|
660 |
logger.error("Could not start " + cdmWebappContext.getContextPath());
|
|
661 |
conf.setStatus(CdmInstanceProperties.Status.error);
|
|
662 |
}
|
|
663 |
}
|
|
639 |
setWebApp(cdmWebappContext, cdmRemoteWebAppFile);
|
|
640 |
|
|
641 |
cdmWebappContext.setAttribute(ATTRIBUTE_CDM_LOGFILE,
|
|
642 |
logPath + File.separator + "cdm-"
|
|
643 |
+ conf.getDataSourceName() + ".log");
|
|
644 |
|
|
645 |
if(cdmRemoteWebAppFile.isDirectory() && isRunningFromCdmRemoteWebAppSource()){
|
|
646 |
|
|
647 |
/*
|
|
648 |
* when running the webapp from {projectpath} src/main/webapp we
|
|
649 |
* must assure that each web application is using it's own
|
|
650 |
* classloader thus we tell the WebAppClassLoader where the
|
|
651 |
* dependencies of the webapplication can be found. Otherwise
|
|
652 |
* the system classloader would load these resources.
|
|
653 |
*/
|
|
654 |
logger.info("Running webapp from source folder, thus adding java.class.path to WebAppClassLoader");
|
|
655 |
|
|
656 |
WebAppClassLoader classLoader = new WebAppClassLoader(cdmWebappContext);
|
|
657 |
|
|
658 |
String classPath = System.getProperty("java.class.path");
|
|
659 |
classLoader.addClassPath(classPath);
|
|
660 |
cdmWebappContext.setClassLoader(classLoader);
|
|
661 |
}
|
|
662 |
|
|
663 |
cdmWebappContext.addLifeCycleListener(new WebAppContextListener(cdmWebappContext));
|
|
664 |
contexts.addHandler(cdmWebappContext);
|
|
665 |
|
|
666 |
if(austostart){
|
|
667 |
try {
|
|
668 |
conf.setStatus(CdmInstanceProperties.Status.starting);
|
|
669 |
cdmWebappContext.start();
|
|
670 |
if(!conf.getStatus().equals(Status.error)){
|
|
671 |
conf.setStatus(CdmInstanceProperties.Status.started);
|
|
672 |
}
|
|
673 |
} catch (Exception e) {
|
|
674 |
logger.error("Could not start " + cdmWebappContext.getContextPath());
|
|
675 |
conf.setStatus(CdmInstanceProperties.Status.error);
|
|
676 |
}
|
|
677 |
}
|
|
664 | 678 |
|
665 | 679 |
} |
666 |
} |
|
667 |
|
|
668 |
/** |
|
669 |
* @param context |
|
670 |
* @param webApplicationResource |
|
671 |
*/ |
|
672 |
private void setWebApp(WebAppContext context, File webApplicationResource) { |
|
673 |
if(webApplicationResource.isDirectory()){ |
|
674 |
context.setResourceBase(webApplicationResource.getAbsolutePath()); |
|
675 |
logger.debug("setting directory " + webApplicationResource.getAbsolutePath() + " as webapplication"); |
|
676 |
} else { |
|
677 |
context.setWar(webApplicationResource.getAbsolutePath()); |
|
678 |
logger.debug("setting war file " + webApplicationResource.getAbsolutePath() + " as webapplication"); |
|
679 |
} |
|
680 |
} |
|
681 |
|
|
682 |
/** |
|
683 |
* @return |
|
684 |
*/ |
|
685 |
private boolean isRunningFromCdmRemoteWebAppSource() { |
|
686 |
String webappPathNormalized = cdmRemoteWebAppFile.getAbsolutePath().replace('\\', '/'); |
|
687 |
return webappPathNormalized.endsWith("src/main/webapp") || webappPathNormalized.endsWith("cdmlib-remote-webapp/target/cdmserver"); |
|
688 |
} |
|
689 |
|
|
690 |
/** |
|
691 |
* @param dataSourceName |
|
692 |
* @return |
|
693 |
*/ |
|
694 |
private CdmInstanceProperties findConfigAndStatusFor(String dataSourceName){ |
|
695 |
for(CdmInstanceProperties props : configAndStatusSet){ |
|
696 |
if(props.getDataSourceName().equals(dataSourceName)){ |
|
697 |
return props; |
|
698 |
} |
|
699 |
} |
|
700 |
return null; |
|
701 |
} |
|
702 |
|
|
703 |
/** |
|
704 |
* @param <T> |
|
705 |
* @param webAppContext |
|
706 |
* @param attributeName |
|
707 |
* @param type |
|
708 |
* @return |
|
709 |
*/ |
|
710 |
@SuppressWarnings("unchecked") |
|
711 |
private <T> T getServletContextAttribute(WebAppContext webAppContext, String attributeName, Class<T> type) { |
|
712 |
|
|
713 |
Context servletContext = webAppContext.getServletContext(); |
|
714 |
Object value = servletContext.getAttribute(attributeName); |
|
715 |
if( value != null && type.isAssignableFrom(value.getClass())){ |
|
716 |
|
|
717 |
} |
|
718 |
return (T) value; |
|
719 |
} |
|
680 |
} |
|
681 |
|
|
682 |
/** |
|
683 |
* @param context |
|
684 |
* @param webApplicationResource |
|
685 |
*/ |
|
686 |
private void setWebApp(WebAppContext context, File webApplicationResource) { |
|
687 |
if(webApplicationResource.isDirectory()){ |
|
688 |
context.setResourceBase(webApplicationResource.getAbsolutePath()); |
|
689 |
logger.debug("setting directory " + webApplicationResource.getAbsolutePath() + " as webapplication"); |
|
690 |
} else { |
|
691 |
context.setWar(webApplicationResource.getAbsolutePath()); |
|
692 |
logger.debug("setting war file " + webApplicationResource.getAbsolutePath() + " as webapplication"); |
|
693 |
} |
|
694 |
} |
|
695 |
|
|
696 |
/** |
|
697 |
* @return |
|
698 |
*/ |
|
699 |
private boolean isRunningFromCdmRemoteWebAppSource() { |
|
700 |
String webappPathNormalized = cdmRemoteWebAppFile.getAbsolutePath().replace('\\', '/'); |
|
701 |
return webappPathNormalized.endsWith("src/main/webapp") || webappPathNormalized.endsWith("cdmlib-remote-webapp/target/cdmserver"); |
|
702 |
} |
|
703 |
|
|
704 |
/** |
|
705 |
* @param dataSourceName |
|
706 |
* @return |
|
707 |
*/ |
|
708 |
private CdmInstanceProperties findConfigAndStatusFor(String dataSourceName){ |
|
709 |
for(CdmInstanceProperties props : configAndStatusSet){ |
|
710 |
if(props.getDataSourceName().equals(dataSourceName)){ |
|
711 |
return props; |
|
712 |
} |
|
713 |
} |
|
714 |
return null; |
|
715 |
} |
|
716 |
|
|
717 |
/** |
|
718 |
* @param <T> |
|
719 |
* @param webAppContext |
|
720 |
* @param attributeName |
|
721 |
* @param type |
|
722 |
* @return |
|
723 |
*/ |
|
724 |
@SuppressWarnings("unchecked") |
|
725 |
private <T> T getServletContextAttribute(WebAppContext webAppContext, String attributeName, Class<T> type) { |
|
726 |
|
|
727 |
Context servletContext = webAppContext.getServletContext(); |
|
728 |
Object value = servletContext.getAttribute(attributeName); |
|
729 |
if( value != null && type.isAssignableFrom(value.getClass())){ |
|
730 |
|
|
731 |
} |
|
732 |
return (T) value; |
|
733 |
} |
|
734 |
|
|
735 |
public Server getServer() { |
|
736 |
return server; |
|
737 |
} |
|
720 | 738 |
} |
Also available in: Unified diff
first commits towards fixing #2324