WebLogic環境下でバーコードイメージパス取得時の例外発生を回避する方法
対象製品
JBarCode 3.0J
詳細
Oracle WebLogic Server 11g/12c の環境下でWebアプリケーションを運用した際、バーコード生成時に例外が発生する場合があります。これはWebLogicがデフォルトの設定ではgetRealPathメソッドがnullを返してしまう問題によるためです。
例えば、以下のようなコードのとき、getRealPathメソッドがnullを返すために例外が発生し、saveImageメソッドの保存先のパスを取得することができません。
■JSPスクリプトレット サンプルコード
<%@ page import = "com.grapecity.barcode.BarCode" %>
<%@ page import = "com.grapecity.barcode.BarCodeException"%>
<%@ page import = "com.grapecity.barcode.valuetype.*"%>
<%@ page import = "java.awt.*"%>
<%
BarCode barcode = new BarCode();
barcode.setValue("12345");
barcode.setType(Type.JAN8);
String filename = new java.util.Date().getTime() + ".png";
barcode.saveImage(application.getRealPath("cache/"+filename), BarCode.PNG);
%>
<html><body>
<img src="<%=filename%>" />
</body></html>
この現象については、getPathメソッドを使用することで回避することができます。以下のサンプルコードをご参照ください。
■JSPスクリプトレット サンプルコード
<%@ page import = "com.grapecity.barcode.BarCode" %>
<%@ page import = "com.grapecity.barcode.BarCodeException"%>
<%@ page import = "com.grapecity.barcode.valuetype.*"%>
<%@ page import = "java.awt.*"%>
<%@ page import = "java.net.URL"%>
<%
BarCode barcode = new BarCode();
barcode.setType(Type.JAN8);
barcode.setValue("12345");
//String filename = new java.util.Date().getTime() + ".png";
//barcode.saveImage(application.getRealPath("/") + filename, BarCode.PNG);
URL resourceUrl = getClass().getClassLoader().getResource("/");
String filename = null;
if (resourceUrl != null) {
filename = resourceUrl.getPath() + new java.util.Date().getTime() + ".png";
barcode.saveImage(filename, BarCode.PNG);
}
%>
<html><body>
<img src="file://<%=filename%>" />
</body></html>
なお、WebLogicのコンフィグレーションを変更することでも回避することができます。
次の内容のweblogic.xml を新たに作成し<show-archived-real-path-enabled>の値をtrueに設定します。
■weblogic.xml
<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
<container-descriptor>
<show-archived-real-path-enabled>true
</show-archived-real-path-enabled>
</container-descriptor>
</weblogic-web-app>
作成したweblogic.xmlファイルをデプロイ先のweb.xmlと同じディレクトリに配置しWebLogicを再起動します。
これによりgetRealPathメソッドから正常にパスを取得することが可能となります。
例えば、以下のようなコードのとき、getRealPathメソッドがnullを返すために例外が発生し、saveImageメソッドの保存先のパスを取得することができません。
■JSPスクリプトレット サンプルコード
<%@ page import = "com.grapecity.barcode.BarCode" %>
<%@ page import = "com.grapecity.barcode.BarCodeException"%>
<%@ page import = "com.grapecity.barcode.valuetype.*"%>
<%@ page import = "java.awt.*"%>
<%
BarCode barcode = new BarCode();
barcode.setValue("12345");
barcode.setType(Type.JAN8);
String filename = new java.util.Date().getTime() + ".png";
barcode.saveImage(application.getRealPath("cache/"+filename), BarCode.PNG);
%>
<html><body>
<img src="<%=filename%>" />
</body></html>
この現象については、getPathメソッドを使用することで回避することができます。以下のサンプルコードをご参照ください。
■JSPスクリプトレット サンプルコード
<%@ page import = "com.grapecity.barcode.BarCode" %>
<%@ page import = "com.grapecity.barcode.BarCodeException"%>
<%@ page import = "com.grapecity.barcode.valuetype.*"%>
<%@ page import = "java.awt.*"%>
<%@ page import = "java.net.URL"%>
<%
BarCode barcode = new BarCode();
barcode.setType(Type.JAN8);
barcode.setValue("12345");
//String filename = new java.util.Date().getTime() + ".png";
//barcode.saveImage(application.getRealPath("/") + filename, BarCode.PNG);
URL resourceUrl = getClass().getClassLoader().getResource("/");
String filename = null;
if (resourceUrl != null) {
filename = resourceUrl.getPath() + new java.util.Date().getTime() + ".png";
barcode.saveImage(filename, BarCode.PNG);
}
%>
<html><body>
<img src="file://<%=filename%>" />
</body></html>
なお、WebLogicのコンフィグレーションを変更することでも回避することができます。
次の内容のweblogic.xml を新たに作成し<show-archived-real-path-enabled>の値をtrueに設定します。
■weblogic.xml
<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
<container-descriptor>
<show-archived-real-path-enabled>true
</show-archived-real-path-enabled>
</container-descriptor>
</weblogic-web-app>
作成したweblogic.xmlファイルをデプロイ先のweb.xmlと同じディレクトリに配置しWebLogicを再起動します。
これによりgetRealPathメソッドから正常にパスを取得することが可能となります。