View Javadoc

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