Example: Reading a list of strings from a file into an indexed global variable

This example reads a file from the file system and stores the strings in the file into an indexed global variable. You can use a technique like this to read a file that contains, for example, a list of your company's locations. After storing the strings in a global variable, you can use the global variable to populate a drop-down list or other widget to enable users to select from a list of values. You can create a global rule to use this widget wherever an appropriate input field occurs. To make sure that the global variable is available as soon as the application is started, add the execute action for this business logic class to the Start event.
Note: If your text file has carriage returns and line feeds between lines, you might need to use "\r\n" as the second argument of the StringTokenizer constructor call in the following example.
////////////////////////////////////////////////////////////////////////////////
//  This sample is provided AS IS.
//  Permission to use, copy and modify this software for any purpose and
//  without fee is hereby granted. provided that the name of IBM not be used in
//  advertising or publicity pertaining to distribution of the software without
//  specific written permission.
//
//  IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SAMPLE, INCLUDING ALL
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL IBM
//  BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
//  DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
//  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
//  OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SAMPLE.
////////////////////////////////////////////////////////////////////////////////
import com.ibm.ejs.container.util.ByteArray;
import com.ibm.hats.common.IBusinessLogicInformation;
import com.ibm.hats.common.GlobalVariable;
import com.ibm.hats.common.IGlobalVariable;

public class ReadNamesFromFile
{

  public static void execute(IBusinessLogicInformation blInfo)
  {
		// Name of indexed global variable to be saved
		String gvOutputName = "namesFromFile";
		
		// The file containing a list of information (in this case, it contains names)
		java.io.File myFileOfNames = new java.io.File("C:" + java.io.File.separator
						 + "temp" + java.io.File.separator + "names.txt");
    	
    	try
    	{
			// First, read the contents of the file
			
			java.io.FileInputStream fis = new java.io.FileInputStream(myFileOfNames);
			
			int buffersize = (int)myFileOfNames.length(); 
			byte[] contents = new byte[buffersize];; 

			long n = fis.read(contents, 0, buffersize);
			 
			fis.close();
			
			String namesFromFile = new String(contents);
			
			// Next, create an indexed global variable from the file contents
			
			java.util.StringTokenizer stok = 
						new java.util.StringTokenizer(namesFromFile, "\n", false);
			
			int count = stok.countTokens();
			String[] names = new String[count];
			for (int i = 0; i < count; i++)
			{
				names[i] = stok.nextToken();
			}
			
			IGlobalVariable gv = new GlobalVariable(gvOutputName, names);
			blInfo.getGlobalVariables().put(gvOutputName,gv);			
    	}
    	catch (java.io.FileNotFoundException fnfe)
    	{
    		fnfe.printStackTrace();
    	}
    	catch (java.io.IOException ioe)
    	{
    		ioe.printStackTrace();
    	}
  }
}