12.1.5 データ変換APIの使用例
キャッシュの登録とデータ変換の使用例を次に示します。
- 〈この項の構成〉
(1) 定義ファイルのキャッシュ登録
Stateless Session Beanを実装し,データ変換APIを使用して定義ファイルのキャッシュの登録処理を呼び出す実装例を次に示します。
●Stateless Session Beanの実装例(Remoteインターフェース)
package test.implementation.ejb;
import java.rmi.RemoteException;
import java.util.Set;
import javax.ejb.EJBObject;
import jp.co.Hitachi.soft.csc.dt.api.transformer.FormatDataKind;
/**
* 受付処理(Stateless Session Bean)の実装例(Remoteインターフェース)
*/
public interface TestEJBRemote extends EJBObject {
public boolean registerCache(String groupName, String cacheName, String filePath) throws RemoteException;
public boolean removeCache(String groupName, String cacheName) throws RemoteException;
public Set<String> getCacheNameList(String groupName, FormatDataKind formatDataKind) throws RemoteException;
}●Stateless Session Beanの実装例(Homeインターフェース)
package test.implementation.ejb;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
/**
* 受付処理(Stateless Session Bean)の実装例(Homeインターフェース)
*/
public interface TestEJBHome extends EJBHome {
public TestEJBRemote create() throws RemoteException, CreateException;
}●Stateless Session Beanの実装例(Beanクラス)
package test.implementation.ejb;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.rmi.RemoteException;
import java.util.Set;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import jp.co.Hitachi.soft.csc.dt.api.transformer.DataTransformerCacheManager;
import jp.co.Hitachi.soft.csc.dt.api.transformer.DataTransformException;
import jp.co.Hitachi.soft.csc.dt.api.transformer.FormatDataKind;
import jp.co.Hitachi.soft.csc.dt.api.transformer.FormatDataCacheController;
import jp.co.Hitachi.soft.csc.dt.api.transformer.FormatDefinition;
/**
* Stateless Session Beanの実装例(Beanクラス)
*/
public class TestEJBBean implements SessionBean {
public TestEJBBean() {
}
public void ejbActivate() throws EJBException, RemoteException {
}
public void ejbCreate() throws CreateException {
}
public void ejbPassivate() throws EJBException, RemoteException {
}
public void ejbRemove() throws EJBException, RemoteException {
}
public boolean registerCache(
String groupName, String cacheName, String filePath) {
boolean ret = false;
try {
// オブジェクト取得
FormatDataCacheController formatDataCacheController =
DataTransformerCacheManager.createFormatDataCacheController(groupName);
byte[] definitionBytes = Files.readAllBytes(Paths.get(filePath));
FormatDefinition outFormatDefinition = new FormatDefinition(
FormatDataKind.FDX, cacheName, definitionBytes);
// キャッシュする
formatDataCacheController.register(outFormatDefinition);
ret = true;
} catch (DataTransformException e) {
// 例外処理を実装する
} catch (IOException e) {
// 例外処理を実装する
} finally {
// 終了処理を実装する
}
return ret;
}
public boolean removeCache(String groupName, String cacheName) {
boolean ret = false;
try {
// オブジェクト取得
FormatDataCacheController formatDataCacheController =
DataTransformerCacheManager.createFormatDataCacheController(groupName);
// 要求電文フォーマット定義のキャッシュの削除
FormatDefinition formatDefinition = new FormatDefinition(
FormatDataKind.FDX, cacheName);
formatDataCacheController.remove(formatDefinition);
ret = true;
} catch (DataTransformException e) {
// 例外処理を実装する
} finally {
// 終了処理を実装する
}
return ret;
}
public Set<String> getCacheNameList(String groupName, FormatDataKind formatDataKind) {
Set<String> cacheNameSet = null;
try {
// オブジェクト取得
FormatDataCacheController formatDataCacheController =
DataTransformerCacheManager.createFormatDataCacheController(groupName);
// キャッシュ名の一覧取得
cacheNameSet = formatDataCacheController.getCacheNameList(formatDataKind);
} catch (DataTransformException e) {
// 例外処理を実装する
} finally {
// 終了処理を実装する
}
return cacheNameSet;
}
public void setSessionContext(final SessionContext sc) throws EJBException {
}
}
(2) データ変換の呼び出し
Message-Driven Beanを実装し,データ変換APIを使用してデータ変換処理を呼び出す実装例を次に示します。
●Message-Driven Beanの実装例(N対1変換の場合)
package test.implementation.mdb;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import jp.co.Hitachi.soft.csc.dt.api.transformer.CacheNotFoundException;
import jp.co.Hitachi.soft.csc.dt.api.transformer.DataTransformerCacheManager;
import jp.co.Hitachi.soft.csc.dt.api.transformer.DataTransformException;
import jp.co.Hitachi.soft.csc.dt.api.transformer.InputDataDefinition;
import jp.co.Hitachi.soft.csc.dt.api.transformer.FormatDataKind;
import jp.co.Hitachi.soft.csc.dt.api.transformer.FormatDefinition;
import jp.co.Hitachi.soft.csc.dt.api.transformer.DataTransformer;
import com.hitachi.software.ejb.adapter.tp1.TP1InMessage;
import com.hitachi.software.ejb.adapter.tp1.TP1MessageListener;
import com.hitachi.software.ejb.adapter.tp1.TP1OutMessage;
/**
* Message-Driven Beanの実装例。(3対1のデータ変換)
*/
public class TestMDB2 implements TP1MessageListener, MessageDrivenBean {
public TestMDB2() {
}
public void setMessageDrivenContext(final MessageDrivenContext context) throws EJBException {
}
public void ejbCreate() {
}
public void ejbRemove() throws EJBException {
}
public TP1OutMessage onMessage(TP1InMessage inMessage) {
TP1OutMessage outMessage = null;
try {
// 入力データの取得
byte[] inputData = inMessage.getInputData();
// 入力データの1〜8バイトをグループ名と見なす
String groupName = new String(Arrays.copyOfRange(inputData, 0, 8)).trim();
// 入力データの9〜16バイト,17〜24バイト,25〜32バイトを要求電文のフォーマット定義のキャッシュ名と見なす
String inFormatCacheName1 = new String(Arrays.copyOfRange(inputData, 8, 16)).trim();
String inFormatCacheName2 = new String(Arrays.copyOfRange(inputData, 16, 24)).trim();
String inFormatCacheName3 = new String(Arrays.copyOfRange(inputData, 24, 32)).trim();
// 入力データの33〜40バイトを応答電文のフォーマット定義のキャッシュ名と見なす
String outFormatCacheName = new String(Arrays.copyOfRange(inputData, 32, 40)).trim();
// 入力データの41〜48バイトをデータ変換定義のキャッシュ名と見なす
String transformCacheName = new String(Arrays.copyOfRange(inputData, 40, 48)).trim();
// 入力データの49〜64バイト,65〜80バイト,81バイト以降を業務データと見なす
byte[] businessData1 = Arrays.copyOfRange(inputData, 48, 64);
byte[] businessData2 = Arrays.copyOfRange(inputData, 64, 80);
byte[] businessData3 = Arrays.copyOfRange(inputData, 80, inputData.length);
// データ変換公開APIのインスタンスの取得
DataTransformer dataTransformer = DataTransformerCacheManager.getDataTransformer(groupName);
// 要求電文フォーマット定義のオブジェクトの生成
byte[] inDefinitionBytes1 = Files.readAllBytes(Paths.get("ファイルパスを指定する"));
FormatDefinition inFormatDefinition1 = new FormatDefinition(
FormatDataKind.FDX, inFormatCacheName1, inDefinitionBytes1);
byte[] inDefinitionBytes2 = Files.readAllBytes(Paths.get("ファイルパスを指定する"));
FormatDefinition inFormatDefinition2 = new FormatDefinition(
FormatDataKind.FDX, inFormatCacheName2, inDefinitionBytes2);
byte[] inDefinitionBytes3 = Files.readAllBytes(Paths.get("ファイルパスを指定する"));
FormatDefinition inFormatDefinition3 = new FormatDefinition(
FormatDataKind.FDX, inFormatCacheName3, inDefinitionBytes3);
// 業務データと要求電文フォーマット定義のオブジェクトをInputDataDefinitionに設定
InputDataDefinition[] inDataDefinition = new InputDataDefinition[3];
inDataDefinition[0] = new InputDataDefinition(businessData1, inFormatDefinition1);
inDataDefinition[1] = new InputDataDefinition(businessData2, inFormatDefinition2);
inDataDefinition[2] = new InputDataDefinition(businessData3, inFormatDefinition3);
// 応答電文フォーマット定義のオブジェクトの生成
byte[] outDefinitionBytes = Files.readAllBytes(Paths.get("ファイルパスを指定する"));
FormatDefinition outFormatDefinition = new FormatDefinition(
FormatDataKind.FDX, outFormatCacheName, outDefinitionBytes);
// データ変換定義のオブジェクトの生成
byte[] transformDefinitionBytes = Files.readAllBytes(Paths.get("ファイルパスを指定する"));
FormatDefinition transformFormatDefinition = new FormatDefinition(
FormatDataKind.XSLT, transformCacheName, transformDefinitionBytes);
byte[] outputData = null;
// データ変換処理の呼び出し
outputData = dataTransformer.transform(inDataDefinition,
outFormatDefinition, transformFormatDefinition);
// 出力データの生成
outMessage = inMessage.createOutMessage();
int outoutLength = outputData.length;
byte[] outBuf = outMessage.getOutputData(outoutLength);
System.arraycopy(outputData, 0, outBuf, 0, outoutLength);
} catch (CacheNotFoundException e) {
// 例外処理を実装する
} catch (DataTransformException e) {
// 例外処理を実装する
} catch (IOException e) {
// 例外処理を実装する
} finally {
// 終了処理を実装する
}
return outMessage;
}
}