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