From a3199624f4b4138f3c1b3034632deb1aa8dfb84b Mon Sep 17 00:00:00 2001 From: MiniDigger Date: Thu, 16 Jul 2020 23:23:04 +0200 Subject: [PATCH] auto gen dao beans --- pom.xml | 4 +++ .../minidigger/hangar/config/JDBIConfig.java | 35 +++++++++++-------- .../hangar/controller/UsersController.java | 7 ++-- .../minidigger/hangar/db/dao/HangarDao.java | 14 ++++++++ .../hangar/db/model/UsersTable.java | 3 +- .../HangarAuthenticationProvider.java | 7 ++-- .../hangar/service/AuthenticationService.java | 9 ++--- 7 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/main/java/me/minidigger/hangar/db/dao/HangarDao.java diff --git a/pom.xml b/pom.xml index b4512dee4..20227818d 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,10 @@ org.jdbi jdbi3-postgres + + org.jdbi + jdbi3-spring4 + diff --git a/src/main/java/me/minidigger/hangar/config/JDBIConfig.java b/src/main/java/me/minidigger/hangar/config/JDBIConfig.java index 407afac3b..e3cd608d8 100644 --- a/src/main/java/me/minidigger/hangar/config/JDBIConfig.java +++ b/src/main/java/me/minidigger/hangar/config/JDBIConfig.java @@ -1,31 +1,25 @@ package me.minidigger.hangar.config; import org.jdbi.v3.core.Jdbi; -import org.jdbi.v3.core.mapper.RowMapper; import org.jdbi.v3.core.spi.JdbiPlugin; import org.jdbi.v3.postgres.PostgresPlugin; +import org.jdbi.v3.spring4.JdbiFactoryBean; import org.jdbi.v3.sqlobject.SqlObjectPlugin; +import org.springframework.beans.factory.InjectionPoint; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; +import org.springframework.context.annotation.Scope; import java.util.List; import javax.sql.DataSource; -import me.minidigger.hangar.db.dao.UserDao; +import me.minidigger.hangar.db.dao.HangarDao; @Configuration public class JDBIConfig { - @Bean - public Jdbi jdbi(DataSource ds, List jdbiPlugins, List> rowMappers) { - TransactionAwareDataSourceProxy proxy = new TransactionAwareDataSourceProxy(ds); - Jdbi jdbi = Jdbi.create(proxy); - jdbiPlugins.forEach(jdbi::installPlugin); - rowMappers.forEach(jdbi::registerRowMapper); - return jdbi; - } - @Bean public JdbiPlugin sqlObjectPlugin() { return new SqlObjectPlugin(); @@ -37,7 +31,20 @@ public class JDBIConfig { } @Bean - public UserDao userDao(Jdbi jdbi) { - return jdbi.onDemand(UserDao.class); // TODO this is bad, replace me with not on demand + public JdbiFactoryBean jdbiFactoryBean(DataSource dataSource, List jdbiPlugins) { + JdbiFactoryBean jdbiFactoryBean = new JdbiFactoryBean(dataSource); + jdbiFactoryBean.setPlugins(jdbiPlugins); + return jdbiFactoryBean; + } + + @Bean + @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) + public HangarDao hangarDao(Jdbi jdbi, InjectionPoint injectionPoint) { + if (injectionPoint instanceof DependencyDescriptor) { + DependencyDescriptor descriptor = (DependencyDescriptor) injectionPoint; + //noinspection unchecked + return new HangarDao<>((T) jdbi.onDemand(descriptor.getResolvableType().getGeneric(0).getRawClass())); + } + return null; } } diff --git a/src/main/java/me/minidigger/hangar/controller/UsersController.java b/src/main/java/me/minidigger/hangar/controller/UsersController.java index f314a9633..bc3469d0f 100644 --- a/src/main/java/me/minidigger/hangar/controller/UsersController.java +++ b/src/main/java/me/minidigger/hangar/controller/UsersController.java @@ -13,6 +13,7 @@ import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpSession; import me.minidigger.hangar.config.HangarConfig; +import me.minidigger.hangar.db.dao.HangarDao; import me.minidigger.hangar.db.dao.UserDao; import me.minidigger.hangar.db.model.UsersTable; import me.minidigger.hangar.service.AuthenticationService; @@ -20,13 +21,13 @@ import me.minidigger.hangar.service.AuthenticationService; @Controller public class UsersController extends HangarController { - private final UserDao userDao; + private final HangarDao userDao; private final HangarConfig hangarConfig; private final AuthenticationService authenticationService; private final ApplicationController applicationController; @Autowired - public UsersController(HangarConfig hangarConfig, UserDao userDao, AuthenticationService authenticationService, ApplicationController applicationController) { + public UsersController(HangarConfig hangarConfig, HangarDao userDao, AuthenticationService authenticationService, ApplicationController applicationController) { this.hangarConfig = hangarConfig; this.userDao = userDao; this.authenticationService = authenticationService; @@ -99,7 +100,7 @@ public class UsersController extends HangarController { @RequestMapping("/{user}") public Object showProjects(@PathVariable String user) { // TODO hacky test shit - UsersTable dbUser = userDao.getByName(user); + UsersTable dbUser = userDao.get().getByName(user); if (dbUser == null) { throw new ResponseStatusException(HttpStatus.NOT_FOUND); } diff --git a/src/main/java/me/minidigger/hangar/db/dao/HangarDao.java b/src/main/java/me/minidigger/hangar/db/dao/HangarDao.java new file mode 100644 index 000000000..68f1cfb3e --- /dev/null +++ b/src/main/java/me/minidigger/hangar/db/dao/HangarDao.java @@ -0,0 +1,14 @@ +package me.minidigger.hangar.db.dao; + +public class HangarDao { + + private final T dao; + + public HangarDao(T dao) { + this.dao = dao; + } + + public T get() { + return dao; + } +} diff --git a/src/main/java/me/minidigger/hangar/db/model/UsersTable.java b/src/main/java/me/minidigger/hangar/db/model/UsersTable.java index 948295797..873e1ea64 100644 --- a/src/main/java/me/minidigger/hangar/db/model/UsersTable.java +++ b/src/main/java/me/minidigger/hangar/db/model/UsersTable.java @@ -1,10 +1,11 @@ package me.minidigger.hangar.db.model; +import java.io.Serializable; import java.time.OffsetDateTime; import java.util.StringJoiner; -public class UsersTable { +public class UsersTable implements Serializable { private long id; private OffsetDateTime createdAt; diff --git a/src/main/java/me/minidigger/hangar/security/HangarAuthenticationProvider.java b/src/main/java/me/minidigger/hangar/security/HangarAuthenticationProvider.java index e02de9d07..35de33521 100644 --- a/src/main/java/me/minidigger/hangar/security/HangarAuthenticationProvider.java +++ b/src/main/java/me/minidigger/hangar/security/HangarAuthenticationProvider.java @@ -8,15 +8,16 @@ import org.springframework.stereotype.Component; import java.util.List; +import me.minidigger.hangar.db.dao.HangarDao; import me.minidigger.hangar.db.dao.UserDao; import me.minidigger.hangar.db.model.UsersTable; @Component public class HangarAuthenticationProvider implements AuthenticationProvider { - private final UserDao userDao; + private final HangarDao userDao; - public HangarAuthenticationProvider(UserDao userDao) { + public HangarAuthenticationProvider(HangarDao userDao) { this.userDao = userDao; } @@ -26,7 +27,7 @@ public class HangarAuthenticationProvider implements AuthenticationProvider { HangarAuthentication auth = (HangarAuthentication) authentication; String name = auth.getName(); - UsersTable usersTable = userDao.getByName(name); + UsersTable usersTable = userDao.get().getByName(name); // TODO validate stuff, guess we need to pass sso stuff here? if (usersTable != null) { diff --git a/src/main/java/me/minidigger/hangar/service/AuthenticationService.java b/src/main/java/me/minidigger/hangar/service/AuthenticationService.java index 4cfc2868e..281b58ef8 100644 --- a/src/main/java/me/minidigger/hangar/service/AuthenticationService.java +++ b/src/main/java/me/minidigger/hangar/service/AuthenticationService.java @@ -12,6 +12,7 @@ import java.time.OffsetDateTime; import java.util.UUID; import me.minidigger.hangar.config.HangarConfig; +import me.minidigger.hangar.db.dao.HangarDao; import me.minidigger.hangar.db.dao.UserDao; import me.minidigger.hangar.db.model.UsersTable; import me.minidigger.hangar.model.generated.ApiSession; @@ -24,11 +25,11 @@ import me.minidigger.hangar.security.HangarAuthentication; public class AuthenticationService { private final HangarConfig hangarConfig; - private final UserDao userDao; + private final HangarDao userDao; private final AuthenticationManager authenticationManager; @Autowired - public AuthenticationService(HangarConfig hangarConfig, UserDao userDao, AuthenticationManager authenticationManager) { + public AuthenticationService(HangarConfig hangarConfig, HangarDao userDao, AuthenticationManager authenticationManager) { this.hangarConfig = hangarConfig; this.userDao = userDao; this.authenticationManager = authenticationManager; @@ -77,7 +78,7 @@ public class AuthenticationService { public void loginAsFakeUser() { String username = hangarConfig.getFakeUserName(); - UsersTable userEntry = userDao.getByName(username); + UsersTable userEntry = userDao.get().getByName(username); if (userEntry == null) { userEntry = new UsersTable(); userEntry.setEmail(hangarConfig.getFakeUserEmail()); @@ -86,7 +87,7 @@ public class AuthenticationService { userEntry.setId(hangarConfig.getFakeUserId()); userEntry.setReadPrompts(new int[0]); - userEntry = userDao.insert(userEntry); + userEntry = userDao.get().insert(userEntry); } // TODO properly do auth, remember me shit too Authentication auth = new HangarAuthentication(userEntry.getName());