/* * Copyright 2004-2007 the Seasar Foundation and the Others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, * either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package org.seasar.framework.container.factory; import org.seasar.framework.container.ExternalContext; import org.seasar.framework.container.ExternalContextComponentDefRegister; import org.seasar.framework.container.S2Container; import org.seasar.framework.exception.EmptyRuntimeException; import org.seasar.framework.util.DisposableUtil; /** * クラスローダ内で唯一の{@link org.seasar.framework.container.S2Container S2コンテナ}を提供するためのファクトリクラスです。 *

* シングルトンS2コンテナファクトリは、設定ファイルに基づいたS2コンテナの生成・初期化を行い、それを保持します。 *

*

* デフォルトの設定ファイル名はapp.diconとなります。 *

* * @author koichik * @author goto */ public final class SingletonS2ContainerFactory { private static String configPath = "app.dicon"; private static ExternalContext externalContext; private static ExternalContextComponentDefRegister externalContextComponentDefRegister; private static S2Container container; private SingletonS2ContainerFactory() { } /** * 設定ファイルのファイルパスを返します。 * * @return 設定ファイルのファイルパス */ public static String getConfigPath() { return configPath; } /** * 設定ファイルのファイルパスを設定します。 * * @param path * 設定ファイルのファイルパス */ public static void setConfigPath(String path) { configPath = path; } /** * {@link org.seasar.framework.container.ExternalContext 外部コンテキスト}を返します。 * * @return 外部コンテキスト */ public static ExternalContext getExternalContext() { return externalContext; } /** * {@link org.seasar.framework.container.ExternalContext 外部コンテキスト}を設定します。 * * @param extCtx * 外部コンテキスト */ public static void setExternalContext(ExternalContext extCtx) { externalContext = extCtx; } /** * {@link org.seasar.framework.container.ExternalContextComponentDefRegister 外部コンテキストコンポーネント定義レジスタ}を返します。 * * @return 外部コンテキストコンポーネント定義レジスタ */ public static ExternalContextComponentDefRegister getExternalContextComponentDefRegister() { return externalContextComponentDefRegister; } /** * {@link org.seasar.framework.container.ExternalContextComponentDefRegister 外部コンテキストコンポーネント定義レジスタ}を設定します。 * * @param extCtxComponentDefRegister * 外部コンテキストコンポーネント定義レジスタ */ public static void setExternalContextComponentDefRegister( ExternalContextComponentDefRegister extCtxComponentDefRegister) { externalContextComponentDefRegister = extCtxComponentDefRegister; } /** * 設定ファイルに基づいたS2コンテナの生成・初期化を行ない、それを保持します。 既にS2コンテナが保持されている場合は何もしません。 *

* S2コンテナを生成した後、初期化を行なう前に、必要に応じて外部コンテキストや外部コンテキストコンポーネント定義レジスタをS2コンテナに設定します。 *

* * @see S2ContainerFactory#create(String) * @see org.seasar.framework.container.ExternalContext * @see org.seasar.framework.container.ExternalContextComponentDefRegister */ public static void init() { if (container != null) { return; } container = S2ContainerFactory.create(configPath); if (container.getExternalContext() == null) { if (externalContext != null) { container.setExternalContext(externalContext); } } else if (container.getExternalContext().getApplication() == null && externalContext != null) { container.getExternalContext().setApplication( externalContext.getApplication()); } if (container.getExternalContextComponentDefRegister() == null && externalContextComponentDefRegister != null) { container .setExternalContextComponentDefRegister(externalContextComponentDefRegister); } container.init(); } /** * S2コンテナやその他の終了処理を行ないます。 */ public static void destroy() { if (container == null) { return; } container.destroy(); container = null; DisposableUtil.dispose(); } /** * クラスローダ内で唯一のS2コンテナを返します。 S2コンテナが保持されていない場合、 * {@link org.seasar.framework.exception.EmptyRuntimeException}をスローします。 * * @return S2コンテナ */ public static S2Container getContainer() { if (container == null) { throw new EmptyRuntimeException("S2Container"); } return container; } /** * 保持するS2コンテナを設定します。 * * @param c * S2コンテナ */ public static void setContainer(S2Container c) { container = c; } /** * S2コンテナを保持しているかどうかを返します。 * * @return S2コンテナを保持している場合はtrue、そうでない場合はfalse */ public static boolean hasContainer() { return container != null; } }