ビジネス・ロジックでのグローバル変数の使用

ZIETrans アプリケーションでグローバル変数を使用して情報を保管している場合は、それらのグローバル変数をビジネス・ロジックで使用できます。

グローバル変数には 、ローカルと共用の 2 つのタイプがあります。ローカル・グローバル変数は、特定の RCP プロジェクト内で作成され、そのプロジェクトでのみ使用可能な変数です。共用グローバル変数は、1 つ以上の RCP プロジェクト内で作成され、同じユーザー環境内で実行中のすべてのアプリケーションで使用可能な変数です。また、ZIETrans グローバル変数には 2 つのリストがあります。1 つはローカル・グローバル変数用で、もう 1 つは共用グローバル変数用です。同じ名前を持つ 2 つのグローバル変数がある場合、1 つがローカルでもう 1 つが共用であれば共存が可能です。

ビジネス・ロジック・クラスを作成する際は、「ビジネス・ロジックの作成」ウィザードを使用して、「グローバル変数ヘルパー・メソッドの作成」チェック・ボックスを選択します。これによって、ビジネス・ロジックに、ローカル変数および共用グローバル変数を取得、設定、および除去するためのメソッドが作成されます。

以下のメソッドが作成されます。
////////////////////////////////////////////////////////////////////////////////
//  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.
////////////////////////////////////////////////////////////////////////////////
/**
	* Example method that sets a named global variable 
	* from the current session to a value
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the global variable
	* @param value - Value of the global variable
	*/
public static void setGlobalVariable(IBusinessLogicInformation blInfo,
					String name,Object value)
{
		IGlobalVariable gv = blInfo.getGlobalVariable(name);
		if ( gv == null )
			{
				gv = new GlobalVariable(name,value);
			}
		else
			{
				gv.set(value); 
			}
		blInfo.getGlobalVariables().put(name,gv);
}

/**
	* Example method that sets a named shared
	* global variable from the current session to a value
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the shared global variable
	* @param value - Value of the shared global variable
	*/ 
public static void setSharedGlobalVariable(IBusinessLogicInformation 
		blInfo,String name,Object value)
{
		IGlobalVariable gv = blInfo.getSharedGlobalVariable(name);
		if ( gv == null )
			{
				gv = new GlobalVariable(name,value);
			}
		else
			{
				gv.set(value); 
			}
		blInfo.getSharedGlobalVariables().put(name,gv);
}

/**
	* Example method that removes a named global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the global variable
*/
public static void removeGlobalVariable(IBusinessLogicInformation blInfo, String  name)
{
		IGlobalVariable gv = blInfo.getGlobalVariable(name);
		if ( gv != null )
			{
				blInfo.getGlobalVariables().remove(name);
				gv.clear();
				gv = null;
			}
}
/**
	* Example method that removes a named shared global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the shared global variable
*/
public static void removeSharedGlobalVariable(IBusinessLogicInformation blInfo, String name)
{
		IGlobalVariable gv = blInfo.getSharedGlobalVariable(name);
			if ( gv != null )
				{
					blInfo.getSharedGlobalVariables().remove(name);
					gv.clear();
					gv = null;
				}
}
/**
	* Example method that retrieves a named global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the global variable
	* @return - an instance of the global variable, or null if not found.
	*/
public static IGlobalVariable getGlobalVariable(IBusinessLogicInformation 
		blInfo,String name)
{ 
		IGlobalVariable gv = blInfo.getGlobalVariable(name);
		return gv;
}

/**
	* Example method that retrieves a named shared 
	* global variable from the current session
	* @param blInfo - IBusinessLogicInformation from current session
	* @param name - Name of the shared global variable
	* @return - an instance of the global variable, or null if not found.
	*/
public static IGlobalVariable getSharedGlobalVariable(IBusinessLogicInformation 
		blInfo,String name)
{ 
		IGlobalVariable gv = blInfo.getSharedGlobalVariable(name);
		return gv;
}
コード内の別の部分でローカル・グローバル変数の値が必要になった場合は、このメソッドを呼び出すことができます。
  GlobalVariable gv1 = getGlobalVariable(blInfo,"varname");
共用グローバル変数の値を取得するには、次のメソッドを使用します。
  GlobalVariable gv1 = getSharedGlobalVariable(blInfo,"varname");