From 483e052fff7008d661b7fa1622f7de7df65904e1 Mon Sep 17 00:00:00 2001 From: zaxar163 Date: Thu, 6 Jun 2019 11:01:30 +0300 Subject: [PATCH] =?UTF-8?q?[FIX]=20SessionFactoryManager.=20=D0=A4=D0=B8?= =?UTF-8?q?=D0=BA=D1=81=20=D0=B4=D0=B5=D1=80=D0=B5=D0=B3=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B8=20=D1=85=D1=83=D0=BA=D0=B0=20Aut?= =?UTF-8?q?hLimiterComponent.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/AuthLimiterComponent.java | 13 ++++++++++-- .../HibernateConfiguratorComponent.java | 5 ++--- .../dao/impl/HibernateUserDAOImpl.java | 20 +++++++++---------- .../hibernate/SessionFactoryManager.java | 18 +++++++++++++++++ 4 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/SessionFactoryManager.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java index 07cad044..61b889c4 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/AuthLimiterComponent.java @@ -8,16 +8,20 @@ import pro.gravit.launchserver.LaunchServer; import pro.gravit.launchserver.socket.Client; import pro.gravit.launchserver.websocket.json.auth.AuthResponse; +import pro.gravit.utils.BiHookSet.Hook; import pro.gravit.utils.HookException; -public class AuthLimiterComponent extends Component implements NeedGarbageCollection { +public class AuthLimiterComponent extends Component implements NeedGarbageCollection, AutoCloseable { + private transient final Hook prA = this::preAuthHook; + private transient LaunchServer srv; @Override public void preInit(LaunchServer launchServer) { + srv = launchServer; } @Override public void init(LaunchServer launchServer) { - launchServer.authHookManager.preHook.registerHook(this::preAuthHook); + launchServer.authHookManager.preHook.registerHook(prA); } @Override @@ -105,4 +109,9 @@ public boolean isLimit(String ip) { map.put(ip, new AuthEntry(1, System.currentTimeMillis())); return false; } + + @Override + public void close() throws Exception { + srv.authHookManager.preHook.unregisterHook(prA); + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java index 87ff23e9..765f1273 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/components/HibernateConfiguratorComponent.java @@ -2,13 +2,13 @@ import java.nio.file.Paths; -import org.hibernate.SessionFactory; 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.impl.HibernateUserDAOImpl; +import pro.gravit.launchserver.hibernate.SessionFactoryManager; import pro.gravit.utils.helper.CommonHelper; public class HibernateConfiguratorComponent extends Component { @@ -19,7 +19,6 @@ public class HibernateConfiguratorComponent extends Component { public String pool_size; public String hibernateConfig; public boolean parallelHibernateInit; - public SessionFactory sessionFactory = null; @Override public void preInit(LaunchServer launchServer) { LaunchServerDaoFactory.setUserDaoProvider(launchServer, HibernateUserDAOImpl::new); @@ -33,7 +32,7 @@ public void preInit(LaunchServer launchServer) { .setProperty("hibernate.connection.pool_size", pool_size); if(hibernateConfig != null) cfg.configure(Paths.get(hibernateConfig).toFile()); - sessionFactory = cfg.buildSessionFactory(); + SessionFactoryManager.forLaunchServer(launchServer).fact = cfg.buildSessionFactory(); }; if(parallelHibernateInit) CommonHelper.newThread("Hibernate Thread", true, init); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java index a7420506..fe4b8686 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/HibernateUserDAOImpl.java @@ -12,25 +12,25 @@ import org.hibernate.Transaction; import pro.gravit.launchserver.LaunchServer; -import pro.gravit.launchserver.components.HibernateConfiguratorComponent; import pro.gravit.launchserver.dao.User; import pro.gravit.launchserver.dao.UserDAO; +import pro.gravit.launchserver.hibernate.SessionFactoryManager; public class HibernateUserDAOImpl implements UserDAO { - private final HibernateConfiguratorComponent hibernate; + private final SessionFactoryManager manager; 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) { - try (Session s = hibernate.sessionFactory.openSession()) { + try (Session s = manager.fact.openSession()) { return s.get(User.class, id); } } public User findByUsername(String username) { - EntityManager em = hibernate.sessionFactory.createEntityManager(); + EntityManager em = manager.fact.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -42,7 +42,7 @@ public User findByUsername(String username) { } public User findByUUID(UUID uuid) { - EntityManager em = hibernate.sessionFactory.createEntityManager(); + EntityManager em = manager.fact.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery personCriteria = cb.createQuery(User.class); @@ -54,7 +54,7 @@ public User findByUUID(UUID uuid) { } public void save(User user) { - try (Session session = hibernate.sessionFactory.openSession()) { + try (Session session = manager.fact.openSession()) { Transaction tx1 = session.beginTransaction(); session.save(user); tx1.commit(); @@ -62,7 +62,7 @@ public void save(User user) { } public void update(User user) { - try (Session session = hibernate.sessionFactory.openSession()) { + try (Session session = manager.fact.openSession()) { Transaction tx1 = session.beginTransaction(); session.update(user); tx1.commit(); @@ -70,7 +70,7 @@ public void update(User user) { } public void delete(User user) { - try (Session session = hibernate.sessionFactory.openSession()) { + try (Session session = manager.fact.openSession()) { Transaction tx1 = session.beginTransaction(); session.delete(user); tx1.commit(); @@ -79,7 +79,7 @@ public void delete(User user) { @SuppressWarnings("unchecked") public List findAll() { - try (Session s = hibernate.sessionFactory.openSession()) { + try (Session s = manager.fact.openSession()) { return (List) s.createQuery("From User").list(); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/SessionFactoryManager.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/SessionFactoryManager.java new file mode 100644 index 00000000..978c8004 --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/SessionFactoryManager.java @@ -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 sessionFactories = new ConcurrentHashMap<>(); + private static final Function cr = e -> new SessionFactoryManager(); + public static SessionFactoryManager forLaunchServer(LaunchServer srv) { + return sessionFactories.computeIfAbsent(srv, cr); + } +}