/* * 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
となります。
*
* 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;
}
}