Coverage Report - org.seasar.cubby.plugins.BinderPlugin
 
Classes in this File Line Coverage Branch Coverage Complexity
BinderPlugin
100%
6/6
N/A
1
BinderPlugin$1
N/A
N/A
1
BinderPlugin$Binder
100%
6/6
N/A
1
 
 1  
 /*
 2  
  * Copyright 2004-2010 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  
 
 17  
 package org.seasar.cubby.plugins;
 18  
 
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 
 23  
 import org.seasar.cubby.internal.util.ServiceLoader;
 24  
 import org.seasar.cubby.plugin.AbstractPlugin;
 25  
 import org.seasar.cubby.spi.Provider;
 26  
 
 27  
 /**
 28  
  * プログラムからサービスを登録できるプラグインです。
 29  
  * <p>
 30  
  * プログラムからサービスとプロバイダをひもづけたい場合に使用してください。
 31  
  * </p>
 32  
  * 
 33  
  * @author baba
 34  
  */
 35  376
 public class BinderPlugin extends AbstractPlugin {
 36  
 
 37  
         /** プロバイダ。 */
 38  139
         private final Map<Class<? extends Provider>, Provider> providers = new HashMap<Class<? extends Provider>, Provider>();
 39  
 
 40  
         /**
 41  
          * {@inheritDoc}
 42  
          */
 43  
         @Override
 44  
         public <S extends Provider> S getProvider(final Class<S> service) {
 45  164
                 return service.cast(providers.get(service));
 46  
         }
 47  
 
 48  
         /**
 49  
          * {@inheritDoc}
 50  
          */
 51  
         @Override
 52  
         public Set<Class<? extends Provider>> getSupportedServices() {
 53  139
                 return providers.keySet();
 54  
         }
 55  
 
 56  
         /**
 57  
          * {@link ServiceLoader} からのインスタンス取得を置換するため、指定されたサービスのバインダーを返します。
 58  
          * <p>
 59  
          * テストで使用することを想定しています。
 60  
          * </p>
 61  
          * 
 62  
          * @param <S>
 63  
          *            サービスの型
 64  
          * @param service
 65  
          *            サービス
 66  
          * @return バインダー
 67  
          */
 68  
         public <S extends Provider> Binder<S> bind(final Class<S> service) {
 69  237
                 return new Binder<S>(service);
 70  
         }
 71  
 
 72  
         /**
 73  
          * サービスをプロバイダに紐づけるためのクラスです。
 74  
          * 
 75  
          * @author baba
 76  
          * 
 77  
          * @param <S>
 78  
          *            サービスの型
 79  
          */
 80  237
         public class Binder<S extends Provider> {
 81  
 
 82  
                 /** サービス。 */
 83  
                 private final Class<S> service;
 84  
 
 85  
                 /**
 86  
                  * 指定されたサービスのバインダーを生成します。
 87  
                  * 
 88  
                  * @param service
 89  
                  *            サービス
 90  
                  */
 91  237
                 private Binder(final Class<S> service) {
 92  237
                         this.service = service;
 93  237
                 }
 94  
 
 95  
                 /**
 96  
                  * サービスを特定のインスタンスにバインドします。
 97  
                  * 
 98  
                  * @param instance
 99  
                  *            インスタンス
 100  
                  */
 101  
                 public void toInstance(final S instance) {
 102  237
                         providers.put(service, instance);
 103  237
                 }
 104  
 
 105  
         }
 106  
 
 107  
 }