2424import java .util .Properties ;
2525
2626import org .springframework .core .convert .converter .Converter ;
27+ import org .springframework .lang .NonNull ;
2728import org .springframework .util .Assert ;
2829
2930/**
3031 * Cassandra connection properties using {@code config/cassandra-connection.properties}. Properties are generated during
3132 * the build and can be override using system properties.
3233 *
3334 * @author Mark Paluch
35+ * @author John Blum
3436 */
35- @ SuppressWarnings ("serial " )
37+ @ SuppressWarnings ("unused " )
3638public class CassandraConnectionProperties extends Properties {
3739
3840 private final static List <WeakReference <CassandraConnectionProperties >> instances = new ArrayList <>();
3941
40- private String resourceName ;
42+ private final String resourceName ;
4143
4244 /**
43- * Create a new {@link CassandraConnectionProperties} using properties from
44- * {@code config/cassandra-connection.properties}.
45+ * Construct a new instance of {@link CassandraConnectionProperties} using properties
46+ * from {@code config/cassandra-connection.properties}.
4547 */
4648 public CassandraConnectionProperties () {
4749 this ("/config/cassandra-connection.properties" );
4850 }
4951
52+ private CassandraConnectionProperties (@ NonNull String resourceName ) {
53+
54+ this .resourceName = resourceName ;
55+
56+ loadProperties ();
57+
58+ instances .add (new WeakReference <>(this ));
59+ }
60+
5061 public void update () {
62+
5163 try {
5264 // Caution: Rewriting properties during initialization.
53- File file = new File (getClass ().getResource (resourceName ).toURI ());
65+ File file = new File (getClass ().getResource (this . resourceName ).toURI ());
5466
55- try (FileOutputStream fos = new FileOutputStream (file )) {
56- store (fos , "" );
67+ try (FileOutputStream out = new FileOutputStream (file )) {
68+ store (out , "" );
5769 }
5870
5971 reload ();
60- } catch (Exception e ) {
61- e .printStackTrace ();
62- throw new IllegalStateException (e );
72+ }
73+ catch (Exception cause ) {
74+ cause .printStackTrace ();
75+ throw new IllegalStateException (cause );
6376 }
6477 }
6578
6679 private static void reload () {
80+
6781 for (WeakReference <CassandraConnectionProperties > ref : instances ) {
6882
6983 CassandraConnectionProperties properties = ref .get ();
84+
7085 if (properties != null ) {
7186 properties .loadProperties ();
7287 }
7388 }
7489 }
7590
76- private CassandraConnectionProperties (String resourceName ) {
77-
78- this .resourceName = resourceName ;
79- loadProperties ();
80-
81- instances .add (new WeakReference <>(this ));
82- }
83-
8491 private void loadProperties () {
85-
8692 loadProperties (this .resourceName );
8793 }
8894
8995 private void loadProperties (String resourceName ) {
9096
91- InputStream in = null ;
92-
93- try {
94- in = getClass ().getResourceAsStream (resourceName );
95-
97+ try (InputStream in = getClass ().getResourceAsStream (resourceName )){
9698 if (in != null ) {
9799 load (in );
98100 }
99- } catch (Exception cause ) {
101+ }
102+ catch (Exception cause ) {
100103 throw new RuntimeException (cause );
101- } finally {
102- if (in != null ) {
103- try {
104- in .close ();
105- } catch (Exception ignore ) {}
106- }
107104 }
108105 }
109106
110107 @ Override
111108 public String getProperty (String key ) {
112109
113110 String value = super .getProperty (key );
111+
114112 if (value == null ) {
115113 value = System .getProperty (key );
116114 }
@@ -154,17 +152,17 @@ public int getCassandraRpcPort() {
154152 }
155153
156154 /**
157- * @return the Cassandra Storage port
155+ * @return the Cassandra SSL Storage port
158156 */
159- public int getCassandraStoragePort () {
160- return getInt ("build.cassandra.storage_port " );
157+ public int getCassandraSslStoragePort () {
158+ return getInt ("build.cassandra.ssl_storage_port " );
161159 }
162160
163161 /**
164- * @return the Cassandra SSL Storage port
162+ * @return the Cassandra Storage port
165163 */
166- public int getCassandraSslStoragePort () {
167- return getInt ("build.cassandra.ssl_storage_port " );
164+ public int getCassandraStoragePort () {
165+ return getInt ("build.cassandra.storage_port " );
168166 }
169167
170168 /**
@@ -174,12 +172,9 @@ public CassandraType getCassandraType() {
174172
175173 String cassandraType = getProperty ("build.cassandra.mode" );
176174
177- if (CassandraType .TESTCONTAINERS .name ().equalsIgnoreCase (cassandraType )) {
178- return CassandraType .TESTCONTAINERS ;
179- }
180-
181- return CassandraType .EXTERNAL .name ().equalsIgnoreCase (cassandraType ) ? CassandraType .EXTERNAL
182- : CassandraType .EMBEDDED ;
175+ return CassandraType .TESTCONTAINERS .name ().equalsIgnoreCase (cassandraType ) ? CassandraType .TESTCONTAINERS
176+ : CassandraType .EXTERNAL .name ().equalsIgnoreCase (cassandraType ) ? CassandraType .EXTERNAL
177+ : CassandraType .EMBEDDED ;
183178 }
184179
185180 /**
@@ -229,6 +224,6 @@ private <T> T convert(String propertyName, Class<T> type, Converter<String, T> c
229224 }
230225
231226 public enum CassandraType {
232- EMBEDDED , EXTERNAL , TESTCONTAINERS ;
227+ EMBEDDED , EXTERNAL , TESTCONTAINERS
233228 }
234229}
0 commit comments