22
33import java .io .FileInputStream ;
44import java .io .IOException ;
5+ import java .io .InputStream ;
56import java .nio .file .Files ;
7+ import java .nio .file .Path ;
68import java .nio .file .Paths ;
79import java .security .DigestInputStream ;
810import java .security .GeneralSecurityException ;
1416import java .util .HashSet ;
1517import java .util .List ;
1618import java .util .Map ;
19+ import java .util .Objects ;
1720import java .util .Optional ;
1821import java .util .Set ;
1922import java .util .UUID ;
@@ -89,60 +92,82 @@ public class ConfigurationApplicationTask {
8992 @ EventListener (ApplicationReadyEvent .class )
9093 @ Transactional
9194 public void afterStartup () throws IOException , GeneralSecurityException , JSchException , SQLException {
92- // Your logic here
95+ String yamlPath = systemOptions .getYamlConfiguration ();
96+ if (StringUtils .isEmpty (yamlPath )) {
97+ log .info ("No configuration file found" );
98+ return ;
99+ }
93100
94- if (!StringUtils .isEmpty (systemOptions .getYamlConfiguration ())) {
95- log .info ("Checking for configuration file {}" , systemOptions .getYamlConfiguration ());
96- var digestStream = new DigestInputStream (
97- new FileInputStream (systemOptions .getYamlConfiguration ()),
98- MessageDigest .getInstance ("SHA256" )
99- );
100- MessageDigest digest = digestStream .getMessageDigest ();
101- var hash = new String (digest .digest ());
102-
103- AtomicBoolean recreate = new AtomicBoolean (false );
104- configurationOptionRepository .findLatestByConfigurationName ("yamlConfigurationFileHash" )
105- .ifPresentOrElse (
106- configurationOption -> {
107- if (!hash .equals (configurationOption .getConfigurationValue ())) {
108- log .info ("Configuration file hash has changed, recreating database" );
109- recreate .set (true );
110- }else {
111- log .info ("Configuration file hash has not changed" );
112- }
113- configurationOption .setConfigurationValue (hash );
114- configurationOptionRepository .save (configurationOption );
115- },
116- () -> {
117- log .info ("No configuration file hash found, creating one" );
118- var configurationOption = new ConfigurationOption ();
119- configurationOption .setConfigurationName ("yamlConfigurationFileHash" );
120- configurationOption .setConfigurationValue (hash );
121- configurationOptionRepository .save (configurationOption );
122- recreate .set (true );
123- }
124- );
101+ Path yamlFile = Paths .get (yamlPath );
102+ if (!Files .exists (yamlFile )) {
103+ log .warn ("Configuration file {} not found" , yamlFile );
104+ return ;
105+ }
125106
126- Boolean deleteFile = systemOptions .getDeleteYamlConfigurationFile ();
127- if (null == deleteFile ) {
128- deleteFile = true ;
129- }
130- if (deleteFile ) {
131- Files .delete (Paths .get (systemOptions .getYamlConfiguration ()));
107+ log .info ("Checking for configuration file {}" , yamlFile );
108+
109+ // --- Compute SHA-256 hash of file ---
110+ String hash ;
111+ try {
112+ MessageDigest md = MessageDigest .getInstance ("SHA-256" );
113+ try (InputStream in = Files .newInputStream (yamlFile );
114+ DigestInputStream dis = new DigestInputStream (in , md )) {
115+ byte [] buffer = new byte [8192 ];
116+ while (dis .read (buffer ) != -1 ) {
117+ // Consume stream fully to update digest
118+ }
132119 }
120+ byte [] digest = md .digest ();
121+ StringBuilder sb = new StringBuilder (digest .length * 2 );
122+ for (byte b : digest ) sb .append (String .format ("%02x" , b ));
123+ hash = sb .toString ();
124+ } catch (Exception e ) {
125+ throw new IOException ("Unable to compute SHA-256 for " + yamlFile , e );
126+ }
127+
128+ AtomicBoolean recreate = new AtomicBoolean (false );
129+
130+ configurationOptionRepository .findLatestByConfigurationName ("yamlConfigurationFileHash" )
131+ .ifPresentOrElse (existing -> {
132+ String oldHash = existing .getConfigurationValue ();
133+ if (!hash .equalsIgnoreCase (oldHash )) {
134+ log .info ("Configuration file hash changed. Recreating database." );
135+ recreate .set (true );
136+ } else {
137+ log .info ("Configuration file hash unchanged ({})." , hash );
138+ }
139+ existing .setConfigurationValue (hash );
140+ configurationOptionRepository .save (existing );
141+ }, () -> {
142+ log .info ("No configuration file hash found; creating one." );
143+ var option = new ConfigurationOption ();
144+ option .setConfigurationName ("yamlConfigurationFileHash" );
145+ option .setConfigurationValue (hash );
146+ configurationOptionRepository .save (option );
147+ recreate .set (true );
148+ });
149+
150+ Boolean deleteFile = systemOptions .getDeleteYamlConfigurationFile ();
151+ if (deleteFile == null ) deleteFile = true ;
133152
134- if (recreate .get ()) {
135- log .info ("Recreating database" );
136- var installConfiguration =
137- InstallConfiguration .fromYaml (new FileInputStream (systemOptions .getYamlConfiguration ()));
138- // recreate the database
153+ if (deleteFile ) {
154+ try {
155+ Files .deleteIfExists (yamlFile );
156+ log .info ("Deleted configuration file {}" , yamlFile );
157+ } catch (IOException e ) {
158+ log .warn ("Failed to delete configuration file {}" , yamlFile , e );
159+ }
160+ }
139161
162+ if (recreate .get ()) {
163+ log .info ("Recreating database using configuration {}" , yamlFile );
164+ try (InputStream in = Files .newInputStream (yamlFile )) {
165+ var installConfiguration = InstallConfiguration .fromYaml (in );
140166 initialize (installConfiguration , true );
141167 }
142- } else {
143- log .info ("No configuration file found" );
144168 }
145169 }
170+
146171 @ Transactional
147172 public List <SideEffect > createStaticType (UserType type , boolean action ) throws SQLException ,
148173 GeneralSecurityException {
@@ -498,9 +523,11 @@ protected List<SideEffect> createSystems(InstallConfiguration installConfigurati
498523 if (null != installConfiguration .getSystems ()) {
499524 for (var system : installConfiguration .getSystems ()) {
500525 var systemObj = HostSystem .fromDTO (system );
526+ log .info ("Processing system {} with host {} and port {}" , systemObj .getDisplayName (), systemObj .getHost (), systemObj .getPort ());
501527 if ( shouldInsertSystem (systemObj )) {
502528 if (action ) {
503529 var sys = systemRepository .save (systemObj );
530+ log .info ("Creating system {}, with id {}" , system .getDisplayName (), sys .getId ());
504531 }
505532 sideEffects .add (
506533 SideEffect .builder ().sideEffectDescription ("Creating system " + system .getDisplayName ()).type (
@@ -518,7 +545,8 @@ protected boolean shouldInsertSystem(HostSystem systemObj) {
518545 return true ;
519546 }
520547 for (HostSystem system : systems ) {
521- if (system .getHost ().equals (systemObj .getHost ()) && system .getPort () == systemObj .getPort ()) {
548+ if (system .getHost ().equals (systemObj .getHost ()) && Objects .equals (system .getPort (), systemObj .getPort ())) {
549+ log .info ("System {} with host {} and port {} already exists" , systemObj .getDisplayName (), systemObj .getHost (), systemObj .getPort ());
522550 return false ;
523551 }
524552 }
0 commit comments