Project

General

Profile

« Previous | Next » 

Revision 11aca88d

Added by Andreas Kohlbecker over 13 years ago

logging to file improved

View differences:

.gitattributes
2621 2621
cdmlib-remote/src/main/java/carbonfive/spring/web/pathparameter/ParameterizedPathMatcher.java -text
2622 2622
cdmlib-remote/src/main/java/carbonfive/spring/web/pathparameter/ParameterizedPathMatcherException.java -text
2623 2623
cdmlib-remote/src/main/java/carbonfive/spring/web/pathparameter/ParameterizedUrlHandlerMapping.java -text
2624
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/DataSourceConfig.java -text
2624
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/AbstractWebApplicationConfigurer.java -text
2625
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/DataSourceConfigurer.java -text
2626
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/LoggingConfigurer.java -text
2625 2627
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/AbstractController.java -text
2626 2628
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/AbstractListController.java -text
2627 2629
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/AbstractOaiPmhController.java -text
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 org.apache.log4j.Logger;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.context.ApplicationContext;
15
import org.springframework.web.context.WebApplicationContext;
16

  
17
/**
18
 * @author a.kohlbecker
19
 * @date 20.07.2010
20
 *
21
 */
22
public abstract class AbstractWebApplicationConfigurer {
23

  
24
	public static final Logger logger = Logger.getLogger(AbstractWebApplicationConfigurer.class);
25
	
26
	WebApplicationContext webApplicationContext;
27

  
28
	
29
	@Autowired
30
	public void setApplicationContext(ApplicationContext applicationContext){
31

  
32
		if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {
33
			this.webApplicationContext = (WebApplicationContext)applicationContext;
34
		} else {
35
			logger.error("The " + this.getClass().getSimpleName() + " only can be used within a WebApplicationContext");
36
		}
37
	}
38

  
39
	protected String findProperty(String property, boolean required) {
40
		// 1. look for the property in the ServletContext
41
		Object obj = webApplicationContext.getServletContext().getAttribute(property);
42
		String value = (String)obj;
43
		// 2. look for the property in environment variables of the OS
44
		if(value == null){
45
			value = System.getProperty(property);
46
		}
47
		if(value == null && required){
48
			logger.error("property {" + property + "} not found.");
49
			logger.error("--> This property can be set in two ways:");
50
			logger.error("--> 		1. as attribute to the ServletContext");
51
			logger.error("--> 		2. as system property e.g. -D" + property);
52
			logger.error("Stopping application ...");
53
			System.exit(-1);
54
		}
55
		return value;
56
	}
57
	
58
   
59

  
60
}
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/DataSourceConfig.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

  
11
package eu.etaxonomy.cdm.remote.config;
12

  
13
import java.io.File;
14
import java.lang.reflect.InvocationTargetException;
15
import java.lang.reflect.Method;
16
import java.util.Properties;
17

  
18
import javax.naming.NamingException;
19
import javax.sql.DataSource;
20

  
21
import org.apache.log4j.Logger;
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.beans.factory.xml.XmlBeanFactory;
24
import org.springframework.context.ApplicationContext;
25
import org.springframework.context.annotation.Bean;
26
import org.springframework.context.annotation.Configuration;
27
import org.springframework.core.io.FileSystemResource;
28
import org.springframework.jndi.JndiObjectFactoryBean;
29
import org.springframework.jndi.JndiTemplate;
30
import org.springframework.web.context.WebApplicationContext;
31

  
32
@Configuration
33
public class DataSourceConfig {
34
	
35
	public static final Logger logger = Logger.getLogger(DataSourceConfig.class);
36

  
37
    private static final String ATTRIBUTE_JDBC_JNDI_NAME = "cdm.jdbcJndiName";
38
    private static final String ATTRIBUTE_HIBERNATE_DIALECT = "hibernate.dialect";
39
    private static final String CDM_BEAN_DEFINITION_FILE = "cdm.beanDefinitionFile";
40
    private static final String ATTRIBUTE_DATASOURCE_NAME = "cdm.datasource";
41

  
42
    private static final String DATASOURCE_BEANDEF_DEFAULT = System.getProperty("user.home")+File.separator+".cdmLibrary"+File.separator+"datasources.xml";
43

  
44
	private static String beanDefinitionFile = DATASOURCE_BEANDEF_DEFAULT;
45
	
46
	public void setBeanDefinitionFile(String filename){
47
		beanDefinitionFile = filename;
48
	}
49
	
50
	private WebApplicationContext webApplicationContext;
51
	private DataSource dataSource;
52
	
53
	private Properties getHibernateProperties() {
54
		Properties hibernateProperties = webApplicationContext.getBean("jndiHibernateProperties", Properties.class);
55
		return hibernateProperties;
56
	}
57

  
58
	@Autowired
59
	public void setApplicationContext(ApplicationContext applicationContext){
60

  
61
		if(WebApplicationContext.class.isAssignableFrom(applicationContext.getClass())) {
62
			this.webApplicationContext = (WebApplicationContext)applicationContext;
63
		} else {
64
			logger.error("The DataSourceConfig class only can be used within a WebApplicationContext");
65
		}
66
	}
67
	
68
	@Bean
69
	public DataSource dataSource() {
70
		if(this.dataSource == null){
71
			String jndiName = findProperty(ATTRIBUTE_JDBC_JNDI_NAME, false);
72
			
73
			if(jndiName != null){
74
				dataSource = useJndiDataSource(jndiName);
75
			} else {
76
				String beanName = findProperty(ATTRIBUTE_DATASOURCE_NAME, true);
77
				dataSource = loadDataSourceBean(beanName);
78
			}			
79
		}
80
		return dataSource; 
81
	}
82

  
83
	private DataSource useJndiDataSource(String jndiName) {
84
		logger.info("using jndi datasource '" + jndiName + "'");
85

  
86
		JndiObjectFactoryBean jndiFactory = new JndiObjectFactoryBean();
87
		/*
88
		JndiTemplate jndiTemplate = new JndiTemplate();
89
		jndiFactory.setJndiTemplate(jndiTemplate); no need to use a JndiTemplate 
90
		if I try using JndiTemplate I get an org.hibernate.AnnotationException: "Unknown Id.generator: system-increment" 
91
		when running multiple intances via the Bootloader
92
		*/
93
		jndiFactory.setResourceRef(true);
94
		jndiFactory.setJndiName(jndiName);
95
		try {
96
			jndiFactory.afterPropertiesSet();
97
		} catch (IllegalArgumentException e) {
98
			logger.error(e);
99
		} catch (NamingException e) {
100
			logger.error(e);
101
		}
102
		Object obj = jndiFactory.getObject();
103
		return (DataSource)obj;
104
	}
105
	
106
	private DataSource loadDataSourceBean(String beanName) {
107
		logger.info("using datasource '"+beanName);
108

  
109
		String beanDefinitionFileFromProperty = findProperty(CDM_BEAN_DEFINITION_FILE, false);
110
		String path = (beanDefinitionFileFromProperty != null ? beanDefinitionFileFromProperty : beanDefinitionFile);
111
		logger.info("loading DataSourceBean '" + beanName + "' from: " + path);
112
		FileSystemResource file = new FileSystemResource(path);
113
		XmlBeanFactory beanFactory  = new XmlBeanFactory(file);
114
	
115
		return beanFactory.getBean(beanName, DataSource.class);
116
	}
117
	
118
	@Bean
119
	public Properties hibernateProperties(){
120
		Properties props = getHibernateProperties();
121
		props.setProperty(ATTRIBUTE_HIBERNATE_DIALECT, inferHibernateDialectName());
122
		return props;
123
	}
124

  
125
	private String findProperty(String property, boolean required) {
126
		// 1. look for the dataSource beanName in the ServletContext
127
		Object obj = webApplicationContext.getServletContext().getAttribute(property);
128
		String value = (String)obj;
129
		// 2. look for the dataSource beanName in environment variables of the OS
130
		if(value == null){
131
			value = System.getProperty(property);
132
		}
133
		if(value == null && required){
134
			logger.error("property {" + property + "} not found.");
135
			logger.error("--> This property can be set in two ways:");
136
			logger.error("--> 		1. as attribute to the ServletContext");
137
			logger.error("--> 		2. as system property e.g. -D" + property);
138
			logger.error("Stopping application ...");
139
			System.exit(-1);
140
		}
141
		return value;
142
	}
143
	
144
	public String inferHibernateDialectName() {
145
		DataSource ds = dataSource();
146
		String url = "<SEE PRIOR REFLECTION ERROR>";
147
		Method m = null;
148
		try {
149
			m = ds.getClass().getMethod("getUrl");
150
		} catch (SecurityException e) {
151
			logger.error(e);
152
		} catch (NoSuchMethodException e) {
153
			try {
154
				m = ds.getClass().getMethod("getJdbcUrl");
155
			} catch (SecurityException e2) {
156
				logger.error(e2);
157
			} catch (NoSuchMethodException e2) {
158
				logger.error(e2);
159
			}
160
		}
161
		try {
162
			url = (String)m.invoke(ds);
163
		} catch (IllegalArgumentException e) {
164
			logger.error(e);
165
		} catch (IllegalAccessException e) {
166
			logger.error(e);
167
		} catch (InvocationTargetException e) {
168
			logger.error(e);
169
		} catch (SecurityException e) {
170
			logger.error(e);
171
		} 
172
		
173
		if(url != null && url.contains("mysql")){
174
			return "org.hibernate.dialect.MySQLDialect";
175
		}
176
		
177
		logger.error("hibernate dialect mapping for "+url+ " not jet implemented or unavailable");
178
		return null;
179
	}
180

  
181
}
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/DataSourceConfigurer.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

  
11
package eu.etaxonomy.cdm.remote.config;
12

  
13
import java.io.File;
14
import java.lang.reflect.InvocationTargetException;
15
import java.lang.reflect.Method;
16
import java.util.Properties;
17

  
18
import javax.naming.NamingException;
19
import javax.sql.DataSource;
20

  
21
import org.apache.log4j.Logger;
22
import org.springframework.beans.factory.xml.XmlBeanFactory;
23
import org.springframework.context.annotation.Bean;
24
import org.springframework.context.annotation.Configuration;
25
import org.springframework.core.io.FileSystemResource;
26
import org.springframework.jndi.JndiObjectFactoryBean;
27

  
28
@Configuration
29
public class DataSourceConfigurer extends AbstractWebApplicationConfigurer {
30
	
31
	public static final Logger logger = Logger.getLogger(DataSourceConfigurer.class);
32

  
33
    private static final String ATTRIBUTE_JDBC_JNDI_NAME = "cdm.jdbcJndiName";
34
    private static final String ATTRIBUTE_HIBERNATE_DIALECT = "hibernate.dialect";
35
    private static final String CDM_BEAN_DEFINITION_FILE = "cdm.beanDefinitionFile";
36
    private static final String ATTRIBUTE_DATASOURCE_NAME = "cdm.datasource";
37

  
38
    private static final String DATASOURCE_BEANDEF_DEFAULT = System.getProperty("user.home")+File.separator+".cdmLibrary"+File.separator+"datasources.xml";
39

  
40
	private static String beanDefinitionFile = DATASOURCE_BEANDEF_DEFAULT;
41
	
42
	public void setBeanDefinitionFile(String filename){
43
		beanDefinitionFile = filename;
44
	}
45
	
46
	
47
	private DataSource dataSource;
48
	
49
	private Properties getHibernateProperties() {
50
		Properties hibernateProperties = webApplicationContext.getBean("jndiHibernateProperties", Properties.class);
51
		return hibernateProperties;
52
	}
53

  
54

  
55
	
56
	@Bean
57
	public DataSource dataSource() {
58
		if(this.dataSource == null){
59
			String jndiName = findProperty(ATTRIBUTE_JDBC_JNDI_NAME, false);
60
			
61
			if(jndiName != null){
62
				dataSource = useJndiDataSource(jndiName);
63
			} else {
64
				String beanName = findProperty(ATTRIBUTE_DATASOURCE_NAME, true);
65
				dataSource = loadDataSourceBean(beanName);
66
			}
67
			
68
		}
69
		return dataSource; 
70
	}
71

  
72
	private DataSource useJndiDataSource(String jndiName) {
73
		logger.info("using jndi datasource '" + jndiName + "'");
74

  
75
		JndiObjectFactoryBean jndiFactory = new JndiObjectFactoryBean();
76
		/*
77
		JndiTemplate jndiTemplate = new JndiTemplate();
78
		jndiFactory.setJndiTemplate(jndiTemplate); no need to use a JndiTemplate 
79
		if I try using JndiTemplate I get an org.hibernate.AnnotationException: "Unknown Id.generator: system-increment" 
80
		when running multiple intances via the Bootloader
81
		*/
82
		jndiFactory.setResourceRef(true);
83
		jndiFactory.setJndiName(jndiName);
84
		try {
85
			jndiFactory.afterPropertiesSet();
86
		} catch (IllegalArgumentException e) {
87
			logger.error(e);
88
		} catch (NamingException e) {
89
			logger.error(e);
90
		}
91
		Object obj = jndiFactory.getObject();
92
		return (DataSource)obj;
93
	}
94
	
95
	private DataSource loadDataSourceBean(String beanName) {
96
		logger.info("using datasource '"+beanName);
97

  
98
		String beanDefinitionFileFromProperty = findProperty(CDM_BEAN_DEFINITION_FILE, false);
99
		String path = (beanDefinitionFileFromProperty != null ? beanDefinitionFileFromProperty : beanDefinitionFile);
100
		logger.info("loading DataSourceBean '" + beanName + "' from: " + path);
101
		FileSystemResource file = new FileSystemResource(path);
102
		XmlBeanFactory beanFactory  = new XmlBeanFactory(file);
103
	
104
		return beanFactory.getBean(beanName, DataSource.class);
105
	}
106
	
107
	@Bean
108
	public Properties hibernateProperties(){
109
		Properties props = getHibernateProperties();
110
		props.setProperty(ATTRIBUTE_HIBERNATE_DIALECT, inferHibernateDialectName());
111
		return props;
112
	}
113

  
114
	public String inferHibernateDialectName() {
115
		DataSource ds = dataSource();
116
		String url = "<SEE PRIOR REFLECTION ERROR>";
117
		Method m = null;
118
		try {
119
			m = ds.getClass().getMethod("getUrl");
120
		} catch (SecurityException e) {
121
			logger.error(e);
122
		} catch (NoSuchMethodException e) {
123
			try {
124
				m = ds.getClass().getMethod("getJdbcUrl");
125
			} catch (SecurityException e2) {
126
				logger.error(e2);
127
			} catch (NoSuchMethodException e2) {
128
				logger.error(e2);
129
			}
130
		}
131
		try {
132
			url = (String)m.invoke(ds);
133
		} catch (IllegalArgumentException e) {
134
			logger.error(e);
135
		} catch (IllegalAccessException e) {
136
			logger.error(e);
137
		} catch (InvocationTargetException e) {
138
			logger.error(e);
139
		} catch (SecurityException e) {
140
			logger.error(e);
141
		} 
142
		
143
		if(url != null && url.contains("mysql")){
144
			return "org.hibernate.dialect.MySQLDialect";
145
		}
146
		
147
		logger.error("hibernate dialect mapping for "+url+ " not jet implemented or unavailable");
148
		return null;
149
	}
150

  
151
}
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/config/LoggingConfigurer.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.IOException;
13

  
14
import org.apache.log4j.Logger;
15
import org.apache.log4j.PatternLayout;
16
import org.apache.log4j.RollingFileAppender;
17
import org.springframework.beans.factory.InitializingBean;
18
import org.springframework.context.annotation.Configuration;
19

  
20
/**
21
 * @author a.kohlbecker
22
 * @date 20.07.2010
23
 * 
24
 */
25
@Configuration
26
public class LoggingConfigurer extends AbstractWebApplicationConfigurer implements InitializingBean  {
27

  
28
	private static final String CDM_LOGFILE = "cdm.logfile";
29

  
30
	protected void configureLogFile() {
31
		PatternLayout layout = new PatternLayout("%d %p [%c] - %m%n");
32
		String logFile = findProperty(CDM_LOGFILE, false);
33
		try {
34
			RollingFileAppender appender = new RollingFileAppender(layout, logFile);
35
			appender.setMaxBackupIndex(3);
36
			appender.setMaxFileSize("250MB");
37
			Logger.getRootLogger().addAppender(appender);
38
			logger.info("logging to :" + logFile);
39
		} catch (IOException e) {
40
			logger.error("Creating RollingFileAppender failed:", e);
41
		}
42
	}
43

  
44
	/* (non-Javadoc)
45
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
46
	 */
47
	@Override
48
	public void afterPropertiesSet() throws Exception {
49
		configureLogFile();
50
	}
51
	
52
	
53

  
54
}

Also available in: Unified diff