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