2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.remote
.config
;
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
;
18 import org
.apache
.logging
.log4j
.LogManager
;
19 import org
.apache
.logging
.log4j
.Logger
;
20 import org
.springframework
.beans
.factory
.InitializingBean
;
21 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
22 import org
.springframework
.context
.ApplicationContext
;
23 import org
.springframework
.core
.env
.ConfigurableEnvironment
;
24 import org
.springframework
.web
.context
.WebApplicationContext
;
26 import eu
.etaxonomy
.cdm
.config
.ConfigFileUtil
;
29 * @author a.kohlbecker
32 public abstract class AbstractWebApplicationConfigurer
implements InitializingBean
{
34 public static final Logger logger
= LogManager
.getLogger();
37 protected ConfigurableEnvironment env
;
40 private ConfigFileUtil configFileUtil
;
42 private static final String CDMLIB_REMOTE_PROPERTIES
= "cdmlib-remote.properties";
45 * see also <code>eu.etaxonomy.cdm.server.instance.SharedAttributes</code>
47 private static final String ATTRIBUTE_ERROR_MESSAGES
= "cdm.errorMessages";
50 protected WebApplicationContext webApplicationContext
;
52 private Properties userDefinedProperties
= null;
56 public void afterPropertiesSet() throws Exception
{
58 if(userDefinedProperties
== null) {
59 userDefinedProperties
= new Properties();
61 InputStream in
= new FileInputStream(
62 configFileUtil
.perUserCdmFolder()
63 + java
.io
.File
.separator
64 + CDMLIB_REMOTE_PROPERTIES
66 userDefinedProperties
.load(in
);
67 } catch (IOException e
) {
68 logger
.debug("No per user " + CDMLIB_REMOTE_PROPERTIES
+ " found.");
74 public void setApplicationContext(ApplicationContext applicationContext
){
76 if(WebApplicationContext
.class.isAssignableFrom(applicationContext
.getClass())) {
77 this.webApplicationContext
= (WebApplicationContext
)applicationContext
;
79 throw new RuntimeException("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
84 * Find a property primarily in the ServletContext and secondarily
85 * in the environment variables of the OS. So a property can be set
88 * <li>As attribute to the ServletContext (the cdm-server makes use of this method)</li>
89 * <li>as system property e.g. by setting the jvm commandline option like for example
90 * <code>-Dcdm.rootpathprefix=my/cdm/remote-instance<code></li>
91 * <li>In a per user Java properties file {@code ~/.cdmLibrary/cdmlib-remote.properties}</li>
94 * @param property usually a string constant defined in a subclass of
95 * <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>
99 protected String
findProperty(String property
, boolean required
) {
103 value
= env
.getRequiredProperty(property
);
104 } catch (IllegalStateException e
) {
105 logger
.error("property {" + property
+ "} not found.");
106 logger
.error("--> This property can be set in three optional ways:");
107 logger
.error("--> 1. as attribute to the ServletContext");
108 logger
.error("--> 2. as system property e.g. -D" + property
);
109 // logger.error("--> 3. in ~/.cdmLibrary/cdmlib-remote.properties");
110 logger
.error("Stopping application ...");
111 RuntimeException re
= new RuntimeException();
112 re
.printStackTrace(System
.err
);
116 value
= env
.getProperty(property
);
121 protected void addErrorMessageToServletContextAttributes(String errorMessage
) {
122 Object o
= webApplicationContext
.getServletContext().getAttribute(ATTRIBUTE_ERROR_MESSAGES
);
123 List
<String
> messages
;
124 if(o
instanceof List
<?
>){
125 messages
= (List
<String
>) o
;
127 messages
= new ArrayList
<>();
129 messages
.add(errorMessage
);
130 webApplicationContext
.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES
, messages
);