mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-11-15 11:39:11 +03:00
[FIX] SessionFactoryManager. Фикс дерегестрации хука AuthLimiterComponent.
This commit is contained in:
parent
11a23b4464
commit
483e052fff
4 changed files with 41 additions and 15 deletions
|
@ -8,16 +8,20 @@
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
import pro.gravit.launchserver.socket.Client;
|
import pro.gravit.launchserver.socket.Client;
|
||||||
import pro.gravit.launchserver.websocket.json.auth.AuthResponse;
|
import pro.gravit.launchserver.websocket.json.auth.AuthResponse;
|
||||||
|
import pro.gravit.utils.BiHookSet.Hook;
|
||||||
import pro.gravit.utils.HookException;
|
import pro.gravit.utils.HookException;
|
||||||
|
|
||||||
public class AuthLimiterComponent extends Component implements NeedGarbageCollection {
|
public class AuthLimiterComponent extends Component implements NeedGarbageCollection, AutoCloseable {
|
||||||
|
private transient final Hook<AuthResponse.AuthContext, Client> prA = this::preAuthHook;
|
||||||
|
private transient LaunchServer srv;
|
||||||
@Override
|
@Override
|
||||||
public void preInit(LaunchServer launchServer) {
|
public void preInit(LaunchServer launchServer) {
|
||||||
|
srv = launchServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(LaunchServer launchServer) {
|
public void init(LaunchServer launchServer) {
|
||||||
launchServer.authHookManager.preHook.registerHook(this::preAuthHook);
|
launchServer.authHookManager.preHook.registerHook(prA);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -105,4 +109,9 @@ public boolean isLimit(String ip) {
|
||||||
map.put(ip, new AuthEntry(1, System.currentTimeMillis()));
|
map.put(ip, new AuthEntry(1, System.currentTimeMillis()));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws Exception {
|
||||||
|
srv.authHookManager.preHook.unregisterHook(prA);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
|
||||||
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.LaunchServerDaoFactory;
|
||||||
import pro.gravit.launchserver.dao.User;
|
import pro.gravit.launchserver.dao.User;
|
||||||
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
|
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
|
||||||
|
import pro.gravit.launchserver.hibernate.SessionFactoryManager;
|
||||||
import pro.gravit.utils.helper.CommonHelper;
|
import pro.gravit.utils.helper.CommonHelper;
|
||||||
|
|
||||||
public class HibernateConfiguratorComponent extends Component {
|
public class HibernateConfiguratorComponent extends Component {
|
||||||
|
@ -19,7 +19,6 @@ 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;
|
||||||
public SessionFactory sessionFactory = null;
|
|
||||||
@Override
|
@Override
|
||||||
public void preInit(LaunchServer launchServer) {
|
public void preInit(LaunchServer launchServer) {
|
||||||
LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new);
|
LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new);
|
||||||
|
@ -33,7 +32,7 @@ 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());
|
||||||
sessionFactory = cfg.buildSessionFactory();
|
SessionFactoryManager.forLaunchServer(launchServer).fact = cfg.buildSessionFactory();
|
||||||
};
|
};
|
||||||
if(parallelHibernateInit)
|
if(parallelHibernateInit)
|
||||||
CommonHelper.newThread("Hibernate Thread", true, init);
|
CommonHelper.newThread("Hibernate Thread", true, init);
|
||||||
|
|
|
@ -12,25 +12,25 @@
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
|
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
import pro.gravit.launchserver.components.HibernateConfiguratorComponent;
|
|
||||||
import pro.gravit.launchserver.dao.User;
|
import pro.gravit.launchserver.dao.User;
|
||||||
import pro.gravit.launchserver.dao.UserDAO;
|
import pro.gravit.launchserver.dao.UserDAO;
|
||||||
|
import pro.gravit.launchserver.hibernate.SessionFactoryManager;
|
||||||
|
|
||||||
public class HibernateUserDAOImpl implements UserDAO {
|
public class HibernateUserDAOImpl implements UserDAO {
|
||||||
private final HibernateConfiguratorComponent hibernate;
|
private final SessionFactoryManager manager;
|
||||||
|
|
||||||
public HibernateUserDAOImpl(LaunchServer srv) {
|
public HibernateUserDAOImpl(LaunchServer srv) {
|
||||||
hibernate = (HibernateConfiguratorComponent) srv.config.components.values().stream().filter(e -> HibernateConfiguratorComponent.class.isInstance(e)).findFirst().get();
|
manager = SessionFactoryManager.forLaunchServer(srv);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findById(int id) {
|
public User findById(int id) {
|
||||||
try (Session s = hibernate.sessionFactory.openSession()) {
|
try (Session s = manager.fact.openSession()) {
|
||||||
return s.get(User.class, id);
|
return s.get(User.class, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public User findByUsername(String username) {
|
||||||
EntityManager em = hibernate.sessionFactory.createEntityManager();
|
EntityManager em = manager.fact.createEntityManager();
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||||
|
@ -42,7 +42,7 @@ public User findByUsername(String username) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUUID(UUID uuid) {
|
public User findByUUID(UUID uuid) {
|
||||||
EntityManager em = hibernate.sessionFactory.createEntityManager();
|
EntityManager em = manager.fact.createEntityManager();
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
||||||
|
@ -54,7 +54,7 @@ public User findByUUID(UUID uuid) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void save(User user) {
|
public void save(User user) {
|
||||||
try (Session session = hibernate.sessionFactory.openSession()) {
|
try (Session session = manager.fact.openSession()) {
|
||||||
Transaction tx1 = session.beginTransaction();
|
Transaction tx1 = session.beginTransaction();
|
||||||
session.save(user);
|
session.save(user);
|
||||||
tx1.commit();
|
tx1.commit();
|
||||||
|
@ -62,7 +62,7 @@ public void save(User user) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(User user) {
|
public void update(User user) {
|
||||||
try (Session session = hibernate.sessionFactory.openSession()) {
|
try (Session session = manager.fact.openSession()) {
|
||||||
Transaction tx1 = session.beginTransaction();
|
Transaction tx1 = session.beginTransaction();
|
||||||
session.update(user);
|
session.update(user);
|
||||||
tx1.commit();
|
tx1.commit();
|
||||||
|
@ -70,7 +70,7 @@ public void update(User user) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(User user) {
|
public void delete(User user) {
|
||||||
try (Session session = hibernate.sessionFactory.openSession()) {
|
try (Session session = manager.fact.openSession()) {
|
||||||
Transaction tx1 = session.beginTransaction();
|
Transaction tx1 = session.beginTransaction();
|
||||||
session.delete(user);
|
session.delete(user);
|
||||||
tx1.commit();
|
tx1.commit();
|
||||||
|
@ -79,7 +79,7 @@ public void delete(User user) {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public List<User> findAll() {
|
public List<User> findAll() {
|
||||||
try (Session s = hibernate.sessionFactory.openSession()) {
|
try (Session s = manager.fact.openSession()) {
|
||||||
return (List<User>) s.createQuery("From User").list();
|
return (List<User>) s.createQuery("From User").list();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package pro.gravit.launchserver.hibernate;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
|
|
||||||
|
public class SessionFactoryManager {
|
||||||
|
public SessionFactory fact;
|
||||||
|
private static final Map<LaunchServer, SessionFactoryManager> sessionFactories = new ConcurrentHashMap<>();
|
||||||
|
private static final Function<LaunchServer, SessionFactoryManager> cr = e -> new SessionFactoryManager();
|
||||||
|
public static SessionFactoryManager forLaunchServer(LaunchServer srv) {
|
||||||
|
return sessionFactories.computeIfAbsent(srv, cr);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue