Subversion Repositories SmartDukaan

Rev

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