cleanup
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / config / AbstractWebApplicationConfigurer.java
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.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;
25
26 import eu.etaxonomy.cdm.config.ConfigFileUtil;
27
28 /**
29 * @author a.kohlbecker
30 * @since 20.07.2010
31 */
32 public abstract class AbstractWebApplicationConfigurer implements InitializingBean {
33
34 public static final Logger logger = LogManager.getLogger();
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 userDefinedProperties.load(in);
67 } catch (IOException e) {
68 logger.debug("No per user " + CDMLIB_REMOTE_PROPERTIES + " found.");
69 }
70 }
71 }
72
73 @Autowired
74 public void setApplicationContext(ApplicationContext applicationContext){
75
76 if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {
77 this.webApplicationContext = (WebApplicationContext)applicationContext;
78 } else {
79 throw new RuntimeException("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
80 }
81 }
82
83 /**
84 * Find a property primarily in the ServletContext and secondarily
85 * in the environment variables of the OS. So a property can be set
86 * by three means:
87 * <ol>
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>
92 * </ol>
93 *
94 * @param property usually a string constant defined in a subclass of
95 * <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>
96 * @param required
97 * @return
98 */
99 protected String findProperty(String property, boolean required) {
100 String value = null;
101 if(required){
102 try {
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);
113 System.exit(-1);
114 }
115 } else {
116 value = env.getProperty(property);
117 }
118 return value;
119 }
120
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;
126 } else {
127 messages = new ArrayList<>();
128 }
129 messages.add(errorMessage);
130 webApplicationContext.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES, messages);
131 }
132 }