]> git.basschouten.com Git - openhab-addons.git/blob
27246f96a56508d11bf6d18dc4ed8d898acb9624
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.jpa.internal;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The configuration required for Jpa binding.
24  *
25  * @author Manfred Bergmann - Initial contribution
26  * @author Kai Kreuzer - migrated to 3.x
27  *
28  */
29 @NonNullByDefault
30 public class JpaConfiguration {
31     private final Logger logger = LoggerFactory.getLogger(JpaConfiguration.class);
32
33     private static final String CFG_CONNECTION_URL = "url";
34     private static final String CFG_DRIVER_CLASS = "driver";
35     private static final String CFG_USERNAME = "user";
36     private static final String CFG_PASSWORD = "password";
37     private static final String CFG_SYNCMAPPING = "syncmappings";
38
39     public final String dbConnectionUrl;
40     public final String dbDriverClass;
41     public final String dbUserName;
42     public final String dbPassword;
43     public final String dbSyncMapping;
44
45     public JpaConfiguration(final Map<String, @Nullable Object> properties) throws IllegalArgumentException {
46         logger.debug("Creating JPA config...");
47
48         String param = (String) properties.get(CFG_CONNECTION_URL);
49         logger.debug("url: {}", param);
50         if (param == null) {
51             throw new IllegalArgumentException("Connection URL is required in JPA configuration!");
52         } else if (param.isBlank()) {
53             throw new IllegalArgumentException("Empty connection URL in JPA configuration!");
54         }
55         dbConnectionUrl = param;
56
57         param = (String) properties.get(CFG_DRIVER_CLASS);
58         logger.debug("driver: {}", param);
59         if (param == null) {
60             throw new IllegalArgumentException("Driver class is required in JPA configuration!");
61         } else if (param.isBlank()) {
62             throw new IllegalArgumentException("Empty driver class in JPA configuration!");
63         }
64         dbDriverClass = param;
65
66         param = (String) properties.get(CFG_USERNAME);
67         if (param == null) {
68             logger.info("{} was not specified in JPA configuration!", CFG_USERNAME);
69         }
70         dbUserName = param == null ? "" : param;
71
72         param = (String) properties.get(CFG_PASSWORD);
73         if (param == null) {
74             logger.info("{} was not specified in JPA configuration!", CFG_PASSWORD);
75         }
76         dbPassword = param == null ? "" : param;
77
78         param = (String) properties.get(CFG_SYNCMAPPING);
79         if (param == null) {
80             logger.debug("{} was not specified in JPA configuration!", CFG_SYNCMAPPING);
81         }
82         dbSyncMapping = param == null ? "" : param;
83
84         logger.debug("Creating JPA config... done");
85     }
86 }