Coverage Report - org.seasar.cubby.plugins.s2.spi.S2PathResolverProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
S2PathResolverProvider
0%
0/35
0%
0/12
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.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.seasar.cubby.plugins.s2.detector.ClassDetector;
 23  
 import org.seasar.cubby.plugins.s2.detector.DetectClassProcessor;
 24  
 import org.seasar.cubby.routing.PathResolver;
 25  
 import org.seasar.cubby.spi.PathResolverProvider;
 26  
 import org.seasar.framework.convention.NamingConvention;
 27  
 import org.seasar.framework.util.ClassUtil;
 28  
 import org.seasar.framework.util.Disposable;
 29  
 import org.seasar.framework.util.DisposableUtil;
 30  
 
 31  
 /**
 32  
  * クラスパスを走査しアクションクラスを登録します。
 33  
  * 
 34  
  * @author baba
 35  
  * @since 2.0.0
 36  
  */
 37  0
 public class S2PathResolverProvider implements PathResolverProvider,
 38  
                 DetectClassProcessor, Disposable {
 39  
 
 40  
         public static final String pathResolver_BINDING = "bindingType=must";
 41  
 
 42  
         public static final String namingConvention_BINDING = "bindingType=must";
 43  
 
 44  
         public static final String classDetector_BINDING = "bindingType=must";
 45  
 
 46  
         /** パスに対応するアクションメソッドを解決するためのクラス。 */
 47  
         private PathResolver pathResolver;
 48  
 
 49  
         /** 命名規約。 */
 50  
         private NamingConvention namingConvention;
 51  
 
 52  
         /** クラスパスを走査してクラスを検出するクラス。 */
 53  
         public ClassDetector classDetector;
 54  
 
 55  
         /** インスタンスが初期化済みであることを示します。 */
 56  
         private boolean initialized;
 57  
 
 58  
         /** アクションクラスのリスト。 */
 59  0
         private final List<Class<?>> actionClasses = new ArrayList<Class<?>>();
 60  
 
 61  
         /**
 62  
          * パスに対応するアクションメソッドを解決するためのクラスを設定します。
 63  
          * 
 64  
          * @param pathResolver
 65  
          *            パスに対応するアクションメソッドを解決するためのクラス
 66  
          * 
 67  
          */
 68  
         public void setPathResolver(final PathResolver pathResolver) {
 69  0
                 this.pathResolver = pathResolver;
 70  0
         }
 71  
 
 72  
         /**
 73  
          * クラスパスを走査してクラスを検出するクラスを設定します。
 74  
          * 
 75  
          * @param classDetector
 76  
          *            クラスパスを走査してクラスを検出するクラス
 77  
          */
 78  
         public void setClassDetector(final ClassDetector classDetector) {
 79  0
                 this.classDetector = classDetector;
 80  0
         }
 81  
 
 82  
         /**
 83  
          * 命名規約を設定します。
 84  
          * 
 85  
          * @param namingConvention
 86  
          *            命名規約
 87  
          */
 88  
         public void setNamingConvention(final NamingConvention namingConvention) {
 89  0
                 this.namingConvention = namingConvention;
 90  0
         }
 91  
 
 92  
         /**
 93  
          * 初期化します。
 94  
          */
 95  
         public void initialize() {
 96  0
                 if (initialized) {
 97  0
                         return;
 98  
                 }
 99  0
                 classDetector.detect();
 100  0
                 pathResolver.addAll(actionClasses);
 101  
 
 102  0
                 DisposableUtil.add(this);
 103  0
                 initialized = true;
 104  0
         }
 105  
 
 106  
         /**
 107  
          * {@inheritDoc}
 108  
          */
 109  
         public void dispose() {
 110  0
                 actionClasses.clear();
 111  0
                 pathResolver.clear();
 112  0
                 initialized = false;
 113  0
         }
 114  
 
 115  
         /**
 116  
          * {@inheritDoc}
 117  
          * <p>
 118  
          * 指定されたパッケージ名、クラス名から導出されるクラスがアクションクラスだった場合はルーティングを登録します。
 119  
          * </p>
 120  
          */
 121  
         public void processClass(final String packageName,
 122  
                         final String shortClassName) {
 123  0
                 if (shortClassName.indexOf('$') != -1) {
 124  0
                         return;
 125  
                 }
 126  0
                 final String className = ClassUtil.concatName(packageName,
 127  
                                 shortClassName);
 128  0
                 if (!namingConvention.isTargetClassName(className)) {
 129  0
                         return;
 130  
                 }
 131  0
                 if (!className.endsWith(namingConvention.getActionSuffix())) {
 132  0
                         return;
 133  
                 }
 134  0
                 final Class<?> clazz = ClassUtil.forName(className);
 135  0
                 if (namingConvention.isSkipClass(clazz)) {
 136  0
                         return;
 137  
                 }
 138  0
                 if (Modifier.isAbstract(clazz.getModifiers())) {
 139  0
                         return;
 140  
                 }
 141  0
                 actionClasses.add(clazz);
 142  0
         }
 143  
 
 144  
         /**
 145  
          * {@inheritDoc}
 146  
          */
 147  
         public PathResolver getPathResolver() {
 148  0
                 initialize();
 149  0
                 return pathResolver;
 150  
         }
 151  
 
 152  
 }