iSeries EGL

All things EGL for the iSeries / i5 / Power System

Archive for the ‘EGL Service Programming’ Category

How to Read the Environment Variable Assigned to Tomcat

leave a comment »

Introduction

This post illustrates how to read the environment variable (EV) assigned to Tomcat’s JVM at the time Tomcat is started. The variable was assigned within Tomcat’s startup script to indicate Tomcat’s role as being a development server, a test server or a production server. The values were “localhost”, “TEST” or “PROD” respectively. To learn how the EV is assigned to Tomcat’s JVM select this link.

Approach

EGL does not support access to JVM variables as Java does. To solve the problem of reading the value a Java program must be employed to be called from EGL. Shown below is the Java program that reads the variable to be returned to the EGL program. Specifically the line of interest is this one:
runtimeEnvironment = System.getProperty(“runtimeEnvironment”).trim() ;


package com.app.runtime;

public class RuntimeEnvironment {


	private String runtimeEnvironment = null;
	private String clazz = null;
	
	
	/**
	 * Constructor
	 */
	public RuntimeEnvironment() {
	
	}
	
	
	/**
	 * 
	 */
	public String getRuntimeEnvironment() {
		

        //Get the Server property that holds the environment value.  This value will be
        //"localhost" for the developer's PC, "TEST" for the test server, or "PROD" for 
        //the production server.  This value is instrumental to acquiring the correct 
        //property values for the machine/environment in which this application is running.
		try {
			runtimeEnvironment = System.getProperty("runtimeEnvironment").trim() ;
		} catch (Exception ex) {
			System.out.println(" StartupTasks.init()  Server instance not found. " + ex.getMessage() );
		}
		
		if ((runtimeEnvironment == null)) {
			String msg = "Server not configured with runtime environment value. Value = "
					+ runtimeEnvironment
					+ ". (Valid config value: -DruntimeEnvironment='localhost' or TEST or PROD)";
			System.out.println(msg);
			runtimeEnvironment = "localhost";
			System.out.println(" RuntimeEnvironment.getRuntimeEnvironment()  Default environment will be " + runtimeEnvironment  );
		}
		
		
		if (runtimeEnvironment.equals("localhost")
				|| runtimeEnvironment.equals("TEST")
				|| runtimeEnvironment.equals("PROD")) {
		} else {
			String msg = 
					"RuntimeEnvironment.getRuntimeEnvironment(): The server is not configured with correct runtime envrionment value. Value = "
					+ runtimeEnvironment;
			System.out.println(msg);
			msg = "StartupServlet.init(): Examples of valid config values are: 'localhost', 'TEST' or 'PROD'.";
			System.out.println(msg);

		}
		
		System.out.println(clazz+ " **********************************************************************");
		System.out.println(clazz+ " LoggingViewer application startup phase");
		System.out.println(clazz+ "   Reading Server Argument for run-time environment parameter.");
		System.out.println(clazz + "     Server Run-Time Environment  : " + runtimeEnvironment);
		System.out.println(clazz + " ");
		System.out.println("LoggingViewer Runtime Environment is '" + runtimeEnvironment + "'.");
	
		return runtimeEnvironment;
	}
	
}

Fig.1 – The Java class ‘RuntimeEnvironment’

 

The strategy employed by RuntimeEnvironment is straight forward; the method getRuntimeEnvironment() reads the variable runtimeEnvironment the value of which has been assigned to the JVM. (Refer to this post to see how that was done.) In the event that a variable is not found, due mostly to the fact the developer may have elected not to assign it to their test server running on their local machine, the Java method will return “localhost” as a default value. As noted in the link which shows how to assign the values to the server, the values for “TEST” and “PROD” have already been assigned to Tomcat.

Since the Java class will need to be in all EGL projects the class has been packaged into a .jar-file so it can be reused. To use the class in your project, simply add the .jar-file to the “lib” directory under “WebContent”. Then add the .jar-file to the project’s class path. To learn how this is done select this link and read the section titled “Importing the Jar File into Your Project”.

 

Calling the Java method from EGL

This section describes how to create the EGL External Type and the EGL service, both of which are required when calling a Java program from EGL.

The EGL External Type Definition

First, create an external type to describe to EGL the Java class and its methods.


externalType RuntimeEnvironment type JavaObject{PackageName = "com.app.runtime"}
    
    constructor();
    function getRuntimeEnvironment() returns(string);
        
end

Fig.2 – The EGL External type named ‘RuntimeEnvironment’

 
The external type is an EGL part that describes an interface to another program of a different type. In this context it is the bridge between the EGL world and the Java world. The type shown above describes the Java constructor and the method named getRuntimeEnvironment() .

Calling Using a Dedicated Service

Next, create an EGL service that will use the Java class described by the external type, above.

import com.mig.logviewer.externaltypes.RuntimeEnvironment;

// service
service RuntimeService

    function getRuntime() returns(string)
        
        //Instantiate an object of RuntimeEnvironment.  Call it 'runtime'.
        //This uses the external type named 'RuntimeEnvironment'
        runtime RuntimeEnvironment = new RuntimeEnvironment();

        //Call runtime's getRuntimeEnvironment() method to get the runtime value.
        runtimeEnvironment string = runtime.getRuntimeEnvironment();

        return(runtimeEnvironment);

    end

end

Fig.3 – EGL service that will use the Java class

 

Calling the Java method from EGL

Finally, below is the EGL program code that calls the Java method to get the runtime environment variable established for Tomcat’s JVM.

The EGL function presented is part of the startup processing phase of the EGL project’s web-page. It begins by creating a dedicated service object called runtimeService which will is derived from the EGL Service program created above. Next, the EGL code calls the service’s getRuntime()method, which returns the value for Tomcat’s runtime environment variable.


    function start()

        //Initialize the application by getting the runtime environment from the 
        //server and reading property file values.
        runtimeService RuntimeService{@dedicatedService};
        call RuntimeService.process() 
            returning to initServiceReturn 
                        onException ServiceLib.runtimeServiceReturn;    
                        
    end

    function runtimeServiceReturn(runtimeEnvironment String in)
        
        globalDataRecord GlobalDataRecord = SessionLibrary.getGlobalDataRecord();
        globalDataRecord.runtimeEnvironment = runtimeEnvironment ;
        SessionLibrary.setGlobalDataRecord(globalDataRecord);  

    end


Fig.4 – EGL function calling a service program

 

The call back function receives the variable runtimeEnvironment returned by the service call. The value of the variable is then stored in an instance of an EGL basic record called globalDataRecord In this manner the runtimeEnvironment variable can be used throughout the life of the program simply by passing the globalDataRecord as the parameter. Think of it as a container for global values.
 

Summary

This post has shown how to create a Java class to provide a function not available by EGL alone. Specifically, we have seen how to create an EGL External Type, an EGL Service and the code used to call the service to receive values returned.

Advertisements