1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.remote.config;
|
10
|
|
11
|
import java.io.FileInputStream;
|
12
|
import java.io.IOException;
|
13
|
import java.io.InputStream;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.List;
|
16
|
import java.util.Properties;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.springframework.beans.factory.InitializingBean;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
import org.springframework.context.ApplicationContext;
|
22
|
import org.springframework.core.env.ConfigurableEnvironment;
|
23
|
import org.springframework.web.context.WebApplicationContext;
|
24
|
|
25
|
import eu.etaxonomy.cdm.config.ConfigFileUtil;
|
26
|
|
27
|
/**
|
28
|
* @author a.kohlbecker
|
29
|
* @since 20.07.2010
|
30
|
*
|
31
|
*/
|
32
|
public abstract class AbstractWebApplicationConfigurer implements InitializingBean {
|
33
|
|
34
|
public static final Logger logger = Logger.getLogger(AbstractWebApplicationConfigurer.class);
|
35
|
|
36
|
@Autowired
|
37
|
protected ConfigurableEnvironment env;
|
38
|
|
39
|
@Autowired
|
40
|
private ConfigFileUtil configFileUtil;
|
41
|
|
42
|
private static final String CDMLIB_REMOTE_PROPERTIES = "cdmlib-remote.properties";
|
43
|
|
44
|
/**
|
45
|
* see also <code>eu.etaxonomy.cdm.server.instance.SharedAttributes</code>
|
46
|
*/
|
47
|
private static final String ATTRIBUTE_ERROR_MESSAGES = "cdm.errorMessages";
|
48
|
|
49
|
|
50
|
protected WebApplicationContext webApplicationContext;
|
51
|
|
52
|
private Properties userDefinedProperties = null;
|
53
|
|
54
|
|
55
|
@Override
|
56
|
public void afterPropertiesSet() throws Exception {
|
57
|
|
58
|
if(userDefinedProperties == null) {
|
59
|
userDefinedProperties = new Properties();
|
60
|
try {
|
61
|
InputStream in = new FileInputStream(
|
62
|
configFileUtil.perUserCdmFolder()
|
63
|
+ java.io.File.separator
|
64
|
+ CDMLIB_REMOTE_PROPERTIES
|
65
|
);
|
66
|
if (in != null) {
|
67
|
userDefinedProperties.load(in);
|
68
|
}
|
69
|
} catch (IOException e) {
|
70
|
logger.debug("No per user " + CDMLIB_REMOTE_PROPERTIES + " found.");
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
|
75
|
@Autowired
|
76
|
public void setApplicationContext(ApplicationContext applicationContext){
|
77
|
|
78
|
if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {
|
79
|
this.webApplicationContext = (WebApplicationContext)applicationContext;
|
80
|
} else {
|
81
|
throw new RuntimeException("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
|
82
|
}
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Find a property primarily in the ServletContext and secondarily
|
87
|
* in the environment variables of the OS. So a property can be set
|
88
|
* by three means:
|
89
|
* <ol>
|
90
|
* <li>As attribute to the ServletContext (the cdm-server makes use of this method)</li>
|
91
|
* <li>as system property e.g. by setting the jvm commandline option like for example
|
92
|
* <code>-Dcdm.rootpathprefix=my/cdm/remote-instance<code></li>
|
93
|
* <li>In a per user Java properties file {@code ~/.cdmLibrary/cdmlib-remote.properties}</li>
|
94
|
* </ol>
|
95
|
*
|
96
|
* @param property usually a string constant defined in a subclass of
|
97
|
* <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>
|
98
|
* @param required
|
99
|
* @return
|
100
|
*/
|
101
|
protected String findProperty(String property, boolean required) {
|
102
|
String value = null;
|
103
|
if(required){
|
104
|
try {
|
105
|
value = env.getRequiredProperty(property);
|
106
|
} catch (IllegalStateException e) {
|
107
|
logger.error("property {" + property + "} not found.");
|
108
|
logger.error("--> This property can be set in three optional ways:");
|
109
|
logger.error("--> 1. as attribute to the ServletContext");
|
110
|
logger.error("--> 2. as system property e.g. -D" + property);
|
111
|
// logger.error("--> 3. in ~/.cdmLibrary/cdmlib-remote.properties");
|
112
|
logger.error("Stopping application ...");
|
113
|
RuntimeException re = new RuntimeException();
|
114
|
re.printStackTrace(System.err);
|
115
|
System.exit(-1);
|
116
|
}
|
117
|
} else {
|
118
|
value = env.getProperty(property);
|
119
|
}
|
120
|
return value;
|
121
|
}
|
122
|
|
123
|
protected void addErrorMessageToServletContextAttributes(String errorMessage) {
|
124
|
Object o = webApplicationContext.getServletContext().getAttribute(ATTRIBUTE_ERROR_MESSAGES);
|
125
|
List<String> messages;
|
126
|
if(o != null && o instanceof List<?>){
|
127
|
messages = (List<String>) o;
|
128
|
} else {
|
129
|
messages = new ArrayList<String>();
|
130
|
}
|
131
|
messages.add(errorMessage);
|
132
|
webApplicationContext.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES, messages);
|
133
|
}
|
134
|
|
135
|
|
136
|
|
137
|
}
|