web application properties for cdmlib-remote can now be defined in a per user propert...
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / config / AbstractWebApplicationConfigurer.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.remote.config;
11
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;
18
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;
23
24 import eu.etaxonomy.cdm.common.CdmUtils;
25
26 /**
27 * @author a.kohlbecker
28 * @date 20.07.2010
29 *
30 */
31 public abstract class AbstractWebApplicationConfigurer {
32
33 private static final String CDMLIB_REMOTE_PROPERTIES = "cdmlib-remote.properties";
34
35 private static final String ATTRIBUTE_ERROR_MESSAGES = "cdm.errorMessages";
36
37 public static final Logger logger = Logger.getLogger(AbstractWebApplicationConfigurer.class);
38
39 WebApplicationContext webApplicationContext;
40
41 static Properties userDefinedProperties = null;
42 static {
43 if(userDefinedProperties == null) {
44 userDefinedProperties = new Properties();
45 try {
46 InputStream in = new FileInputStream(
47 CdmUtils.perUserCdmFolder
48 + java.io.File.separator
49 + CDMLIB_REMOTE_PROPERTIES
50 );
51 if (in != null) {
52 userDefinedProperties.load(in);
53 }
54 } catch (IOException e) {
55 logger.debug("No per user " + CDMLIB_REMOTE_PROPERTIES + " found.");
56 }
57 }
58 }
59
60 @Autowired
61 public void setApplicationContext(ApplicationContext applicationContext){
62
63 if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {
64 this.webApplicationContext = (WebApplicationContext)applicationContext;
65 } else {
66 logger.error("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
67 }
68 }
69
70 /**
71 * Find a property primarily in the ServletContext and secondarily
72 * in the environment variables of the OS. So a property can be set
73 * by three means:
74 * <ol>
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>
79 * </ol>
80 *
81 * @param property usually a string constant defined in a subclass of
82 * <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>
83 * @param required
84 * @return
85 */
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
91 if(value == null){
92 value = System.getProperty(property);
93 }
94 if(value == null && userDefinedProperties != null){
95 value = userDefinedProperties.getProperty(property);
96 }
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 ...");
104 System.exit(-1);
105 }
106 return value;
107 }
108
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;
114 } else {
115 messages = new ArrayList<String>();
116 }
117 messages.add(errorMessage);
118 webApplicationContext.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES, messages);
119 }
120
121
122
123 }