forked from redhat-developer/quarkus-ls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support microprofile-config.properties on JDT LS side
See redhat-developer/vscode-quarkus#181 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
4fdf9d2
commit b006f34
Showing
5 changed files
with
220 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...e/src/main/java/com/redhat/microprofile/jdt/internal/core/project/AbstractConfigFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package com.redhat.microprofile.jdt.internal.core.project; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.eclipse.core.runtime.IPath; | ||
import org.eclipse.jdt.core.IClasspathEntry; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.JavaModelException; | ||
import org.eclipse.jdt.internal.core.JavaProject; | ||
|
||
public abstract class AbstractConfigFile<T> implements IConfigFile { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(AbstractConfigFile.class.getName()); | ||
|
||
private final String configFileName; | ||
private final IJavaProject javaProject; | ||
private File configFile; | ||
private long lastModified; | ||
private T config; | ||
|
||
private Integer serverPort; | ||
|
||
public AbstractConfigFile(String configFileName, IJavaProject javaProject) { | ||
this.configFileName = configFileName; | ||
this.javaProject = javaProject; | ||
} | ||
|
||
/** | ||
* Returns the target/classes/application.properties and null otherwise. | ||
* | ||
* <p> | ||
* Using this file instead of using src/main/resources/application.properties | ||
* gives the capability to get the filtered value. | ||
* </p> | ||
* | ||
* @return the target/classes/application.properties and null otherwise. | ||
*/ | ||
protected File getConfigFile() { | ||
if (configFile != null && configFile.exists()) { | ||
return configFile; | ||
} | ||
try { | ||
List<IPath> outputs = Stream.of(((JavaProject) javaProject).getResolvedClasspath(true)) // | ||
.filter(entry -> !entry.isTest()) // | ||
.filter(entry -> entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) // | ||
.map(entry -> entry.getOutputLocation()) // | ||
.filter(output -> output != null) // | ||
.distinct() // | ||
.collect(Collectors.toList()); | ||
for (IPath output : outputs) { | ||
File file = javaProject.getProject().getLocation().append(output.removeFirstSegments(1)) | ||
.append(configFileName).toFile(); | ||
if (file.exists()) { | ||
configFile = file; | ||
return configFile; | ||
} | ||
} | ||
return null; | ||
} catch (JavaModelException e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the loaded application.properties and null otherwise. | ||
* | ||
* @return the loaded application.properties and null otherwise | ||
* @throws JavaModelException | ||
*/ | ||
public T getConfig() { | ||
File configFile = getConfigFile(); | ||
if (configFile == null) { | ||
reset(); | ||
return null; | ||
} | ||
if (configFile.lastModified() != lastModified) { | ||
reset(); | ||
try (InputStream input = new FileInputStream(configFile)) { | ||
config = loadConfig(input); | ||
lastModified = configFile.lastModified(); | ||
} catch (IOException e) { | ||
reset(); | ||
LOGGER.log(Level.SEVERE, "Error while loading properties from '" + configFile.getPath() + "'.", e); | ||
} | ||
} | ||
return config; | ||
} | ||
|
||
private void reset() { | ||
config = null; | ||
serverPort = null; | ||
} | ||
|
||
/** | ||
* Returns the server port. | ||
* | ||
* @return the server port. | ||
*/ | ||
public Integer getServerPort() { | ||
try { | ||
// Get application properties and reset the cache if needs | ||
T properties = getConfig(); | ||
if (serverPort == null) { | ||
if (properties != null) { | ||
String port = getProperty("quarkus.http.port", ""); | ||
if (!port.trim().isEmpty()) { | ||
serverPort = Integer.parseInt(port.trim()); | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
LOGGER.log(Level.SEVERE, "Error while getting 'quarkus.http.port", e); | ||
} | ||
return serverPort; | ||
} | ||
|
||
protected abstract T loadConfig(InputStream input) throws IOException; | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...jdt.core/src/main/java/com/redhat/microprofile/jdt/internal/core/project/IConfigFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.redhat.microprofile.jdt.internal.core.project; | ||
|
||
public interface IConfigFile { | ||
|
||
String getProperty(String key, String defaultValue); | ||
|
||
Integer getServerPort(); | ||
} |
Oops, something went wrong.