この章では、REXクライアントのローカライズのユースケースについて説明します。このユースケースで、Oracle Enterprise RepositoryのREX例外およびREX監査メッセージのローカライズ済のメッセージを作成する方法について解説します。
この章では、次の項目について説明します。
ステータスは、OpenAPIExceptionまたはAuditMsgオブジェクトのいずれかとして、REXクライアントに渡されます。OpenAPIExceptionオブジェクトは例外用に使用されますが、AuditMsgオブジェクトは非同期的に実行されるプロセス用に使用されます。これらのオブジェクトの両方がREXクライアントにテキストのエラー・メッセージを返します。
両方のオブジェクトのインタフェースがエラー・コードおよびメッセージの引数のリストを含めるために拡張されたため、REXクライアントは別の言語でエラー・メッセージやステータス・メッセージを表示できます。クライアントは標準のエラー・メッセージを使用し続けるか、またはメッセージを無視し、エラー・コードとメッセージ引数を使用して専用のエラー・メッセージを作成できます。
たとえば、REXを使用するアプリケーションをローカライズする場合、最初に、想定されるすべてのエラー・メッセージを表示するプロパティ・ファイルを取得します。メッセージは次のようになります。
ERR_9008 = Error updating project with ID = [{0}].
次に、必要に応じて、すべてのメッセージを次のように翻訳する必要があります。
ERR_9008 = Errorway updatingay ojectpray ithway IDway = [{0}].
クライアント・コードはプロジェクトをID=123で変更しようとして、その変更が失敗した場合、エンドユーザーに例外が発生し、次のエラー・メッセージが表示されます。
Error updating project with ID = [123].
このエラーをローカルの言語(ピッグ・ラテン語など)で表示する場合、エラー・コード9008を選択し、これを翻訳済ファイルで検索して文字列"Errorway updatingay ojectpray ithway IDway = [{0}]."を取得します。次に、メッセージ引数を使用して、トークンを置き換えます。この場合、123という文字列が1つのみ検索されるので、メッセージ引数を1つ見つけることができます。
次のように、エンドユーザー用にカスタムのエラー・メッセージを作成できます。
Errorway updatingay ojectpray ithway IDway = [123].
この項では、REXクライアントのローカライズのユースケースについて説明します。内容は次のとおりです。
説明
OpenAPIExceptionから、サーバー・エラー・コードとメッセージ引数を取得します。
クライアント・ロケールに適切なOpenAPIExceptionsのリソース・バンドルを取得します。
エラー・コードに関連付けられた文字列を取得し、トークンをメッセージ引数で置き換えます。
サンプル・コード
例23-1 ユースケース: REX例外からのローカライズ済のメッセージの作成
package com.flashline.sample.localization;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.Project;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import
com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;
public class SyncTest {
private static final int INVALID_PROJECT_ID = 8672609;
public static void main(String[] args) throws Exception {
URL lURL = new
URL("http://localhost:9080/registry/services/FlashlineRegistry");
FlashlineRegistry reg = new
FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);
AuthToken token = reg.authTokenCreate("admin", "n0pa55w0rd");
try {
Project project = reg.projectRead(token,INVALID_PROJECT_ID);
} catch (OpenAPIException ex) {
String msg =
createMessage(ex.getServerErrorCode(),ex.getMessageArguments());
System.out.println(msg);
}
}
private static String createMessage(int pServerErrorCode, Object[] pArgs) {
ResourceBundle mResourceBundle =
ResourceBundle.getBundle("com.flashline.sample.localization.sync_error
_messages");
return MessageFormat.format(mResourceBundle.getString("ERR
_"+pServerErrorCode), pArgs);
}
}
説明
AuditMsgとImpExpJobから、サーバー・エラー・コードとメッセージ引数を取得し、AuditMsgからメッセージ引数を取得します。
クライアント・ロケールに適切な監査メッセージのリソース・バンドルを取得します。
エラー・コードに関連付けられた文字列を取得し、トークンをメッセージ引数で置き換えます。
サンプル・コード
例23-2 REX監査メッセージからのローカライズ済のメッセージの作成
package com.flashline.sample.localization;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import javax.activation.DataHandler;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Stub;
import org.apache.soap.util.mime.ByteArrayDataSource;
import com.flashline.registry.openapi.base.OpenAPIException;
import com.flashline.registry.openapi.entity.AuthToken;
import com.flashline.registry.openapi.entity.ImpExpJob;
import com.flashline.registry.openapi.service.v300.FlashlineRegistry;
import
com.flashline.registry.openapi.service.v300.FlashlineRegistryServiceLocator;
public class AsyncTest {
public void run() throws MalformedURLException, ServiceException,
OpenAPIException, RemoteException{
URL lURL = new
URL("http://localhost:9080/registry/services/FlashlineRegistry");
FlashlineRegistry reg = new
FlashlineRegistryServiceLocator().getFlashlineRegistry(lURL);
AuthToken token = reg.authTokenCreate("admin", "n0pa55w0rd");
try {
File lFile = new
File("samples/com/flashline/sample/localization/asynctest.zip");
//Import the file and save to db
InputStream lIS = new FileInputStream(lFile);
ByteArrayDataSource lDataSource = new ByteArrayDataSource(lIS,
"application/x-zip-compressed");
DataHandler lDH = new DataHandler(lDataSource);
// add the attachment
((Stub)reg).addAttachment(lDH);
ImpExpJob lJob = reg.importExecute(token, "flashline", null, "Import Assets
Test", null);
boolean lPassed = false;
for(int i=0; i<1000; i++){
lJob = reg.importStatus(token, lJob);
System.out.println("Import Job ["+lJob.getID()+"] - State:
"+lJob.getState());
String msg =
createMessage(lJob.getAuditMsg().getSummaryID(),lJob.getAuditMsg().getSummaryArgs
());
System.out.println(msg);
if( lJob.getState().equals("completed")){
lPassed = true;
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (lPassed){
System.out.println("Import Completed");
}
} catch (OpenAPIException ex) {
String msg =
createMessage(ex.getServerErrorCode(),ex.getMessageArguments());
System.out.println(msg);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
* @throws ServiceException
* @throws RemoteException
* @throws MalformedURLException
* @throws OpenAPIException
*/
public static void main(String[] args) throws OpenAPIException,
MalformedURLException, RemoteException, ServiceException {
AsyncTest test = new AsyncTest();
test.run();
}
private static String createMessage(int pServerErrorCode, Object[] pArgs) {
ResourceBundle mResourceBundle =
ResourceBundle.getBundle("com.flashline.sample.localization.async_error
_messages");
return MessageFormat.format(mResourceBundle.getString("ERR
_"+pServerErrorCode), pArgs);
}
private String readZip(String pFileName) throws IOException {
int lNumRead = 0;
char[] lBuf = new char[2048];
StringBuffer lQuery = new StringBuffer();
InputStreamReader lReader = new
InputStreamReader(getClass().getResourceAsStream(pFileName));
while( (lNumRead=lReader.read(lBuf)) != -1){
lQuery.append(lBuf, 0, lNumRead);
}
return lQuery.toString();
}
}