1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /***
19 * <p>Title: WSMO Studio</p>
20 * <p>Description: Semantic Web Service Editor</p>
21 * <p>Copyright: Copyright (c) 2004-2006</p>
22 * <p>Company: OntoText Lab. / SIRMA </p>
23 */
24
25 package org.wsmostudio.repository;
26
27 import java.util.*;
28
29 import org.eclipse.core.runtime.IConfigurationElement;
30 import org.eclipse.core.runtime.IExtension;
31 import org.eclipse.core.runtime.IExtensionPoint;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.core.runtime.Preferences;
34 import org.eclipse.jface.resource.ImageDescriptor;
35 import org.eclipse.swt.graphics.Image;
36 import org.osgi.framework.Bundle;
37 import org.wsmo.datastore.WsmoRepository;
38 import org.wsmostudio.runtime.LogManager;
39 import org.wsmostudio.runtime.extension.Configurator;
40 import org.wsmostudio.runtime.extension.Initialisable;
41
42 /***
43 * A manager class responsible for working with all registered extensions of the
44 * Repository extension point (<code>org.wsmostudio.repository.Repository</code>).
45 * The class instanciates and configures the various repository extensions.
46 *
47 * @author not attributable
48 * @version $Revision: 855 $ $Date: 2006-07-11 19:06:16 +0300 $
49 */
50
51 public class ExtensionManager {
52
53 public static final String REPOSITIRY_EXT_ID = "org.wsmostudio.repository.Repository";
54
55 public static final String REP_CONFIG_ELEMENT = "repository";
56 public static final String CONF_ATTR_IMPL_NAME = "class";
57 public static final String CONF_ATTR_TYPE_NAME = "rtype";
58 public static final String CONF_ATTR_CONFIGURATOR = "configurator";
59 public static final String CONF_ATTR_MULTIPLE = "allowsMultiple";
60 public static final String CONF_ATTR_ICON = "icon";
61
62 public static final String REP_PROP_PREFIX = "$repository$";
63
64 public static Map<WsmoRepository, Configurator> loadRepositories() {
65
66 Map<WsmoRepository, Configurator> container = new HashMap<WsmoRepository, Configurator>();
67 Preferences pluginPrefs = RepositoryPlugin.getDefault().getPluginPreferences();
68 String[] props = pluginPrefs.propertyNames();
69 for (int i = 0; i < props.length; i++) {
70 if (false == props[i].startsWith(REP_PROP_PREFIX)) {
71 continue;
72 }
73 String repoType = pluginPrefs.getString(props[i]);
74 try {
75 WsmoRepository repo = createRepository(
76 props[i].substring(REP_PROP_PREFIX.length()),
77 repoType);
78 Configurator conf = getConfiguratorFor(repoType);
79 container.put(repo, conf);
80 }
81 catch(Exception ex) {
82 LogManager.logError(ex);
83 if (findConfigData(repoType) == null) {
84 pluginPrefs.setToDefault(props[i]);
85 }
86 continue;
87 }
88 }
89 return container;
90 }
91
92 public static String getRepositoryType(WsmoRepository repo) {
93
94 Preferences pluginPrefs = RepositoryPlugin.getDefault().getPluginPreferences();
95 String[] props = pluginPrefs.propertyNames();
96 for (int i = 0; i < props.length; i++) {
97 if (false == props[i].startsWith(REP_PROP_PREFIX)) {
98 continue;
99 }
100 if (repo.getDescription().equals(
101 props[i].substring(REP_PROP_PREFIX.length()))) {
102 return pluginPrefs.getString(props[i]);
103 }
104 }
105 return null;
106 }
107
108 public static Set<String> collectExistingRepositoriesTypes() {
109
110 Set<String> container = new HashSet<String>();
111 Preferences pluginPrefs = RepositoryPlugin.getDefault().getPluginPreferences();
112 String[] props = pluginPrefs.propertyNames();
113 for (int i = 0; i < props.length; i++) {
114 if (false == props[i].startsWith(REP_PROP_PREFIX)) {
115 continue;
116 }
117 String repoType = pluginPrefs.getString(props[i]);
118 container.add(repoType);
119 }
120 return container;
121 }
122
123 public static boolean allowsMultiInstances(String repoType) throws Exception {
124 IConfigurationElement conf = findConfigData(repoType);
125 if (conf == null) {
126 throw new Exception("Extension '" + repoType + "' not found!");
127 }
128 String multipleStat = conf.getAttribute(CONF_ATTR_MULTIPLE);
129 return (multipleStat == null
130 || false == multipleStat.equals("false"));
131 }
132
133 public static IConfigurationElement findConfigData(String repositoryType) {
134 IExtensionPoint iExtPoint = Platform
135 .getExtensionRegistry()
136 .getExtensionPoint(
137 REPOSITIRY_EXT_ID);
138 IExtension[] exts = iExtPoint.getExtensions();
139 for (int i = 0; i < exts.length; i++) {
140 IConfigurationElement[] confs = exts[i].getConfigurationElements();
141 for (int j = 0; j < confs.length; j++) {
142 if (!confs[j].getName().equals(REP_CONFIG_ELEMENT)) {
143 continue;
144 }
145 if (repositoryType.equals(confs[j].getAttribute(CONF_ATTR_TYPE_NAME))) {
146 return confs[j];
147 }
148 }
149 }
150 return null;
151 }
152
153 public static WsmoRepository createRepository(String name, String type) throws Exception {
154
155 IConfigurationElement conf = findConfigData(type);
156 if (conf == null) {
157 throw new Exception("Extension '" + type + "' not found!");
158 }
159 String repoClass = conf.getAttribute(CONF_ATTR_IMPL_NAME);
160 if (repoClass == null) {
161 throw new Exception("No implementation for '" + type + "' supplied!");
162 }
163 WsmoRepository resultRepository = null;
164 try {
165 resultRepository = (WsmoRepository)conf.createExecutableExtension(CONF_ATTR_IMPL_NAME);
166 if (false == resultRepository instanceof Initialisable) {
167 throw new ClassCastException();
168 }
169 }
170 catch(ClassCastException cce) {
171 throw new Exception("The supplied implementation class '"
172 + resultRepository
173 + "' does not implement 'Repository' or 'Initialisable'!");
174 }
175 catch(Exception ex) {
176 throw new Exception("Unable to create repository '"
177 + repoClass
178 + "'\n"
179 + ex.getMessage());
180 }
181 Preferences pluginPrefs = RepositoryPlugin.getDefault().getPluginPreferences();
182 if (false == type.equals(pluginPrefs.getString(REP_PROP_PREFIX + name))) {
183 pluginPrefs.setValue(REP_PROP_PREFIX + name, type);
184 }
185
186 resultRepository.setDescription(name);
187 return resultRepository;
188 }
189
190 public static boolean configureRepository(WsmoRepository repo, Configurator configurer, boolean forceConfig) throws Exception {
191
192 Map confData = configurer.getConfigurationData(repo, forceConfig);
193 if (confData == null) {
194 return false;
195 }
196 ((Initialisable)repo).initialise(confData);
197 return true;
198 }
199
200 public static Image loadImageForType(String repositoryType) {
201 IConfigurationElement conf = findConfigData(repositoryType);
202 if (conf == null) {
203 return null;
204 }
205 String iconPath = conf.getAttribute(CONF_ATTR_ICON);
206 if (iconPath == null) {
207 return null;
208 }
209 Bundle bundle = Platform.getBundle(conf.getNamespaceIdentifier());
210 ImageDescriptor desc = ImageDescriptor.createFromURL(
211 bundle.getEntry(iconPath));
212 return desc.createImage();
213 }
214
215 public static Configurator getConfiguratorFor(String repositoryType) throws Exception {
216
217 IConfigurationElement conf = findConfigData(repositoryType);
218 if (conf == null) {
219 throw new Exception("Extension '" + repositoryType + "' not found!");
220 }
221 String configClass = conf.getAttribute(CONF_ATTR_CONFIGURATOR);
222 if (configClass == null) {
223 throw new Exception("No configurator for '" + repositoryType + "' supplied!");
224 }
225 Configurator resultConfig = null;
226 try {
227 resultConfig = (Configurator)conf.createExecutableExtension(CONF_ATTR_CONFIGURATOR);
228 }
229 catch(ClassCastException cce) {
230 throw new Exception("The supplied configurator class '"
231 + configClass
232 + "' does not implement 'Configurator'");
233 }
234 catch(Exception ex) {
235 throw new Exception("Unable to create configurator '"
236 + configClass
237 + "'\n"
238 + ex.getMessage());
239 }
240 return resultConfig;
241 }
242
243 public static String[] listConfigTypes() {
244 IExtensionPoint iExtPoint = Platform
245 .getExtensionRegistry()
246 .getExtensionPoint(
247 REPOSITIRY_EXT_ID);
248 IExtension[] exts = iExtPoint.getExtensions();
249 LinkedList<String> buffer = new LinkedList<String>();
250 for (int i = 0; i < exts.length; i++) {
251 IConfigurationElement[] confs = exts[i].getConfigurationElements();
252 for (int j = 0; j < confs.length; j++) {
253 if (!confs[j].getName().equals(REP_CONFIG_ELEMENT)) {
254 continue;
255 }
256 if (confs[j].getAttribute(CONF_ATTR_TYPE_NAME) != null) {
257 buffer.add(confs[j].getAttribute(CONF_ATTR_TYPE_NAME));
258 }
259 }
260 }
261 String[] result = new String[buffer.size()];
262 if (result.length > 0) {
263 System.arraycopy(buffer.toArray(), 0 , result, 0, result.length);
264 }
265 return result;
266 }
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295