1 package dst.ass2.service.auth;
3 import javax.persistence.EntityManager;
4 import javax.persistence.PersistenceContext;
6 import org.springframework.beans.BeansException;
7 import org.springframework.beans.factory.annotation.Value;
8 import org.springframework.beans.factory.config.BeanPostProcessor;
9 import org.springframework.boot.SpringBootConfiguration;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Profile;
12 import org.springframework.context.annotation.PropertySource;
13 import org.springframework.orm.jpa.JpaTransactionManager;
14 import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
15 import org.springframework.transaction.PlatformTransactionManager;
17 import dst.ass1.jpa.dao.IDAOFactory;
18 import dst.ass1.jpa.dao.impl.DAOFactory;
19 import dst.ass1.jpa.model.IModelFactory;
20 import dst.ass1.jpa.model.impl.ModelFactory;
21 import dst.ass1.jpa.tests.TestData;
22 import dst.ass1.jpa.util.Constants;
23 import dst.ass2.service.auth.grpc.GrpcServerProperties;
25 @SpringBootConfiguration
26 @PropertySource("classpath:/dst/ass2/service/auth/grpc.properties")
27 public class AuthenticationServiceApplicationConfig {
30 private EntityManager em;
33 public IModelFactory modelFactory() {
34 return new ModelFactory();
38 public IDAOFactory daoFactory() {
39 return new DAOFactory(em);
43 public GrpcServerProperties grpcServerProperties(@Value("${grpc.port}") int port) {
44 return new GrpcServerProperties(port);
48 public SpringGrpcServerRunner springGrpcServerRunner() {
49 return new SpringGrpcServerRunner();
53 public LocalEntityManagerFactoryBean entityManagerFactoryBean() {
54 LocalEntityManagerFactoryBean bean = new LocalEntityManagerFactoryBean();
55 bean.setPersistenceUnitName(Constants.JPA_PERSISTENCE_UNIT);
56 // fixes collection proxy problem when using jersey
57 bean.getJpaPropertyMap().put("hibernate.enable_lazy_load_no_trans", true);
62 public PlatformTransactionManager transactionManager(LocalEntityManagerFactoryBean entityManagerFactoryBean) {
63 JpaTransactionManager transactionManager = new JpaTransactionManager();
64 transactionManager.setPersistenceUnitName(Constants.JPA_PERSISTENCE_UNIT);
65 transactionManager.setEntityManagerFactory(entityManagerFactoryBean.getObject());
66 return transactionManager;
71 public TestData testData() {
72 return new TestData();
77 public TestDataInserter testDataInserter(TestData testData, IModelFactory modelFactory, PlatformTransactionManager transactionManager) {
78 return new TestDataInserter(testData, modelFactory, transactionManager);
83 public AuthServiceDataInjector dataInjector(TestDataInserter testDataInserter) {
84 return new AuthServiceDataInjector(em, testDataInserter);
88 * Makes sure data is in the database before the {@link ICachingAuthenticationService} is initialized.
90 public static class AuthServiceDataInjector implements BeanPostProcessor {
91 private boolean dataInjected = false;
93 private EntityManager em;
94 private TestDataInserter testDataInserter;
96 public AuthServiceDataInjector(EntityManager em, TestDataInserter testDataInserter) {
98 this.testDataInserter = testDataInserter;
102 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
103 if (!dataInjected && (bean instanceof ICachingAuthenticationService)) {
104 testDataInserter.insertTestData(em);
111 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {