Skip to content
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
5 changes: 3 additions & 2 deletions common/src/main/java/com/genexus/ModelContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public ModelContext(Class packageClass)
}

SpecificImplementation.Application.setContextClassName(this.packageClass);

if (threadModelContext.get() == null)
threadModelContext.set(this);
try
{
this.staticContentBase = getClientPreferences().getWEB_IMAGE_DIR();
Expand All @@ -104,8 +107,6 @@ public ModelContext(Class packageClass)
}
if (httpContext != null)
httpContext.setStaticContentBase(staticContentBase);
if (threadModelContext.get() == null)
threadModelContext.set(this);
}

public ModelContext(ModelContext modelContext)
Expand Down
21 changes: 19 additions & 2 deletions common/src/main/java/com/genexus/util/EnvVarReader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.genexus.util;

import com.genexus.ModelContext;

public class EnvVarReader {

static String[] m_invalidChars = { ".", "|" };
Expand All @@ -12,15 +14,30 @@ public static String getEnvironmentVar(String section, String key, String mapped
}else{
String prefix = ENVVAR_PREFIX;
if (section != null && !section.isEmpty() && section != "Client") {
for (int i = 0; i < m_invalidChars.length; i++)
section = section.replace(m_invalidChars[i], "_");
section = replaceInvalidChars(section);
String realKey = key;
key = String.format("%s%s_%s", prefix, section.toUpperCase(), key.toUpperCase());
String envVar = System.getenv(key);
if (envVar != null)
return envVar;
if (ModelContext.getModelContext() != null) {
section = section.replace(replaceInvalidChars(ModelContext.getModelContext().getPackageName() + "|").toUpperCase(), "");
key = String.format("%s%s_%s", prefix, section.toUpperCase(), realKey.toUpperCase());
}
else
return null;
} else
key = String.format("%s%s", prefix, key.toUpperCase());
return System.getenv(key);
}
}

private static String replaceInvalidChars(String section) {
for (int i = 0; i < m_invalidChars.length; i++)
section = section.replace(m_invalidChars[i], "_");
return section;
}

public static String getEnvironmentValue(String serviceType, String serviceName, String propertyName) {
String envVarName = String.format("%s%s_%s", ENVVAR_PREFIX, serviceType.toUpperCase(), propertyName.toUpperCase());
String value = System.getenv(envVarName);
Expand Down
18 changes: 18 additions & 0 deletions gxawsserverless/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gxcommon</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gxwrapperjavax</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gxwrappercommon</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-jersey</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.genexus.cloud.serverless.aws.LambdaHandler;
import com.genexus.specific.java.Connect;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Before;
import org.junit.Ignore;
Expand All @@ -45,6 +46,8 @@ public class GeneXusAppAwsProxyTest {

@Before
public void setUpStreams() {
Connect.init();

try {
System.setProperty("LAMBDA_TASK_ROOT", ".");
l = new LambdaHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.FileInputStream;
import java.io.IOException;

import com.genexus.specific.java.Connect;
import com.genexus.webpanels.GXObjectUploadServices;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.Test;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class JerseyAwsProxyTest {

@Before
public void setUpStreams() {
Connect.init();

try {
System.setProperty("LAMBDA_TASK_ROOT", ".");
l = new LambdaHandler();
Expand Down
75 changes: 75 additions & 0 deletions java/src/test/java/com/genexus/TestGxReadEnvVar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.genexus;


import com.genexus.specific.java.Connect;
import com.genexus.util.EnvVarReader;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class TestGxReadEnvVar {

private static final String GX_PROP = "DB_URL";
private static final String GX_DATASTORE = "COM.MOCKDB|DEFAULT";
private static final String FIRST_ENV_VAR = "GX_DEFAULT_DB_URL";
private static final String SECOND_ENV_VAR = "GX_COM_MOCKDB_DEFAULT_DB_URL";
private static final String FIRST_VALUE = "FirstDB_URL";
private static final String SECOND_VALUE = "SecondDB_URL";

@Test
public void testGxReadEnvVar()
{
Connect.init();
ModelContext modelContext = ModelContext.getModelContext(com.mockdb.GXcfg.class);
try {
Map<String, String> newenv = new HashMap<>();
newenv.put(FIRST_ENV_VAR, FIRST_VALUE);
setEnvVar(newenv);
String envVarValue = EnvVarReader.getEnvironmentVar(GX_DATASTORE, GX_PROP, null);
Assert.assertEquals(FIRST_VALUE, envVarValue);

newenv.put(SECOND_ENV_VAR, SECOND_VALUE);
setEnvVar(newenv);
envVarValue = EnvVarReader.getEnvironmentVar(GX_DATASTORE, GX_PROP, null);
Assert.assertEquals(SECOND_VALUE, envVarValue);
newenv.put(FIRST_ENV_VAR, null);
setEnvVar(newenv);
newenv.put(SECOND_ENV_VAR, null);
setEnvVar(newenv);
} catch (Exception e) {
e.printStackTrace();
}
}

private void setEnvVar(Map<String, String> newenv) throws Exception{
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newenv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newenv);
} catch (NoSuchFieldException e)
{
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for (Class cl : classes) {
if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.putAll(newenv);
}
}
}
}
}