Factory Service

ファクトリサービス

The Factory Service instantiates objects from the given class name using either the given class loader or an applicable one found from the class loader repository. If neither one is specified, the default class loader will be used.

ファクトリサービスは指定されたクラスローダ、 またはクラスローダリポジトリから該当するクラスローダのどちらかを使用し、 与えられたクラス名からオブジェクトのインスタンス化を行います。 もしどちらも設定されていなかった場合、デフォルトクラスローダが使用されます。

The service provides the following benefits compared to Class.forName():

このサービスはClass.forName()に比べ、以下の利点を提供します:

  • support for parameters in constructors,
  • コンストラクタのパラメータをサポートし、
  • internal class loader repository, which can be specified in resources,
  • リソース内で設定可能な内部クラスローダリポジトリ、
  • optional class specific factories, which can be used for customized instantiation, and
  • インスタンス化をカスタマイズ可能にするオプションクラス設定ファクトリ、そして
  • integration with the Pool Service supporting recycling of instances created by the service.
  • このサービスで作成されたインスタンスの再利用をサポートするプールサービスとの統合。

Configuration

設定

# -------------------------------------------------------------------
#
#  S E R V I C E S
#
# -------------------------------------------------------------------

# Classes for Turbine Services should be defined here.
# Format: services.[name].classname=[implementing class]
#
# To specify properties of a service use the following syntax:
# service.[name].[property]=[value]

# Turbineのサービスのためのクラス群はここに定義します。
# フォーマット: services.[name].classname=[implementing class]
#
# サービスのプロパティを設定するには、以下の書式を使用します:
# service.[name].[property]=[value]

services.FactoryService.classname=org.apache.fulcrum.factory.TurbineFactoryService
.
.
.
# -------------------------------------------------------------------
#
#  F A C T O R Y  S E R V I C E
#
# -------------------------------------------------------------------


# A comma separated list of classloaders (very optional)
#
# Example: org.foo.bar.MyClassLoader, org.ack.joe.YourClassLoader
#
#services.FactoryService.class.loaders=

# クラスローダのカンマ区切りのリスト(任意指定)
#
# 例: org.foo.bar.MyClassLoader, org.ack.joe.YourClassLoader
#
#services.FactoryService.class.loaders=


# Customized factories to be used instead of the default factory.
# E.g. to instantiate XML parsers, SSL sockets, etc., which require
# specific instantiation not supported by the default factory.
# The property name is prefixed with "factory" followed by the
# name of the production class. The value is the class name of
# the factory implementing the Factory interface. The factory
# will be instantiated by using the service itself.
#
# Examples:
#
#services.FactoryService.factory.javax.xml.parsers.DocumentBuilder=org.foo.xml.DomBuilderFactory
#services.FactoryService.factory.javax.xml.parsers.SAXParser=org.foo.xml.SaxParserFactory
#services.FactoryService.factory.java.net.ServerSocket=org.foo.net.SslServerSocketFactory

# デフォルトのファクトリの代わりに使用されるカスタマイズしたファクトリ。
# 例えば、デフォルトファクトリではサポートされないXMLパーザ、
# SSLソケットなどのインスタンス化の設定が必要であるもののインスタンス化。
# プロパティ名は接頭辞"factory"に、生成されるクラス名を続ける。
# 値はファクトリのインターフェースを実装するファクトリのクラス名。
# ファクトリは、それ自身でサービスを使用することによりインスタンス化される。
#
# 例:
#
#services.FactoryService.factory.javax.xml.parsers.DocumentBuilder=org.foo.xml.DomBuilderFactory
#services.FactoryService.factory.javax.xml.parsers.SAXParser=org.foo.xml.SaxParserFactory
#services.FactoryService.factory.java.net.ServerSocket=org.foo.net.SslServerSocketFactory

Usage

使用方法

In Turbine, the Factory Service is currently only used internally by the Pool Service. Applications can also use the service instead of Class.forName() and for unifying initialization, configuration and access to vendor specific object factories. The following is a simplified example of a customized DOM parser factory:

Turbineでは、ファクトリサービスは現在のところプールサービスの内部からのみ使用されます。 アプリケーションもClass.forName()の代わりとして、また、統一された初期化、設定、特定のベンダのオブジェクトファクトリへのアクセスのためにサービスを使用することができます。 以下の例はDOMパーザファクトリをカスタマイズした簡単な例です:

package org.foo.xml;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.fulcrum.ServiceException;
import org.apache.fulcrum.factory.Factory;

/**

 * A factory for instantiating DOM parsers.

 * DOMパーザをインスタンス化するファクトリです。
 */
public class DomBuilderFactory implements Factory
{
    /**

     * The implementation of the factory.

     * ファクトリの実装です。
     */
    private DocumentBuilderFactory factory;

    /**

     * Initializes the factory.

     * ファクトリを初期化します。
     */
    public void init(String className)
        throws ServiceException
    {
        factory = DocumentBuilderFactory.newInstance();
    }

    /**

     * Gets a DocumentBuilder instance.

     * DocumentBuilderのインスタンスを取得します。
     */
    public Object getInstance()
        throws ServiceException
    {
        try
        {
            return factory.newDocumentBuilder();
        }
        catch (ParserConfigurationException x)
        {
            throw new TurbineException(x);
        }
    }

    /**

     * Gets a DocumentBuilder instance.
     * The given loader is ignored.

     * DocumentBuilderのインスタンスを取得します。
     * 与えられたローダは無視されます。
     */
    public Object getInstance(ClassLoader loader)
        throws ServiceException
    {
        return getInstance();
    }

    /**

     * Gets a DocumentBuilder instance.
     * Constructor parameters are ignored.

     * DocumentBuilderのインスタンスを取得します。
     * コンストラクタのパラメータは無視されます。
     */
    public Object getInstance(Object[] params,
                              String[] signature)
        throws ServiceException
    {
        return getInstance();
    }

    /**

     * Gets a DocumentBuilder instance.
     * The given loader and constructor parameters are ignored.

     * DocumentBuilderのインスタンスを取得します。
     * 与えられたローダとコンストラクタのパラメータは無視されます。
     */
    public Object getInstance(ClassLoader loader,
                              Object[] params,
                              String[] signature)
        throws ServiceException
    {
        return getInstance();
    }

    /**

     * Returns false as given class loaders are not supported.

     * 与えられたクラスローダはサポートされないのでfalseを返却します。
     */
    public boolean isLoaderSupported()
    {
        return false;
    }
}

The customized DOM parser factory must be specified in Turbine Resources before it can be used:

カスタマイズされたDOMパーザファクトリは事前にTurbine Resourceに設定しておかなければなりません:

services.FactoryService.factory.javax.xml.parsers.DocumentBuilder=org.foo.xml.DomBuilderFactory

A DOM parser can now be instantiated with the following code fragment:

DOMパーザは以下のコードでインスタンス化できます。


// Access the service singleton.

// サービスシングルトンへのアクセス
FactoryService service = (FactoryService)
    TurbineServices.getInstance().getService(FactoryService.SERVICE_NAME);


// Create a new DOM parser.

// 新しいDOMパーザの作成
DocumentBuilder parser =
    (DocumentBuilder)service.getInstance("javax.xml.parsers.DocumentBuilder");