Coverage Report - org.seasar.cubby.internal.plugin.PluginManager
 
Classes in this File Line Coverage Branch Coverage Complexity
PluginManager
91%
41/45
61%
16/26
2.625
 
 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  
          * @throws Exception
 62  
          *             プラグインの初期化や準備に失敗した場合
 63  
          */
 64  
         public void init(final ServletContext servletContext) throws Exception {
 65  1
                 final Collection<Plugin> plugins = loadPlugins();
 66  1
                 initializePlugins(servletContext, plugins);
 67  1
                 registerPlugins(pluginRegistry, plugins);
 68  1
                 readyPlugins(plugins);
 69  1
         }
 70  
 
 71  
         /**
 72  
          * プラグインの破棄を行います。
 73  
          */
 74  
         public void destroy() {
 75  1
                 final Set<? extends Plugin> plugins = pluginRegistry.getPlugins();
 76  1
                 destroyPlugins(plugins);
 77  1
                 pluginRegistry.clear();
 78  1
         }
 79  
 
 80  
         /**
 81  
          * プラグインをロードします。
 82  
          * 
 83  
          * @return ロードしたプラグインのコレクション
 84  
          */
 85  
         protected Collection<Plugin> loadPlugins() {
 86  0
                 final Set<Plugin> plugins = new HashSet<Plugin>();
 87  0
                 for (final Plugin plugin : ServiceLoader.load(Plugin.class)) {
 88  0
                         plugins.add(plugin);
 89  
                 }
 90  0
                 return plugins;
 91  
         }
 92  
 
 93  
         /**
 94  
          * プラグインを初期化します。
 95  
          * 
 96  
          * @param servletContext
 97  
          *            呼び出し元が現在実行している {@link ServletContext} への参照
 98  
          * @param plugins
 99  
          *            プラグインのコレクション
 100  
          * @throws Exception
 101  
          *             プラグインの初期化に失敗した場合
 102  
          */
 103  
         protected void initializePlugins(final ServletContext servletContext,
 104  
                         final Collection<Plugin> plugins) throws Exception {
 105  1
                 for (final Plugin plugin : plugins) {
 106  1
                         if (logger.isDebugEnabled()) {
 107  1
                                 logger.debug(format("DCUB0019", plugin));
 108  
                         }
 109  1
                         plugin.initialize(servletContext);
 110  1
                         if (logger.isInfoEnabled()) {
 111  1
                                 logger.info(format("ICUB0002", plugin));
 112  
                         }
 113  
                 }
 114  1
         }
 115  
 
 116  
         /**
 117  
          * プラグインをレジストリに登録します。
 118  
          * 
 119  
          * @param pluginRegistry
 120  
          *            プラグインのレジストリ
 121  
          * @param plugins
 122  
          *            登録するプラグインのコレクション
 123  
          */
 124  
         protected void registerPlugins(final PluginRegistry pluginRegistry,
 125  
                         final Collection<Plugin> plugins) {
 126  1
                 for (final Plugin plugin : plugins) {
 127  1
                         if (logger.isDebugEnabled()) {
 128  1
                                 logger.debug(format("DCUB0020", plugin));
 129  
                         }
 130  1
                         pluginRegistry.register(plugin);
 131  1
                         if (logger.isInfoEnabled()) {
 132  1
                                 logger.info(format("ICUB0003", plugin));
 133  
                         }
 134  
                 }
 135  1
         }
 136  
 
 137  
         /**
 138  
          * プラグインを準備します。
 139  
          * 
 140  
          * @param plugins
 141  
          *            プラグインのコレクション
 142  
          * @throws Exception
 143  
          *             プラグインの準備に失敗した場合
 144  
          */
 145  
         private void readyPlugins(final Collection<Plugin> plugins)
 146  
                         throws Exception {
 147  1
                 for (final Plugin plugin : plugins) {
 148  1
                         if (logger.isDebugEnabled()) {
 149  1
                                 logger.debug(format("DCUB0021", plugin));
 150  
                         }
 151  1
                         plugin.ready();
 152  1
                         if (logger.isInfoEnabled()) {
 153  1
                                 logger.info(format("ICUB0004", plugin));
 154  
                         }
 155  
                 }
 156  1
         }
 157  
 
 158  
         /**
 159  
          * プラグインを破棄します。
 160  
          * 
 161  
          * @param plugins
 162  
          *            プラグインのコレクション
 163  
          */
 164  
         protected void destroyPlugins(final Set<? extends Plugin> plugins) {
 165  1
                 for (final Plugin plugin : plugins) {
 166  1
                         if (logger.isDebugEnabled()) {
 167  1
                                 logger.debug(format("DCUB0022", plugin));
 168  
                         }
 169  1
                         plugin.destroy();
 170  1
                         if (logger.isInfoEnabled()) {
 171  1
                                 logger.info(format("ICUB0005", plugin));
 172  
                         }
 173  
                 }
 174  1
         }
 175  
 
 176  
 }