View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.desc;
17  
18  import java.util.concurrent.ConcurrentMap;
19  
20  import org.seasar.framework.util.Disposable;
21  import org.seasar.framework.util.DisposableUtil;
22  import org.seasar.framework.util.tiger.CollectionsUtil;
23  import org.seasar.uruma.desc.impl.FormDescImpl;
24  
25  /**
26   * {@link FormDesc} を取得するためのファクトリクラスです。<br />
27   * 
28   * @author y-komori
29   */
30  public class FormDescFactory {
31      private static volatile boolean initialized;
32  
33      protected static final ConcurrentMap<Class<?>, FormDesc> formDescs = CollectionsUtil
34              .newConcurrentHashMap();
35  
36      /**
37       * 本ファクトリの初期化を行います。<br />
38       */
39      public static void initialize() {
40          DisposableUtil.add(new Disposable() {
41              public void dispose() {
42                  FormDescFactory.dispose();
43              }
44          });
45          initialized = true;
46      }
47  
48      /**
49       * 本ファクトリが保持するキャッシュをクリアします。<br />
50       */
51      public static synchronized void dispose() {
52          formDescs.clear();
53          initialized = false;
54      }
55  
56      /**
57       * 指定したクラスに対応する {@link FormDesc} オブジェクトを取得します。<br />
58       * 
59       * @param formClass
60       *            {@link FormDesc} を取得するクラス
61       * @return {@link FormDesc} オブジェクト
62       */
63      public static FormDesc getFormDesc(final Class<?> formClass) {
64          if (!initialized) {
65              initialize();
66          }
67          FormDesc formDesc = formDescs.get(formClass);
68          if (formDesc == null) {
69              formDesc = CollectionsUtil.putIfAbsent(formDescs, formClass,
70                      new FormDescImpl(formClass));
71          }
72          return formDesc;
73      }
74  }