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
{
33 private static final String CDMLIB_REMOTE_PROPERTIES
= "cdmlib-remote.properties";
35 private static final String ATTRIBUTE_ERROR_MESSAGES
= "cdm.errorMessages";
37 public static final Logger logger
= Logger
.getLogger(AbstractWebApplicationConfigurer
.class);
39 WebApplicationContext webApplicationContext
;
41 static Properties userDefinedProperties
= null;
43 if(userDefinedProperties
== null) {
44 userDefinedProperties
= new Properties();
46 InputStream in
= new FileInputStream(
47 CdmUtils
.perUserCdmFolder
48 + java
.io
.File
.separator
49 + CDMLIB_REMOTE_PROPERTIES
52 userDefinedProperties
.load(in
);
54 } catch (IOException e
) {
55 logger
.debug("No per user " + CDMLIB_REMOTE_PROPERTIES
+ " found.");
61 public void setApplicationContext(ApplicationContext applicationContext
){
63 if(WebApplicationContext
.class.isAssignableFrom(applicationContext
.getClass())) {
64 this.webApplicationContext
= (WebApplicationContext
)applicationContext
;
66 logger
.error("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
71 * Find a property primarily in the ServletContext and secondarily
72 * in the environment variables of the OS. So a property can be set
75 * <li>As attribute to the ServletContext (the cdm-server makes use of this method)</li>
76 * <li>as system property e.g. by setting the jvm commandline option like for example
77 * <code>-Dcdm.rootpathprefix=my/cdm/remote-instance<code></li>
78 * <li>In a per user Java properties file {@code ~/.cdmLibrary/cdmlib-remote.properties}</li>
81 * @param property usually a string constant defined in a subclass of
82 * <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>
86 protected String
findProperty(String property
, boolean required
) {
87 // 1. look for the property in the ServletContext
88 Object obj
= webApplicationContext
.getServletContext().getAttribute(property
);
89 String value
= (String
)obj
;
90 // 2. look for the property in environment variables of the OS
92 value
= System
.getProperty(property
);
94 if(value
== null && userDefinedProperties
!= null){
95 value
= userDefinedProperties
.getProperty(property
);
97 if(value
== null && required
){
98 logger
.error("property {" + property
+ "} not found.");
99 logger
.error("--> This property can be set in two ways:");
100 logger
.error("--> 1. as attribute to the ServletContext");
101 logger
.error("--> 2. as system property e.g. -D" + property
);
102 logger
.error("--> 3. in ~/.cdmLibrary/cdmlib-remote.properties");
103 logger
.error("Stopping application ...");
109 protected void addErrorMessageToServletContextAttributes(String errorMessage
) {
110 Object o
= webApplicationContext
.getServletContext().getAttribute(ATTRIBUTE_ERROR_MESSAGES
);
111 List
<String
> messages
;
112 if(o
!= null && o
instanceof List
<?
>){
113 messages
= (List
<String
>) o
;
115 messages
= new ArrayList
<String
>();
117 messages
.add(errorMessage
);
118 webApplicationContext
.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES
, messages
);