moved all jetty + cdmlib related stuff (except web.xml) to test
[cdm-vaadin.git] / src / test / 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 public static final Logger logger = Logger.getLogger(AbstractWebApplicationConfigurer.class);
33
34 private static final String CDMLIB_REMOTE_PROPERTIES = "cdmlib-remote.properties";
35
36 /**
37 * see also <code>eu.etaxonomy.cdm.server.instance.SharedAttributes</code>
38 */
39 private static final String ATTRIBUTE_ERROR_MESSAGES = "cdm.errorMessages";
40
41
42 WebApplicationContext webApplicationContext;
43
44 static Properties userDefinedProperties = null;
45 static {
46 if(userDefinedProperties == null) {
47 userDefinedProperties = new Properties();
48 try {
49 InputStream in = new FileInputStream(
50 CdmUtils.perUserCdmFolder
51 + java.io.File.separator
52 + CDMLIB_REMOTE_PROPERTIES
53 );
54 if (in != null) {
55 userDefinedProperties.load(in);
56 }
57 } catch (IOException e) {
58 logger.debug("No per user " + CDMLIB_REMOTE_PROPERTIES + " found.");
59 }
60 }
61 }
62
63 @Autowired
64 public void setApplicationContext(ApplicationContext applicationContext){
65
66 if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {
67 this.webApplicationContext = (WebApplicationContext)applicationContext;
68 } else {
69 logger.error("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
70 }
71 }
72
73 /**
74 * Find a property primarily in the ServletContext and secondarily
75 * in the environment variables of the OS. So a property can be set
76 * by three means:
77 * <ol>
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>
82 * </ol>
83 *
84 * @param property usually a string constant defined in a subclass of
85 * <code>AbstractWebApplicationConfigurer</code> names <code>ATTRIBUTE_*</code>
86 * @param required
87 * @return
88 */
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
94 if(value == null){
95 value = System.getProperty(property);
96 }
97 if(value == null && userDefinedProperties != null){
98 value = userDefinedProperties.getProperty(property);
99 }
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 ...");
107 System.exit(-1);
108 }
109 return value;
110 }
111
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;
117 } else {
118 messages = new ArrayList<String>();
119 }
120 messages.add(errorMessage);
121 webApplicationContext.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES, messages);
122 }
123
124
125
126 }