25
25
import java .util .List ;
26
26
import java .util .Map ;
27
27
import java .util .Set ;
28
- import java .util .logging .Level ;
29
28
import java .util .logging .Logger ;
30
29
import java .util .regex .Matcher ;
31
30
import java .util .regex .Pattern ;
51
50
52
51
import static java .util .logging .Level .FINE ;
53
52
import static java .util .logging .Level .INFO ;
53
+ import static java .util .logging .Level .SEVERE ;
54
54
import static java .util .regex .Pattern .compile ;
55
55
56
56
/**
@@ -79,36 +79,62 @@ public class SpringBootServiceImpl implements SpringBootService {
79
79
private SimpleConfigurationMetadataRepository repo = new SimpleConfigurationMetadataRepository ();
80
80
private final Map <String , ConfigurationMetadataRepository > reposInJars = new HashMap <>();
81
81
private NbMavenProjectImpl mvnPrj ;
82
+ private String springBootVersion ;
82
83
private ClassPath cpExec ;
83
84
private Map <String , ConfigurationMetadataProperty > cachedProperties ;
84
- private Map <String , Boolean > cachedDepsPresence = new HashMap <>();
85
+ private final Map <String , Boolean > cachedDepsPresence = new HashMap <>();
85
86
private final Set <String > collectionProperties = new HashSet <>();
86
87
private final Set <String > mapProperties = new HashSet <>();
87
88
private final Map <String , HintProvider > providerMap = new HashMap <>();
88
89
89
90
public SpringBootServiceImpl (Project p ) {
90
- if (p instanceof NbMavenProjectImpl ) {
91
- this .mvnPrj = (NbMavenProjectImpl ) p ;
91
+ final FileObject projectDirectory = p .getProjectDirectory ();
92
+ if (p instanceof NbMavenProjectImpl ){
93
+ logger .log (INFO , "Creating Spring Boot service for project {0}" , FileUtil .getFileDisplayName (projectDirectory ));
94
+ this .mvnPrj = (NbMavenProjectImpl ) p ;
95
+
96
+ logger .fine ("Checking maven project has a spring boot dependency" );
97
+ // check maven project is a spring-boot project
98
+ this .springBootVersion = Utils .getSpringBootVersion (mvnPrj ).orElse (null );
99
+ // early exit if no spring boot dependency detected
100
+ if (springBootVersion == null ) {
101
+ return ;
102
+ }
103
+
104
+ // listen for pom changes
105
+ logger .info ("Adding maven pom listener..." );
106
+ this .mvnPrj .getProjectWatcher ().addPropertyChangeListener (new PropertyChangeListener () {
107
+ @ Override
108
+ public void propertyChange (PropertyChangeEvent evt ) {
109
+ if (NbMavenProject .PROP_PROJECT .equals (evt .getPropertyName ())) {
110
+ logger .log (FINE , "Maven pom change ({0})" , evt .getPropertyName ());
111
+ long start = System .currentTimeMillis ();
112
+ refresh ();
113
+ long elapsedMs = System .currentTimeMillis () - start ;
114
+ logger .log (FINE , "Spring Boot service refresh took {0}ms" , elapsedMs );
115
+ }
116
+ }
117
+ });
118
+ } else {
119
+ logger .log (SEVERE , "Error creating Spring Boot service for project {0}" , FileUtil .getFileDisplayName (projectDirectory ));
92
120
}
93
- final FileObject projectDirectory = mvnPrj .getProjectDirectory ();
94
- logger .log (Level .INFO , "Creating Spring Boot service for project {0}" , FileUtil .getFileDisplayName (projectDirectory ));
95
121
}
96
122
97
123
@ Override
98
124
public void refresh () {
99
125
logger .info ("Refreshing Spring Boot service" );
100
126
// check maven project has a dependency starting with 'spring-boot'
101
127
logger .fine ("Checking maven project has a spring boot dependency" );
102
- boolean springBootAvailable = Utils .dependencyArtifactIdContains (mvnPrj . getProjectWatcher (), "spring-boot" );
128
+ springBootVersion = Utils .getSpringBootVersion (mvnPrj ). orElse ( null );
103
129
// clear and exit if no spring boot dependency detected
104
- if (! springBootAvailable ) {
130
+ if (springBootVersion == null ) {
105
131
reposInJars .clear ();
106
132
collectionProperties .clear ();
107
133
mapProperties .clear ();
108
134
// TODO delete nbactions.xml file from project dir ?
109
135
return ;
110
136
}
111
- cachedDepsPresence .clear ();
137
+ cachedDepsPresence .clear ();
112
138
if (cpExec == null ) {
113
139
init ();
114
140
} else {
@@ -194,16 +220,10 @@ public List<ConfigurationMetadataProperty> queryPropertyMetadata(String filter)
194
220
195
221
@ Override
196
222
public boolean hasPomDependency (String artifactId ) {
197
- if (cpExec == null ) {
198
- init ( );
223
+ if (! cachedDepsPresence . containsKey ( artifactId ) ) {
224
+ cachedDepsPresence . put ( artifactId , Utils . dependencyArtifactIdContains ( mvnPrj . getProjectWatcher (), artifactId ) );
199
225
}
200
- if (cachedDepsPresence != null ) {
201
- if (!cachedDepsPresence .containsKey (artifactId )) {
202
- cachedDepsPresence .put (artifactId , Utils .dependencyArtifactIdContains (mvnPrj .getProjectWatcher (), artifactId ));
203
- }
204
- return cachedDepsPresence .get (artifactId );
205
- }
206
- return false ;
226
+ return cachedDepsPresence .get (artifactId );
207
227
}
208
228
209
229
@ Override
@@ -217,34 +237,9 @@ public String getPluginPropsPrefix() {
217
237
}
218
238
219
239
private void init () {
220
- if (mvnPrj == null ) {
221
- return ;
222
- }
223
- logger .info ("Initializing Spring Boot service" );
224
- // check maven project has a dependency starting with 'spring-boot'
225
- boolean springBootAvailable = Utils .dependencyArtifactIdContains (mvnPrj .getProjectWatcher (), "spring-boot" );
226
- logger .fine ("Checking maven project has a spring boot dependency" );
227
- // early exit if no spring boot dependency detected
228
- if (!springBootAvailable ) {
229
- return ;
230
- }
231
- logger .log (INFO , "Initializing SpringBootService for project {0}" , new Object []{mvnPrj .toString ()});
232
- cachedDepsPresence .clear ();
233
240
// set up a reference to the execute classpath object
234
241
cpExec = Utils .execClasspathForProj (mvnPrj );
235
242
if (cpExec != null ) {
236
- // listen for pom changes
237
- logger .info ("Adding maven pom listener..." );
238
- mvnPrj .getProjectWatcher ().addPropertyChangeListener (new PropertyChangeListener () {
239
- @ Override
240
- public void propertyChange (PropertyChangeEvent evt ) {
241
- final String propertyName = String .valueOf (evt .getPropertyName ());
242
- logger .log (FINE , "Maven pom change ({0})" , propertyName );
243
- if (propertyName .equals ("MavenProject" )) {
244
- refresh ();
245
- }
246
- }
247
- });
248
243
// populate hint providers map
249
244
FileObject resourcesFolder = Utils .resourcesFolderForProj (mvnPrj );
250
245
providerMap .put ("logger-name" , new LoggerNameHintProvider (resourcesFolder ));
@@ -305,21 +300,14 @@ private void updateConfigRepo() {
305
300
306
301
// tell if the project currently uses Spring Boot 2.x
307
302
private boolean isBoot2 () {
308
- boolean flag = false ;
309
- if (mvnPrj != null ) {
310
- // retrieve boot version from parent pom declaration if present
311
- // TODO also look into parent hierarchy
312
- // TODO also try to look in dependency management section (inclusion of spring boot BOM)
313
- String bootVer = mvnPrj .getOriginalMavenProject ().getParentArtifact ().getVersion ();
314
- flag = bootVer .startsWith ("2" );
315
- }
316
- return flag ;
303
+ return springBootVersion != null && springBootVersion .startsWith ("2" );
317
304
}
318
305
319
306
private void adjustNbActions () {
320
307
final FileObject foPrjDir = mvnPrj .getProjectDirectory ();
321
308
FileObject foNbAct = foPrjDir .getFileObject ("nbactions.xml" );
322
- if (foNbAct != null ) {
309
+ // only adjust nbactions.xml if necessary
310
+ if (foNbAct != null && isAdjustingNeeded (foNbAct )) {
323
311
logger .fine ("Adjusting nbactions.xml file" );
324
312
try (FileLock lock = foNbAct .lock ()) {
325
313
try (PrintWriter pw = new PrintWriter (foPrjDir .createAndOpen ("nbactions.tmp" ))) {
@@ -352,4 +340,16 @@ private void adjustNbActions() {
352
340
}
353
341
}
354
342
343
+ private boolean isAdjustingNeeded (FileObject nbActions ){
344
+ try {
345
+ String wrongPluginPropsPrefix = isBoot2 () ? "<run." : "<spring-boot.run." ;
346
+ String wrongRestartTriggerFile = isBoot2 () ? ENV_RESTART_15 : ENV_RESTART_20 ;
347
+ return nbActions .asLines ().stream ()
348
+ .anyMatch (line -> line .contains (wrongPluginPropsPrefix ) || line .contains (wrongRestartTriggerFile ));
349
+ } catch (IOException ex ){
350
+ Exceptions .printStackTrace (ex );
351
+ return false ;
352
+ }
353
+ }
354
+
355
355
}
0 commit comments