24
24
import java .util .Locale ;
25
25
import java .util .Random ;
26
26
import java .util .TimeZone ;
27
+ import lombok .SneakyThrows ;
27
28
import org .apache .commons .lang3 .StringUtils ;
28
29
import org .apache .hertzbeat .common .constants .CommonConstants ;
29
30
import org .apache .hertzbeat .common .entity .manager .GeneralConfig ;
37
38
import org .apache .hertzbeat .manager .service .impl .SystemSecretServiceImpl ;
38
39
import org .apache .hertzbeat .manager .service .impl .TemplateConfigServiceImpl ;
39
40
import org .springframework .beans .factory .annotation .Value ;
40
- import org .springframework .boot . CommandLineRunner ;
41
+ import org .springframework .context . SmartLifecycle ;
41
42
import org .springframework .core .Ordered ;
42
43
import org .springframework .core .annotation .Order ;
43
44
import org .springframework .stereotype .Component ;
44
45
45
46
/**
46
- * Common CommandLineRunner class
47
+ * Config Initializer
47
48
*/
48
49
@ Component
49
50
@ Order (value = Ordered .HIGHEST_PRECEDENCE + 2 )
50
- public class CommonCommandLineRunner implements CommandLineRunner {
51
+ public class ConfigInitializer implements SmartLifecycle {
51
52
52
- private static final String DEFAULT_JWT_SECRET = "CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R "
53
+ private boolean running = false ;
54
+
55
+ private static final String DEFAULT_JWT_SECRET = "CyaFv0bwq2Eik0jdrKUtsA6bx3sDJeFV643R "
53
56
+ "LnfKefTjsIfJLBa2YkhEqEGtcHDTNe4CU6+9 "
54
57
+ "8tVt4bisXQ13rbN0oxhUZR73M6EByXIO+SV5 "
55
58
+ "dKhaX0csgOCTlCxq20yhmUea6H6JIpSE2Rwp" ;
56
-
59
+
57
60
@ Value ("${sureness.jwt.secret:" + DEFAULT_JWT_SECRET + "}" )
58
61
private String currentJwtSecret ;
59
-
62
+
60
63
@ Resource
61
64
private SystemGeneralConfigServiceImpl systemGeneralConfigService ;
62
-
65
+
63
66
@ Resource
64
67
private SystemSecretServiceImpl systemSecretService ;
65
-
68
+
66
69
@ Resource
67
70
private TemplateConfigServiceImpl templateConfigService ;
68
-
71
+
69
72
@ Resource
70
73
private AppService appService ;
71
-
74
+
72
75
@ Resource
73
76
protected GeneralConfigDao generalConfigDao ;
74
-
77
+
75
78
@ Resource
76
79
protected ObjectMapper objectMapper ;
77
80
78
- @ Override
79
- public void run ( String ... args ) throws Exception {
81
+ @ SneakyThrows
82
+ public void initConfig () {
80
83
// for system config
81
84
SystemConfig systemConfig = systemGeneralConfigService .getConfig ();
82
85
if (systemConfig != null ) {
@@ -89,7 +92,7 @@ public void run(String... args) throws Exception {
89
92
} else {
90
93
// init system config data
91
94
systemConfig = SystemConfig .builder ().timeZoneId (TimeZone .getDefault ().getID ())
92
- .locale (Locale .getDefault ().getLanguage () + CommonConstants .LOCALE_SEPARATOR
95
+ .locale (Locale .getDefault ().getLanguage () + CommonConstants .LOCALE_SEPARATOR
93
96
+ Locale .getDefault ().getCountry ())
94
97
.build ();
95
98
String contentJson = objectMapper .writeValueAsString (systemConfig );
@@ -126,4 +129,25 @@ public void run(String... args) throws Exception {
126
129
// set the jwt secret token in util
127
130
JsonWebTokenUtil .setDefaultSecretKey (currentJwtSecret );
128
131
}
132
+
133
+ @ Override
134
+ public void start () {
135
+ initConfig ();
136
+ running = true ;
137
+ }
138
+
139
+ @ Override
140
+ public void stop () {
141
+ running = false ;
142
+ }
143
+
144
+ @ Override
145
+ public boolean isRunning () {
146
+ return running ;
147
+ }
148
+
149
+ @ Override
150
+ public int getPhase () {
151
+ return Ordered .HIGHEST_PRECEDENCE ;
152
+ }
129
153
}
0 commit comments