3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.remote
.config
;
12 import java
.io
.FileInputStream
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
17 import java
.util
.Properties
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
21 import org
.springframework
.context
.ApplicationContext
;
22 import org
.springframework
.web
.context
.WebApplicationContext
;
24 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 * @author a.kohlbecker
31 public abstract class AbstractWebApplicationConfigurer
{
32 public static final Logger logger
= Logger
.getLogger(AbstractWebApplicationConfigurer
.class);
34 private static final String CDMLIB_REMOTE_PROPERTIES
= "cdmlib-remote.properties";
37 * see also <code>eu.etaxonomy.cdm.server.instance.SharedAttributes</code>
39 private static final String ATTRIBUTE_ERROR_MESSAGES
= "cdm.errorMessages";
42 WebApplicationContext webApplicationContext
;
44 static Properties userDefinedProperties
= null;
46 if(userDefinedProperties
== null) {
47 userDefinedProperties
= new Properties();
49 InputStream in
= new FileInputStream(
50 CdmUtils
.perUserCdmFolder
51 + java
.io
.File
.separator
52 + CDMLIB_REMOTE_PROPERTIES
55 userDefinedProperties
.load(in
);
57 } catch (IOException e
) {
58 logger
.debug("No per user " + CDMLIB_REMOTE_PROPERTIES
+ " found.");
64 public void setApplicationContext(ApplicationContext applicationContext
){
66 if(WebApplicationContext
.class.isAssignableFrom(applicationContext
.getClass())) {
67 this.webApplicationContext
= (WebApplicationContext
)applicationContext
;
69 logger
.error("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
74 * Find a property primarily in the ServletContext and secondarily
75 * in the environment variables of the OS. So a property can be set
78 * <li>As attribute to the ServletContext (the cdm-server makes use of this method)</li>
79 * <li>as system property e.g. by setting the jvm commandline option like for example
80 * <code>-Dcdm.rootpathprefix=my/cdm/remote-instance<code></li>
81 * <li>In a per user Java properties file {@code ~/.cdmLibrary/cdmlib-remote.properties}</li>
84 * @param property usually a string constant defined in a subclass of
85 * <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>
89 protected String
findProperty(String property
, boolean required
) {
90 // 1. look for the property in the ServletContext
91 Object obj
= webApplicationContext
.getServletContext().getAttribute(property
);
92 String value
= (String
)obj
;
93 // 2. look for the property in environment variables of the OS
95 value
= System
.getProperty(property
);
97 if(value
== null && userDefinedProperties
!= null){
98 value
= userDefinedProperties
.getProperty(property
);
100 if(value
== null && required
){
101 logger
.error("property {" + property
+ "} not found.");
102 logger
.error("--> This property can be set in two ways:");
103 logger
.error("--> 1. as attribute to the ServletContext");
104 logger
.error("--> 2. as system property e.g. -D" + property
);
105 logger
.error("--> 3. in ~/.cdmLibrary/cdmlib-remote.properties");
106 logger
.error("Stopping application ...");
112 protected void addErrorMessageToServletContextAttributes(String errorMessage
) {
113 Object o
= webApplicationContext
.getServletContext().getAttribute(ATTRIBUTE_ERROR_MESSAGES
);
114 List
<String
> messages
;
115 if(o
!= null && o
instanceof List
<?
>){
116 messages
= (List
<String
>) o
;
118 messages
= new ArrayList
<String
>();
120 messages
.add(errorMessage
);
121 webApplicationContext
.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES
, messages
);