|
@@ -51,8 +51,9 @@ import java.util.Map;
|
|
import java.util.Properties;
|
|
import java.util.Properties;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
-public class PwmEnvironment {
|
|
|
|
- private static final PwmLogger LOGGER = PwmLogger.forClass(PwmEnvironment.class);
|
|
|
|
|
|
+public class PwmEnvironment
|
|
|
|
+{
|
|
|
|
+ private static final PwmLogger LOGGER = PwmLogger.forClass( PwmEnvironment.class );
|
|
|
|
|
|
// data elements
|
|
// data elements
|
|
private final PwmApplicationMode applicationMode;
|
|
private final PwmApplicationMode applicationMode;
|
|
@@ -62,11 +63,12 @@ public class PwmEnvironment {
|
|
private final File configurationFile;
|
|
private final File configurationFile;
|
|
private final ContextManager contextManager;
|
|
private final ContextManager contextManager;
|
|
private final Collection<ApplicationFlag> flags;
|
|
private final Collection<ApplicationFlag> flags;
|
|
- private final Map<ApplicationParameter,String> parameters;
|
|
|
|
|
|
+ private final Map<ApplicationParameter, String> parameters;
|
|
|
|
|
|
private final FileLocker fileLocker;
|
|
private final FileLocker fileLocker;
|
|
|
|
|
|
- public enum ApplicationParameter {
|
|
|
|
|
|
+ public enum ApplicationParameter
|
|
|
|
+ {
|
|
AutoExportHttpsKeyStoreFile,
|
|
AutoExportHttpsKeyStoreFile,
|
|
AutoExportHttpsKeyStorePassword,
|
|
AutoExportHttpsKeyStorePassword,
|
|
AutoExportHttpsKeyStoreAlias,
|
|
AutoExportHttpsKeyStoreAlias,
|
|
@@ -74,70 +76,72 @@ public class PwmEnvironment {
|
|
AutoWriteTomcatConfOutputFile,
|
|
AutoWriteTomcatConfOutputFile,
|
|
AppliancePort,
|
|
AppliancePort,
|
|
ApplianceHostnameFile,
|
|
ApplianceHostnameFile,
|
|
- ApplianceTokenFile,
|
|
|
|
-
|
|
|
|
- ;
|
|
|
|
|
|
+ ApplianceTokenFile,;
|
|
|
|
|
|
- public static ApplicationParameter forString(final String input) {
|
|
|
|
- return JavaHelper.readEnumFromString(ApplicationParameter.class, null, input);
|
|
|
|
|
|
+ public static ApplicationParameter forString( final String input )
|
|
|
|
+ {
|
|
|
|
+ return JavaHelper.readEnumFromString( ApplicationParameter.class, null, input );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public enum ApplicationFlag {
|
|
|
|
|
|
+ public enum ApplicationFlag
|
|
|
|
+ {
|
|
Appliance,
|
|
Appliance,
|
|
Docker,
|
|
Docker,
|
|
ManageHttps,
|
|
ManageHttps,
|
|
NoFileLock,
|
|
NoFileLock,
|
|
- CommandLineInstance,
|
|
|
|
-
|
|
|
|
- ;
|
|
|
|
|
|
+ CommandLineInstance,;
|
|
|
|
|
|
- public static ApplicationFlag forString(final String input) {
|
|
|
|
- return JavaHelper.readEnumFromString(ApplicationFlag.class, null, input);
|
|
|
|
|
|
+ public static ApplicationFlag forString( final String input )
|
|
|
|
+ {
|
|
|
|
+ return JavaHelper.readEnumFromString( ApplicationFlag.class, null, input );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public enum EnvironmentParameter {
|
|
|
|
|
|
+ public enum EnvironmentParameter
|
|
|
|
+ {
|
|
applicationPath,
|
|
applicationPath,
|
|
applicationFlags,
|
|
applicationFlags,
|
|
- applicationParamFile,
|
|
|
|
-
|
|
|
|
- ;
|
|
|
|
|
|
+ applicationParamFile,;
|
|
|
|
|
|
- public String conicalJavaOptionSystemName() {
|
|
|
|
|
|
+ public String conicalJavaOptionSystemName( )
|
|
|
|
+ {
|
|
return PwmConstants.PWM_APP_NAME.toLowerCase() + "." + this.toString();
|
|
return PwmConstants.PWM_APP_NAME.toLowerCase() + "." + this.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- public String conicalEnvironmentSystemName() {
|
|
|
|
- return (PwmConstants.PWM_APP_NAME.toLowerCase() + "_" + this.toString()).toUpperCase();
|
|
|
|
|
|
+ public String conicalEnvironmentSystemName( )
|
|
|
|
+ {
|
|
|
|
+ return ( PwmConstants.PWM_APP_NAME.toLowerCase() + "_" + this.toString() ).toUpperCase();
|
|
}
|
|
}
|
|
|
|
|
|
- public List<String> possibleNames(final String contextName) {
|
|
|
|
|
|
+ public List<String> possibleNames( final String contextName )
|
|
|
|
+ {
|
|
final List<String> returnValues = new ArrayList<>();
|
|
final List<String> returnValues = new ArrayList<>();
|
|
- if (contextName != null) {
|
|
|
|
|
|
+ if ( contextName != null )
|
|
|
|
+ {
|
|
// java property format <app>.<context>.<paramName> like pwm.pwm.applicationFlag
|
|
// java property format <app>.<context>.<paramName> like pwm.pwm.applicationFlag
|
|
final String value = PwmConstants.PWM_APP_NAME.toLowerCase()
|
|
final String value = PwmConstants.PWM_APP_NAME.toLowerCase()
|
|
+ "."
|
|
+ "."
|
|
+ contextName
|
|
+ contextName
|
|
+ "."
|
|
+ "."
|
|
+ this.toString();
|
|
+ this.toString();
|
|
- returnValues.add(value);
|
|
|
|
- returnValues.add(value.toUpperCase());
|
|
|
|
- returnValues.add(value.replace(".", "_"));
|
|
|
|
- returnValues.add(value.toUpperCase().replace(".", "_"));
|
|
|
|
|
|
+ returnValues.add( value );
|
|
|
|
+ returnValues.add( value.toUpperCase() );
|
|
|
|
+ returnValues.add( value.replace( ".", "_" ) );
|
|
|
|
+ returnValues.add( value.toUpperCase().replace( ".", "_" ) );
|
|
}
|
|
}
|
|
{
|
|
{
|
|
// java property format <app>.<paramName> like pwm.applicationFlag
|
|
// java property format <app>.<paramName> like pwm.applicationFlag
|
|
final String value = PwmConstants.PWM_APP_NAME.toLowerCase()
|
|
final String value = PwmConstants.PWM_APP_NAME.toLowerCase()
|
|
+ "."
|
|
+ "."
|
|
+ this.toString();
|
|
+ this.toString();
|
|
- returnValues.add(value);
|
|
|
|
- returnValues.add(value.toUpperCase());
|
|
|
|
- returnValues.add(value.replace(".","_"));
|
|
|
|
- returnValues.add(value.toUpperCase().replace(".", "_"));
|
|
|
|
|
|
+ returnValues.add( value );
|
|
|
|
+ returnValues.add( value.toUpperCase() );
|
|
|
|
+ returnValues.add( value.replace( ".", "_" ) );
|
|
|
|
+ returnValues.add( value.toUpperCase().replace( ".", "_" ) );
|
|
}
|
|
}
|
|
|
|
|
|
- return Collections.unmodifiableList(returnValues);
|
|
|
|
|
|
+ return Collections.unmodifiableList( returnValues );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -149,74 +153,87 @@ public class PwmEnvironment {
|
|
final File configurationFile,
|
|
final File configurationFile,
|
|
final ContextManager contextManager,
|
|
final ContextManager contextManager,
|
|
final Collection<ApplicationFlag> flags,
|
|
final Collection<ApplicationFlag> flags,
|
|
- final Map<ApplicationParameter,String> parameters
|
|
|
|
- ) {
|
|
|
|
|
|
+ final Map<ApplicationParameter, String> parameters
|
|
|
|
+ )
|
|
|
|
+ {
|
|
this.applicationMode = applicationMode == null ? PwmApplicationMode.ERROR : applicationMode;
|
|
this.applicationMode = applicationMode == null ? PwmApplicationMode.ERROR : applicationMode;
|
|
this.config = config;
|
|
this.config = config;
|
|
this.applicationPath = applicationPath;
|
|
this.applicationPath = applicationPath;
|
|
this.internalRuntimeInstance = internalRuntimeInstance;
|
|
this.internalRuntimeInstance = internalRuntimeInstance;
|
|
this.configurationFile = configurationFile;
|
|
this.configurationFile = configurationFile;
|
|
this.contextManager = contextManager;
|
|
this.contextManager = contextManager;
|
|
- this.flags = flags == null ? Collections.emptySet() : Collections.unmodifiableSet(new HashSet<>(flags));
|
|
|
|
- this.parameters = parameters == null ? Collections.emptyMap() : Collections.unmodifiableMap(parameters);
|
|
|
|
|
|
+ this.flags = flags == null ? Collections.emptySet() : Collections.unmodifiableSet( new HashSet<>( flags ) );
|
|
|
|
+ this.parameters = parameters == null ? Collections.emptyMap() : Collections.unmodifiableMap( parameters );
|
|
|
|
|
|
this.fileLocker = new FileLocker();
|
|
this.fileLocker = new FileLocker();
|
|
|
|
|
|
verify();
|
|
verify();
|
|
}
|
|
}
|
|
|
|
|
|
- public PwmApplicationMode getApplicationMode() {
|
|
|
|
|
|
+ public PwmApplicationMode getApplicationMode( )
|
|
|
|
+ {
|
|
return applicationMode;
|
|
return applicationMode;
|
|
}
|
|
}
|
|
|
|
|
|
- public Configuration getConfig() {
|
|
|
|
|
|
+ public Configuration getConfig( )
|
|
|
|
+ {
|
|
return config;
|
|
return config;
|
|
}
|
|
}
|
|
|
|
|
|
- public File getApplicationPath() {
|
|
|
|
|
|
+ public File getApplicationPath( )
|
|
|
|
+ {
|
|
return applicationPath;
|
|
return applicationPath;
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean isInternalRuntimeInstance() {
|
|
|
|
|
|
+ public boolean isInternalRuntimeInstance( )
|
|
|
|
+ {
|
|
return internalRuntimeInstance;
|
|
return internalRuntimeInstance;
|
|
}
|
|
}
|
|
|
|
|
|
- public File getConfigurationFile() {
|
|
|
|
|
|
+ public File getConfigurationFile( )
|
|
|
|
+ {
|
|
return configurationFile;
|
|
return configurationFile;
|
|
}
|
|
}
|
|
|
|
|
|
- public ContextManager getContextManager() {
|
|
|
|
|
|
+ public ContextManager getContextManager( )
|
|
|
|
+ {
|
|
return contextManager;
|
|
return contextManager;
|
|
}
|
|
}
|
|
|
|
|
|
- public Collection<ApplicationFlag> getFlags() {
|
|
|
|
|
|
+ public Collection<ApplicationFlag> getFlags( )
|
|
|
|
+ {
|
|
return flags;
|
|
return flags;
|
|
}
|
|
}
|
|
|
|
|
|
- public Map<ApplicationParameter, String> getParameters() {
|
|
|
|
|
|
+ public Map<ApplicationParameter, String> getParameters( )
|
|
|
|
+ {
|
|
return parameters;
|
|
return parameters;
|
|
}
|
|
}
|
|
|
|
|
|
- private void verify() {
|
|
|
|
|
|
+ private void verify( )
|
|
|
|
+ {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public void verifyIfApplicationPathIsSetProperly()
|
|
|
|
|
|
+ public void verifyIfApplicationPathIsSetProperly( )
|
|
throws PwmUnrecoverableException
|
|
throws PwmUnrecoverableException
|
|
{
|
|
{
|
|
final File applicationPath = this.getApplicationPath();
|
|
final File applicationPath = this.getApplicationPath();
|
|
|
|
|
|
- verifyApplicationPath(applicationPath);
|
|
|
|
|
|
+ verifyApplicationPath( applicationPath );
|
|
|
|
|
|
boolean applicationPathIsWebInfPath = false;
|
|
boolean applicationPathIsWebInfPath = false;
|
|
- if (applicationPath.getAbsolutePath().endsWith("/WEB-INF")) {
|
|
|
|
- final File webXmlFile = new File(applicationPath.getAbsolutePath() + File.separator + "web.xml");
|
|
|
|
- if (webXmlFile.exists()) {
|
|
|
|
|
|
+ if ( applicationPath.getAbsolutePath().endsWith( "/WEB-INF" ) )
|
|
|
|
+ {
|
|
|
|
+ final File webXmlFile = new File( applicationPath.getAbsolutePath() + File.separator + "web.xml" );
|
|
|
|
+ if ( webXmlFile.exists() )
|
|
|
|
+ {
|
|
applicationPathIsWebInfPath = true;
|
|
applicationPathIsWebInfPath = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if (applicationPathIsWebInfPath) {
|
|
|
|
- LOGGER.trace("applicationPath appears to be servlet /WEB-INF directory");
|
|
|
|
|
|
+ if ( applicationPathIsWebInfPath )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.trace( "applicationPath appears to be servlet /WEB-INF directory" );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -225,76 +242,105 @@ public class PwmEnvironment {
|
|
)
|
|
)
|
|
throws PwmUnrecoverableException
|
|
throws PwmUnrecoverableException
|
|
{
|
|
{
|
|
- return new Builder(this)
|
|
|
|
- .setApplicationMode(PwmApplicationMode.NEW)
|
|
|
|
- .setInternalRuntimeInstance(true)
|
|
|
|
- .setConfigurationFile(null)
|
|
|
|
- .setConfig(configuration)
|
|
|
|
|
|
+ return new Builder( this )
|
|
|
|
+ .setApplicationMode( PwmApplicationMode.NEW )
|
|
|
|
+ .setInternalRuntimeInstance( true )
|
|
|
|
+ .setConfigurationFile( null )
|
|
|
|
+ .setConfig( configuration )
|
|
.createPwmEnvironment();
|
|
.createPwmEnvironment();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public static void verifyApplicationPath(final File applicationPath) throws PwmUnrecoverableException {
|
|
|
|
|
|
+ public static void verifyApplicationPath( final File applicationPath ) throws PwmUnrecoverableException
|
|
|
|
+ {
|
|
|
|
|
|
- if (applicationPath == null) {
|
|
|
|
- throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, "unable to determine valid applicationPath"));
|
|
|
|
|
|
+ if ( applicationPath == null )
|
|
|
|
+ {
|
|
|
|
+ throw new PwmUnrecoverableException(
|
|
|
|
+ new ErrorInformation( PwmError.ERROR_STARTUP_ERROR,
|
|
|
|
+ "unable to determine valid applicationPath" )
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
- LOGGER.trace("examining applicationPath of " + applicationPath.getAbsolutePath() + "");
|
|
|
|
|
|
+ LOGGER.trace( "examining applicationPath of " + applicationPath.getAbsolutePath() + "" );
|
|
|
|
|
|
- if (!applicationPath.exists()) {
|
|
|
|
- throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, "applicationPath " + applicationPath.getAbsolutePath() + " does not exist"));
|
|
|
|
|
|
+ if ( !applicationPath.exists() )
|
|
|
|
+ {
|
|
|
|
+ throw new PwmUnrecoverableException(
|
|
|
|
+ new ErrorInformation( PwmError.ERROR_STARTUP_ERROR,
|
|
|
|
+ "applicationPath " + applicationPath.getAbsolutePath() + " does not exist" )
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
- if (!applicationPath.canRead()) {
|
|
|
|
- throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, "unable to read from applicationPath " + applicationPath.getAbsolutePath() + ""));
|
|
|
|
|
|
+ if ( !applicationPath.canRead() )
|
|
|
|
+ {
|
|
|
|
+ throw new PwmUnrecoverableException(
|
|
|
|
+ new ErrorInformation( PwmError.ERROR_STARTUP_ERROR,
|
|
|
|
+ "unable to read from applicationPath " + applicationPath.getAbsolutePath() + "" )
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
- if (!applicationPath.canWrite()) {
|
|
|
|
- throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, "unable to write to applicationPath " + applicationPath.getAbsolutePath() + ""));
|
|
|
|
|
|
+ if ( !applicationPath.canWrite() )
|
|
|
|
+ {
|
|
|
|
+ throw new PwmUnrecoverableException(
|
|
|
|
+ new ErrorInformation( PwmError.ERROR_STARTUP_ERROR,
|
|
|
|
+ "unable to write to applicationPath " + applicationPath.getAbsolutePath() + "" )
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
- final File infoFile = new File(applicationPath.getAbsolutePath() + File.separator + PwmConstants.APPLICATION_PATH_INFO_FILE);
|
|
|
|
- LOGGER.trace("checking " + infoFile.getAbsolutePath() + " status");
|
|
|
|
- if (infoFile.exists()) {
|
|
|
|
|
|
+ final File infoFile = new File( applicationPath.getAbsolutePath() + File.separator + PwmConstants.APPLICATION_PATH_INFO_FILE );
|
|
|
|
+ LOGGER.trace( "checking " + infoFile.getAbsolutePath() + " status" );
|
|
|
|
+ if ( infoFile.exists() )
|
|
|
|
+ {
|
|
final String errorMsg = "The file " + infoFile.getAbsolutePath() + " exists, and an applicationPath was not explicitly specified."
|
|
final String errorMsg = "The file " + infoFile.getAbsolutePath() + " exists, and an applicationPath was not explicitly specified."
|
|
+ " This happens when an applicationPath was previously configured, but is not now being specified."
|
|
+ " This happens when an applicationPath was previously configured, but is not now being specified."
|
|
- + " An explicit applicationPath parameter must be specified, or the file can be removed if the applicationPath should be changed to the default /WEB-INF directory.";
|
|
|
|
- throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_STARTUP_ERROR, errorMsg));
|
|
|
|
|
|
+ + " An explicit applicationPath parameter must be specified, or the file can be removed if the applicationPath"
|
|
|
|
+ + " should be changed to the default /WEB-INF directory.";
|
|
|
|
+ throw new PwmUnrecoverableException( new ErrorInformation( PwmError.ERROR_STARTUP_ERROR, errorMsg ) );
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public static class ParseHelper {
|
|
|
|
- public static Collection<ApplicationFlag> readApplicationFlagsFromSystem(final String contextName) {
|
|
|
|
- final String rawValue = readValueFromSystem(EnvironmentParameter.applicationFlags, contextName);
|
|
|
|
- if (rawValue != null) {
|
|
|
|
- return parseApplicationFlagValueParameter(rawValue);
|
|
|
|
|
|
+ public static class ParseHelper
|
|
|
|
+ {
|
|
|
|
+ public static Collection<ApplicationFlag> readApplicationFlagsFromSystem( final String contextName )
|
|
|
|
+ {
|
|
|
|
+ final String rawValue = readValueFromSystem( EnvironmentParameter.applicationFlags, contextName );
|
|
|
|
+ if ( rawValue != null )
|
|
|
|
+ {
|
|
|
|
+ return parseApplicationFlagValueParameter( rawValue );
|
|
}
|
|
}
|
|
return Collections.emptyList();
|
|
return Collections.emptyList();
|
|
}
|
|
}
|
|
|
|
|
|
- public static Map<ApplicationParameter,String> readApplicationParmsFromSystem(final String contextName) {
|
|
|
|
- final String rawValue = readValueFromSystem(EnvironmentParameter.applicationParamFile, contextName);
|
|
|
|
- if (rawValue != null) {
|
|
|
|
- return parseApplicationParamValueParameter(rawValue);
|
|
|
|
|
|
+ public static Map<ApplicationParameter, String> readApplicationParmsFromSystem( final String contextName )
|
|
|
|
+ {
|
|
|
|
+ final String rawValue = readValueFromSystem( EnvironmentParameter.applicationParamFile, contextName );
|
|
|
|
+ if ( rawValue != null )
|
|
|
|
+ {
|
|
|
|
+ return parseApplicationParamValueParameter( rawValue );
|
|
}
|
|
}
|
|
return Collections.emptyMap();
|
|
return Collections.emptyMap();
|
|
}
|
|
}
|
|
|
|
|
|
- public static String readValueFromSystem(final PwmEnvironment.EnvironmentParameter parameter, final String contextName) {
|
|
|
|
- final List<String> namePossibilities = parameter.possibleNames(contextName);
|
|
|
|
|
|
+ public static String readValueFromSystem( final PwmEnvironment.EnvironmentParameter parameter, final String contextName )
|
|
|
|
+ {
|
|
|
|
+ final List<String> namePossibilities = parameter.possibleNames( contextName );
|
|
|
|
|
|
- for (final String propertyName : namePossibilities){
|
|
|
|
- final String propValue = System.getProperty(propertyName);
|
|
|
|
- if (propValue != null && !propValue.isEmpty()) {
|
|
|
|
|
|
+ for ( final String propertyName : namePossibilities )
|
|
|
|
+ {
|
|
|
|
+ final String propValue = System.getProperty( propertyName );
|
|
|
|
+ if ( propValue != null && !propValue.isEmpty() )
|
|
|
|
+ {
|
|
return propValue;
|
|
return propValue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- for (final String propertyName : namePossibilities){
|
|
|
|
- final String propValue = System.getenv(propertyName);
|
|
|
|
- if (propValue != null && !propValue.isEmpty()) {
|
|
|
|
|
|
+ for ( final String propertyName : namePossibilities )
|
|
|
|
+ {
|
|
|
|
+ final String propValue = System.getenv( propertyName );
|
|
|
|
+ if ( propValue != null && !propValue.isEmpty() )
|
|
|
|
+ {
|
|
return propValue;
|
|
return propValue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -302,67 +348,91 @@ public class PwmEnvironment {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
|
|
|
|
- public static Collection<ApplicationFlag> parseApplicationFlagValueParameter(final String input) {
|
|
|
|
- if (input == null) {
|
|
|
|
|
|
+ public static Collection<ApplicationFlag> parseApplicationFlagValueParameter( final String input )
|
|
|
|
+ {
|
|
|
|
+ if ( input == null )
|
|
|
|
+ {
|
|
return Collections.emptyList();
|
|
return Collections.emptyList();
|
|
}
|
|
}
|
|
|
|
|
|
- try {
|
|
|
|
- final List<String> jsonValues = JsonUtil.deserializeStringList(input);
|
|
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ final List<String> jsonValues = JsonUtil.deserializeStringList( input );
|
|
final List<ApplicationFlag> returnFlags = new ArrayList<>();
|
|
final List<ApplicationFlag> returnFlags = new ArrayList<>();
|
|
- for (final String value : jsonValues) {
|
|
|
|
- final ApplicationFlag flag = ApplicationFlag.forString(value);
|
|
|
|
- if (value != null) {
|
|
|
|
- returnFlags.add(flag);
|
|
|
|
- } else {
|
|
|
|
- LOGGER.warn("unknown " + EnvironmentParameter.applicationFlags.toString() + " value: " + input);
|
|
|
|
|
|
+ for ( final String value : jsonValues )
|
|
|
|
+ {
|
|
|
|
+ final ApplicationFlag flag = ApplicationFlag.forString( value );
|
|
|
|
+ if ( value != null )
|
|
|
|
+ {
|
|
|
|
+ returnFlags.add( flag );
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ LOGGER.warn( "unknown " + EnvironmentParameter.applicationFlags.toString() + " value: " + input );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return Collections.unmodifiableList(returnFlags);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
|
|
+ return Collections.unmodifiableList( returnFlags );
|
|
|
|
+ }
|
|
|
|
+ catch ( Exception e )
|
|
|
|
+ {
|
|
//
|
|
//
|
|
}
|
|
}
|
|
|
|
|
|
final List<ApplicationFlag> returnFlags = new ArrayList<>();
|
|
final List<ApplicationFlag> returnFlags = new ArrayList<>();
|
|
- for (final String value : input.split(",")) {
|
|
|
|
- final ApplicationFlag flag = ApplicationFlag.forString(value);
|
|
|
|
- if (value != null) {
|
|
|
|
- returnFlags.add(flag);
|
|
|
|
- } else {
|
|
|
|
- LOGGER.warn("unknown " + EnvironmentParameter.applicationFlags.toString() + " value: " + input);
|
|
|
|
|
|
+ for ( final String value : input.split( "," ) )
|
|
|
|
+ {
|
|
|
|
+ final ApplicationFlag flag = ApplicationFlag.forString( value );
|
|
|
|
+ if ( value != null )
|
|
|
|
+ {
|
|
|
|
+ returnFlags.add( flag );
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ LOGGER.warn( "unknown " + EnvironmentParameter.applicationFlags.toString() + " value: " + input );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return returnFlags;
|
|
return returnFlags;
|
|
}
|
|
}
|
|
|
|
|
|
- public static Map<ApplicationParameter,String> parseApplicationParamValueParameter(final String input) {
|
|
|
|
- if (input == null) {
|
|
|
|
|
|
+ public static Map<ApplicationParameter, String> parseApplicationParamValueParameter( final String input )
|
|
|
|
+ {
|
|
|
|
+ if ( input == null )
|
|
|
|
+ {
|
|
return Collections.emptyMap();
|
|
return Collections.emptyMap();
|
|
}
|
|
}
|
|
|
|
|
|
final Properties propValues = new Properties();
|
|
final Properties propValues = new Properties();
|
|
- try (FileInputStream fileInputStream = new FileInputStream(new File(input))) {
|
|
|
|
- propValues.load(fileInputStream);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- LOGGER.warn("error reading properties file '" + input + "' specified by environment setting " + EnvironmentParameter.applicationParamFile.toString() + ", error: " + e.getMessage());
|
|
|
|
|
|
+ try ( FileInputStream fileInputStream = new FileInputStream( new File( input ) ) )
|
|
|
|
+ {
|
|
|
|
+ propValues.load( fileInputStream );
|
|
|
|
+ }
|
|
|
|
+ catch ( Exception e )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.warn( "error reading properties file '" + input + "' specified by environment setting "
|
|
|
|
+ + EnvironmentParameter.applicationParamFile.toString() + ", error: " + e.getMessage() );
|
|
}
|
|
}
|
|
|
|
|
|
- if (propValues != null) {
|
|
|
|
- try {
|
|
|
|
- final Map<ApplicationParameter, String> returnParams = new HashMap<>();
|
|
|
|
- for (final Object key : propValues.keySet()) {
|
|
|
|
-
|
|
|
|
- final ApplicationParameter param = ApplicationParameter.forString(key.toString());
|
|
|
|
- if (param != null) {
|
|
|
|
- returnParams.put(param, propValues.getProperty(key.toString()));
|
|
|
|
- } else {
|
|
|
|
- LOGGER.warn("unknown " + EnvironmentParameter.applicationParamFile.toString() + " value: " + input);
|
|
|
|
- }
|
|
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ final Map<ApplicationParameter, String> returnParams = new HashMap<>();
|
|
|
|
+ for ( final Object key : propValues.keySet() )
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ final ApplicationParameter param = ApplicationParameter.forString( key.toString() );
|
|
|
|
+ if ( param != null )
|
|
|
|
+ {
|
|
|
|
+ returnParams.put( param, propValues.getProperty( key.toString() ) );
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ LOGGER.warn( "unknown " + EnvironmentParameter.applicationParamFile.toString() + " value: " + input );
|
|
}
|
|
}
|
|
- return Collections.unmodifiableMap(returnParams);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- LOGGER.warn("unable to parse jason value of " + EnvironmentParameter.applicationParamFile.toString() + ", error: " + e.getMessage());
|
|
|
|
}
|
|
}
|
|
|
|
+ return Collections.unmodifiableMap( returnParams );
|
|
|
|
+ }
|
|
|
|
+ catch ( Exception e )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.warn( "unable to parse jason value of " + EnvironmentParameter.applicationParamFile.toString() + ", error: " + e.getMessage() );
|
|
}
|
|
}
|
|
|
|
|
|
return Collections.emptyMap();
|
|
return Collections.emptyMap();
|
|
@@ -370,8 +440,8 @@ public class PwmEnvironment {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
- public static class Builder {
|
|
|
|
|
|
+ public static class Builder
|
|
|
|
+ {
|
|
private PwmApplicationMode applicationMode;
|
|
private PwmApplicationMode applicationMode;
|
|
private Configuration config;
|
|
private Configuration config;
|
|
private File applicationPath;
|
|
private File applicationPath;
|
|
@@ -379,71 +449,86 @@ public class PwmEnvironment {
|
|
private File configurationFile;
|
|
private File configurationFile;
|
|
private ContextManager contextManager;
|
|
private ContextManager contextManager;
|
|
private Collection<ApplicationFlag> flags = new HashSet<>();
|
|
private Collection<ApplicationFlag> flags = new HashSet<>();
|
|
- private Map<ApplicationParameter,String> params = new HashMap<>();
|
|
|
|
|
|
+ private Map<ApplicationParameter, String> params = new HashMap<>();
|
|
|
|
|
|
- public Builder(final PwmEnvironment pwmEnvironment) {
|
|
|
|
- this.applicationMode = pwmEnvironment.applicationMode;
|
|
|
|
- this.config = pwmEnvironment.config;
|
|
|
|
- this.applicationPath = pwmEnvironment.applicationPath;
|
|
|
|
|
|
+ public Builder( final PwmEnvironment pwmEnvironment )
|
|
|
|
+ {
|
|
|
|
+ this.applicationMode = pwmEnvironment.applicationMode;
|
|
|
|
+ this.config = pwmEnvironment.config;
|
|
|
|
+ this.applicationPath = pwmEnvironment.applicationPath;
|
|
this.internalRuntimeInstance = pwmEnvironment.internalRuntimeInstance;
|
|
this.internalRuntimeInstance = pwmEnvironment.internalRuntimeInstance;
|
|
- this.configurationFile = pwmEnvironment.configurationFile;
|
|
|
|
- this.contextManager = pwmEnvironment.contextManager;
|
|
|
|
- this.flags = pwmEnvironment.flags;
|
|
|
|
- this.params = pwmEnvironment.parameters;
|
|
|
|
|
|
+ this.configurationFile = pwmEnvironment.configurationFile;
|
|
|
|
+ this.contextManager = pwmEnvironment.contextManager;
|
|
|
|
+ this.flags = pwmEnvironment.flags;
|
|
|
|
+ this.params = pwmEnvironment.parameters;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder(final Configuration config, final File applicationPath) {
|
|
|
|
|
|
+ public Builder( final Configuration config, final File applicationPath )
|
|
|
|
+ {
|
|
this.config = config;
|
|
this.config = config;
|
|
this.applicationPath = applicationPath;
|
|
this.applicationPath = applicationPath;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder setApplicationMode(final PwmApplicationMode applicationMode) {
|
|
|
|
- if (PwmConstants.TRIAL_MODE && applicationMode == PwmApplicationMode.RUNNING) {
|
|
|
|
- LOGGER.info("application is in trial mode");
|
|
|
|
|
|
+ public Builder setApplicationMode( final PwmApplicationMode applicationMode )
|
|
|
|
+ {
|
|
|
|
+ if ( PwmConstants.TRIAL_MODE && applicationMode == PwmApplicationMode.RUNNING )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.info( "application is in trial mode" );
|
|
this.applicationMode = PwmApplicationMode.CONFIGURATION;
|
|
this.applicationMode = PwmApplicationMode.CONFIGURATION;
|
|
- } else {
|
|
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
this.applicationMode = applicationMode;
|
|
this.applicationMode = applicationMode;
|
|
}
|
|
}
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder setInternalRuntimeInstance(final boolean internalRuntimeInstance) {
|
|
|
|
|
|
+ public Builder setInternalRuntimeInstance( final boolean internalRuntimeInstance )
|
|
|
|
+ {
|
|
this.internalRuntimeInstance = internalRuntimeInstance;
|
|
this.internalRuntimeInstance = internalRuntimeInstance;
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder setConfigurationFile(final File configurationFile) {
|
|
|
|
|
|
+ public Builder setConfigurationFile( final File configurationFile )
|
|
|
|
+ {
|
|
this.configurationFile = configurationFile;
|
|
this.configurationFile = configurationFile;
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder setContextManager(final ContextManager contextManager) {
|
|
|
|
|
|
+ public Builder setContextManager( final ContextManager contextManager )
|
|
|
|
+ {
|
|
this.contextManager = contextManager;
|
|
this.contextManager = contextManager;
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder setFlags(final Collection<ApplicationFlag> flags) {
|
|
|
|
|
|
+ public Builder setFlags( final Collection<ApplicationFlag> flags )
|
|
|
|
+ {
|
|
this.flags.clear();
|
|
this.flags.clear();
|
|
- if (flags != null) {
|
|
|
|
- this.flags.addAll(flags);
|
|
|
|
|
|
+ if ( flags != null )
|
|
|
|
+ {
|
|
|
|
+ this.flags.addAll( flags );
|
|
}
|
|
}
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder setParams(final Map<ApplicationParameter,String> params) {
|
|
|
|
|
|
+ public Builder setParams( final Map<ApplicationParameter, String> params )
|
|
|
|
+ {
|
|
this.params.clear();
|
|
this.params.clear();
|
|
- if (params != null) {
|
|
|
|
- this.params.putAll(params);
|
|
|
|
|
|
+ if ( params != null )
|
|
|
|
+ {
|
|
|
|
+ this.params.putAll( params );
|
|
}
|
|
}
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public Builder setConfig(final Configuration config) {
|
|
|
|
|
|
+ public Builder setConfig( final Configuration config )
|
|
|
|
+ {
|
|
this.config = config;
|
|
this.config = config;
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
- public PwmEnvironment createPwmEnvironment() {
|
|
|
|
|
|
+ public PwmEnvironment createPwmEnvironment( )
|
|
|
|
+ {
|
|
return new PwmEnvironment(
|
|
return new PwmEnvironment(
|
|
applicationMode,
|
|
applicationMode,
|
|
config,
|
|
config,
|
|
@@ -457,104 +542,131 @@ public class PwmEnvironment {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- public void attemptFileLock() {
|
|
|
|
|
|
+ public void attemptFileLock( )
|
|
|
|
+ {
|
|
fileLocker.attemptFileLock();
|
|
fileLocker.attemptFileLock();
|
|
}
|
|
}
|
|
|
|
|
|
- public void releaseFileLock()
|
|
|
|
|
|
+ public void releaseFileLock( )
|
|
{
|
|
{
|
|
fileLocker.releaseFileLock();
|
|
fileLocker.releaseFileLock();
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean isFileLocked() {
|
|
|
|
|
|
+ public boolean isFileLocked( )
|
|
|
|
+ {
|
|
return fileLocker.isLocked();
|
|
return fileLocker.isLocked();
|
|
}
|
|
}
|
|
|
|
|
|
- public void waitForFileLock() throws PwmUnrecoverableException {
|
|
|
|
- final int maxWaitSeconds = this.getFlags().contains(ApplicationFlag.CommandLineInstance)
|
|
|
|
|
|
+ public void waitForFileLock( ) throws PwmUnrecoverableException
|
|
|
|
+ {
|
|
|
|
+ final int maxWaitSeconds = this.getFlags().contains( ApplicationFlag.CommandLineInstance )
|
|
? 1
|
|
? 1
|
|
- : Integer.parseInt(getConfig().readAppProperty(AppProperty.APPLICATION_FILELOCK_WAIT_SECONDS));
|
|
|
|
|
|
+ : Integer.parseInt( getConfig().readAppProperty( AppProperty.APPLICATION_FILELOCK_WAIT_SECONDS ) );
|
|
final Instant startTime = Instant.now();
|
|
final Instant startTime = Instant.now();
|
|
- final int attemptInterval = 5021; //ms
|
|
|
|
|
|
+ final TimeDuration attemptInterval = new TimeDuration( 5021, TimeUnit.MILLISECONDS );
|
|
|
|
|
|
- while (!this.isFileLocked() && TimeDuration.fromCurrent(startTime).isShorterThan(maxWaitSeconds, TimeUnit.SECONDS)) {
|
|
|
|
|
|
+ while ( !this.isFileLocked() && TimeDuration.fromCurrent( startTime ).isShorterThan( maxWaitSeconds, TimeUnit.SECONDS ) )
|
|
|
|
+ {
|
|
attemptFileLock();
|
|
attemptFileLock();
|
|
|
|
|
|
- if (!isFileLocked()) {
|
|
|
|
- LOGGER.debug("can't establish application file lock after "
|
|
|
|
- + TimeDuration.fromCurrent(startTime).asCompactString()
|
|
|
|
- + ", will retry;");
|
|
|
|
- JavaHelper.pause(attemptInterval);
|
|
|
|
|
|
+ if ( !isFileLocked() )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.debug( "can't establish application file lock after "
|
|
|
|
+ + TimeDuration.fromCurrent( startTime ).asCompactString()
|
|
|
|
+ + ", will retry;" );
|
|
|
|
+ attemptInterval.pause();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if (!isFileLocked()) {
|
|
|
|
|
|
+ if ( !isFileLocked() )
|
|
|
|
+ {
|
|
final String errorMsg = "unable to obtain application path file lock";
|
|
final String errorMsg = "unable to obtain application path file lock";
|
|
- final ErrorInformation errorInformation = new ErrorInformation(PwmError.ERROR_STARTUP_ERROR,errorMsg);
|
|
|
|
- throw new PwmUnrecoverableException(errorInformation);
|
|
|
|
|
|
+ final ErrorInformation errorInformation = new ErrorInformation( PwmError.ERROR_STARTUP_ERROR, errorMsg );
|
|
|
|
+ throw new PwmUnrecoverableException( errorInformation );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private class FileLocker {
|
|
|
|
|
|
+ private class FileLocker
|
|
|
|
+ {
|
|
private FileLock lock;
|
|
private FileLock lock;
|
|
private final File lockfile;
|
|
private final File lockfile;
|
|
|
|
|
|
- FileLocker() {
|
|
|
|
- final String lockfileName = config.readAppProperty(AppProperty.APPLICATION_FILELOCK_FILENAME);
|
|
|
|
- lockfile = new File(getApplicationPath(), lockfileName);
|
|
|
|
|
|
+ FileLocker( )
|
|
|
|
+ {
|
|
|
|
+ final String lockfileName = config.readAppProperty( AppProperty.APPLICATION_FILELOCK_FILENAME );
|
|
|
|
+ lockfile = new File( getApplicationPath(), lockfileName );
|
|
}
|
|
}
|
|
|
|
|
|
- private boolean lockingAllowed() {
|
|
|
|
- return !isInternalRuntimeInstance() && !getFlags().contains(ApplicationFlag.NoFileLock);
|
|
|
|
|
|
+ private boolean lockingAllowed( )
|
|
|
|
+ {
|
|
|
|
+ return !isInternalRuntimeInstance() && !getFlags().contains( ApplicationFlag.NoFileLock );
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean isLocked() {
|
|
|
|
|
|
+ public boolean isLocked( )
|
|
|
|
+ {
|
|
return !lockingAllowed() || lock != null && lock.isValid();
|
|
return !lockingAllowed() || lock != null && lock.isValid();
|
|
}
|
|
}
|
|
|
|
|
|
- public void attemptFileLock() {
|
|
|
|
- if (lockingAllowed() && !isLocked()) {
|
|
|
|
- try {
|
|
|
|
- final RandomAccessFile file = new RandomAccessFile(lockfile, "rw");
|
|
|
|
|
|
+ public void attemptFileLock( )
|
|
|
|
+ {
|
|
|
|
+ if ( lockingAllowed() && !isLocked() )
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ final RandomAccessFile file = new RandomAccessFile( lockfile, "rw" );
|
|
final FileChannel f = file.getChannel();
|
|
final FileChannel f = file.getChannel();
|
|
lock = f.tryLock();
|
|
lock = f.tryLock();
|
|
- if (lock != null) {
|
|
|
|
- LOGGER.debug("obtained file lock on file " + lockfile.getAbsolutePath() + " lock is valid=" + lock.isValid());
|
|
|
|
- writeLockFileContents(file);
|
|
|
|
- } else {
|
|
|
|
- LOGGER.debug("unable to obtain file lock on file " + lockfile.getAbsolutePath());
|
|
|
|
|
|
+ if ( lock != null )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.debug( "obtained file lock on file " + lockfile.getAbsolutePath() + " lock is valid=" + lock.isValid() );
|
|
|
|
+ writeLockFileContents( file );
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ LOGGER.debug( "unable to obtain file lock on file " + lockfile.getAbsolutePath() );
|
|
}
|
|
}
|
|
- } catch (Exception e) {
|
|
|
|
- LOGGER.error("unable to obtain file lock on file " + lockfile.getAbsolutePath() + " due to error: " + e.getMessage());
|
|
|
|
|
|
+ }
|
|
|
|
+ catch ( Exception e )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.error( "unable to obtain file lock on file " + lockfile.getAbsolutePath() + " due to error: " + e.getMessage() );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- void writeLockFileContents(final RandomAccessFile file) {
|
|
|
|
- try {
|
|
|
|
|
|
+ void writeLockFileContents( final RandomAccessFile file )
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
final Properties props = new Properties();
|
|
final Properties props = new Properties();
|
|
- props.put("timestamp", JavaHelper.toIsoDate(new Date()));
|
|
|
|
- props.put("applicationPath",PwmEnvironment.this.getApplicationPath() == null ? "n/a" : PwmEnvironment.this.getApplicationPath().getAbsolutePath());
|
|
|
|
- props.put("configurationFile", PwmEnvironment.this.getConfigurationFile() == null ? "n/a" : PwmEnvironment.this.getConfigurationFile().getAbsolutePath());
|
|
|
|
|
|
+ props.put( "timestamp", JavaHelper.toIsoDate( new Date() ) );
|
|
|
|
+ props.put( "applicationPath", PwmEnvironment.this.getApplicationPath() == null ? "n/a" : PwmEnvironment.this.getApplicationPath().getAbsolutePath() );
|
|
|
|
+ props.put( "configurationFile", PwmEnvironment.this.getConfigurationFile() == null ? "n/a" : PwmEnvironment.this.getConfigurationFile().getAbsolutePath() );
|
|
final String comment = PwmConstants.PWM_APP_NAME + " file lock";
|
|
final String comment = PwmConstants.PWM_APP_NAME + " file lock";
|
|
final StringWriter stringWriter = new StringWriter();
|
|
final StringWriter stringWriter = new StringWriter();
|
|
- props.store(stringWriter, comment);
|
|
|
|
- file.write(stringWriter.getBuffer().toString().getBytes(PwmConstants.DEFAULT_CHARSET));
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- LOGGER.error("unable to write contents of application lock file: " + e.getMessage());
|
|
|
|
|
|
+ props.store( stringWriter, comment );
|
|
|
|
+ file.write( stringWriter.getBuffer().toString().getBytes( PwmConstants.DEFAULT_CHARSET ) );
|
|
|
|
+ }
|
|
|
|
+ catch ( IOException e )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.error( "unable to write contents of application lock file: " + e.getMessage() );
|
|
}
|
|
}
|
|
// do not close FileWriter, otherwise lock is released.
|
|
// do not close FileWriter, otherwise lock is released.
|
|
}
|
|
}
|
|
|
|
|
|
- public void releaseFileLock() {
|
|
|
|
- if (lock != null && lock.isValid()) {
|
|
|
|
- try {
|
|
|
|
|
|
+ public void releaseFileLock( )
|
|
|
|
+ {
|
|
|
|
+ if ( lock != null && lock.isValid() )
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
lock.release();
|
|
lock.release();
|
|
- } catch (IOException e) {
|
|
|
|
- LOGGER.error("error releasing file lock: " + e.getMessage());
|
|
|
|
|
|
+ }
|
|
|
|
+ catch ( IOException e )
|
|
|
|
+ {
|
|
|
|
+ LOGGER.error( "error releasing file lock: " + e.getMessage() );
|
|
}
|
|
}
|
|
|
|
|
|
- LOGGER.debug("released file lock on file " + lockfile.getAbsolutePath());
|
|
|
|
|
|
+ LOGGER.debug( "released file lock on file " + lockfile.getAbsolutePath() );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|