Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
PhilHibbs
Creator II
Creator II

Loading context variables that exist only

We use a framework that reads context variables from a database table at the beginning of a job. The framework also prints out the context variables, but masks any defined as "Password".

 

This is fine, as long as every job contains all of the context variables. However, if a job reads a context set but does not have a particular context variable defined, it gets printed out in plain text.

 

For instance, lets say I have several jobs that all use the same context set. I add "SFTP_Password" to one of them, define it as a password, and add a value to the database. Now, when I run any of the other jobs that have not had "SFTP_Password" added to them (because they do not need the SFTP password), the password is printed out in plain text in the job log.

 

The framework runs by feeding the values in from a tMySqlInput component to a tContextLoad, and then a tContextDump prints them out. I would have thought that the extraneous values would be ignored by the tContextLoad, because there is nowhere in the context object to put them, but that is not the case. The context is expanded to hold all the values, and they then get printed out later on.

 

Is there any way to prevent the nonexistent context variables from being added to the context?

Labels (2)
1 Solution

Accepted Solutions
billimmer
Creator III
Creator III

For instance, if you had a context variable in your job called "dir_bulk", then in a tJava you could:

 

String myVar = "dir_bulk";

java.lang.reflect.Field field = context.getClass().getField(myVar);
System.out.println(field.getName());
System.out.println(String.valueOf(field.get(context)));

 

This would print the variable name and the default value. 

 

EDIT:  You can also set your context values using:

 

field.set(context, "some new value");

 

Then you could set your values without using the tContext component (if you wanted).  

View solution in original post

4 Replies
billimmer
Creator III
Creator III

Maybe you could use a tjava row to filter before sending to tContextLoad?  You could try to read the default value for each variable from your job's context in a try{} block, and if the variable doesn't exist you can skip it.

PhilHibbs
Creator II
Creator II
Author

How would I read the default value?

billimmer
Creator III
Creator III

For instance, if you had a context variable in your job called "dir_bulk", then in a tJava you could:

 

String myVar = "dir_bulk";

java.lang.reflect.Field field = context.getClass().getField(myVar);
System.out.println(field.getName());
System.out.println(String.valueOf(field.get(context)));

 

This would print the variable name and the default value. 

 

EDIT:  You can also set your context values using:

 

field.set(context, "some new value");

 

Then you could set your values without using the tContext component (if you wanted).  

PhilHibbs
Creator II
Creator II
Author

Brilliant thanks! Here's the code I added to a tJavaRow:

 

output_row.found = true;
try {
  String myVar = input_row.key;
  java.lang.reflect.Field field = context.getClass().getField(myVar);
} catch(java.lang.NoSuchFieldException e) {
  output_row.found = false;
}

That is followed by a tFilterRow with advanced condition of just "row2.found".