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
9713bc6
commit 10a7897
Showing
8 changed files
with
350 additions
and
144 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
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
156 changes: 156 additions & 0 deletions
156
...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,156 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
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; | ||
|
||
/** | ||
* Abstract class for config file. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
* @param <T> the config model (ex: Properties for *.properties file) | ||
*/ | ||
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; | ||
|
||
public AbstractConfigFile(String configFileName, IJavaProject javaProject) { | ||
this.configFileName = configFileName; | ||
this.javaProject = javaProject; | ||
} | ||
|
||
/** | ||
* Returns the target/classes/$configFile and null otherwise. | ||
* | ||
* <p> | ||
* Using this file instead of using src/main/resources/$configFile gives the | ||
* capability to get the filtered value. | ||
* </p> | ||
* | ||
* @return the target/classes/$configFile 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) { | ||
LOGGER.log(Level.SEVERE, "Error while getting configuration", e); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the loaded config and null otherwise. | ||
* | ||
* @return the loaded config and null otherwise | ||
*/ | ||
private 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; | ||
} | ||
|
||
@Override | ||
public final String getProperty(String key) { | ||
T config = getConfig(); | ||
if (config == null) { | ||
return null; | ||
} | ||
return getProperty(key, config); | ||
} | ||
|
||
@Override | ||
public Integer getPropertyAsInt(String key) { | ||
String property = getProperty(key); | ||
if (property != null && !property.trim().isEmpty()) { | ||
try { | ||
return Integer.parseInt(property.trim()); | ||
} catch (NumberFormatException e) { | ||
LOGGER.log(Level.SEVERE, "Error while getting '" + key + "'", e); | ||
return null; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private void reset() { | ||
config = null; | ||
} | ||
|
||
/** | ||
* Load the config model from the given input stream <code>input</code>. | ||
* | ||
* @param input the input stream | ||
* @return he config model from the given input stream <code>input</code>. | ||
* @throws IOException | ||
*/ | ||
protected abstract T loadConfig(InputStream input) throws IOException; | ||
|
||
/** | ||
* Returns the property from the given <code>key</code> and null otherwise. | ||
* | ||
* @param key | ||
* @param config | ||
* @return the property from the given <code>key</code> and null otherwise. | ||
*/ | ||
protected abstract String getProperty(String key, T config); | ||
|
||
} |
Oops, something went wrong.