forked from vladmihalcea/hypersistence-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.java
373 lines (332 loc) · 13.9 KB
/
Configuration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package com.vladmihalcea.hibernate.type.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vladmihalcea.hibernate.util.ClassLoaderUtils;
import com.vladmihalcea.hibernate.util.ReflectionUtils;
import com.vladmihalcea.hibernate.util.StringUtils;
import org.hibernate.cfg.Environment;
import javax.persistence.EntityManagerFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.function.Supplier;
import static com.vladmihalcea.hibernate.util.LogUtils.LOGGER;
/**
* <code>Configuration</code> - It allows declarative configuration through the <code>hibernate.properties</code> file
* or the <code>hibernate-types.properties</code> file.
*
* The properties from <code>hibernate-types.properties</code> can override the ones from the <code>hibernate.properties</code> file.
*
* It loads the {@link Properties} configuration file and makes them available to other components.
*
* @author Vlad Mihalcea
* @since 2.1.0
*/
public class Configuration implements Serializable {
public static final Configuration INSTANCE = new Configuration();
public static final String PROPERTIES_FILE_PATH = "hibernate-types.properties.path";
public static final String PROPERTIES_FILE_NAME = "hibernate-types.properties";
public static final String APPLICATION_PROPERTIES_FILE_NAME = "application.properties";
/**
* Each Property has a well-defined key.
*/
public enum PropertyKey {
JACKSON_OBJECT_MAPPER("hibernate.types.jackson.object.mapper"),
JSON_SERIALIZER("hibernate.types.json.serializer"),
PRINT_BANNER("hibernate.types.print.banner");
private final String key;
PropertyKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
private final Properties properties = Environment.getProperties();
private Configuration() {
load();
printBanner();
}
/**
* Load {@link Properties} from the resolved {@link InputStream}
*/
private void load() {
String[] propertiesFilePaths = new String[] {
APPLICATION_PROPERTIES_FILE_NAME,
PROPERTIES_FILE_NAME,
System.getProperty(PROPERTIES_FILE_NAME),
};
for (String propertiesFilePath : propertiesFilePaths) {
if (propertiesFilePath != null) {
InputStream propertiesInputStream = null;
try {
propertiesInputStream = propertiesInputStream(propertiesFilePath);
if (propertiesInputStream != null) {
properties.load(propertiesInputStream);
}
} catch (IOException e) {
LOGGER.error("Can't load properties", e);
} finally {
try {
if (propertiesInputStream != null) {
propertiesInputStream.close();
}
} catch (IOException e) {
LOGGER.error("Can't close the properties InputStream", e);
}
}
}
}
}
/**
* Get {@link Properties} file {@link InputStream}
*
* @param propertiesFilePath properties file path
* @return {@link Properties} file {@link InputStream}
* @throws IOException the file couldn't be loaded properly
*/
private InputStream propertiesInputStream(String propertiesFilePath) throws IOException {
if (propertiesFilePath != null) {
URL propertiesFileUrl;
try {
propertiesFileUrl = new URL(propertiesFilePath);
} catch (MalformedURLException ignore) {
propertiesFileUrl = ClassLoaderUtils.getResource(propertiesFilePath);
if (propertiesFileUrl == null) {
File f = new File(propertiesFilePath);
if (f.exists() && f.isFile()) {
try {
propertiesFileUrl = f.toURI().toURL();
} catch (MalformedURLException e) {
LOGGER.error(
"The property " + propertiesFilePath + " can't be resolved to either a URL, " +
"a classpath resource or a File"
);
}
}
}
}
if (propertiesFileUrl != null) {
return propertiesFileUrl.openStream();
}
}
return ClassLoaderUtils.getResourceAsStream(propertiesFilePath);
}
/**
* Get all properties.
*
* @return properties.
*/
public Properties getProperties() {
return properties;
}
/**
* Get {@link ObjectMapperWrapper} reference
*
* @return {@link ObjectMapperWrapper} reference
*/
public ObjectMapperWrapper getObjectMapperWrapper() {
Object objectMapperPropertyInstance = instantiateClass(PropertyKey.JACKSON_OBJECT_MAPPER);
ObjectMapperWrapper objectMapperWrapper = new ObjectMapperWrapper();
if (objectMapperPropertyInstance != null) {
if(objectMapperPropertyInstance instanceof ObjectMapperSupplier) {
ObjectMapper objectMapper = ((ObjectMapperSupplier) objectMapperPropertyInstance).get();
if(objectMapper != null) {
objectMapperWrapper = new ObjectMapperWrapper(objectMapper);
}
}
else if (objectMapperPropertyInstance instanceof Supplier) {
Supplier<ObjectMapper> objectMapperSupplier = (Supplier<ObjectMapper>) objectMapperPropertyInstance;
objectMapperWrapper = new ObjectMapperWrapper(objectMapperSupplier.get());
}
else if (objectMapperPropertyInstance instanceof ObjectMapper) {
ObjectMapper objectMapper = (ObjectMapper) objectMapperPropertyInstance;
objectMapperWrapper = new ObjectMapperWrapper(objectMapper);
}
}
Object jsonSerializerPropertyInstance = instantiateClass(PropertyKey.JSON_SERIALIZER);
if (jsonSerializerPropertyInstance != null) {
JsonSerializer jsonSerializer = null;
if(jsonSerializerPropertyInstance instanceof JsonSerializerSupplier) {
jsonSerializer = ((JsonSerializerSupplier) jsonSerializerPropertyInstance).get();
}
else if (jsonSerializerPropertyInstance instanceof Supplier) {
Supplier<JsonSerializer> jsonSerializerSupplier = (Supplier<JsonSerializer>) jsonSerializerPropertyInstance;
jsonSerializer = jsonSerializerSupplier.get();
}
else if (jsonSerializerPropertyInstance instanceof JsonSerializer) {
jsonSerializer = (JsonSerializer) jsonSerializerPropertyInstance;
}
if (jsonSerializer != null) {
objectMapperWrapper.setJsonSerializer(jsonSerializer);
}
}
return objectMapperWrapper;
}
/**
* Get Integer property value
*
* @param propertyKey property key
* @return Integer property value
*/
public Integer integerProperty(PropertyKey propertyKey) {
Integer value = null;
String property = properties.getProperty(propertyKey.getKey());
if (property != null) {
value = Integer.valueOf(property);
}
return value;
}
/**
* Get Long property value
*
* @param propertyKey property key
* @return Long property value
*/
public Long longProperty(PropertyKey propertyKey) {
Long value = null;
String property = properties.getProperty(propertyKey.getKey());
if (property != null) {
value = Long.valueOf(property);
}
return value;
}
/**
* Get Boolean property value
*
* @param propertyKey property key
* @return Boolean property value
*/
public Boolean booleanProperty(PropertyKey propertyKey) {
Boolean value = null;
String property = properties.getProperty(propertyKey.getKey());
if (property != null) {
value = Boolean.valueOf(property);
}
return value;
}
/**
* Get Class property value
*
* @param propertyKey property key
* @param <T> class generic type
* @return Class property value
*/
public <T> Class<T> classProperty(PropertyKey propertyKey) {
Class<T> clazz = null;
String property = properties.getProperty(propertyKey.getKey());
if (property != null) {
try {
return ClassLoaderUtils.loadClass(property);
} catch (ClassNotFoundException e) {
LOGGER.error("Couldn't load the " + property + " class given by the " + propertyKey + " property", e);
}
}
return clazz;
}
/**
* Instantiate class associated to the given property key
*
* @param propertyKey property key
* @param <T> class parameter type
* @return class instance
*/
private <T> T instantiateClass(PropertyKey propertyKey) {
T object = null;
String property = properties.getProperty(propertyKey.getKey());
if (property != null) {
try {
Class<T> clazz = ClassLoaderUtils.loadClass(property);
LOGGER.debug("Instantiate {}", clazz);
object = clazz.newInstance();
} catch (ClassNotFoundException e) {
LOGGER.error("Couldn't load the " + property + " class given by the " + propertyKey + " property", e);
} catch (InstantiationException e) {
LOGGER.error("Couldn't instantiate the " + property + " class given by the " + propertyKey + " property", e);
} catch (IllegalAccessException e) {
LOGGER.error("Couldn't access the " + property + " class given by the " + propertyKey + " property", e);
}
}
return object;
}
/**
* Print the banner into the log.
*/
private void printBanner() {
if (this.isHypersistenceOptimizer() || bannerPrinted) {
return;
}
String printBannerValue = properties.getProperty(PropertyKey.PRINT_BANNER.getKey());
if(printBannerValue != null && !Boolean.valueOf(printBannerValue)) {
return;
}
LOGGER.info(
StringUtils.join(
StringUtils.LINE_SEPARATOR,
"This framework is proudly powered by:",
"",
">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",
" _ _ _ _",
"| | | | (_) | |",
"| |__| |_ _ _ __ ___ _ __ ___ _ ___| |_ ___ _ __ ___ ___",
"| __ | | | | '_ \\ / _ \\ '__/ __| / __| __/ _ \\ '_ \\ / __/ _ \\",
"| | | | |_| | |_) | __/ | \\__ \\ \\__ \\ || __/ | | | (_| __/",
"|_| |_|\\__, | .__/ \\___|_| |___/_|___/\\__\\___|_| |_|\\___\\___|",
" __/ | |",
" |___/|_|",
"",
"At Hypersistence, we only build amazing tools, like Hibernate Types, Flexy Pool, or Hypersistence Optimizer.",
"",
"What if there were a tool that could automatically detect JPA and Hibernate performance issues?",
"",
"Hypersistence Optimizer is that tool! For more details, go to: ",
"",
"https://vladmihalcea.com/hypersistence-optimizer/",
"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<",
""
)
);
bannerPrinted = true;
}
private boolean isHypersistenceOptimizer() {
try {
Class licenseClass = ReflectionUtils.getClassOrNull("io.hypersistence.optimizer.core.License");
if(licenseClass != null) {
Class jpaConfigClass = ReflectionUtils.getClassOrNull("io.hypersistence.optimizer.core.config.JpaConfig");
Object jpaConfig = ReflectionUtils.newInstance(
jpaConfigClass,
new Object[] {
null
},
new Class[] {
EntityManagerFactory.class
}
);
Object license = ReflectionUtils.newInstance(
licenseClass,
new Object[] {
jpaConfig
},
new Class[] {
ReflectionUtils.getClassOrNull("io.hypersistence.optimizer.core.LicenseConfig")
}
);
Properties properties = ReflectionUtils.invokeGetter(license, "properties");
Class propertyClass = ReflectionUtils.getClassOrNull("io.hypersistence.optimizer.core.License$Property");
Object trialVersionKey = Enum.valueOf(propertyClass, "TRIAL_VERSION");
Object validUntilMillisKey = Enum.valueOf(propertyClass, "VALID_UNTIL_MILLIS");
boolean trialVersion = Boolean.parseBoolean(properties.getProperty(ReflectionUtils.invokeGetter(trialVersionKey, "key")));
long validUntilMillis = Long.parseLong(properties.getProperty(ReflectionUtils.invokeGetter(validUntilMillisKey, "key")));
if(!trialVersion && validUntilMillis > System.currentTimeMillis()) {
return true;
}
}
} catch (Exception ignore) {
}
return false;
}
private static boolean bannerPrinted = false;
}