View Javadoc
1   /*
2    * Prometheus: Application Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.prometheus.database;
18  
19  import io.github.tonywasher.joceanus.prometheus.database.PrometheusDatabase.PrometheusDatabasePreferenceKey;
20  import io.github.tonywasher.joceanus.prometheus.database.PrometheusDatabase.PrometheusDatabasePreferences;
21  
22  /**
23   * Database config.
24   */
25  public class PrometheusDBConfig {
26      /**
27       * Default user name.
28       */
29      private static final String DEFAULT_USER = "testUser";
30  
31      /**
32       * Default password.
33       */
34      private static final char[] DEFAULT_PASS = "testPass".toCharArray();
35  
36      /**
37       * Default server.
38       */
39      private static final String DEFAULT_SERVER = "localhost";
40  
41      /**
42       * Default batchSize.
43       */
44      private static final int DEFAULT_BATCH = 50;
45  
46      /**
47       * JDBC Driver.
48       */
49      private PrometheusJDBCDriver theDriver;
50  
51      /**
52       * Username.
53       */
54      private String theUser;
55  
56      /**
57       * Password.
58       */
59      private char[] thePassword;
60  
61      /**
62       * Server.
63       */
64      private String theServer;
65  
66      /**
67       * Instance.
68       */
69      private String theInstance;
70  
71      /**
72       * Port.
73       */
74      private Integer thePort;
75  
76      /**
77       * BatchSize.
78       */
79      private int theBatch;
80  
81      /**
82       * Constructor.
83       */
84      public PrometheusDBConfig() {
85      }
86  
87      /**
88       * Set the driver.
89       *
90       * @param pDriver the driver
91       */
92      public void setDriver(final PrometheusJDBCDriver pDriver) {
93          theDriver = pDriver;
94      }
95  
96      /**
97       * Get driver.
98       *
99       * @return the driver
100      */
101     public PrometheusJDBCDriver getDriver() {
102         return theDriver;
103     }
104 
105     /**
106      * Set the user.
107      *
108      * @param pUser the user
109      */
110     public void setUser(final String pUser) {
111         theUser = pUser;
112     }
113 
114     /**
115      * Get user.
116      *
117      * @return the user
118      */
119     public String getUser() {
120         return theUser;
121     }
122 
123     /**
124      * Set the password.
125      *
126      * @param pPassword the password
127      */
128     public void setPassword(final char[] pPassword) {
129         thePassword = pPassword;
130     }
131 
132     /**
133      * Get password.
134      *
135      * @return the password
136      */
137     public char[] getPassword() {
138         return thePassword;
139     }
140 
141     /**
142      * Set the server.
143      *
144      * @param pServer the server
145      */
146     public void setServer(final String pServer) {
147         theServer = pServer;
148     }
149 
150     /**
151      * Get server.
152      *
153      * @return the server
154      */
155     public String getServer() {
156         return theServer;
157     }
158 
159     /**
160      * Set the instance.
161      *
162      * @param pInstance the instance
163      */
164     public void setInstance(final String pInstance) {
165         theInstance = pInstance;
166     }
167 
168     /**
169      * Get instance.
170      *
171      * @return the instance
172      */
173     public String getInstance() {
174         return theInstance;
175     }
176 
177     /**
178      * Set the port.
179      *
180      * @param pPort the port
181      */
182     public void setPort(final Integer pPort) {
183         thePort = pPort;
184     }
185 
186     /**
187      * Get port.
188      *
189      * @return the port
190      */
191     public Integer getPort() {
192         return thePort;
193     }
194 
195     /**
196      * Set the batchSize.
197      *
198      * @param pSize the batchSize
199      */
200     public void setBatchSize(final int pSize) {
201         theBatch = pSize;
202     }
203 
204     /**
205      * Get batchSize.
206      *
207      * @return the batchSize
208      */
209     public int getBatchSize() {
210         return theBatch;
211     }
212 
213     /**
214      * Construct config from prefs.
215      *
216      * @param pPreferences the preferences
217      * @return the config
218      */
219     public static PrometheusDBConfig fromPrefs(final PrometheusDatabasePreferences pPreferences) {
220         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
221         myConfig.setDriver(pPreferences.getEnumValue(PrometheusDatabasePreferenceKey.DBDRIVER, PrometheusJDBCDriver.class));
222         myConfig.setUser(pPreferences.getStringValue(PrometheusDatabasePreferenceKey.DBUSER));
223         myConfig.setPassword(pPreferences.getCharArrayValue(PrometheusDatabasePreferenceKey.DBPASS));
224         myConfig.setServer(pPreferences.getStringValue(PrometheusDatabasePreferenceKey.DBSERVER));
225         myConfig.setBatchSize(pPreferences.getIntegerValue(PrometheusDatabasePreferenceKey.DBBATCH));
226         if (myConfig.getDriver().usePort()) {
227             myConfig.setPort(pPreferences.getIntegerValue(PrometheusDatabasePreferenceKey.DBPORT));
228         }
229         if (myConfig.getDriver().useInstance()) {
230             myConfig.setInstance(pPreferences.getStringValue(PrometheusDatabasePreferenceKey.DBINSTANCE));
231         }
232         return myConfig;
233     }
234 
235     /**
236      * Construct a simple postgres config.
237      *
238      * @return the config
239      */
240     public static PrometheusDBConfig postgres() {
241         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
242         myConfig.setDriver(PrometheusJDBCDriver.POSTGRESQL);
243         myConfig.setUser(DEFAULT_USER);
244         myConfig.setPassword(DEFAULT_PASS);
245         myConfig.setServer(DEFAULT_SERVER);
246         myConfig.setPort(PrometheusJDBCDriver.PORT_POSTGRESQL);
247         myConfig.setBatchSize(DEFAULT_BATCH);
248         return myConfig;
249     }
250 
251     /**
252      * Construct a simple mysql config.
253      *
254      * @return the config
255      */
256     public static PrometheusDBConfig mysql() {
257         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
258         myConfig.setDriver(PrometheusJDBCDriver.MYSQL);
259         myConfig.setUser(DEFAULT_USER);
260         myConfig.setPassword(DEFAULT_PASS);
261         myConfig.setServer(DEFAULT_SERVER);
262         myConfig.setPort(PrometheusJDBCDriver.PORT_MARIADB);
263         myConfig.setBatchSize(DEFAULT_BATCH);
264         return myConfig;
265     }
266 
267     /**
268      * Construct a simple mariaDB config.
269      *
270      * @return the config
271      */
272     public static PrometheusDBConfig mariaDB() {
273         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
274         myConfig.setDriver(PrometheusJDBCDriver.MARIADB);
275         myConfig.setUser(DEFAULT_USER);
276         myConfig.setPassword(DEFAULT_PASS);
277         myConfig.setServer(DEFAULT_SERVER);
278         myConfig.setPort(PrometheusJDBCDriver.PORT_MARIADB);
279         myConfig.setBatchSize(DEFAULT_BATCH);
280         return myConfig;
281     }
282 
283     /**
284      * Construct a simple selserver config.
285      *
286      * @return the config
287      */
288     public static PrometheusDBConfig sqlserver() {
289         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
290         myConfig.setDriver(PrometheusJDBCDriver.SQLSERVER);
291         myConfig.setUser(DEFAULT_USER);
292         myConfig.setPassword(DEFAULT_PASS);
293         myConfig.setServer(DEFAULT_SERVER);
294         myConfig.setInstance(PrometheusJDBCDriver.INSTANCE_SQLEXPRESS);
295         myConfig.setBatchSize(DEFAULT_BATCH);
296         return myConfig;
297     }
298 
299     /**
300      * Construct a simple h2 config.
301      *
302      * @return the config
303      */
304     public static PrometheusDBConfig h2() {
305         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
306         myConfig.setDriver(PrometheusJDBCDriver.H2);
307         myConfig.setUser(DEFAULT_USER);
308         myConfig.setPassword(DEFAULT_PASS);
309         myConfig.setServer(DEFAULT_SERVER);
310         myConfig.setBatchSize(DEFAULT_BATCH);
311         return myConfig;
312     }
313 }