[Fix] Fixed possible NPE

This commit is contained in:
Robert von Burg 2023-04-06 14:32:03 +02:00
parent 78ac6e524c
commit f55780ffb4
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
1 changed files with 15 additions and 14 deletions

View File

@ -1,12 +1,12 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,6 +15,8 @@
*/
package li.strolch.runtime.configuration;
import static java.text.MessageFormat.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@ -42,7 +44,7 @@ public class StrolchEnvironment {
String environment = System.getProperties().getProperty(StrolchConstants.ENV_STROLCH);
if (StringHelper.isEmpty(environment)) {
String msg = "The system property {0} is missing!";
throw new StrolchConfigurationException(MessageFormat.format(msg, StrolchConstants.ENV_STROLCH));
throw new StrolchConfigurationException(format(msg, StrolchConstants.ENV_STROLCH));
}
return environment;
@ -50,21 +52,20 @@ public class StrolchEnvironment {
public static String getEnvironmentFromEnvProperties(File rootPath) {
File envF = new File(rootPath, ENV_PROPERTIES_FILE);
DBC.PRE.assertExists(
MessageFormat.format("{0} does not exist in {1}", ENV_PROPERTIES_FILE, rootPath.getAbsolutePath()),
DBC.PRE.assertExists(format("{0} does not exist in {1}", ENV_PROPERTIES_FILE, rootPath.getAbsolutePath()),
envF);
Properties envP = new Properties();
try (InputStream fin = Files.newInputStream(envF.toPath())) {
envP.load(fin);
} catch (Exception e) {
throw new StrolchConfigurationException(
MessageFormat.format("Failed to load {0} in {1}", ENV_PROPERTIES_FILE, rootPath), e);
throw new StrolchConfigurationException(format("Failed to load {0} in {1}", ENV_PROPERTIES_FILE, rootPath),
e);
}
String environment = envP.getProperty(StrolchConstants.ENV_STROLCH);
if (StringHelper.isEmpty(environment)) {
String msg = "The property {0} does not exist in {1}";
msg = MessageFormat.format(msg, StrolchConstants.ENV_STROLCH, envF.getAbsolutePath());
msg = format(msg, StrolchConstants.ENV_STROLCH, envF.getAbsolutePath());
throw new StrolchConfigurationException(msg);
}
@ -73,14 +74,14 @@ public class StrolchEnvironment {
public static String getEnvironmentFromResourceEnv(Class<?> clazz) {
InputStream stream = clazz.getResourceAsStream("/" + ENV_PROPERTIES_FILE);
DBC.PRE.assertNotNull(
MessageFormat.format("{0} does not exist as root resource for class {1}", ENV_PROPERTIES_FILE, clazz),
stream);
if (stream == null)
throw new IllegalStateException(
format("{0} does not exist as root resource for class {1}", ENV_PROPERTIES_FILE, clazz));
Properties envP = new Properties();
try {
envP.load(stream);
} catch (Exception e) {
throw new StrolchConfigurationException(MessageFormat.format("Failed to load {0}", ENV_PROPERTIES_FILE), e);
throw new StrolchConfigurationException(format("Failed to load {0}", ENV_PROPERTIES_FILE), e);
} finally {
try {
stream.close();
@ -92,7 +93,7 @@ public class StrolchEnvironment {
String environment = envP.getProperty(StrolchConstants.ENV_STROLCH);
if (StringHelper.isEmpty(environment)) {
String msg = "The property {0} does not exist in {1}";
msg = MessageFormat.format(msg, StrolchConstants.ENV_STROLCH, ENV_PROPERTIES_FILE);
msg = format(msg, StrolchConstants.ENV_STROLCH, ENV_PROPERTIES_FILE);
throw new StrolchConfigurationException(msg);
}