Subversion Repositories SmartDukaan

Rev

Rev 21749 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21749 ashik.ali 1
package com.spice.profitmandi.web.config;
21541 ashik.ali 2
 
3
import java.io.IOException;
25300 tejbeer 4
import java.time.LocalDateTime;
21541 ashik.ali 5
import java.util.Properties;
6
 
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.context.annotation.Bean;
10
import org.springframework.context.annotation.ComponentScan;
11
import org.springframework.context.annotation.Configuration;
12
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
13
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
14
import org.springframework.core.io.ClassPathResource;
15
import org.springframework.core.io.Resource;
16
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
17
import org.springframework.web.servlet.view.InternalResourceViewResolver;
18
 
25300 tejbeer 19
import com.google.gson.Gson;
20
import com.google.gson.GsonBuilder;
21
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
22
 
21541 ashik.ali 23
@Configuration
24
@ComponentScan("com.spice.profitmandi.*")
25
public class AppConfig {
26
 
25300 tejbeer 27
	// private static final String FTL_PATH_PREFIX="/WEB-INF/views/ftl/";
28
	// private static final String FTL_PATH_SUFFIX=".ftl";
29
	private static final String HTML_PATH_PREFIX = "/WEB-INF/views/";
30
	private static final String HTML_PATH_SUFFIX = ".jsp";
31
	private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
32
	private static final Logger LOGGER = LoggerFactory.getLogger(AppConfig.class);
21541 ashik.ali 33
	private static Resource resource;
25300 tejbeer 34
 
21541 ashik.ali 35
	public static Resource getResource() {
36
		return resource;
37
	}
38
 
39
	public void setResource(Resource resource) {
40
		AppConfig.resource = resource;
41
	}
42
 
25300 tejbeer 43
	/*
44
	 * @Bean public FreeMarkerConfigurer freemarkerConfig() { FreeMarkerConfigurer
45
	 * freeMarkerConfigurer = new FreeMarkerConfigurer();
46
	 * freeMarkerConfigurer.setTemplateLoaderPath(PATH_PREFIX); return
47
	 * freeMarkerConfigurer; }
48
	 * 
49
	 * @Bean public FreeMarkerViewResolver freemarkerViewResolver() {
50
	 * FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
51
	 * resolver.setCache(false); resolver.setPrefix("");
52
	 * resolver.setSuffix(PATH_SUFFIX); return resolver; }
53
	 */
54
 
21541 ashik.ali 55
	@Bean(name = "viewResolver")
56
	public InternalResourceViewResolver getViewResolver() {
25300 tejbeer 57
		LOGGER.debug(
58
				"creating view resolver bean with prefix : " + HTML_PATH_PREFIX + " and suffix : " + HTML_PATH_SUFFIX);
21541 ashik.ali 59
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
60
		viewResolver.setPrefix(HTML_PATH_PREFIX);
61
		viewResolver.setSuffix(HTML_PATH_SUFFIX);
62
		return viewResolver;
63
	}
64
 
25300 tejbeer 65
	@Bean(name = "messageSource")
21541 ashik.ali 66
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
25300 tejbeer 67
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
68
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21541 ashik.ali 69
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
70
		return messageSource;
71
	}
72
 
25300 tejbeer 73
	@Bean(name = "multipartResolver")
21541 ashik.ali 74
	public CommonsMultipartResolver getCommonsMultipartResolver() {
75
		LOGGER.info("creating common multipart resolver bean");
76
		return new CommonsMultipartResolver();
77
	}
78
 
79
	@Bean
80
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
81
		String activeProfile;
82
 
25300 tejbeer 83
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
84
 
21541 ashik.ali 85
		Properties properties = new Properties();
86
		try {
87
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
88
		} catch (IOException e) {
25300 tejbeer 89
			LOGGER.error("Error in reading env property file.Adding default property" + e);
21541 ashik.ali 90
			properties.put("profile", "dev");
91
		}
92
		activeProfile = (String) properties.get("profile");
93
 
94
		if ("prod".equals(activeProfile)) {
95
			resource = new ClassPathResource("/META-INF/prod.properties");
96
		} else if ("staging".equals(activeProfile)) {
97
			resource = new ClassPathResource("/META-INF/staging.properties");
98
		} else {
99
			resource = new ClassPathResource("/META-INF/dev.properties");
100
		}
101
 
102
		propertySourcesPlaceholderConfigurer.setLocation(resource);
103
 
104
		return propertySourcesPlaceholderConfigurer;
105
	}
25300 tejbeer 106
 
107
	@Bean(name = "gson")
108
	public Gson gson() {
109
 
110
		Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
111
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
112
 
113
		return gson;
114
 
115
	}
116
 
21541 ashik.ali 117
}