How To Map Password Type Global Variable In Activity Input

As you may already know, you cannot map a global variable that is a password type to the Activity Input. You can only map it to a Shared Configuration resource when possible. One way to retrieve the global variable password value is to write a Java code. But there is another preferable way to have one less activity in your process definition, and that is using a Java Custom Function. Of course, you will still write Java code one time, but it's a whole lot reusable; and you can export it as a Project Library that you can use in a different Designer project. So you want to write a Java code, but you are not quite sure which JAR file the Java class is located; on the other hand, you know the fully qualified name of the class and the method name to invoke. Personally, I find it appropriate to use The Reflection API to write a few lines of code in order to achieve this.

We want to have a function that retrieves the value of a password global variable passing the global variable path as an argument. The path means something like a Unix file path. You will have to write a Java class such as below. The getProperty is a static method, so you can pass null in lieu of an object as an argument. As you can see, it's very simple and straightforward, but through this approach you can compile this Java class without including any JAR file to the class path.

public class PasswordDecryptor {

    public static String getPasswordGlobalVariable(String path) {
        String result = null;
        try {
            Class<?> c = Class.forName("com.tibco.pe.plugin.PluginProperties");
            Method m = c.getMethod("getProperty", String.class);
            result = (String) m.invoke(null, "tibco.clientVar."+path);
        } catch(Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}



Comments

  1. This is indeed a scenario that comes up quite a bit. We've done this same kind of thing to solve the problem, but we use the Java Method activity instead. This requires no code; just supply the class name ('com.tibco.pe.plugin.PluginProperties') in the Class field and select the proper method signature ('getProperty(java.lang.String)') in the Method dropdown. Then you just have to go to the input tab and provide the value to pass to the method, which in this case is the property path (ie- 'tibco.clientVar.HTTPConnection/Password', as in your example. We've agreed as an enterprise that we prefer using the Java Method activity over straight Java code for a few reasons. One, it provides a layer of abstraction for non-Java BW developers. Also, it doesn't require any compilation, so you don't have to recompile anything when upgrading between BW versions that utilize different versions of Java (BW 5.9 uses JDK 1.6, whereas the latest BW 5.12 uses JDK 1.7; this means you have to recompile custom Java code activities prior to upgrading).

    ReplyDelete
  2. Hey Levi, I believe the JavaCustomFunction uses a Java class file that you don't need to recompile when upgrading to a higher JDK version as long as you set the minimum JDK compability to 1.6 before compiling the code; that means it should work for both JDK 1.6 and 1.7. Besides, the JavaCustomFunction exposes an XPath function which can be used directly in the XSLT mapping. It is a bit different from JavaCode where you write and compile the code in Designer.

    ReplyDelete
  3. Hello Ralph, thanks for sharing this awesome trick. However, I had to import java.lang.reflect.* in order to get the Method object working (this note is mainly for other users who would like to use this custom java function in case they run into some compile time issues)

    ReplyDelete
  4. It is amazing to visit your site. Thanks for sharing this information, this is useful to me...
    Mule ESB Training
    Mule 4 Training

    ReplyDelete

Post a Comment

Popular posts from this blog

XML Schema and JSON Schema Validation in Mule 4

Using XML To Java in TIBCO BW

Parsing a JSON String and Converting it to XML in TIBCO