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