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

GamCryptography EO migration to GamUtils EO on Github #885

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add DynamicCall EO to GamUtils
sgrampone committed Aug 19, 2024
commit c1be143e4f10576b6eddb5fce092fd4aa6020e23
5 changes: 5 additions & 0 deletions gamutils/pom.xml
Original file line number Diff line number Diff line change
@@ -41,6 +41,11 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>gxcommon</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
100 changes: 100 additions & 0 deletions gamutils/src/main/java/com/genexus/gam/utils/DynamicCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.genexus.gam.utils;

import com.genexus.ModelContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

@SuppressWarnings("unused")
public class DynamicCall {
private final ModelContext mContext;
private final Integer mRemoteHandle;
private static final Logger logger = LogManager.getLogger(DynamicCall.class);

/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/

@SuppressWarnings("unused")
public DynamicCall(ModelContext context, Integer remoteHandle) {
mContext = context;
mRemoteHandle = remoteHandle;
}

@SuppressWarnings("unused")
public boolean execute(String assembly, String typeName, boolean useContext, String method, String jsonParms, String[] jsonOutput) {
logger.debug("execute");
return doCall(assembly, typeName, useContext, method, false, "", jsonParms, jsonOutput);
}

@SuppressWarnings("unused")
public boolean executeEventHandler(String assembly, String typeName, boolean useContext, String method, String eventType, String jsonInput, String[] jsonOutput) {
logger.debug("executeEventHandler");
return doCall(assembly, typeName, useContext, method, true, eventType, jsonInput, jsonOutput);
}

/********EXTERNAL OBJECT PUBLIC METHODS - END ********/


private boolean doCall(String assembly, String typeName, boolean useContext, String method, boolean isEventHandler, String parm1, String parm2, String[] jsonOutput) {
logger.debug("doCall");
Object[] parms;
Class<?>[] parmTypes;
if (isEventHandler) {
parms = new Object[]{parm1, parm2, new String[]{jsonOutput[0]}};
parmTypes = new Class[]{String.class, String.class, String[].class};
} else {
parms = new Object[]{parm2, new String[]{jsonOutput[0]}};
parmTypes = new Class[]{String.class, String[].class};
}

try {
Class<?> myClass = Class.forName(typeName);
Class<?>[] constructorParms;
Constructor<?> constructor;
Object instance = null;

if (useContext && (mContext != null)) {
try {
constructorParms = new Class[]{int.class, ModelContext.class};
constructor = myClass.getConstructor(constructorParms);
instance = constructor.newInstance(new Object[]{mRemoteHandle, mContext});
} catch (NoSuchMethodException e) {
logger.error("doCall", e);
}
}

if (instance == null) {
constructorParms = new Class[]{int.class};
constructor = myClass.getConstructor(constructorParms);
instance = constructor.newInstance(new Object[]{-2});
}

myClass.getMethod(method, parmTypes).invoke(instance, parms);
} catch (ClassNotFoundException e) {
logger.error("doCall", e);
jsonOutput[0] = "{\"error\":\"" + " class " + typeName + " not found" + "\"}";
return false;
} catch (NoSuchMethodException e) {
logger.error("doCall", e);
jsonOutput[0] = "{\"error\":\"" + " method " + method + " not found" + "\"}";
return false;
} catch (InstantiationException e) {
logger.error("doCall", e);
jsonOutput[0] = "{\"error\":\"" + " cannot instantiate type " + typeName + "\"}";
return false;
} catch (IllegalAccessException e) {
logger.error("doCall", e);
jsonOutput[0] = "{\"error\":\"" + " cannot access method " + method + "\"}";
return false;
} catch (InvocationTargetException e) {
logger.error("doCall", e);
jsonOutput[0] = "{\"error\":\"" + " InvocationTargetException in class " + typeName + "\"}";
return false;
}
String[] result = (String[]) parms[parms.length - 1];
jsonOutput[0] = result[0];
logger.debug("doCall result {}", result[0]);
return true;
}
}