diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java index 6799d7b7..6e7be2ce 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/LaunchServer.java @@ -64,6 +64,7 @@ import pro.gravit.launchserver.components.Component; import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; import pro.gravit.launchserver.dao.UserService; +import pro.gravit.launchserver.dao.provider.DaoProvider; import pro.gravit.launchserver.manangers.LaunchServerGsonManager; import pro.gravit.launchserver.manangers.MirrorManager; import pro.gravit.launchserver.manangers.ModulesManager; @@ -113,6 +114,8 @@ public static final class Config { public AuthProviderPair[] auth; + public DaoProvider dao; + private transient AuthProviderPair authDefault; public AuthProviderPair getAuthProviderPair(String name) { @@ -425,8 +428,6 @@ public static void main(String... args) throws Throwable { public final ModulesManager modulesManager; - public final UserService userService; - public final MirrorManager mirrorManager; public final ReloadManager reloadManager; @@ -590,7 +591,6 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException reconfigurableManager = new ReconfigurableManager(); authHookManager = new AuthHookManager(); configManager = new ConfigManager(); - userService = new UserService(this); GarbageManager.registerNeedGC(sessionManager); reloadManager.registerReloadable("launchServer", this); registerObject("permissionsHandler", config.permissionsHandler); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java index 1dc4df16..883807e9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java @@ -8,31 +8,31 @@ public class HibernateAuthHandler extends CachedAuthHandler { @Override protected Entry fetchEntry(String username) throws IOException { - User user = srv.userService.findUserByUsername(username); + User user = srv.config.dao.userService.findUserByUsername(username); if(user == null) return null; return new Entry(user.uuid, username, user.getAccessToken(), user.serverID); } @Override protected Entry fetchEntry(UUID uuid) throws IOException { - User user = srv.userService.findUserByUUID(uuid); + User user = srv.config.dao.userService.findUserByUUID(uuid); if(user == null) return null; return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID); } @Override protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { - User user = srv.userService.findUserByUUID(uuid); + User user = srv.config.dao.userService.findUserByUUID(uuid); user.setAccessToken(accessToken); - srv.userService.updateUser(user); + srv.config.dao.userService.updateUser(user); return true; } @Override protected boolean updateServerID(UUID uuid, String serverID) throws IOException { - User user = srv.userService.findUserByUUID(uuid); + User user = srv.config.dao.userService.findUserByUUID(uuid); user.serverID = serverID; - srv.userService.updateUser(user); + srv.config.dao.userService.updateUser(user); return true; } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java index 8298b76a..2e1be761 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/permissions/HibernatePermissionsHandler.java @@ -7,17 +7,17 @@ public class HibernatePermissionsHandler extends PermissionsHandler { @Override public ClientPermissions getPermissions(String username) { - User user = srv.userService.findUserByUsername(username); + User user = srv.config.dao.userService.findUserByUsername(username); if(user == null) return ClientPermissions.DEFAULT; return user.getPermissions(); } @Override public void setPermissions(String username, ClientPermissions permissions) { - User user = srv.userService.findUserByUsername(username); + User user = srv.config.dao.userService.findUserByUsername(username); if(user == null) return; user.setPermissions(permissions); - srv.userService.updateUser(user); + srv.config.dao.userService.updateUser(user); } @Override diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java index b9ec6985..b737f79e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/HibernateAuthProvider.java @@ -9,7 +9,7 @@ public class HibernateAuthProvider extends AuthProvider { @Override public AuthProviderResult auth(String login, String password, String ip) throws Exception { - User user = srv.userService.findUserByUsername(login); + User user = srv.config.dao.userService.findUserByUsername(login); if(user == null || !user.verifyPassword(password)) { if(user ==null) throw new AuthException("Username incorrect"); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java index e5aa13fa..c0664aca 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetAllUsersCommand.java @@ -23,7 +23,7 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { int count = 0; - for(User user : server.userService.findAllUsers()) + for(User user : server.config.dao.userService.findAllUsers()) { LogHelper.subInfo("[%s] UUID: %s", user.username, user.uuid.toString()); count++; diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java index 1d681228..d5788850 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/GetUserCommand.java @@ -24,7 +24,7 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { verifyArgs(args, 1); - User user = server.userService.findUserByUsername(args[0]); + User user = server.config.dao.userService.findUserByUsername(args[0]); if(user == null) { LogHelper.error("User %s not found", args[0]); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java index 40a43f80..04b759e3 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/RegisterCommand.java @@ -29,7 +29,7 @@ public void invoke(String... args) throws Exception { user.username = args[0]; user.setPassword(args[1]); user.uuid = UUID.randomUUID(); - server.userService.saveUser(user); + server.config.dao.userService.saveUser(user); LogHelper.info("User %s registered. UUID: %s", user.username, user.uuid.toString()); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java index e0b38d9c..75ef14ff 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/command/dao/SetUserPasswordCommand.java @@ -24,14 +24,14 @@ public String getUsageDescription() { @Override public void invoke(String... args) throws Exception { verifyArgs(args, 2); - User user = server.userService.findUserByUsername(args[0]); + User user = server.config.dao.userService.findUserByUsername(args[0]); if(user == null) { LogHelper.error("User %s not found", args[1]); return; } user.setPassword(args[1]); - server.userService.updateUser(user); + server.config.dao.userService.updateUser(user); LogHelper.info("[%s] UUID: %s | New Password: %s", user.username, user.uuid.toString(), args[1]); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java index 966c3389..f19ebe1e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/Component.java @@ -11,7 +11,6 @@ public static void registerComponents() { if (!registredComp) { providers.register("authLimiter", AuthLimiterComponent.class); providers.register("commandRemover", CommandRemoverComponent.class); - providers.register("hibernate", HibernateConfiguratorComponent.class); registredComp = true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java deleted file mode 100644 index 9c5b39ef..00000000 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/LaunchServerDaoFactory.java +++ /dev/null @@ -1,22 +0,0 @@ -package pro.gravit.launchserver.dao; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; - -import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.dao.impl.DefaultUserDAOImpl; - -public class LaunchServerDaoFactory { - private static final Function defDao = DefaultUserDAOImpl::new; - private static final Map> daos = new ConcurrentHashMap<>(); - - public static void setUserDaoProvider(LaunchServer srv, Function getDao) { - daos.put(srv, getDao); - } - - public static UserDAO createUserDao(LaunchServer srv) - { - return daos.getOrDefault(srv, defDao).apply(srv); - } -} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java index 57155f32..d2cef2a9 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/UserService.java @@ -9,8 +9,8 @@ public class UserService { private final UserDAO usersDao; - public UserService(LaunchServer server) { - usersDao = LaunchServerDaoFactory.createUserDao(server); + public UserService(UserDAO usersDAO) { + this.usersDao = usersDAO; } public User findUser(int id) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/DaoProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/DaoProvider.java new file mode 100644 index 00000000..ab5586cd --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/DaoProvider.java @@ -0,0 +1,17 @@ +package pro.gravit.launchserver.dao.provider; + +import pro.gravit.launchserver.LaunchServer; +import pro.gravit.launchserver.dao.UserDAO; +import pro.gravit.launchserver.dao.UserService; +import pro.gravit.utils.ProviderMap; + +public abstract class DaoProvider { + public static ProviderMap providers = new ProviderMap<>("DaoProvider"); + public UserDAO userDAO; + public UserService userService; + public static void registerProviders() + { + providers.register("hibernate", HibernateDaoProvider.class); + } + public abstract void init(LaunchServer server); +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java similarity index 60% rename from LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java rename to LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java index 62818726..c000e80e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java @@ -1,18 +1,17 @@ -package pro.gravit.launchserver.components; - -import java.nio.file.Paths; +package pro.gravit.launchserver.dao.provider; import org.hibernate.cfg.Configuration; - import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.dao.LaunchServerDaoFactory; import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.UserHWID; +import pro.gravit.launchserver.dao.UserService; import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; import pro.gravit.launchserver.hibernate.SessionFactoryManager; import pro.gravit.utils.helper.CommonHelper; -public class HibernateConfiguratorComponent extends Component { +import java.nio.file.Paths; + +public class HibernateDaoProvider extends DaoProvider { public String driver; public String url; public String username; @@ -20,9 +19,11 @@ public class HibernateConfiguratorComponent extends Component { public String pool_size; public String hibernateConfig; public boolean parallelHibernateInit; + @Override - public void preInit(LaunchServer launchServer) { - LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new); + public void init(LaunchServer server) { + userDAO = new HibernateUserDAOImpl(server); + userService = new UserService(userDAO); Runnable init = () -> { Configuration cfg = new Configuration() .addAnnotatedClass(User.class) @@ -34,30 +35,11 @@ public void preInit(LaunchServer launchServer) { .setProperty("hibernate.connection.pool_size", pool_size); if(hibernateConfig != null) cfg.configure(Paths.get(hibernateConfig).toFile()); - SessionFactoryManager.forLaunchServer(launchServer).fact = cfg.buildSessionFactory(); + SessionFactoryManager.forLaunchServer(server).fact = cfg.buildSessionFactory(); }; if(parallelHibernateInit) CommonHelper.newThread("Hibernate Thread", true, init); else init.run(); } - - @Override - public void init(LaunchServer launchServer) { - - } - - @Override - public void postInit(LaunchServer launchServer) { - //UserService service = new UserService(); - //List users = service.findAllUsers(); - //User newUser = new User(); - //newUser.username = "VeryTestUser"; - //newUser.setPassword("12345"); - //service.saveUser(newUser); - //for(User u : users) - //{ - // LogHelper.info("Found User %s", u.username); - //} - } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java index b39a9a9a..cc07d782 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/manangers/LaunchServerGsonManager.java @@ -10,6 +10,7 @@ import pro.gravit.launchserver.auth.provider.AuthProvider; import pro.gravit.launchserver.auth.texture.TextureProvider; import pro.gravit.launchserver.components.Component; +import pro.gravit.launchserver.dao.provider.DaoProvider; import pro.gravit.utils.UniversalJsonAdapter; public class LaunchServerGsonManager extends GsonManager { @@ -23,5 +24,6 @@ public void registerAdapters(GsonBuilder builder) { builder.registerTypeAdapter(HWIDHandler.class, new UniversalJsonAdapter<>(HWIDHandler.providers)); builder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers)); builder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers)); + builder.registerTypeAdapter(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers)); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java index b9508345..b5fae77e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/socket/response/auth/RegisterResponse.java @@ -24,7 +24,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception sendError("Hash invalid"); return; } - User checkUser = server.userService.findUserByUsername(login); + User checkUser = server.config.dao.userService.findUserByUsername(login); if(checkUser != null) { sendError("User already register"); @@ -34,7 +34,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception user.username = login; user.setPassword(password); user.uuid = UUID.randomUUID(); - server.userService.saveUser(user); + server.config.dao.userService.saveUser(user); } @Override public String getType() {