Coverage Report - org.seasar.cubby.internal.plugin.PluginManager
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginManager
91%
41/45
62%
16/26
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.internal.plugin;
 17  
 
 18  
 import static org.seasar.cubby.internal.util.LogMessages.format;
 19  
 
 20  
 import java.util.Collection;
 21  
 import java.util.HashSet;
 22  
 import java.util.Set;
 23  
 
 24  
 import javax.servlet.ServletContext;
 25  
 
 26  
 import org.seasar.cubby.internal.util.ServiceLoader;
 27  
 import org.seasar.cubby.plugin.Plugin;
 28  
 import org.seasar.cubby.plugin.PluginRegistry;
 29  
 import org.slf4j.Logger;
 30  
 import org.slf4j.LoggerFactory;
 31  
 
 32  
 /**
 33  
  * プラグインマネージャ。
 34  
  * 
 35  
  * @author baba
 36  
  */
 37  
 public class PluginManager {
 38  
 
 39  
         /** ロガー。 */
 40  1
         private static final Logger logger = LoggerFactory
 41  
                         .getLogger(PluginManager.class);
 42  
 
 43  
         /** プラグインレジストリ。 */
 44  
         private final PluginRegistry pluginRegistry;
 45  
 
 46  
         /**
 47  
          * インスタンス化します。
 48  
          * 
 49  
          * @param pluginRegistry
 50  
          *            プラグインレジストリ
 51  
          */
 52  1
         public PluginManager(final PluginRegistry pluginRegistry) {
 53  1
                 this.pluginRegistry = pluginRegistry;
 54  1
         }
 55  
 
 56  
         /**
 57  
          * プラグインの初期化とレジストリへの登録を行います。
 58  
          * 
 59  
          * @param servletContext
 60  
          *            サーブレットコンテキスト
 61  
          */
 62  
         public void init(final ServletContext servletContext) {
 63  1
                 final Collection<Plugin> plugins = loadPlugins();
 64  1
                 initializePlugins(servletContext, plugins);
 65  1
                 registerPlugins(pluginRegistry, plugins);
 66  1
                 readyPlugins(plugins);
 67  1
         }
 68  
 
 69  
         /**
 70  
          * プラグインの破棄を行います。
 71  
          */
 72  
         public void destroy() {
 73  1
                 final Set<? extends Plugin> plugins = pluginRegistry.getPlugins();
 74  1
                 destroyPlugins(plugins);
 75  1
                 pluginRegistry.clear();
 76  1
         }
 77  
 
 78  
         /**
 79  
          * プラグインをロードします。
 80  
          * 
 81  
          * @return ロードしたプラグインのコレクション
 82  
          */
 83  
         protected Collection<Plugin> loadPlugins() {
 84  0
                 final Set<Plugin> plugins = new HashSet<Plugin>();
 85  0
                 for (final Plugin plugin : ServiceLoader.load(Plugin.class)) {
 86  0
                         plugins.add(plugin);
 87  
                 }
 88  0
                 return plugins;
 89  
         }
 90  
 
 91  
         /**
 92  
          * プラグインを初期化します。
 93  
          * 
 94  
          * @param servletContext
 95  
          *            呼び出し元が現在実行している {@link ServletContext} への参照
 96  
          * @param plugins
 97  
          *            プラグインのコレクション
 98  
          */
 99  
         protected void initializePlugins(final ServletContext servletContext,
 100  
                         final Collection<Plugin> plugins) {
 101  1
                 for (final Plugin plugin : plugins) {
 102  1
                         if (logger.isDebugEnabled()) {
 103  1
                                 logger.debug(format("DCUB0019", plugin));
 104  
                         }
 105  1
                         plugin.initialize(servletContext);
 106  1
                         if (logger.isInfoEnabled()) {
 107  1
                                 logger.info(format("ICUB0002", plugin));
 108  
                         }
 109  
                 }
 110  1
         }
 111  
 
 112  
         /**
 113  
          * プラグインをレジストリに登録します。
 114  
          * 
 115  
          * @param pluginRegistry
 116  
          *            プラグインのレジストリ
 117  
          * @param plugins
 118  
          *            登録するプラグインのコレクション
 119  
          */
 120  
         protected void registerPlugins(final PluginRegistry pluginRegistry,
 121  
                         final Collection<Plugin> plugins) {
 122  1
                 for (final Plugin plugin : plugins) {
 123  1
                         if (logger.isDebugEnabled()) {
 124  1
                                 logger.debug(format("DCUB0020", plugin));
 125  
                         }
 126  1
                         pluginRegistry.register(plugin);
 127  1
                         if (logger.isInfoEnabled()) {
 128  1
                                 logger.info(format("ICUB0003", plugin));
 129  
                         }
 130  
                 }
 131  1
         }
 132  
 
 133  
         /**
 134  
          * プラグインを準備します。
 135  
          * 
 136  
          * @param plugins
 137  
          *            プラグインのコレクション
 138  
          */
 139  
         private void readyPlugins(final Collection<Plugin> plugins) {
 140  1
                 for (final Plugin plugin : plugins) {
 141  1
                         if (logger.isDebugEnabled()) {
 142  1
                                 logger.debug(format("DCUB0021", plugin));
 143  
                         }
 144  1
                         plugin.ready();
 145  1
                         if (logger.isInfoEnabled()) {
 146  1
                                 logger.info(format("ICUB0004", plugin));
 147  
                         }
 148  
                 }
 149  1
         }
 150  
 
 151  
         /**
 152  
          * プラグインを破棄します。
 153  
          * 
 154  
          * @param plugins
 155  
          *            プラグインのコレクション
 156  
          */
 157  
         protected void destroyPlugins(final Set<? extends Plugin> plugins) {
 158  1
                 for (final Plugin plugin : plugins) {
 159  1
                         if (logger.isDebugEnabled()) {
 160  1
                                 logger.debug(format("DCUB0022", plugin));
 161  
                         }
 162  1
                         plugin.destroy();
 163  1
                         if (logger.isInfoEnabled()) {
 164  1
                                 logger.info(format("ICUB0005", plugin));
 165  
                         }
 166  
                 }
 167  1
         }
 168  
 
 169  
 }