Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enabling context variable to define dhis2_home #6573

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@
import java.io.InputStream;
import java.io.OutputStream;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.annotation.PostConstruct;


import lombok.extern.slf4j.Slf4j;

import org.hisp.dhis.external.util.LogOnceLogger;
Expand All @@ -57,22 +61,26 @@ public class DefaultLocationManager extends LogOnceLogger
private static final String DEFAULT_ENV_VAR = "DHIS2_HOME";

private static final String DEFAULT_SYS_PROP = "dhis2.home";

private static final String DEFAULT_CTX_VAR = "dhis2-home";

private String externalDir;

private String environmentVariable;

private String systemProperty;

private String contextVariable;

public DefaultLocationManager( String environmentVariable, String systemProperty )
public DefaultLocationManager( String environmentVariable, String systemProperty, String contextVariable )
{
this.environmentVariable = environmentVariable;
this.systemProperty = systemProperty;
this.contextVariable = contextVariable;
}

public static DefaultLocationManager getDefault()
{
return new DefaultLocationManager( DEFAULT_ENV_VAR, DEFAULT_SYS_PROP );
return new DefaultLocationManager( DEFAULT_ENV_VAR, DEFAULT_SYS_PROP, DEFAULT_CTX_VAR );
}

// -------------------------------------------------------------------------
Expand All @@ -82,52 +90,92 @@ public static DefaultLocationManager getDefault()
@PostConstruct
public void init()
{
String path = System.getProperty( systemProperty );
// check the most specific to the least specific
// 1- context variable
externalDir = getPathFromContext();
//2- system property
if ( externalDir == null ) externalDir = getPathFromSysProperty();
//3- env variable
if ( externalDir == null ) externalDir = getPathFromEnvVariable();
//4- default value
if ( externalDir == null ) externalDir = getPathDefault();
}
// -------------------------------------------------------------------------
// LocationManager implementation
// -------------------------------------------------------------------------
private String getPathFromContext(){
String path = null;
try
{
Context initCtx = new InitialContext();
Context envCtx = ( Context ) initCtx.lookup( "java:comp/env" );
path = ( String ) envCtx.lookup( this.contextVariable );
}
catch ( NamingException e )
{
log( log, Level.INFO, "Context variable " + contextVariable + " not set" );
}
if ( path != null )
{
log( log, Level.INFO, "Context variable " + contextVariable + " points to " + path );

if ( directoryIsValid( new File( path ) ) )
{
return path;
}
}
return null;
}

private String getPathFromSysProperty(){
String path = System.getProperty( systemProperty );
if ( path != null )
{
log( log, Level.INFO, "System property " + systemProperty + " points to " + path );

if ( directoryIsValid( new File( path ) ) )
{
externalDir = path;
return path;
}
}
else
{
log( log, Level.INFO, "System property " + systemProperty + " not set" );
}
return null;
}

path = System.getenv( environmentVariable );

if ( path != null )
{
log( log, Level.INFO, "Environment variable " + environmentVariable + " points to " + path );
private String getPathFromEnvVariable(){
String path = System.getenv( environmentVariable );
if ( path != null )
{
log( log, Level.INFO, "Environment variable " + environmentVariable + " points to " + path );

if ( directoryIsValid( new File( path ) ) )
{
externalDir = path;
}
}
else
if ( directoryIsValid( new File( path ) ) )
{
log( log, Level.INFO, "Environment variable " + environmentVariable + " not set" );

path = DEFAULT_DHIS2_HOME;

if ( directoryIsValid( new File( path ) ) )
{
externalDir = path;
log( log, Level.INFO, "Home directory set to " + DEFAULT_DHIS2_HOME );

}
return path;
}
}
else
{
log( log, Level.INFO, "Environment variable " + environmentVariable + " not set" );
}
return null;
}

// -------------------------------------------------------------------------
// LocationManager implementation
// -------------------------------------------------------------------------

private String getPathDefault(){
String path = DEFAULT_DHIS2_HOME;
if ( directoryIsValid( new File( path ) ) )
{
log( log, Level.INFO, "Home directory set to " + DEFAULT_DHIS2_HOME );
return path;
}
else
{
log( log, Level.ERROR, "No Home directory set, and " + DEFAULT_DHIS2_HOME + " is not a directory" );
return null;
}
}
// -------------------------------------------------------------------------
// InputStream
// -------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public void testString_MissingQuotes()
JsonDocument doc = new JsonDocument( "{\"a\": hello }" );

JsonFormatException ex = assertThrows( JsonFormatException.class, () -> doc.get( ".a" ) );
assertEquals( "Unexpected character at position 6,\n" + "{\"a\": hello }\n" + " ^ expected start of value",
assertEquals( "Unexpected character at position 6," + System.getProperty("line.separator") + "{\"a\": hello }" + System.getProperty("line.separator") + " ^ expected start of value",
ex.getMessage() );
}

Expand Down