Subversion Repositories SmartDukaan

Rev

Rev 27626 | Rev 31514 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
24472 amit.gupta 1
 
21165 ashik.ali 2
package com.spice.profitmandi.web.config;
3
 
21343 kshitij.so 4
import java.io.IOException;
29637 amit.gupta 5
import java.time.LocalDate;
25300 tejbeer 6
import java.time.LocalDateTime;
21343 kshitij.so 7
import java.util.Properties;
8
 
24472 amit.gupta 9
import org.apache.logging.log4j.LogManager;
23568 govind 10
import org.apache.logging.log4j.Logger;
27028 tejbeer 11
import org.apache.velocity.app.VelocityEngine;
12
import org.apache.velocity.exception.VelocityException;
21165 ashik.ali 13
import org.springframework.context.annotation.Bean;
14
import org.springframework.context.annotation.ComponentScan;
15
import org.springframework.context.annotation.Configuration;
21304 kshitij.so 16
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
21165 ashik.ali 17
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
21304 kshitij.so 18
import org.springframework.core.io.ClassPathResource;
19
import org.springframework.core.io.Resource;
22398 amit.gupta 20
import org.springframework.mail.javamail.JavaMailSender;
21
import org.springframework.mail.javamail.JavaMailSenderImpl;
27028 tejbeer 22
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21165 ashik.ali 23
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
24
 
25300 tejbeer 25
import com.google.gson.Gson;
26
import com.google.gson.GsonBuilder;
29637 amit.gupta 27
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
25300 tejbeer 28
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
29
 
21165 ashik.ali 30
@Configuration
31
@ComponentScan("com.spice.profitmandi.*")
32
public class AppConfig {
21304 kshitij.so 33
 
25300 tejbeer 34
	private static final String PATH_PREFIX = "/WEB-INF/views/";
35
	private static final String PATH_SUFFIX = ".jsp";
36
	private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
37
	private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
21362 kshitij.so 38
	private static Resource resource;
25300 tejbeer 39
 
21362 kshitij.so 40
	public static Resource getResource() {
41
		return resource;
42
	}
43
 
44
	public void setResource(Resource resource) {
45
		AppConfig.resource = resource;
46
	}
47
 
27028 tejbeer 48
	/*
49
	 * @Bean(name = "viewResolver") public InternalResourceViewResolver
50
	 * getViewResolver() { LOGGER.debug("creating view resolver bean with prefix : "
51
	 * + PATH_PREFIX + " and suffix : " + PATH_SUFFIX); InternalResourceViewResolver
52
	 * viewResolver = new InternalResourceViewResolver();
53
	 * viewResolver.setPrefix(PATH_PREFIX); viewResolver.setSuffix(PATH_SUFFIX);
54
	 * return viewResolver; }
55
	 */
21304 kshitij.so 56
 
25300 tejbeer 57
	@Bean(name = "messageSource")
21165 ashik.ali 58
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
25300 tejbeer 59
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
60
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21165 ashik.ali 61
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
62
		return messageSource;
63
	}
21304 kshitij.so 64
 
25300 tejbeer 65
	@Bean(name = "multipartResolver")
21165 ashik.ali 66
	public CommonsMultipartResolver getCommonsMultipartResolver() {
67
		LOGGER.info("creating common multipart resolver bean");
24472 amit.gupta 68
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
69
		multipartResolver.setMaxUploadSizePerFile(5000000);
70
		multipartResolver.setMaxUploadSize(5000000);
71
		return multipartResolver;
21304 kshitij.so 72
	}
73
 
74
	@Bean
21643 ashik.ali 75
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
21304 kshitij.so 76
		String activeProfile;
77
 
25300 tejbeer 78
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
79
 
21343 kshitij.so 80
		Properties properties = new Properties();
81
		try {
82
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
83
		} catch (IOException e) {
25300 tejbeer 84
			LOGGER.error("Error in reading env property file.Adding default property" + e);
21343 kshitij.so 85
			properties.put("profile", "dev");
86
		}
87
		activeProfile = (String) properties.get("profile");
21304 kshitij.so 88
 
89
		if ("prod".equals(activeProfile)) {
90
			resource = new ClassPathResource("/META-INF/prod.properties");
91
		} else if ("staging".equals(activeProfile)) {
21342 kshitij.so 92
			resource = new ClassPathResource("/META-INF/staging.properties");
21304 kshitij.so 93
		} else {
94
			resource = new ClassPathResource("/META-INF/dev.properties");
95
		}
96
 
97
		propertySourcesPlaceholderConfigurer.setLocation(resource);
98
 
99
		return propertySourcesPlaceholderConfigurer;
100
	}
25300 tejbeer 101
 
102
	@Bean(name = "mailSender")
103
	public JavaMailSender getGmailSender() {
104
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
105
 
106
		// Using gmail
107
		mailSender.setHost("smtp.sendgrid.net");
108
		mailSender.setPort(587);
27626 amit.gupta 109
		mailSender.setUsername("apikey");
110
		mailSender.setPassword("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");
25300 tejbeer 111
 
112
		Properties javaMailProperties = new Properties();
113
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
114
		javaMailProperties.put("mail.smtp.auth", "true");
115
		javaMailProperties.put("mail.transport.protocol", "smtp");
116
		javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
117
 
118
		mailSender.setJavaMailProperties(javaMailProperties);
119
		return mailSender;
120
	}
121
 
122
	@Bean(name = "gson")
123
	public Gson gson() {
124
 
125
		Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
29637 amit.gupta 126
				.registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
127
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter())
128
				.create();
25300 tejbeer 129
 
130
		return gson;
131
 
132
	}
133
 
27028 tejbeer 134
	@Bean(name = "veloctyEngine")
135
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
136
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
137
		// Properties props = new Properties();
138
		// props.put("resource.loader", "file");
139
 
140
		// props.put("file.resource.loader.description", "Velocity File Resource
141
		// Loader");
142
		// props.put("file.resource.loader.class",
143
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
144
		// props.put("file.resource.loader.cache", true);
145
		// props.put("file.resource.loader.path", ".");
146
 
147
		Properties velocityProperties = new Properties();
148
		velocityProperties.put("resource.loader", "class");
149
		velocityProperties.put("class.resource.loader.class",
150
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
151
		velocityProperties.put("file.resource.loader.cache", true);
152
		velocityProperties.put("file.resource.loader.unicode", true);
153
		velocityProperties.put("input.encoding", "UTF-8");
154
		velocityProperties.put("output.encoding", "UTF-8");
155
		velocityProperties.put("overrideLogging", true);
156
 
157
	//	velocityProperties.put("file.resource.loader.path", ".");
158
 
159
		factory.setVelocityProperties(velocityProperties);
160
 
161
		return factory.createVelocityEngine();
162
 
163
	}
21165 ashik.ali 164
}