@@ -112,99 +112,104 @@ public class DatabaseOptions {
112112 .hidden ()
113113 .build ();
114114
115- /**
116- * Options that have their sibling for a named datasource
117- * Example: for `db-dialect`, `db-dialect-<datasource>` is created
118- */
119- public static final List <Option <?>> OPTIONS_DATASOURCES = List .of (
120- DB_DIALECT ,
121- DB_DRIVER ,
122- DB ,
123- DB_URL ,
124- DB_URL_HOST ,
125- DB_URL_DATABASE ,
126- DB_URL_PORT ,
127- DB_URL_PROPERTIES ,
128- DB_USERNAME ,
129- DB_PASSWORD ,
130- DB_SCHEMA ,
131- DB_POOL_INITIAL_SIZE ,
132- DB_POOL_MIN_SIZE ,
133- DB_POOL_MAX_SIZE ,
134- DB_SQL_JPA_DEBUG ,
135- DB_SQL_LOG_SLOW_QUERIES
136- );
137-
138- /**
139- * In order to avoid ambiguity, we need to have unique option names for wildcard options.
140- * This map controls overriding option name to be unique for wildcard option.
141- */
142- private static final Map <String , String > DATASOURCES_OVERRIDES_SUFFIX = Map .of (
143- DatabaseOptions .DB .getKey (), "-kind" , // db-kind
144- DatabaseOptions .DB_URL .getKey (), "-full" // db-url-full
145- );
146-
147- /**
148- * You can override some {@link OptionBuilder} methods for additional datasources in this map
149- */
150- private static final Map <Option <?>, Consumer <OptionBuilder <?>>> DATASOURCES_OVERRIDES_OPTIONS = Map .of (
151- DatabaseOptions .DB , builder -> builder .defaultValue (Optional .empty ()) // no default value for DB kind for datasources
152- );
153-
154- private static final Map <String , Option <?>> cachedDatasourceOptions = new HashMap <>();
155-
115+ public static final class Datasources {
116+ /**
117+ * Options that have their sibling for a named datasource
118+ * Example: for `db-dialect`, `db-dialect-<datasource>` is created
119+ */
120+ public static final List <Option <?>> OPTIONS_DATASOURCES = List .of (
121+ DB_DIALECT ,
122+ DB_DRIVER ,
123+ DB ,
124+ DB_URL ,
125+ DB_URL_HOST ,
126+ DB_URL_DATABASE ,
127+ DB_URL_PORT ,
128+ DB_URL_PROPERTIES ,
129+ DB_USERNAME ,
130+ DB_PASSWORD ,
131+ DB_SCHEMA ,
132+ DB_POOL_INITIAL_SIZE ,
133+ DB_POOL_MIN_SIZE ,
134+ DB_POOL_MAX_SIZE ,
135+ DB_SQL_JPA_DEBUG ,
136+ DB_SQL_LOG_SLOW_QUERIES
137+ );
138+
139+ /**
140+ * In order to avoid ambiguity, we need to have unique option names for wildcard options.
141+ * This map controls overriding option name to be unique for wildcard option.
142+ */
143+ private static final Map <String , String > DATASOURCES_OVERRIDES_SUFFIX = Map .of (
144+ DatabaseOptions .DB .getKey (), "-kind" , // db-kind
145+ DatabaseOptions .DB_URL .getKey (), "-full" // db-url-full
146+ );
147+
148+ /**
149+ * You can override some {@link OptionBuilder} methods for additional datasources in this map
150+ */
151+ private static final Map <Option <?>, Consumer <OptionBuilder <?>>> DATASOURCES_OVERRIDES_OPTIONS = Map .of (
152+ DatabaseOptions .DB , builder -> builder
153+ .defaultValue (Optional .empty ()) // no default value for DB kind for datasources
154+ .connectedOptions (
155+ getDatasourceOption (DatabaseOptions .DB_URL ).orElseThrow (),
156+ TransactionOptions .TRANSACTION_XA_ENABLED_DATASOURCE
157+ )
158+ );
159+
160+ private static final Map <String , Option <?>> cachedDatasourceOptions = new HashMap <>();
161+
162+ /**
163+ * Get datasource option containing named datasource mapped to parent DB options.
164+ * <p>
165+ * We map DB options to named datasource options like:
166+ * <ul>
167+ * <li>{@code db-url-host --> db-url-host-<datasource>}</li>
168+ * <li>{@code db-username --> db-username-<datasource>}</li>
169+ * <li>{@code db --> db-kind-<datasource>}</li>
170+ * </ul>
171+ */
172+ @ SuppressWarnings ("unchecked" )
173+ public static <T > Optional <Option <T >> getDatasourceOption (Option <T > parentOption ) {
174+ if (!OPTIONS_DATASOURCES .contains (parentOption )) {
175+ return Optional .empty ();
176+ }
156177
157- /**
158- * Get datasource option containing named datasource mapped to parent DB options.
159- * <p>
160- * We map DB options to named datasource options like:
161- * <ul>
162- * <li>{@code db-url-host --> db-url-host-<datasource>}</li>
163- * <li>{@code db-username --> db-username-<datasource>}</li>
164- * <li>{@code db --> db-kind-<datasource>}</li>
165- * </ul>
166- */
167- @ SuppressWarnings ("unchecked" )
168- public static <T > Optional <Option <T >> getDatasourceOption (Option <T > parentOption ) {
169- if (!OPTIONS_DATASOURCES .contains (parentOption )) {
170- return Optional .empty ();
171- }
178+ var key = getKeyForDatasource (parentOption );
179+ if (key .isEmpty ()) {
180+ return Optional .empty ();
181+ }
172182
173- var key = getKeyForDatasource (parentOption );
174- if (key .isEmpty ()) {
175- return Optional .empty ();
176- }
183+ // check if we already created the same option and return it from the cache
184+ Option <?> option = cachedDatasourceOptions .get (key .get ());
177185
178- // check if we already created the same option and return it from the cache
179- Option <?> option = cachedDatasourceOptions .get (key .get ());
186+ if (option == null ) {
187+ var builder = parentOption .toBuilder ()
188+ .key (key .get ())
189+ .category (OptionCategory .DATABASE_DATASOURCES );
180190
181- if (option == null ) {
182- var builder = parentOption .toBuilder ()
183- .key (key .get ())
184- .category (OptionCategory .DATABASE_DATASOURCES );
191+ if (!parentOption .isHidden ()) {
192+ builder .description ("Used for named <datasource>. " + parentOption .getDescription ());
193+ }
185194
186- if (!parentOption .isHidden ()) {
187- builder .description ("Used for named <datasource>. " + parentOption .getDescription ());
188- }
195+ // override some settings for options
196+ var override = DATASOURCES_OVERRIDES_OPTIONS .get (parentOption );
197+ if (override != null ) {
198+ override .accept (builder );
199+ }
189200
190- // override some settings for options
191- var override = DATASOURCES_OVERRIDES_OPTIONS .get (parentOption );
192- if (override != null ) {
193- override .accept (builder );
201+ option = builder .build ();
202+ cachedDatasourceOptions .put (key .get (), option );
194203 }
195-
196- option = builder .build ();
197- cachedDatasourceOptions .put (key .get (), option );
204+ return Optional .of ((Option <T >) option );
198205 }
199- return Optional .of ((Option <T >) option );
200- }
201206
202- /**
203- * Get mapped datasource key based on DB option {@param option}
204- */
205- public static Optional <String > getKeyForDatasource (Option <?> option ) {
206- return getKeyForDatasource (option .getKey ());
207- }
207+ /**
208+ * Get mapped datasource key based on DB option {@param option}
209+ */
210+ public static Optional <String > getKeyForDatasource (Option <?> option ) {
211+ return getKeyForDatasource (option .getKey ());
212+ }
208213
209214 /**
210215 * Get mapped datasource key based on DB option {@param option}
@@ -216,17 +221,18 @@ public static Optional<String> getKeyForDatasource(String option) {
216221 .map (key -> key .concat ("-<datasource>" ));
217222 }
218223
219- /**
220- * Returns datasource option based on DB option {@code option} with actual wildcard value.
221- * It replaces the {@code <datasource>} with actual value in {@code namedProperty}.
222- * <p>
223- * f.e. Consider {@code option}={@link DatabaseOptions#DB_DRIVER}, and {@code namedProperty}=my-store.
224- * <p>
225- * Result: {@code db-driver-my-store}
226- */
227- public static Optional <String > getNamedKey (Option <?> option , String namedProperty ) {
228- return getKeyForDatasource (option )
229- .map (key -> key .substring (0 , key .indexOf ("<" )))
230- .map (key -> key .concat (namedProperty ));
224+ /**
225+ * Returns datasource option based on DB option {@code option} with actual wildcard value.
226+ * It replaces the {@code <datasource>} with actual value in {@code namedProperty}.
227+ * <p>
228+ * f.e. Consider {@code option}={@link DatabaseOptions#DB_DRIVER}, and {@code namedProperty}=my-store.
229+ * <p>
230+ * Result: {@code db-driver-my-store}
231+ */
232+ public static Optional <String > getNamedKey (Option <?> option , String namedProperty ) {
233+ return getKeyForDatasource (option )
234+ .map (key -> key .substring (0 , key .indexOf ("<" )))
235+ .map (key -> key .concat (namedProperty ));
236+ }
231237 }
232238}
0 commit comments