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       * Default constructor.
83       */
84      PrometheusDBConfig() {
85          /* NoOp */
86      }
87  
88      /**
89       * Set the driver.
90       *
91       * @param pDriver the driver
92       */
93      public void setDriver(final PrometheusJDBCDriver pDriver) {
94          theDriver = pDriver;
95      }
96  
97      /**
98       * Get driver.
99       *
100      * @return the driver
101      */
102     public PrometheusJDBCDriver getDriver() {
103         return theDriver;
104     }
105 
106     /**
107      * Set the user.
108      *
109      * @param pUser the user
110      */
111     public void setUser(final String pUser) {
112         theUser = pUser;
113     }
114 
115     /**
116      * Get user.
117      *
118      * @return the user
119      */
120     public String getUser() {
121         return theUser;
122     }
123 
124     /**
125      * Set the password.
126      *
127      * @param pPassword the password
128      */
129     public void setPassword(final char[] pPassword) {
130         thePassword = pPassword;
131     }
132 
133     /**
134      * Get password.
135      *
136      * @return the password
137      */
138     public char[] getPassword() {
139         return thePassword;
140     }
141 
142     /**
143      * Set the server.
144      *
145      * @param pServer the server
146      */
147     public void setServer(final String pServer) {
148         theServer = pServer;
149     }
150 
151     /**
152      * Get server.
153      *
154      * @return the server
155      */
156     public String getServer() {
157         return theServer;
158     }
159 
160     /**
161      * Set the instance.
162      *
163      * @param pInstance the instance
164      */
165     public void setInstance(final String pInstance) {
166         theInstance = pInstance;
167     }
168 
169     /**
170      * Get instance.
171      *
172      * @return the instance
173      */
174     public String getInstance() {
175         return theInstance;
176     }
177 
178     /**
179      * Set the port.
180      *
181      * @param pPort the port
182      */
183     public void setPort(final Integer pPort) {
184         thePort = pPort;
185     }
186 
187     /**
188      * Get port.
189      *
190      * @return the port
191      */
192     public Integer getPort() {
193         return thePort;
194     }
195 
196     /**
197      * Set the batchSize.
198      *
199      * @param pSize the batchSize
200      */
201     public void setBatchSize(final int pSize) {
202         theBatch = pSize;
203     }
204 
205     /**
206      * Get batchSize.
207      *
208      * @return the batchSize
209      */
210     public int getBatchSize() {
211         return theBatch;
212     }
213 
214     /**
215      * Construct config from prefs.
216      *
217      * @param pPreferences the preferences
218      * @return the config
219      */
220     public static PrometheusDBConfig fromPrefs(final PrometheusDatabasePreferences pPreferences) {
221         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
222         myConfig.setDriver(pPreferences.getEnumValue(PrometheusDatabasePreferenceKey.DBDRIVER, PrometheusJDBCDriver.class));
223         myConfig.setUser(pPreferences.getStringValue(PrometheusDatabasePreferenceKey.DBUSER));
224         myConfig.setPassword(pPreferences.getCharArrayValue(PrometheusDatabasePreferenceKey.DBPASS));
225         myConfig.setServer(pPreferences.getStringValue(PrometheusDatabasePreferenceKey.DBSERVER));
226         myConfig.setBatchSize(pPreferences.getIntegerValue(PrometheusDatabasePreferenceKey.DBBATCH));
227         if (myConfig.getDriver().usePort()) {
228             myConfig.setPort(pPreferences.getIntegerValue(PrometheusDatabasePreferenceKey.DBPORT));
229         }
230         if (myConfig.getDriver().useInstance()) {
231             myConfig.setInstance(pPreferences.getStringValue(PrometheusDatabasePreferenceKey.DBINSTANCE));
232         }
233         return myConfig;
234     }
235 
236     /**
237      * Construct a simple postgres config.
238      *
239      * @return the config
240      */
241     public static PrometheusDBConfig postgres() {
242         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
243         myConfig.setDriver(PrometheusJDBCDriver.POSTGRESQL);
244         myConfig.setUser(DEFAULT_USER);
245         myConfig.setPassword(DEFAULT_PASS);
246         myConfig.setServer(DEFAULT_SERVER);
247         myConfig.setPort(PrometheusJDBCDriver.PORT_POSTGRESQL);
248         myConfig.setBatchSize(DEFAULT_BATCH);
249         return myConfig;
250     }
251 
252     /**
253      * Construct a simple mysql config.
254      *
255      * @return the config
256      */
257     public static PrometheusDBConfig mysql() {
258         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
259         myConfig.setDriver(PrometheusJDBCDriver.MYSQL);
260         myConfig.setUser(DEFAULT_USER);
261         myConfig.setPassword(DEFAULT_PASS);
262         myConfig.setServer(DEFAULT_SERVER);
263         myConfig.setPort(PrometheusJDBCDriver.PORT_MARIADB);
264         myConfig.setBatchSize(DEFAULT_BATCH);
265         return myConfig;
266     }
267 
268     /**
269      * Construct a simple mariaDB config.
270      *
271      * @return the config
272      */
273     public static PrometheusDBConfig mariaDB() {
274         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
275         myConfig.setDriver(PrometheusJDBCDriver.MARIADB);
276         myConfig.setUser(DEFAULT_USER);
277         myConfig.setPassword(DEFAULT_PASS);
278         myConfig.setServer(DEFAULT_SERVER);
279         myConfig.setPort(PrometheusJDBCDriver.PORT_MARIADB);
280         myConfig.setBatchSize(DEFAULT_BATCH);
281         return myConfig;
282     }
283 
284     /**
285      * Construct a simple selserver config.
286      *
287      * @return the config
288      */
289     public static PrometheusDBConfig sqlserver() {
290         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
291         myConfig.setDriver(PrometheusJDBCDriver.SQLSERVER);
292         myConfig.setUser(DEFAULT_USER);
293         myConfig.setPassword(DEFAULT_PASS);
294         myConfig.setServer(DEFAULT_SERVER);
295         myConfig.setInstance(PrometheusJDBCDriver.INSTANCE_SQLEXPRESS);
296         myConfig.setBatchSize(DEFAULT_BATCH);
297         return myConfig;
298     }
299 
300     /**
301      * Construct a simple h2 config.
302      *
303      * @return the config
304      */
305     public static PrometheusDBConfig h2() {
306         final PrometheusDBConfig myConfig = new PrometheusDBConfig();
307         myConfig.setDriver(PrometheusJDBCDriver.H2);
308         myConfig.setUser(DEFAULT_USER);
309         myConfig.setPassword(DEFAULT_PASS);
310         myConfig.setServer(DEFAULT_SERVER);
311         myConfig.setBatchSize(DEFAULT_BATCH);
312         return myConfig;
313     }
314 }