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