Commit d10bdeac authored by igoshin's avatar igoshin

#12869 java based configuration

parent d9fbdfab
package org.genesys2.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
@Configuration
@Import({
SpringCommonConfig.class,
SpringAclConfig.class,
SpringSchedulerConfig.class,
SpringDataBaseConfig.class,
SpringMailConfig.class,
SpringSecurityOauthConfig.class,
SpringCacheConfig.class})
@ImportResource({"classpath:/spring/spring-security.xml"})
public class ApplicationConfig {
}
package org.genesys2.spring.config;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.genesys2.server.cache.HazelcastAclCache;
import org.genesys2.server.security.Slf4jAuditLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.acls.AclPermissionEvaluator;
import org.springframework.security.acls.domain.AclAuthorizationStrategyImpl;
import org.springframework.security.acls.domain.DefaultPermissionGrantingStrategy;
import org.springframework.security.acls.jdbc.BasicLookupStrategy;
import org.springframework.security.acls.jdbc.JdbcMutableAclService;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@Configuration
public class SpringAclConfig {
@Autowired
private DataSource dataSource;
@Autowired
private CacheManager cacheManager;
@Bean(name = "permissionEvaluator")
public AclPermissionEvaluator aclPermissionEvaluator() {
return new AclPermissionEvaluator(aclService());
}
@Bean
public JdbcMutableAclService aclService() {
return new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache());
}
@Bean
public AclAuthorizationStrategyImpl aclAuthorizationStrategy() {
return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ADMINISTRATOR"));
}
@Bean
public Slf4jAuditLogger slf4jAuditLogger() {
return new Slf4jAuditLogger();
}
@Bean
public DefaultPermissionGrantingStrategy permissionGrantingStrategy() {
return new DefaultPermissionGrantingStrategy(slf4jAuditLogger());
}
@Bean
public HazelcastAclCache aclCache() {
return new HazelcastAclCache(aclCacheCache(), permissionGrantingStrategy(), aclAuthorizationStrategy());
}
@Bean
public Cache aclCacheCache() {
return cacheManager.getCache("aclcache");
}
@Bean
public BasicLookupStrategy lookupStrategy() {
return new BasicLookupStrategy(dataSource, aclCache(), aclAuthorizationStrategy(), permissionGrantingStrategy());
}
}
package org.genesys2.spring.config;
import com.hazelcast.web.WebFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import java.util.Properties;
@Configuration
@EnableCaching
@ImportResource("classpath:/spring/spring-cache.xml")
public class SpringCacheConfig {
@Value("${base.cookie-domain}")
private String cookieDomain;
@Value("${base.cookie-secure}")
private String cookieSecure;
@Value("${base.cookie-http-only}")
private String cookieHttpOnly;
@Bean
public WebFilter hazelcastWebFilter() {
return new WebFilter(filterProps());
}
private Properties filterProps() {
Properties properties = new Properties();
properties.setProperty("use-client", "false");
properties.setProperty("map-name", "jetty-sessions");
properties.setProperty("sticky-session", "false");
properties.setProperty("cookie-name", "hz-session-id");
properties.setProperty("cookie-domain", cookieDomain);
properties.setProperty("cookie-secure", cookieSecure);
properties.setProperty("cookie-http-only", cookieHttpOnly);
properties.setProperty("debug", "true");
properties.setProperty("instance-name", "genesys");
properties.setProperty("shutdown-on-destroy", "true");
return properties;
}
}
package org.genesys2.spring.config;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.genesys2.spring.validation.oval.spring.SpringOvalValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.security.acls.AclPermissionEvaluator;
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
import org.springframework.validation.Validator;
import javax.persistence.Entity;
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"org.genesys2.spring","org.genesys2.server"}, includeFilters = @ComponentScan.Filter(value = Entity.class, type = FilterType.ANNOTATION))
@Configuration
public class SpringCommonConfig {
@Autowired
private AclPermissionEvaluator aclPermissionEvaluator;
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("application.properties"));
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("spring/spring.properties"));
return propertyPlaceholderConfigurer;
}
@Scope("singleton")
@Bean
public Validator validator() {
SpringOvalValidator validator = new SpringOvalValidator();
validator.setValidator(new net.sf.oval.Validator());
return validator;
}
@Bean
public Jackson2ObjectMapperFactoryBean objectMapper(){
Jackson2ObjectMapperFactoryBean mapperFactoryBean = new Jackson2ObjectMapperFactoryBean();
mapperFactoryBean.setFeaturesToDisable(SerializationFeature.FAIL_ON_EMPTY_BEANS, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapperFactoryBean;
}
@Bean
public DefaultWebSecurityExpressionHandler webExpressionHandler(){
DefaultWebSecurityExpressionHandler handler= new DefaultWebSecurityExpressionHandler();
handler.setPermissionEvaluator(aclPermissionEvaluator);
return handler;
}
}
package org.genesys2.spring.config;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.util.Properties;
@EnableJpaRepositories(
basePackages = {"org.genesys2.server.persistence.domain", "org.genesys2.server.persistence.acl"},
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager",
repositoryImplementationPostfix = "CustomImpl")
//@EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableTransactionManagement
@Configuration
@ImportResource("classpath:/spring/spring-db.xml")
public class SpringDataBaseConfig {
@Value("${db.url}")
private String dbUrl;
@Value("${db.driverClassName}")
private String dbDriverClassName;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
@Value("${db.showSql}")
private boolean dbShowSql;
@Value("${db.dialect}")
private String dbDialect;
@Value("${db.hbm2ddl}")
private String dbHbm2ddl;
@Value("${lucene.indexDir}")
private String luceneIndexDir;
@Bean
public DataSource dataSource() {
DataSource dataSource = new DataSource();
dataSource.setUrl(dbUrl);
dataSource.setDriverClassName(dbDriverClassName);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(true);
return dataSource;
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
entityManager.setPersistenceUnitName("spring-jpa");
entityManager.setJpaVendorAdapter(hibernateJpaVendorAdapter());
entityManager.setJpaProperties(jpaProperties());
entityManager.setPackagesToScan(
"org.genesys2.server.model.acl",
"org.genesys2.server.model.impl",
"org.genesys2.server.model.genesys",
"org.genesys2.server.model.oauth");
return entityManager;
}
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setDataSource(dataSource());
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
private Properties jpaProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", dbDialect);
properties.setProperty("hibernate.connection.charSet", "utf8");
properties.setProperty("hibernate.connection.autocommit", "false");
properties.setProperty("hibernate.hbm2ddl.auto", dbHbm2ddl);
properties.setProperty("hibernate.search.default.indexBase", luceneIndexDir);
properties.setProperty("hibernate.search.default.exclusive_index_use", "false");
properties.setProperty("hibernate.search.default.​worker.execution", "async");
return properties;
}
private HibernateJpaVendorAdapter hibernateJpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(dbShowSql);
return jpaVendorAdapter;
}
}
package org.genesys2.spring.config;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import java.util.Properties;
@Configuration
public class SpringMailConfig {
@Value("${mail.host}")
private String host;
@Value("${mail.port}")
private Integer port;
@Value("${mail.user.name}")
private String userName;
@Value("${mail.user.password}")
private String pwd;
@Value("${mail.smtp.ssl.enable}")
private String sslEnable;
@Value("${mail.smtp.starttls.enable}")
private String smtpStarttlsEnable;
@Value("${mail.smtp.auth}")
private String smtpAuth;
@Value("${mail.transport.protocol}")
private String protocol;
@Bean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setHost(host);
sender.setPort(port);
sender.setUsername(userName);
sender.setPassword(pwd);
sender.setJavaMailProperties(javaMailProperties());
return sender;
}
@Bean
public VelocityEngine velocityEngine() {
VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean();
velocityEngineFactoryBean.setVelocityProperties(velocityProperties());
return velocityEngineFactoryBean.getObject();
}
private Properties javaMailProperties() {
Properties properties = new Properties();
properties.setProperty("mail.smtp.ssl.enable", sslEnable);
properties.setProperty("mail.smtp.starttls.enable", smtpStarttlsEnable);
properties.setProperty("mail.smtp.auth", smtpAuth);
properties.setProperty("mail.transport.protocol", protocol);
return properties;
}
private Properties velocityProperties() {
Properties properties = new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
properties.setProperty("velocimacro.permissions.allow.inline.local.scope", "false");
properties.setProperty("runtime.log.logsystem.log4j.logger", "velocity");
properties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.Log4JLogChute");
return properties;
}
}
package org.genesys2.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@EnableAsync
@EnableScheduling
@Configuration
public class SpringSchedulerConfig {
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(16);
taskExecutor.afterPropertiesSet();
return taskExecutor;
}
}
package org.genesys2.spring.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource("classpath:/spring/spring-security-oauth.xml")
public class SpringSecurityOauthConfig {
}
package org.genesys2.spring.config;
import org.genesys2.spring.AddStuffInterceptor;
import org.genesys2.spring.BetterSessionLocaleResolver;
import org.genesys2.spring.validation.oval.spring.SpringOvalValidator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.Validator;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.theme.CookieThemeResolver;
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.HashSet;
import java.util.Locale;
import java.util.Properties;
import java.util.Set;
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = {"org.genesys2.server.servlet.filter","org.genesys2.server.servlet.controller"})
@Configuration
public class SpringServletConfig extends WebMvcConfigurerAdapter {
@Value("${theme.defaultThemeName}")
private String defaultThemeName;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/html/**").addResourceLocations("/html/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
ThemeChangeInterceptor themeChangeInterceptor = new ThemeChangeInterceptor();
themeChangeInterceptor.setParamName("theme");
registry.addInterceptor(localeChangeInterceptor);
registry.addInterceptor(themeChangeInterceptor);
registry.addInterceptor(new AddStuffInterceptor());
}
@Scope("singleton")
@Bean
@Override
public Validator getValidator() {
SpringOvalValidator validator = new SpringOvalValidator();
validator.setValidator(new net.sf.oval.Validator());
return validator;
}
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("application.properties"));
propertyPlaceholderConfigurer.setLocation(new ClassPathResource("spring/spring.properties"));
return propertyPlaceholderConfigurer;
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
resolver.setExposedContextBeanNames(new String[]{"props", "jspHelper"});
resolver.setRedirectHttp10Compatible(false);
resolver.setRequestContextAttribute("requestContext");
return resolver;
}
@Bean
public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
Properties properties = new Properties();
properties.setProperty("javax.servlet.UnavailableException", "/404");
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
resolver.setExceptionMappings(properties);
return resolver;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("content/language");
source.setDefaultEncoding("UTF-8");
source.setUseCodeAsDefaultMessage(true);
return source;
}
@Bean
public BetterSessionLocaleResolver localeResolver() {
Set<String> supportedLocales = new HashSet();
supportedLocales.add("en");
supportedLocales.add("ar");
supportedLocales.add("de");
supportedLocales.add("fa");
supportedLocales.add("fr");
supportedLocales.add("pt");
supportedLocales.add("ru");
supportedLocales.add("zh");
supportedLocales.add("sl");
BetterSessionLocaleResolver resolver = new BetterSessionLocaleResolver();
resolver.setDefaultLocale(Locale.forLanguageTag("en"));
resolver.setSupportedLocales(supportedLocales);
return resolver;
}
@Bean
public CookieThemeResolver themeResolver() {
CookieThemeResolver cookieThemeResolver = new CookieThemeResolver();
cookieThemeResolver.setDefaultThemeName(defaultThemeName);
return cookieThemeResolver;
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2014 Global Crop Diversity Trust
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:property-placeholder ignore-resource-not-found="true" location="classpath:/application.properties,classpath:/spring/spring.properties,classpath:/genesys.properties" />
<!-- This ensures things are loaded in correct order -->