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  	 * @throws Exception
62  	 *             プラグインの初期化や準備に失敗した場合
63  	 */
64  	public void init(final ServletContext servletContext) throws Exception {
65  		final Collection<Plugin> plugins = loadPlugins();
66  		initializePlugins(servletContext, plugins);
67  		registerPlugins(pluginRegistry, plugins);
68  		readyPlugins(plugins);
69  	}
70  
71  	/**
72  	 * プラグインの破棄を行います。
73  	 */
74  	public void destroy() {
75  		final Set<? extends Plugin> plugins = pluginRegistry.getPlugins();
76  		destroyPlugins(plugins);
77  		pluginRegistry.clear();
78  	}
79  
80  	/**
81  	 * プラグインをロードします。
82  	 * 
83  	 * @return ロードしたプラグインのコレクション
84  	 */
85  	protected Collection<Plugin> loadPlugins() {
86  		final Set<Plugin> plugins = new HashSet<Plugin>();
87  		for (final Plugin plugin : ServiceLoader.load(Plugin.class)) {
88  			plugins.add(plugin);
89  		}
90  		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 		for (final Plugin plugin : plugins) {
106 			if (logger.isDebugEnabled()) {
107 				logger.debug(format("DCUB0019", plugin));
108 			}
109 			plugin.initialize(servletContext);
110 			if (logger.isInfoEnabled()) {
111 				logger.info(format("ICUB0002", plugin));
112 			}
113 		}
114 	}
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 		for (final Plugin plugin : plugins) {
127 			if (logger.isDebugEnabled()) {
128 				logger.debug(format("DCUB0020", plugin));
129 			}
130 			pluginRegistry.register(plugin);
131 			if (logger.isInfoEnabled()) {
132 				logger.info(format("ICUB0003", plugin));
133 			}
134 		}
135 	}
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 		for (final Plugin plugin : plugins) {
148 			if (logger.isDebugEnabled()) {
149 				logger.debug(format("DCUB0021", plugin));
150 			}
151 			plugin.ready();
152 			if (logger.isInfoEnabled()) {
153 				logger.info(format("ICUB0004", plugin));
154 			}
155 		}
156 	}
157 
158 	/**
159 	 * プラグインを破棄します。
160 	 * 
161 	 * @param plugins
162 	 *            プラグインのコレクション
163 	 */
164 	protected void destroyPlugins(final Set<? extends Plugin> plugins) {
165 		for (final Plugin plugin : plugins) {
166 			if (logger.isDebugEnabled()) {
167 				logger.debug(format("DCUB0022", plugin));
168 			}
169 			plugin.destroy();
170 			if (logger.isInfoEnabled()) {
171 				logger.info(format("ICUB0005", plugin));
172 			}
173 		}
174 	}
175 
176 }