BrightSide Workbench Full Report + Source Code
JpaConfig.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.jpa.config;
20 
21 import java.io.IOException;
22 import java.nio.file.Path;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Properties;
26 import org.turro.elephant.context.ElephantContext;
27 import org.turro.file.Document;
28 import org.turro.file.Folder;
29 import org.turro.json.IJSONizable;
30 import org.turro.log.WebLoggers;
31 
36 public class JpaConfig implements IJSONizable {
37 
38  private String url, username, password;
39 
40  public void setUrl(String url) {
41  this.url = url;
42  }
43 
44  public void setUsername(String username) {
45  this.username = username;
46  }
47 
48  public void setPassword(String password) {
49  this.password = password;
50  }
51 
52  private Properties properties() {
53  Properties properties = new Properties();
54  properties.setProperty("hibernate.connection.url", url);
55  properties.setProperty("hibernate.connection.username", username);
56  properties.setProperty("hibernate.connection.password", password);
57  addDefaults(properties);
58  return properties;
59  }
60 
61  private void addDefaults(Properties properties) {
62  properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
63  properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
64  properties.setProperty("hibernate.hbm2ddl.auto", "update");
65  properties.setProperty("hibernate.show_sql", "false");
66  properties.setProperty("hibernate.format_sql", "false");
67  properties.setProperty("hibernate.use_sql_comments", "false");
68  properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
69  properties.setProperty("hibernate.c3p0.max_size", "6");
70  properties.setProperty("hibernate.c3p0.min_size", "2");
71  properties.setProperty("hibernate.c3p0.timeout", "5000");
72  properties.setProperty("hibernate.c3p0.max_statements", "200");
73  properties.setProperty("hibernate.c3p0.idle_test_period", "3000");
74  properties.setProperty("hibernate.c3p0.acquire_increment", "2");
75  properties.setProperty("hibernate.c3p0.validate", "false");
76  }
77 
78  /* Utils */
79 
80  public static List<Document> daos() {
81  try {
82  Folder daoFolder = Folder.from(Path.of(ElephantContext.getRealPath(DAO_FOLDER)));
83  return daoFolder.documents("*.json");
84  } catch (IOException ex) {
86  }
87  return null;
88  }
89 
90  /* IJSONizable */
91 
92  @Override
93  public String toJson() {
94  return toJson(this);
95  }
96 
97  @Override
98  public String toJson(Map<String, Object> properties) {
99  return toJson(this, properties);
100  }
101 
102  /* Factory */
103 
104  private static final String DAO_FOLDER = "/WEB-INF/elephant/conf/dao";
105 
106  public static Properties from(String pu, String name) {
107  try {
108  Document configFile = Document.from(Path.of(ElephantContext.getRealPath(DAO_FOLDER))
109  .resolve(pu + ".json"));
110  if(!configFile.exists()) {
111  JpaConfigMigrate.migrate(configFile, name);
112  }
113  return IJSONizable.fromJson(configFile.content(), JpaConfig.class).properties();
114  } catch (IOException ex) {
115  WebLoggers.severe(JpaConfig.class).exception(ex).log();
116  }
117  return null;
118  }
119 
120 }
static void migrate(Document document, String name)
void setUsername(String username)
Definition: JpaConfig.java:44
static Properties from(String pu, String name)
Definition: JpaConfig.java:106
void setPassword(String password)
Definition: JpaConfig.java:48
String toJson(Map< String, Object > properties)
Definition: JpaConfig.java:98
static List< Document > daos()
Definition: JpaConfig.java:80
static WebLoggers severe(Object entity)
Definition: WebLoggers.java:51
WebLoggers exception(Throwable throwable)
Definition: WebLoggers.java:29