<?xml version="1.0" encoding="Shift_JIS"?>
<document>

<properties>
  <title>WebLogic で Velocity を使う</title>
  <originaltitle>Using Velocity With WebLogic</originaltitle>
  <author email="Paw Dybdahl (pdy@csg.csc.dk)">Velocity Documentation Team</author>
  <translator>高橋達男</translator>
  <translator>野瀬直樹</translator>
  <original>velocity_and_weblogic</original>
</properties>

<body>

<section name="Contents" alias="目次">

<primary>
<ol>
  <li>
    <a href="velocity_and_weblogic.html#Assumptions">Assumptions</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#The Scope Of This Guide">The Scope Of This Guide</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Where to Put the Template Files">
    Where To Put the Template Files</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Setting the Configuration Properties">
    Setting the Configuration Properties</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#What About Stylesheets?">
    What about stylesheets?</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Where to Put the Velocity Jar">
      Where to put the Velocity Jar</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Making Life Easier for the Developer">
    Making Life Easier for the Developer</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#A Build Process">
    A Build Process</a>
  </li>
</ol>
</primary>
<ol>
  <li>
    <a href="velocity_and_weblogic.html#Assumptions">前提</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#The Scope Of This Guide">このガイドの目的</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Where to Put the Template Files">
    テンプレートファイルをどこに置くか?</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Setting the Configuration Properties">
    設定プロパティ</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#What About Stylesheets?">
    スタイルシートはどうするか?</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Where to Put the Velocity Jar">
      Velocity JAR ファイルをどこに置くか?</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#Making Life Easier for the Developer">
    開発を楽にするには?</a>
  </li>
  <li>
    <a href="velocity_and_weblogic.html#A Build Process">
    ビルドプロセス</a>
  </li>
</ol>

</section>

<section name="Assumptions" alias="前提">

<primary>
<p>
This short guide can only be keept short because it assumes you already have
a working knowledge af Velocity and how to use it in the J2EE environment,
for example with Apache-Tomcat.
</p>
</primary>
<p>
この簡単なガイドは、Velocity の基本と、Apache Tomcat などの
J2EE 環境での Velocity の使い方をすでに十分に理解していることを前提にしています。
</p>
</section>

<section name="The Scope Of This Guide" alias="このガイドの目的">

<primary>
<p>
Deploying web-applications to Tomcat can be accomplished by a war-file, but
at startup Tomcat explodes the war-file so the directory structure are available
for the Velocity engine at runtime and the <code>getRealPath()</code> method in 
<code>ServletContext</code> returns the real path to the given directory.
</p>
</primary>
<p>
Web アプリケーションの Tomcat への配備は WAR ファイルで行なえますが、
Tomcat は起動時に WAR ファイルを展開するため、Velocity
エンジンは実行時にディレクトリ構造を使えますし、
<code>ServletContext</code> の <code>getRealPath()</code> メソッドは、
対象となるディレクトリの実際のパスを返せます。
</p>

<primary>
<p>
Deploying the same war-file in WebLogic will not have the same effect,
since WebLogic keeps its deployed war-files unexploded, and the 
<code>getRealPath()</code> method in <code>ServletContext</code> 
returns <code>null</code>. So how should you
organize your properties file, templates, stylesheets, servlets and build
process in this environment? 
</p>
</primary>
<p>
WebLogic は配備した WAR ファイルを展開しないため、
WebLogic で同じ WAR ファイルを配備する場合は、結果は異なり、
<code>ServletContext</code> の <code>getRealPath()</code> メソッドは
<code>null</code> を返します。では、この環境で、
プロパティファイル／テンプレート／スタイルシート／Servlet／ビルドプロセスは、
どのように扱えばよいのでしょうか?
</p>

<primary>
<p>
The scope of this little guide is to give you answers to these
questions, so you can enjoy using Velocity even in the context of a commercial
app server.
</p>
</primary>
<p>
この短いガイドはこうした疑問に答えるもので、
これを読めば商用のアプリケーションサーバにおいても
Velocity をうまく使えることでしょう。
</p>

<primary>
<p>
While all the relevant information can be found in the JavaDoc documentation,
this quick guide will help you get started quickly.
</p>
</primary>
<p>
関連する情報はすべて JavaDoc ドキュメントにありますが、
このクイックガイドがあればすぐに作業を始められることでしょう。
</p>

</section>

<section name="Where to Put the Template Files" alias="テンプレートはどこに置くか?">

<primary>
<p>
Since the <code>ClasspathResourceLoader</code> can load a template from a
resource found in the servlet engines classpath, it seems like a very good idea
to jar all the template-files together and place this jar (<code>template.jar</code>
for example) in <code>WEB-INF/lib</code> which will make it available as a 
resource in the classpath.
</p>
</primary>
<p>
<code>ClasspathResourceLoader</code> は、Servlet
エンジンのクラスパスにあるリソースからテンプレートをロードできるので、
テンプレートファイル全てを JAR ファイルに入れて、この JAR ファイル (例えば
<code>template.jar</code>) をクラスパス上のリソースとして使えるように
<code>WEB-INF/lib</code> に置くのも良いかも知れません。
</p>

</section>

<section name="Setting the Configuration Properties" alias="設定プロパティ">

<primary>
<p>
Although the properties file could be given any name, for this guide we 
will use <code>velocity.properties</code> since
all documentation and examples on the Velocity site uses this filename.
</p>
</primary>
<p>
プロパティファイルはどんな名前でもよいのですが、Velocity
サイトの全てのドキュメントやサンプルで <code>velocity.properties</code>
という名前を使っているので、このガイドでもこのファイル名を使うことにします。
</p>

<primary>
<p>
This file should include the following two lines to configure the resource
loader:
</p>
</primary>
<p>
リソースローダを設定するため、このファイルには以下の2行が必要です。
</p>

<pre>
    resource.loader = class
    class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</pre>

<primary>
<p>
The file can contain other configurations as well.
</p>
</primary>
<p>
このファイルには他の設定も含まれています。
</p>

<primary>
<strong>Where to put <code>velocity.properties</code></strong>
</primary>
<strong><code>velocity.properties</code> はどこに置くか?</strong>

<primary>
<p>
Put the <code>velocity.properties</code> file directly in the <code>WEB-INF</code>
directory and include the following lines in your <code>web.xml</code>:
</p>
</primary>
<p>
<code>velocity.properties</code> は <code>WEB-INF</code>
ディレクトリ直下に置き、<code>web.xml</code> に以下の数行を加えます。
</p>

<pre>
&lt;servlet&gt;
  &lt;servlet-name&gt;...&lt;/servlet-name&gt;
  &lt;servlet-class&gt;...&lt;/servlet-class&gt;
    &lt;init-param&gt;
      &lt;param-name&gt;properties&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/velocity.properties&lt;/param-value&gt;
    &lt;/init-param&gt;
&lt;/servlet&gt;
</pre>

<primary>
<p>
and include the following implementation in your servlet(s):
</p>
</primary>
<p>
そして、Servlet では以下のように実装します。
</p>

<pre>
  protected Properties loadConfiguration (ServletConfig config) throws IOException, FileNotFoundException  {
    String propsFile = config.getInitParameter(INIT_PROPS_KEY);
    Properties p = new Properties();

    if ( propsFile != null ) {
      InputStream iStream = getServletContext().getResourceAsStream( propsFile );

      if ( iStream != null ) {
        p.load( iStream );
      }
    }

    return p;
  }
</pre>

</section>

<section name="What About Stylesheets?" alias="スタイルシートはどうするか?">

<primary>
<p>
If you have any stylesheets you can place them anywhere you like, as long as
you only make relative references to these files from your template files.
</p>
</primary>
<p>
スタイルシートについては、テンプレートファイルから相対的にファイルを参照できる限り、
どこに置いてもかまいません。
</p>

<primary>
<p>
I prefer to have my stylesheets keept in a directory called <code>stylesheets</code>
located under the root-directory of the web application (i.e. at the same
level as the <code>WEB-INF</code> directory).
</p>
</primary>
<p>
筆者 (Paw Dybdahl) としては、Web アプリケーションのルートディレクトリの下
(つまり、<code>WEB-INF</code> ディレクトリと同じレベル)に
<code>stylesheets</code> という名前のディレクトリを作って、
そこにスタイルシートを置くのがいいと思っています。
</p>

<primary>
<p>
In your template files you can reference the stylesheets like this:
</p>
</primary>
<p>
テンプレートファイルでは以下のようにスタイルシートを参照します。
</p>

<pre>
&lt;link REL=&quot;stylesheet&quot; TYPE=&quot;text/css&quot; HREF=&quot;./stylesheets/style.css&quot;&gt;
</pre>

</section>

<section name="Where to Put the Velocity Jar"
alias="Velocity JAR ファイルをどこに置くか?">

<primary>
<p>
First of all you have to decide whether you will use the dependency-free version of
velocity.jar or the version including all dependend jars. If you are not worried about
collisions with Avalon Logkit, Commons Collections or Jakarta Oro, using the 
jar containing all dependencies is very convenient.
</p>
</primary>
<p>
まず最初に、Velocity JAR ファイルとして、
依存するライブラリを含まないバージョンを使うか、依存する JAR
ファイルを全て含んだバージョンを使うかを決めないといけません。Avalon Logkit、
Common Collections、Jakarta ORO との不整合を気にしないのであれば、
依存するライブラリを全て含む JAR ファイルを使うと非常に便利です。
</p>

<primary>
<p>
Putting the velocity jar in <code>WEB-INF/lib</code> your web application will result
in it's classes being available in the classpath for that web application.
</p>
</primary>
<p>
Velocity JAR ファイルを Web アプリケーションの <code>WEB-INF/lib</code>
ディレクトリに置けば、その Web アプリケーションのクラスパスで
Velocity の各クラスが使えるようになります。
</p>

</section>

<section name="Making Life Easier for the Developer" alias="開発を楽にするには?">

<primary>
<p>
Using Velocity in 'real life' you quickly recognize a lot of
similarity between your servlets. The first abstraction that will make life a
little easier for the developers is a servlet like my <code>MainServlet</code>, which all
application specific servlets can subclass. The only method the subclasses must
override is the <code>getTemplateName()</code> method, which should return the name of
this servlets template file.
</p>
</primary>
<p>
Velocity を「実際に」使っているとすぐに、複数の Servlet
で共通した部分に気がつくと思います。まずは開発を楽にする抽象クラスとして、
アプリケーション固有の Servlet をサブクラス化できる自分用の 
<code>MainServlet</code> を作るとよいでしょう。
サブクラスが必ずオーバーライドしなければならないメソッドは、
Servlet のテンプレートファイルの名前を返す <code>getTemplateName()</code> だけです。
</p>

<primary>
<p>If the subclasses needs to put data into context (which most servlets do)
they can implement the <code>loadData()</code> method.
</p>
</primary>
<p>
サブクラスで、(ほとんどの Servlet で行なうように)
データをコンテキストにセットする必要があるなら、<code>loadData()</code>
メソッドを実装できます。
</p>

<primary>
<p>
That's it. The rest is done by <code>MainServlet</code> (and <code>VelocityServlet</code> of course).
</p>
</primary>
<p>
これでおしまいです。それ以外は <code>MainServlet</code>
(と、当たり前ですが <code>VelocityServlet</code>) がやってくれます。
</p>

<pre>
public abstract class MainServlet extends VelocityServlet {

  public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) {
    loadData( request, response, ctx );
    return getMyTemplate();
  }

  protected Properties loadConfiguration (ServletConfig config) throws IOException, FileNotFoundException  {
    String propsFile = config.getInitParameter(INIT_PROPS_KEY);
    Properties p = new Properties();

    if ( propsFile != null ) {
      InputStream iStream = getServletContext().getResourceAsStream( propsFile );

      if ( iStream != null ) {
        p.load( iStream );
      }
    }

    return p;
  }

  protected Template getMyTemplate( ) {
    Template template = null;
    try {
      template =  getTemplate( getTemplateName() + &quot;.vm&quot; );
    }
    catch (ParseErrorException pee) {
      mylog(&quot;Parse error for template &quot; + pee);
    }
    catch (ResourceNotFoundException rnfe) {
      mylog(&quot;Template not found &quot; + rnfe);
    }
    catch (Exception e) {
      mylog(&quot;Error &quot; + e);
    }
    return template;
  }
  
  /** <primary>
   * Gets the name of the template file to be processed from this servlet. Should be 
   * overridden by every subclass. </primary>
   * この Servlet で処理されるテンプレートファイル名を取得する。
   * 各サブクラスでオーバーライドされなければならない。
   */
  abstract protected String getTemplateName();

  /** <primary>
   * Load data into context. </primary>
   * データをコンテキストにロードする。
   */
  protected void loadData(HttpServletRequest request, HttpServletResponse response, Context ctx ) {
    ctx.put( &quot;dummy&quot;, &quot;dummy&quot; );
  }
}
</pre>

</section>

<section name="A Build Process" alias="ビルドプロセス">

<primary>
<p>
A simple build process that supports the outlined directory contains the
follwing steps:
</p>
</primary>
<p>
上で概略を説明したディレクトリをサポートする単純なビルドプロセスは、
以下のようなステップとなります。
</p>

<primary>
<ol>
  <li>Define shortcuts for the directories used during the build process</li>
  <li>Prepare the build by coping all necessary files to the staging (build)
    directory</li>
  <li>Compile all java files</li>
  <li>Make a jar containing all templates</li>
  <li>Make a war containing the web application (in the distribution directory)</li>
</ol>
</primary>
<ol>
  <li>ビルドプロセスで使うディレクトリのショートカットを定義する。</li>
  <li>出力先の (ビルド) ディレクトリに必要なファイルをコピーして、
    ビルドを準備する</li>
  <li>全ての Java ファイルをコンパイルする。</li>
  <li>全てのテンプレートが入った JAR ファイルを作る。</li>
  <li>(配布ディレクトリに) Web アプリケーションが入った WAR ファイルを作る。</li>
</ol>

<primary>
<p>
A concrete example following the above scheme:
</p>
</primary>
<p>
上記の枠組みにしたがった具体例を以下に示します。
</p>

<pre>
&lt;project name=&quot;carImportAdmin&quot; default=&quot;all&quot; basedir=&quot;.&quot;&gt;

<primary>  &lt;!--	Source directory for deployment descriptors, manifest, properties,...  --&gt; </primary>
  &lt;!--	配備記述子(DD)、マニフェスト、プロパティなどのソースディレクトリ  --&gt;
  &lt;property name=&quot;dd&quot; value=&quot;./dd&quot;/&gt;

<primary>  &lt;!--  Work directory used during build process  --&gt; </primary>
  &lt;!--  ビルドプロセスで使用するワークディレクトリ  --&gt;	
  &lt;property name=&quot;build&quot; value=&quot;./staging&quot;/&gt;

<primary>  &lt;!--  Target directory for the final war-file  --&gt; </primary>
  &lt;!--  最終的なWAR ファイルのターゲットディレクトリ  --&gt;
  &lt;property name=&quot;dist&quot; value=&quot;../build-ear/modules&quot;/&gt;

<primary>  &lt;!--  Source directory for all java files  --&gt; </primary>
  &lt;!--  全ての Java ファイルのソースディレクトリ  --&gt;
  &lt;property name=&quot;src&quot; value=&quot;./src&quot;/&gt;

<primary>  &lt;!--  Source directory for template files  --&gt; </primary>
  &lt;!--  テンプレートファイルのソースディレクトリ  --&gt;
  &lt;property name=&quot;templates&quot; value=&quot;./templates&quot;/&gt;

<primary>  &lt;!--  Source directory for stylesheets  --&gt; </primary>
  &lt;!--  スタイルシートのソースディレクトリ  --&gt;
  &lt;property name=&quot;stylesheets&quot; value=&quot;./stylesheets&quot;/&gt;

<primary>  &lt;!--  Libraries used in compile  --&gt; </primary>
  &lt;!--  コンパイルで使用するライブラリ  --&gt;
  &lt;property name=&quot;lib&quot; value=&quot;${dist}/CarImport.jar;${dist}/FPGuiden.jar;${dist}/velocity-dep-1.2-rc3.jar&quot;/&gt;


  &lt;target name=&quot;all&quot; depends=&quot;init, compile, jar, war&quot;/&gt;

  &lt;target name=&quot;init&quot; depends=&quot;clean&quot;&gt;
    &lt;tstamp/&gt;

    &lt;mkdir dir=&quot;${build}&quot;/&gt;
    &lt;mkdir dir=&quot;${build}/stylesheets&quot;/&gt;
    &lt;mkdir dir=&quot;${build}/WEB-INF&quot;/&gt;
    &lt;mkdir dir=&quot;${build}/WEB-INF/classes&quot;/&gt;
    &lt;mkdir dir=&quot;${build}/WEB-INF/lib&quot;/&gt;

    &lt;copy todir=&quot;${build}/WEB-INF&quot;&gt;
      &lt;fileset dir=&quot;${dd}&quot;&gt;
        &lt;include name=&quot;velocity.properties&quot;/&gt;
      &lt;/fileset&gt;
      &lt;fileset dir=&quot;${dd}/WEB-INF&quot;&gt;
        &lt;include name=&quot;web.xml&quot;/&gt;
      &lt;/fileset&gt;
    &lt;/copy&gt;
    &lt;copy todir=&quot;${build}/WEB-INF/lib&quot;&gt;
      &lt;fileset dir=&quot;${dist}&quot;&gt;
        &lt;include name=&quot;velocity-dep-1.2-rc3.jar&quot;/&gt;
      &lt;/fileset&gt;
    &lt;/copy&gt;
    &lt;copy todir=&quot;${build}/stylesheets&quot;&gt;
      &lt;fileset dir=&quot;${stylesheets}&quot;&gt;

      &lt;/fileset&gt;
    &lt;/copy&gt;
  &lt;/target&gt;


  &lt;target name=&quot;compile&quot;&gt;
    &lt;javac srcdir=&quot;${src}&quot; destdir=&quot;${build}/WEB-INF/classes&quot; classpath=&quot;${CLASSPATH};${lib}&quot;&gt;
    &lt;/javac&gt;
  &lt;/target&gt;

  &lt;target name=&quot;jar&quot;&gt;
    &lt;jar jarfile=&quot;${build}/WEB-INF/lib/templates.jar&quot;
      basedir=&quot;${templates}&quot;&gt;
    &lt;/jar&gt;
  &lt;/target&gt;

  &lt;target name=&quot;war&quot; depends=&quot;init&quot;&gt;
    &lt;jar jarfile=&quot;${dist}/carImportAdmin.war&quot;
      basedir=&quot;${build}&quot;&gt;
    &lt;/jar&gt;
  &lt;/target&gt;

  &lt;target name=&quot;clean&quot;&gt;
    &lt;delete dir=&quot;${build}&quot;/&gt;
  &lt;/target&gt;

&lt;/project&gt;
</pre>

</section>

</body>

</document>

