View Javadoc

1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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; // not a repository extension
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);//repoClass);
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; // not a repository extension
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  * $Log$
271  * Revision 1.13  2006/07/11 16:02:42  alex_simov
272  * depricated API usage removed
273  *
274  * Revision 1.12  2006/02/23 15:55:04  alex_simov
275  * getRepositoryType()  added allowing repository type determination
276  * having concrete instance
277  *
278  * Revision 1.11  2006/01/09 12:51:11  alex_simov
279  * Copyright message in header updated
280  *
281  * Revision 1.10  2005/12/01 14:09:00  alex_simov
282  * repository extension point update.
283  * Extensions can supply custom image icons for Rep.Explorer
284  * and control the number of instances of a certain repository
285  *
286  * Revision 1.9  2005/11/30 14:55:00  alex_simov
287  * code refactoring
288  *
289  * Revision 1.8  2005/09/08 16:46:23  alex_simov
290  * Migrating to Java 1.5
291  *
292  * Revision 1.7  2005/07/21 11:46:31  alex_simov
293  * added javadoc: class description, footer
294  *
295  */