Skip to content
Snippets Groups Projects
Commit 6c440647 authored by Fakhri Mahendra's avatar Fakhri Mahendra
Browse files

refactor: change how bootstraping for hibernate works

this change make such that changing docker and local environment for testing easier
parent c6900149
No related merge requests found
......@@ -4,14 +4,14 @@ services:
# container_name: edujin-soap-service
# build: .
# ports:
# - "8001:8001"
# - "8082:8082"
# env_file: .env
# volumes:
# - .:/usr/src/app
# networks:
# - soap-services
mysql-soap-database:
soap-db:
image: mysql:latest
restart: unless-stopped
ports:
......@@ -25,7 +25,7 @@ services:
phpmyadmin:
image: phpmyadmin
environment:
- PMA_HOST=mysql-soap-database
- PMA_HOST=soap-db
- PMA_PORT=3306
ports:
- "8005:80"
......
package edujin.util;
import io.github.cdimascio.dotenv.Dotenv;
public class ConfigHandler {
private static ConfigHandler instance = null;
private final PropertiesHandler ph;
private final String USE_DOCKER_CONFIG_KEY = "USE_DOCKER_CONFIG";
private final String CONFIG_FILE = "config.properties";
private final String CONFIG_FILE_DOCKER = "config.docker.properties";
private ConfigHandler() {
String useDockerConfig = Dotenv.load().get(USE_DOCKER_CONFIG_KEY);
if (useDockerConfig == null || useDockerConfig.equals("false")) {
this.ph = new PropertiesHandler(CONFIG_FILE);
} else {
this.ph = new PropertiesHandler(CONFIG_FILE_DOCKER);
}
}
public static ConfigHandler getInstance() {
if (instance == null) {
instance = new ConfigHandler();
}
return instance;
}
public String get(String key) {
return this.ph.get(key);
}
public PropertiesHandler getPropertyHandler() {
return this.ph;
}
}
package edujin.util;
import io.github.cdimascio.dotenv.Dotenv;
import edujin.model.Token;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
......@@ -9,29 +9,29 @@ import org.hibernate.service.ServiceRegistry;
import java.util.Properties;
public class HibernateUtil {
private static final String DB_HOST = Dotenv.load().get("MYSQL_HOST");
private static final String DB_PORT = Dotenv.load().get("MYSQL_PORT");
private static final String DB_NAME = Dotenv.load().get("MYSQL_DATABASE");
private static final String DB_USER = Dotenv.load().get("MYSQL_USER");
private static final String DB_PASS = Dotenv.load().get("MYSQL_PASSWORD");
private static final String DB_URL = String.format("jdbc:mysql://%s:%s/%s", DB_HOST, 3307, DB_NAME);
private static final String DB_URL_KEY = "db.url";
private static final String DB_USER_KEY = "db.username";
private static final String DB_PASS_KEY = "db.password";
private static SessionFactory sessionFactory;
private static Configuration getConfiguration() {
ConfigHandler configHandler = ConfigHandler.getInstance();
String dbUrl = configHandler.get(DB_URL_KEY);
String dbUser = configHandler.get(DB_USER_KEY);
String dbPass = configHandler.get(DB_PASS_KEY);
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.connection.url", HibernateUtil.DB_URL);
properties.setProperty("hibernate.connection.username", HibernateUtil.DB_USER);
properties.setProperty("hibernate.connection.password", HibernateUtil.DB_PASS);
properties.setProperty("hibernate.connection.url", dbUrl);
properties.setProperty("hibernate.connection.username", dbUser);
properties.setProperty("hibernate.connection.password", dbPass);
properties.setProperty("hibernate.hbm2ddl.auto", "update");
properties.setProperty("hibernate.use_second_level_cache", "false");
properties.put("hibernate.current_session_context_class", "thread");
Configuration configuration = new Configuration();
configuration.addAnnotatedClass(edujin.model.Subscription.class);
configuration.addAnnotatedClass(edujin.model.PasswordResetToken.class);
configuration.addAnnotatedClass(Token.class);
configuration.setProperties(properties);
return configuration;
}
......@@ -55,16 +55,10 @@ public class HibernateUtil {
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
if (HibernateUtil.sessionFactory == null) {
HibernateUtil.sessionFactory = HibernateUtil.buildSessionFactory();
}
return HibernateUtil.sessionFactory;
}
public static void flushAndClear() {
HibernateUtil.getSessionFactory().getCurrentSession().flush();
HibernateUtil.getSessionFactory().getCurrentSession().clear();
}
}
package test;
import javax.xml.ws.Endpoint;
public class Main {
public static void main(String[] args) {
try {
Endpoint endpoint = Endpoint.create(new TestingServices());
endpoint.publish("http://0.0.0.0:8001/ws");
System.out.println("Server started");
} catch (Exception e) {
System.out.println("Server failed: " + e);
}
}
}
\ No newline at end of file
db.username = root
db.password = root
db.url = jdbc:mysql://soap-db:3306/edujin-soap
server.host=0.0.0.0
server.port=8083
edujin_app.url=http://localhost:8080
edujin_rest.url=
db.username = root
db.password = root
db.url = jdbc:mysql://localhost:3307/edujin-soap
server.host=localhost
server.port=8083
edujin_app.url=http://localhost:8080
edujin_rest.url=
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment