[FEATURE] DaoProvider

This commit is contained in:
Gravit 2019-07-01 15:22:48 +07:00
parent c8211ae7dd
commit 8ef61b8673
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
15 changed files with 51 additions and 73 deletions

View file

@ -64,6 +64,7 @@
import pro.gravit.launchserver.components.Component; import pro.gravit.launchserver.components.Component;
import pro.gravit.launchserver.config.LaunchServerRuntimeConfig; import pro.gravit.launchserver.config.LaunchServerRuntimeConfig;
import pro.gravit.launchserver.dao.UserService; import pro.gravit.launchserver.dao.UserService;
import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.launchserver.manangers.LaunchServerGsonManager; import pro.gravit.launchserver.manangers.LaunchServerGsonManager;
import pro.gravit.launchserver.manangers.MirrorManager; import pro.gravit.launchserver.manangers.MirrorManager;
import pro.gravit.launchserver.manangers.ModulesManager; import pro.gravit.launchserver.manangers.ModulesManager;
@ -113,6 +114,8 @@ public static final class Config {
public AuthProviderPair[] auth; public AuthProviderPair[] auth;
public DaoProvider dao;
private transient AuthProviderPair authDefault; private transient AuthProviderPair authDefault;
public AuthProviderPair getAuthProviderPair(String name) { public AuthProviderPair getAuthProviderPair(String name) {
@ -425,8 +428,6 @@ public static void main(String... args) throws Throwable {
public final ModulesManager modulesManager; public final ModulesManager modulesManager;
public final UserService userService;
public final MirrorManager mirrorManager; public final MirrorManager mirrorManager;
public final ReloadManager reloadManager; public final ReloadManager reloadManager;
@ -590,7 +591,6 @@ public LaunchServer(Path dir, boolean testEnv, String[] args) throws IOException
reconfigurableManager = new ReconfigurableManager(); reconfigurableManager = new ReconfigurableManager();
authHookManager = new AuthHookManager(); authHookManager = new AuthHookManager();
configManager = new ConfigManager(); configManager = new ConfigManager();
userService = new UserService(this);
GarbageManager.registerNeedGC(sessionManager); GarbageManager.registerNeedGC(sessionManager);
reloadManager.registerReloadable("launchServer", this); reloadManager.registerReloadable("launchServer", this);
registerObject("permissionsHandler", config.permissionsHandler); registerObject("permissionsHandler", config.permissionsHandler);

View file

@ -8,31 +8,31 @@
public class HibernateAuthHandler extends CachedAuthHandler { public class HibernateAuthHandler extends CachedAuthHandler {
@Override @Override
protected Entry fetchEntry(String username) throws IOException { 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; if(user == null) return null;
return new Entry(user.uuid, username, user.getAccessToken(), user.serverID); return new Entry(user.uuid, username, user.getAccessToken(), user.serverID);
} }
@Override @Override
protected Entry fetchEntry(UUID uuid) throws IOException { 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; if(user == null) return null;
return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID); return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID);
} }
@Override @Override
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { 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); user.setAccessToken(accessToken);
srv.userService.updateUser(user); srv.config.dao.userService.updateUser(user);
return true; return true;
} }
@Override @Override
protected boolean updateServerID(UUID uuid, String serverID) throws IOException { 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; user.serverID = serverID;
srv.userService.updateUser(user); srv.config.dao.userService.updateUser(user);
return true; return true;
} }

View file

@ -7,17 +7,17 @@ public class HibernatePermissionsHandler extends PermissionsHandler {
@Override @Override
public ClientPermissions getPermissions(String username) { 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; if(user == null) return ClientPermissions.DEFAULT;
return user.getPermissions(); return user.getPermissions();
} }
@Override @Override
public void setPermissions(String username, ClientPermissions permissions) { 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; if(user == null) return;
user.setPermissions(permissions); user.setPermissions(permissions);
srv.userService.updateUser(user); srv.config.dao.userService.updateUser(user);
} }
@Override @Override

View file

@ -9,7 +9,7 @@
public class HibernateAuthProvider extends AuthProvider { public class HibernateAuthProvider extends AuthProvider {
@Override @Override
public AuthProviderResult auth(String login, String password, String ip) throws Exception { 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 || !user.verifyPassword(password))
{ {
if(user ==null) throw new AuthException("Username incorrect"); if(user ==null) throw new AuthException("Username incorrect");

View file

@ -23,7 +23,7 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
int count = 0; 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()); LogHelper.subInfo("[%s] UUID: %s", user.username, user.uuid.toString());
count++; count++;

View file

@ -24,7 +24,7 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
verifyArgs(args, 1); verifyArgs(args, 1);
User user = server.userService.findUserByUsername(args[0]); User user = server.config.dao.userService.findUserByUsername(args[0]);
if(user == null) if(user == null)
{ {
LogHelper.error("User %s not found", args[0]); LogHelper.error("User %s not found", args[0]);

View file

@ -29,7 +29,7 @@ public void invoke(String... args) throws Exception {
user.username = args[0]; user.username = args[0];
user.setPassword(args[1]); user.setPassword(args[1]);
user.uuid = UUID.randomUUID(); 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()); LogHelper.info("User %s registered. UUID: %s", user.username, user.uuid.toString());
} }
} }

View file

@ -24,14 +24,14 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
verifyArgs(args, 2); verifyArgs(args, 2);
User user = server.userService.findUserByUsername(args[0]); User user = server.config.dao.userService.findUserByUsername(args[0]);
if(user == null) if(user == null)
{ {
LogHelper.error("User %s not found", args[1]); LogHelper.error("User %s not found", args[1]);
return; return;
} }
user.setPassword(args[1]); 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]); LogHelper.info("[%s] UUID: %s | New Password: %s", user.username, user.uuid.toString(), args[1]);
} }
} }

View file

@ -11,7 +11,6 @@ public static void registerComponents() {
if (!registredComp) { if (!registredComp) {
providers.register("authLimiter", AuthLimiterComponent.class); providers.register("authLimiter", AuthLimiterComponent.class);
providers.register("commandRemover", CommandRemoverComponent.class); providers.register("commandRemover", CommandRemoverComponent.class);
providers.register("hibernate", HibernateConfiguratorComponent.class);
registredComp = true; registredComp = true;
} }
} }

View file

@ -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<LaunchServer, UserDAO> defDao = DefaultUserDAOImpl::new;
private static final Map<LaunchServer, Function<LaunchServer, UserDAO>> daos = new ConcurrentHashMap<>();
public static void setUserDaoProvider(LaunchServer srv, Function<LaunchServer, UserDAO> getDao) {
daos.put(srv, getDao);
}
public static UserDAO createUserDao(LaunchServer srv)
{
return daos.getOrDefault(srv, defDao).apply(srv);
}
}

View file

@ -9,8 +9,8 @@ public class UserService {
private final UserDAO usersDao; private final UserDAO usersDao;
public UserService(LaunchServer server) { public UserService(UserDAO usersDAO) {
usersDao = LaunchServerDaoFactory.createUserDao(server); this.usersDao = usersDAO;
} }
public User findUser(int id) { public User findUser(int id) {

View file

@ -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<DaoProvider> 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);
}

View file

@ -1,18 +1,17 @@
package pro.gravit.launchserver.components; package pro.gravit.launchserver.dao.provider;
import java.nio.file.Paths;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.LaunchServer;
import pro.gravit.launchserver.dao.LaunchServerDaoFactory;
import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.User;
import pro.gravit.launchserver.dao.UserHWID; import pro.gravit.launchserver.dao.UserHWID;
import pro.gravit.launchserver.dao.UserService;
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl; import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
import pro.gravit.launchserver.hibernate.SessionFactoryManager; import pro.gravit.launchserver.hibernate.SessionFactoryManager;
import pro.gravit.utils.helper.CommonHelper; 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 driver;
public String url; public String url;
public String username; public String username;
@ -20,9 +19,11 @@ public class HibernateConfiguratorComponent extends Component {
public String pool_size; public String pool_size;
public String hibernateConfig; public String hibernateConfig;
public boolean parallelHibernateInit; public boolean parallelHibernateInit;
@Override @Override
public void preInit(LaunchServer launchServer) { public void init(LaunchServer server) {
LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new); userDAO = new HibernateUserDAOImpl(server);
userService = new UserService(userDAO);
Runnable init = () -> { Runnable init = () -> {
Configuration cfg = new Configuration() Configuration cfg = new Configuration()
.addAnnotatedClass(User.class) .addAnnotatedClass(User.class)
@ -34,30 +35,11 @@ public void preInit(LaunchServer launchServer) {
.setProperty("hibernate.connection.pool_size", pool_size); .setProperty("hibernate.connection.pool_size", pool_size);
if(hibernateConfig != null) if(hibernateConfig != null)
cfg.configure(Paths.get(hibernateConfig).toFile()); cfg.configure(Paths.get(hibernateConfig).toFile());
SessionFactoryManager.forLaunchServer(launchServer).fact = cfg.buildSessionFactory(); SessionFactoryManager.forLaunchServer(server).fact = cfg.buildSessionFactory();
}; };
if(parallelHibernateInit) if(parallelHibernateInit)
CommonHelper.newThread("Hibernate Thread", true, init); CommonHelper.newThread("Hibernate Thread", true, init);
else else
init.run(); init.run();
} }
@Override
public void init(LaunchServer launchServer) {
}
@Override
public void postInit(LaunchServer launchServer) {
//UserService service = new UserService();
//List<User> 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);
//}
}
} }

View file

@ -10,6 +10,7 @@
import pro.gravit.launchserver.auth.provider.AuthProvider; import pro.gravit.launchserver.auth.provider.AuthProvider;
import pro.gravit.launchserver.auth.texture.TextureProvider; import pro.gravit.launchserver.auth.texture.TextureProvider;
import pro.gravit.launchserver.components.Component; import pro.gravit.launchserver.components.Component;
import pro.gravit.launchserver.dao.provider.DaoProvider;
import pro.gravit.utils.UniversalJsonAdapter; import pro.gravit.utils.UniversalJsonAdapter;
public class LaunchServerGsonManager extends GsonManager { public class LaunchServerGsonManager extends GsonManager {
@ -23,5 +24,6 @@ public void registerAdapters(GsonBuilder builder) {
builder.registerTypeAdapter(HWIDHandler.class, new UniversalJsonAdapter<>(HWIDHandler.providers)); builder.registerTypeAdapter(HWIDHandler.class, new UniversalJsonAdapter<>(HWIDHandler.providers));
builder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers)); builder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers));
builder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers)); builder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers));
builder.registerTypeAdapter(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers));
} }
} }

View file

@ -24,7 +24,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception
sendError("Hash invalid"); sendError("Hash invalid");
return; return;
} }
User checkUser = server.userService.findUserByUsername(login); User checkUser = server.config.dao.userService.findUserByUsername(login);
if(checkUser != null) if(checkUser != null)
{ {
sendError("User already register"); sendError("User already register");
@ -34,7 +34,7 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception
user.username = login; user.username = login;
user.setPassword(password); user.setPassword(password);
user.uuid = UUID.randomUUID(); user.uuid = UUID.randomUUID();
server.userService.saveUser(user); server.config.dao.userService.saveUser(user);
} }
@Override @Override
public String getType() { public String getType() {