Tuesday, October 21, 2014

Reading .properties file on app server file system using Java in PeopleCode

Here is an example of how we can read .properties files on the app server file system easily using Java in PeopleCode.

Why: There might be some useful information in the .properties files on the app server that might not be accessible to developers directly in PeopleCode. Some examples: OS Type and Version Details in peopletools.properties.

In this example, let us assume we want to access the OS Type and Version properties that is set on the peopletools.properties file (located either in PS_CFG_HOME or PS_HOME).

Here is an example of the contents in the peopletools.properties file:

#Peopletools Install Settings
#Fri Oct 17 10:19:17 EDT 2014
installlocation=<PS_HOME>
dbcodessuffix=PT
unicodedb=0
licensegroupname=PeopleTools
hstplt=oel-5-x86_64
psplatformregname=ORACLE
psserver=App
psdbbin=
psenv=App
dbtypedescr=Oracle
dbtype=ORA
licensegroup=06
tuxedodir=
productversion=8.52.07
psplatform=Linux

Sample PeopleCode:

/* Get the file (peopletools.properties) */
Local JavaObject &fis = CreateJavaObject("java.io.FileInputStream", GetEnv("PS_CFG_HOME") | "/peopletools.properties");
/* Note: If PS_CFG_HOME is not configured in your environment, then try PS_HOME. */

/* Create Properties JavaObject */
Local JavaObject &props = CreateJavaObject("java.util.Properties");


/* Load file to Properties Class */
&props.load(&fis);

/* Use Reflection to call the appropriate getProperty (overloaded) method of the Properties Class */

/******* REFLECTION ********/
/* Get a reference to a Java class instance for the String Class */
Local JavaObject &stringClass = GetJavaClass("java.lang.String");

/* Get a reference to the method we want to call */
Local JavaObject &methodArgTypes = CreateJavaObject("java.lang.Class[]", &stringClass);
Local JavaObject &methodReference = &props.getClass().getMethod("getProperty", &methodArgTypes);

/* Call the method */
Local JavaObject &psplatform = &methodReference.invoke(&props, CreateJavaObject("java.lang.Object[]", "psplatform"));
Local JavaObject &hstplt = &methodReference.invoke(&props, CreateJavaObject("java.lang.Object[]", "hstplt"));
/******* REFLECTION ********/

/* psplatform returns OS Type */
MessageBox(0, "", 0, 0, "psplatform: " | &psplatform.toString());
/* hstplt returns OS Details (including version) */
MessageBox(0, "", 0, 0, "hstplt: " | &hstplt.toString());

&fis.close();

Granted that the same can also be achieved without using Java and parsing through the .properties file using PeopleCode File Object. This post is just to show an alternative approach which might be applicable in certain scenarios.

This approach could be easily extended to any .properties file on the application server or process scheduler server.

No comments:

Post a Comment