Skip to content

Commit 46dfd40

Browse files
committed
Add DynamicCall EO to GamUtils
(cherry picked from commit c1be143)
1 parent 17778ce commit 46dfd40

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

gamutils/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
<artifactId>commons-io</artifactId>
4242
<version>2.11.0</version>
4343
</dependency>
44+
<dependency>
45+
<groupId>${project.groupId}</groupId>
46+
<artifactId>gxcommon</artifactId>
47+
<version>${project.version}</version>
48+
</dependency>
4449
<dependency>
4550
<groupId>commons-codec</groupId>
4651
<artifactId>commons-codec</artifactId>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.genexus.gam.utils;
2+
3+
import com.genexus.ModelContext;
4+
import org.apache.logging.log4j.LogManager;
5+
import org.apache.logging.log4j.Logger;
6+
7+
import java.lang.reflect.Constructor;
8+
import java.lang.reflect.InvocationTargetException;
9+
10+
@SuppressWarnings("unused")
11+
public class DynamicCall {
12+
private final ModelContext mContext;
13+
private final Integer mRemoteHandle;
14+
private static final Logger logger = LogManager.getLogger(DynamicCall.class);
15+
16+
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
17+
18+
@SuppressWarnings("unused")
19+
public DynamicCall(ModelContext context, Integer remoteHandle) {
20+
mContext = context;
21+
mRemoteHandle = remoteHandle;
22+
}
23+
24+
@SuppressWarnings("unused")
25+
public boolean execute(String assembly, String typeName, boolean useContext, String method, String jsonParms, String[] jsonOutput) {
26+
logger.debug("execute");
27+
return doCall(assembly, typeName, useContext, method, false, "", jsonParms, jsonOutput);
28+
}
29+
30+
@SuppressWarnings("unused")
31+
public boolean executeEventHandler(String assembly, String typeName, boolean useContext, String method, String eventType, String jsonInput, String[] jsonOutput) {
32+
logger.debug("executeEventHandler");
33+
return doCall(assembly, typeName, useContext, method, true, eventType, jsonInput, jsonOutput);
34+
}
35+
36+
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/
37+
38+
39+
private boolean doCall(String assembly, String typeName, boolean useContext, String method, boolean isEventHandler, String parm1, String parm2, String[] jsonOutput) {
40+
logger.debug("doCall");
41+
Object[] parms;
42+
Class<?>[] parmTypes;
43+
if (isEventHandler) {
44+
parms = new Object[]{parm1, parm2, new String[]{jsonOutput[0]}};
45+
parmTypes = new Class[]{String.class, String.class, String[].class};
46+
} else {
47+
parms = new Object[]{parm2, new String[]{jsonOutput[0]}};
48+
parmTypes = new Class[]{String.class, String[].class};
49+
}
50+
51+
try {
52+
Class<?> myClass = Class.forName(typeName);
53+
Class<?>[] constructorParms;
54+
Constructor<?> constructor;
55+
Object instance = null;
56+
57+
if (useContext && (mContext != null)) {
58+
try {
59+
constructorParms = new Class[]{int.class, ModelContext.class};
60+
constructor = myClass.getConstructor(constructorParms);
61+
instance = constructor.newInstance(new Object[]{mRemoteHandle, mContext});
62+
} catch (NoSuchMethodException e) {
63+
logger.error("doCall", e);
64+
}
65+
}
66+
67+
if (instance == null) {
68+
constructorParms = new Class[]{int.class};
69+
constructor = myClass.getConstructor(constructorParms);
70+
instance = constructor.newInstance(new Object[]{-2});
71+
}
72+
73+
myClass.getMethod(method, parmTypes).invoke(instance, parms);
74+
} catch (ClassNotFoundException e) {
75+
logger.error("doCall", e);
76+
jsonOutput[0] = "{\"error\":\"" + " class " + typeName + " not found" + "\"}";
77+
return false;
78+
} catch (NoSuchMethodException e) {
79+
logger.error("doCall", e);
80+
jsonOutput[0] = "{\"error\":\"" + " method " + method + " not found" + "\"}";
81+
return false;
82+
} catch (InstantiationException e) {
83+
logger.error("doCall", e);
84+
jsonOutput[0] = "{\"error\":\"" + " cannot instantiate type " + typeName + "\"}";
85+
return false;
86+
} catch (IllegalAccessException e) {
87+
logger.error("doCall", e);
88+
jsonOutput[0] = "{\"error\":\"" + " cannot access method " + method + "\"}";
89+
return false;
90+
} catch (InvocationTargetException e) {
91+
logger.error("doCall", e);
92+
jsonOutput[0] = "{\"error\":\"" + " InvocationTargetException in class " + typeName + "\"}";
93+
return false;
94+
}
95+
String[] result = (String[]) parms[parms.length - 1];
96+
jsonOutput[0] = result[0];
97+
logger.debug("doCall result {}", result[0]);
98+
return true;
99+
}
100+
}

0 commit comments

Comments
 (0)