1
1
/*
2
- * Copyright (c) 2000, 2023 , Oracle and/or its affiliates.
2
+ * Copyright (c) 2000, 2024 , Oracle and/or its affiliates.
3
3
*
4
4
* Licensed under the Universal Permissive License v 1.0 as shown at
5
5
* https://oss.oracle.com/licenses/upl.
@@ -36,7 +36,7 @@ public interface CoherenceConfiguration
36
36
*/
37
37
static Builder builder ()
38
38
{
39
- return new Builder ();
39
+ return new Builder (). discoverSessions () ;
40
40
}
41
41
42
42
/**
@@ -115,6 +115,14 @@ default String getDefaultSessionName()
115
115
*/
116
116
class Builder
117
117
{
118
+ /**
119
+ * Create a {@link Builder}.
120
+ */
121
+ public Builder ()
122
+ {
123
+ m_fDiscoverSessions = true ;
124
+ }
125
+
118
126
/**
119
127
* Set the name of the {@link Coherence} instance.
120
128
* <p>
@@ -166,7 +174,18 @@ public Builder withDefaultSession(String sName)
166
174
*/
167
175
public Builder discoverSessions ()
168
176
{
169
- withSessions (ServiceLoader .load (SessionConfiguration .class ));
177
+ return discoverSessions (true );
178
+ }
179
+
180
+ /**
181
+ * Add all of the {@link SessionConfiguration} instances discovered
182
+ * using the {@link ServiceLoader}.
183
+ *
184
+ * @return this {@link Builder}
185
+ */
186
+ public Builder discoverSessions (boolean f )
187
+ {
188
+ m_fDiscoverSessions = f ;
170
189
return this ;
171
190
}
172
191
@@ -188,6 +207,12 @@ public Builder discoverSessions()
188
207
* @return this {@link Builder}
189
208
*/
190
209
public Builder withSession (SessionConfiguration config )
210
+ {
211
+ withSession (config , f_mapConfig );
212
+ return this ;
213
+ }
214
+
215
+ private void withSession (SessionConfiguration config , Map <String , SessionConfiguration > map )
191
216
{
192
217
if (config != null && config .isEnabled ())
193
218
{
@@ -196,9 +221,8 @@ public Builder withSession(SessionConfiguration config)
196
221
{
197
222
throw new IllegalArgumentException ("A session configuration must provide a non-null name" );
198
223
}
199
- f_mapConfig .put (sName , config );
224
+ map .put (sName , config );
200
225
}
201
- return this ;
202
226
}
203
227
204
228
/**
@@ -320,12 +344,15 @@ public Builder withApplicationContext(Context context)
320
344
*/
321
345
public CoherenceConfiguration build ()
322
346
{
323
- if (Coherence .getInstance (m_sName ) != null )
347
+ Map <String , SessionConfiguration > mapConfig = new HashMap <>();
348
+ if (m_fDiscoverSessions )
324
349
{
325
- throw new IllegalStateException ("A Coherence instance already exists with the name " + m_sName );
350
+ for (SessionConfiguration configuration : ServiceLoader .load (SessionConfiguration .class ))
351
+ {
352
+ withSession (configuration , mapConfig );
353
+ }
326
354
}
327
-
328
- Map <String , SessionConfiguration > mapConfig = new HashMap <>(f_mapConfig );
355
+ mapConfig .putAll (f_mapConfig );
329
356
330
357
if (mapConfig .isEmpty ())
331
358
{
@@ -349,6 +376,11 @@ public CoherenceConfiguration build()
349
376
*/
350
377
private String m_sDefaultSession = Coherence .DEFAULT_NAME ;
351
378
379
+ /**
380
+ * A flag to determine whether to automatically discover session configurations.
381
+ */
382
+ private boolean m_fDiscoverSessions ;
383
+
352
384
/**
353
385
* A map of named {@link SessionConfiguration} instances.
354
386
*/
0 commit comments