Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
If one wanted to list all the defined context keys, can this be accomplished in Java code?
I attempted using context.keySet; however, only one key was listed.
Yes, you can do this. You can even return all of the values in their String form (if you are not sure of the actual type and don't want to write code to identify the type). The code below demonstrates how to do this. Just add this to a tJava component and you will see.
java.util.Iterator<String> it = context.stringPropertyNames().iterator();
while(it.hasNext()){
String key = it.next();
System.out.println(key);
System.out.println(context.getProperty(key));
}
Yes, you can do this. You can even return all of the values in their String form (if you are not sure of the actual type and don't want to write code to identify the type). The code below demonstrates how to do this. Just add this to a tJava component and you will see.
java.util.Iterator<String> it = context.stringPropertyNames().iterator();
while(it.hasNext()){
String key = it.next();
System.out.println(key);
System.out.println(context.getProperty(key));
}
How, after setting context value of variable, would it be seen using "get" method?"
e.g.
context.myVar is set to null in job.
context.myVar is set to 42 in tPreJob code.
String key does find myVar and context.getProperty("myVar") returns null. However, context.myVar returns 42.
How to print all the runtime values of context variables without manually coding for every context variable name?
This sounds like a timing issue. If you context variables start their life as null, then this code will only work after they have been set. I presume that they are being set using your tPreJob. As an example, I just put this job together to prove this....
I set the values using the tFixedFlowInput and the tContextLoad and then ran the code in the tJava after that had run.
Hello,
Without knowing why you want to do this it's hard to help. But for hacking contexts one can use java reflection.
Here's a routine I created for this:
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
public class modifyContext {
public static void test(Object context) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Field[] allFields = context.getClass().getDeclaredFields();
for(Field field : allFields) {
field.setAccessible(true);
String fieldName = field.getName();
if( field.getType().equals(String.class) ) {
String value = (String)field.get(context);
System.err.println("Hello I am: " + fieldName + " and my value was " + value + " but lets add HACKED to it!");
value = value + " Hacked!";
field.set(context, value);
}
}
Method[] methods = context.getClass().getDeclaredMethods();
for(Method method : methods ) {
if(method.getName().equals("synchronizeContext")) {
System.out.println("We have method: " + method.getName() + " lets call it");
method.invoke(context);
}
}
}
public static void replaceThings(Object context) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Field[] allFields = context.getClass().getDeclaredFields();
HashMap<String,String> fieldValues = new HashMap<>();
for(Field field : allFields) {
field.setAccessible(true);
String fieldName = field.getName();
if( field.getType().equals(String.class) ) {
fieldValues.put(fieldName, (String)field.get(context));
}
}
System.err.println(fieldValues.toString());
for(Field field : allFields) {
String fieldName = field.getName();
if( field.getType().equals(String.class) ) {
if(field.get(context) == null) continue;
String value = (String)field.get(context);
if(value.length() > 3 && value.length() != value.replaceAll("\\$[A-Z_]+\\$", "").length()){
System.err.println("Looking for patterns for " + fieldName);
for(String name : fieldValues.keySet() ) {
if(value.length() != value.replace("$"+name+"$", fieldValues.get(name)).length()) {
System.err.println("Found pattern ("+ name +") in " + fieldName + " replacing with " + fieldValues.get(name));
value = value.replace("$"+name+"$", fieldValues.get(name));
}
}
field.set(context, value);
}
}
}
Method[] methods = context.getClass().getDeclaredMethods();
for(Method method : methods ) {
if(method.getName().equals("synchronizeContext")) {
System.out.println("We have method: " + method.getName() + " lets call it");
method.invoke(context);
}
}
//return context;
}
}