Coverage Report - org.seasar.cubby.plugins.s2.spi.S2ConverterProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
S2ConverterProvider
0%
0/40
0%
0/14
0
 
 1  
 /*
 2  
  * Copyright 2004-2009 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.cubby.plugins.s2.spi;
 17  
 
 18  
 import java.lang.reflect.Modifier;
 19  
 import java.util.Arrays;
 20  
 import java.util.Collection;
 21  
 import java.util.LinkedHashSet;
 22  
 import java.util.Set;
 23  
 
 24  
 import org.seasar.cubby.converter.Converter;
 25  
 import org.seasar.cubby.plugins.s2.detector.ClassDetector;
 26  
 import org.seasar.cubby.plugins.s2.detector.DetectClassProcessor;
 27  
 import org.seasar.cubby.spi.impl.AbstractCachedConverterProvider;
 28  
 import org.seasar.framework.container.S2Container;
 29  
 import org.seasar.framework.convention.NamingConvention;
 30  
 import org.seasar.framework.util.ClassUtil;
 31  
 import org.seasar.framework.util.Disposable;
 32  
 import org.seasar.framework.util.DisposableUtil;
 33  
 
 34  
 /**
 35  
  * コンバータプロバイダの実装クラスです。
 36  
  * 
 37  
  * @author baba
 38  
  * @since 2.0.0
 39  
  */
 40  0
 public class S2ConverterProvider extends AbstractCachedConverterProvider
 41  
                 implements DetectClassProcessor, Disposable {
 42  
 
 43  
         public static final String s2Container_BINDING = "bindingType=must";
 44  
 
 45  
         public static final String namingConvention_BINDING = "bindingType=must";
 46  
 
 47  
         public static final String classDetector_BINDING = "bindingType=must";
 48  
 
 49  
         /** S2 コンテナ。 */
 50  
         private S2Container s2Container;
 51  
 
 52  
         /** 命名規約。 */
 53  
         private NamingConvention namingConvention;
 54  
 
 55  
         /** クラスパスを走査してクラスを検出するクラス。 */
 56  
         private ClassDetector classDetector;
 57  
 
 58  
         /** インスタンスが初期化済みであることを示します。 */
 59  
         private boolean initialized;
 60  
 
 61  
         /** コンバータのセットです。 */
 62  0
         private final Set<Converter> converters = new LinkedHashSet<Converter>();
 63  
 
 64  
         /**
 65  
          * S2 コンテナを設定します。
 66  
          * 
 67  
          * @param s2Container
 68  
          *            S2 コンテナ
 69  
          */
 70  
         public void setS2Container(final S2Container s2Container) {
 71  0
                 this.s2Container = s2Container;
 72  0
         }
 73  
 
 74  
         /**
 75  
          * 命名規約を設定します。
 76  
          * 
 77  
          * @param namingConvention
 78  
          *            命名規約
 79  
          */
 80  
         public void setNamingConvention(final NamingConvention namingConvention) {
 81  0
                 this.namingConvention = namingConvention;
 82  0
         }
 83  
 
 84  
         /**
 85  
          * クラスパスを走査してクラスを検出するクラスを設定します。
 86  
          * 
 87  
          * @param classDetector
 88  
          *            クラスパスを走査してクラスを設定します。
 89  
          */
 90  
         public void setClassDetector(final ClassDetector classDetector) {
 91  0
                 this.classDetector = classDetector;
 92  0
         }
 93  
 
 94  
         @Override
 95  
         protected Collection<Converter> getConverters() {
 96  0
                 return converters;
 97  
         }
 98  
 
 99  
         /**
 100  
          * インスタンスを初期化します。
 101  
          */
 102  
         public void initialize() {
 103  0
                 if (initialized) {
 104  0
                         return;
 105  
                 }
 106  0
                 classDetector.detect();
 107  
 
 108  0
                 final Converter[] converters = (Converter[]) s2Container.getRoot()
 109  
                                 .findAllComponents(Converter.class);
 110  0
                 this.converters.addAll(Arrays.asList(converters));
 111  0
                 DisposableUtil.add(this);
 112  0
                 initialized = true;
 113  0
         }
 114  
 
 115  
         /**
 116  
          * キャッシュ情報等を破棄し、インスタンスを未初期化状態に戻します。
 117  
          * 
 118  
          */
 119  
         public void dispose() {
 120  0
                 this.converters.clear();
 121  0
                 super.clear();
 122  0
                 initialized = false;
 123  0
         }
 124  
 
 125  
         /**
 126  
          * {@inheritDoc}
 127  
          */
 128  
         @Override
 129  
         public Converter getConverter(final Class<?> parameterType,
 130  
                         final Class<?> objectType) {
 131  0
                 initialize();
 132  0
                 return super.getConverter(parameterType, objectType);
 133  
         }
 134  
 
 135  
         /**
 136  
          * {@inheritDoc}
 137  
          * <p>
 138  
          * 指定されたパッケージ名、クラス名から導出されるクラスがコンバータだった場合はファクトリにコンバータを登録します。
 139  
          * </p>
 140  
          */
 141  
         public void processClass(final String packageName,
 142  
                         final String shortClassName) {
 143  0
                 if (shortClassName.indexOf('$') != -1) {
 144  0
                         return;
 145  
                 }
 146  0
                 final String className = ClassUtil.concatName(packageName,
 147  
                                 shortClassName);
 148  0
                 if (!namingConvention.isTargetClassName(className)) {
 149  0
                         return;
 150  
                 }
 151  0
                 if (!className.endsWith(namingConvention.getConverterSuffix())) {
 152  0
                         return;
 153  
                 }
 154  0
                 final Class<?> clazz = ClassUtil.forName(className);
 155  0
                 if (namingConvention.isSkipClass(clazz)) {
 156  0
                         return;
 157  
                 }
 158  0
                 if ((clazz.getModifiers() & Modifier.ABSTRACT) != 0) {
 159  0
                         return;
 160  
                 }
 161  0
                 if (!Converter.class.isAssignableFrom(clazz)) {
 162  0
                         return;
 163  
                 }
 164  0
                 final Converter converter = (Converter) s2Container.getRoot()
 165  
                                 .getComponent(clazz);
 166  0
                 this.converters.add(converter);
 167  0
         }
 168  
 
 169  
 }