Project

General

Profile

Download (2.71 KB) Statistics
| Branch: | Tag: | Revision:
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.util.ArrayList;
13
import java.util.List;
14

    
15
import org.apache.log4j.Logger;
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.context.ApplicationContext;
18
import org.springframework.web.context.WebApplicationContext;
19

    
20
/**
21
 * @author a.kohlbecker
22
 * @date 20.07.2010
23
 *
24
 */
25
public abstract class AbstractWebApplicationConfigurer {
26

    
27
    private static final String ATTRIBUTE_ERROR_MESSAGES = "cdm.errorMessages";
28

    
29
	public static final Logger logger = Logger.getLogger(AbstractWebApplicationConfigurer.class);
30
	
31
	WebApplicationContext webApplicationContext;
32

    
33
	
34
	@Autowired
35
	public void setApplicationContext(ApplicationContext applicationContext){
36

    
37
		if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {
38
			this.webApplicationContext = (WebApplicationContext)applicationContext;
39
		} else {
40
			logger.error("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
41
		}
42
	}
43

    
44
	/**
45
	 * Find a property in the ServletContext if not found search in a second
46
	 * step in the environment variables of the OS
47
	 * 
48
	 * @param property
49
	 * @param required
50
	 * @return
51
	 */
52
	protected String findProperty(String property, boolean required) {
53
		// 1. look for the property in the ServletContext
54
		Object obj = webApplicationContext.getServletContext().getAttribute(property);
55
		String value = (String)obj;
56
		// 2. look for the property in environment variables of the OS
57
		if(value == null){
58
			value = System.getProperty(property);
59
		}
60
		if(value == null && required){
61
			logger.error("property {" + property + "} not found.");
62
			logger.error("--> This property can be set in two ways:");
63
			logger.error("--> 		1. as attribute to the ServletContext");
64
			logger.error("--> 		2. as system property e.g. -D" + property);
65
			logger.error("Stopping application ...");
66
			System.exit(-1);
67
		}
68
		return value;
69
	}
70

    
71
	protected void addErrorMessageToServletContextAttributes(String errorMessage) {
72
		Object o = webApplicationContext.getServletContext().getAttribute(ATTRIBUTE_ERROR_MESSAGES);
73
		List<String> messages;
74
		if(o != null  && o instanceof List<?>){
75
			messages = (List<String>) o;
76
		} else {
77
			messages = new ArrayList<String>();
78
		}
79
		messages.add(errorMessage);
80
		webApplicationContext.getServletContext().setAttribute(ATTRIBUTE_ERROR_MESSAGES, messages);
81
	}
82
	
83
   
84

    
85
}
(1-1/3)