Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PluginRegistry |
|
| 2.2857142857142856;2.286 |
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.plugin; | |
17 | ||
18 | import static org.seasar.cubby.internal.util.LogMessages.format; | |
19 | ||
20 | import java.util.HashMap; | |
21 | import java.util.HashSet; | |
22 | import java.util.Map; | |
23 | import java.util.Set; | |
24 | ||
25 | import org.seasar.cubby.spi.Provider; | |
26 | import org.slf4j.Logger; | |
27 | import org.slf4j.LoggerFactory; | |
28 | ||
29 | /** | |
30 | * プラグインのレジストリです。 | |
31 | * | |
32 | * @author baba | |
33 | */ | |
34 | public class PluginRegistry { | |
35 | ||
36 | /** ロガー。 */ | |
37 | 1 | private static final Logger logger = LoggerFactory |
38 | .getLogger(PluginRegistry.class); | |
39 | ||
40 | /** シングルトンインスタンス。 */ | |
41 | 1 | private static final PluginRegistry INSTANCE = new PluginRegistry(); |
42 | ||
43 | /** プラグインのセット。 */ | |
44 | 1 | private final Set<Plugin> plugins = new HashSet<Plugin>(); |
45 | ||
46 | /** サービスとそのサービスを提供するプラグインとのマッピング。 */ | |
47 | 1 | private final Map<Class<? extends Provider>, Plugin> serviceToPlugins = new HashMap<Class<? extends Provider>, Plugin>(); |
48 | ||
49 | /** | |
50 | * インスタンス化を禁止するためのコンストラクタ。 | |
51 | */ | |
52 | 1 | private PluginRegistry() { |
53 | 1 | } |
54 | ||
55 | /** | |
56 | * {@link PluginRegistry} のシングルトンを取得します。 | |
57 | * | |
58 | * @return {@link PluginRegistry} のシングルトン | |
59 | */ | |
60 | public static PluginRegistry getInstance() { | |
61 | 323 | return INSTANCE; |
62 | } | |
63 | ||
64 | /** | |
65 | * 登録されたプラグインをクリアします。 | |
66 | */ | |
67 | public synchronized void clear() { | |
68 | 142 | this.plugins.clear(); |
69 | 142 | this.serviceToPlugins.clear(); |
70 | 142 | } |
71 | ||
72 | /** | |
73 | * 指定されたプラグインを登録します。 | |
74 | * | |
75 | * @param plugin | |
76 | * プラグイン | |
77 | */ | |
78 | public synchronized void register(final Plugin plugin) { | |
79 | 137 | this.plugins.add(plugin); |
80 | 137 | for (final Class<? extends Provider> service : plugin |
81 | .getSupportedServices()) { | |
82 | 230 | this.serviceToPlugins.put(service, plugin); |
83 | 230 | if (logger.isInfoEnabled()) { |
84 | 230 | logger.info(format("ICUB0001", plugin, service)); |
85 | } | |
86 | } | |
87 | 137 | } |
88 | ||
89 | /** | |
90 | * 指定されたサービスのプロバイダを取得します。 | |
91 | * | |
92 | * @param <S> | |
93 | * サービスの型 | |
94 | * @param service | |
95 | * サービス | |
96 | * @return プロバイダ | |
97 | * @throws IllegalArgumentException | |
98 | * <code>service</code> を提供するプラグインが登録されていない場合 | |
99 | * @throws IllegalStateException | |
100 | * {@link Plugin#getSupportedServices()} が <code>service</code> | |
101 | * を返すプラグインから取得したサービスプロバイダが <code>null</code> の場合 | |
102 | */ | |
103 | public <S extends Provider> S getProvider(final Class<S> service) { | |
104 | 167 | final Plugin plugin = this.serviceToPlugins.get(service); |
105 | 167 | if (plugin == null) { |
106 | 3 | throw new IllegalArgumentException(format("ECUB0054", service)); |
107 | } | |
108 | 164 | final S provider = service.cast(plugin.getProvider(service)); |
109 | 164 | if (provider == null) { |
110 | 0 | throw new IllegalStateException(format("ECUB0053", plugin, service)); |
111 | } | |
112 | 164 | return provider; |
113 | } | |
114 | ||
115 | /** | |
116 | * 登録されているプラグインのセットを取得します。 | |
117 | * | |
118 | * @return 登録されているプラグインのセット | |
119 | */ | |
120 | public Set<Plugin> getPlugins() { | |
121 | 6 | return plugins; |
122 | } | |
123 | ||
124 | /** | |
125 | * 登録されたプラグインから指定された型のプラグインを取得します。 | |
126 | * <p> | |
127 | * 該当するプラグインが登録されていない場合は <code>null</code> を返します。 | |
128 | * </p> | |
129 | * | |
130 | * @param <T> | |
131 | * プラグインの型 | |
132 | * @param pluginType | |
133 | * プラグインの型 | |
134 | * @return 指定された型のプラグイン | |
135 | */ | |
136 | public <T extends Plugin> T getPlugin(final Class<T> pluginType) { | |
137 | 2 | for (final Plugin plugin : plugins) { |
138 | 1 | if (plugin.getClass().equals(pluginType)) { |
139 | 1 | return pluginType.cast(plugin); |
140 | } | |
141 | } | |
142 | 1 | return null; |
143 | } | |
144 | ||
145 | } |