View Javadoc

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  	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  	public PluginManager(final PluginRegistry pluginRegistry) {
53  		this.pluginRegistry = pluginRegistry;
54  	}
55  
56  	/**
57  	 * プラグインの初期化とレジストリへの登録を行います。
58  	 * 
59  	 * @param servletContext
60  	 *            サーブレットコンテキスト
61  	 */
62  	public void init(final ServletContext servletContext) {
63  		final Collection<Plugin> plugins = loadPlugins();
64  		initializePlugins(servletContext, plugins);
65  		registerPlugins(pluginRegistry, plugins);
66  		readyPlugins(plugins);
67  	}
68  
69  	/**
70  	 * プラグインの破棄を行います。
71  	 */
72  	public void destroy() {
73  		final Set<? extends Plugin> plugins = pluginRegistry.getPlugins();
74  		destroyPlugins(plugins);
75  		pluginRegistry.clear();
76  	}
77  
78  	/**
79  	 * プラグインをロードします。
80  	 * 
81  	 * @return ロードしたプラグインのコレクション
82  	 */
83  	protected Collection<Plugin> loadPlugins() {
84  		final Set<Plugin> plugins = new HashSet<Plugin>();
85  		for (final Plugin plugin : ServiceLoader.load(Plugin.class)) {
86  			plugins.add(plugin);
87  		}
88  		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 		for (final Plugin plugin : plugins) {
102 			if (logger.isDebugEnabled()) {
103 				logger.debug(format("DCUB0019", plugin));
104 			}
105 			plugin.initialize(servletContext);
106 			if (logger.isInfoEnabled()) {
107 				logger.info(format("ICUB0002", plugin));
108 			}
109 		}
110 	}
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 		for (final Plugin plugin : plugins) {
123 			if (logger.isDebugEnabled()) {
124 				logger.debug(format("DCUB0020", plugin));
125 			}
126 			pluginRegistry.register(plugin);
127 			if (logger.isInfoEnabled()) {
128 				logger.info(format("ICUB0003", plugin));
129 			}
130 		}
131 	}
132 
133 	/**
134 	 * プラグインを準備します。
135 	 * 
136 	 * @param plugins
137 	 *            プラグインのコレクション
138 	 */
139 	private void readyPlugins(final Collection<Plugin> plugins) {
140 		for (final Plugin plugin : plugins) {
141 			if (logger.isDebugEnabled()) {
142 				logger.debug(format("DCUB0021", plugin));
143 			}
144 			plugin.ready();
145 			if (logger.isInfoEnabled()) {
146 				logger.info(format("ICUB0004", plugin));
147 			}
148 		}
149 	}
150 
151 	/**
152 	 * プラグインを破棄します。
153 	 * 
154 	 * @param plugins
155 	 *            プラグインのコレクション
156 	 */
157 	protected void destroyPlugins(final Set<? extends Plugin> plugins) {
158 		for (final Plugin plugin : plugins) {
159 			if (logger.isDebugEnabled()) {
160 				logger.debug(format("DCUB0022", plugin));
161 			}
162 			plugin.destroy();
163 			if (logger.isInfoEnabled()) {
164 				logger.info(format("ICUB0005", plugin));
165 			}
166 		}
167 	}
168 
169 }