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.runtime;
26
27
28 import java.util.*;
29
30 import org.deri.wsmo4j.choreography.ChoreographyFactoryRI;
31 import org.eclipse.core.runtime.*;
32 import org.eclipse.jface.dialogs.MessageDialog;
33 import org.eclipse.jface.viewers.StructuredSelection;
34 import org.eclipse.jface.wizard.WizardDialog;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.ui.*;
37 import org.wsmo.common.*;
38 import org.wsmo.factory.*;
39 import org.wsmo.validator.WsmlValidator;
40 import org.wsmo.wsml.*;
41 import org.wsmostudio.runtime.cache.WSMOCache;
42 import org.wsmostudio.runtime.io.Utils;
43 import org.wsmostudio.runtime.io.WorkspaceIOManager;
44 import org.wsmostudio.runtime.model.WSMOImplManager;
45
46 /***
47 * The WSMORuntime is a singleton class which represents the heart of the studio's
48 * environment. It instanciates key support objects of the WSMO4J library: WSML parser,
49 * WSML serializer, WSMO factory.
50 * The runtime maintains a dynamic registry for the known WSMO entities and their physical
51 * location in the workspace.
52 *
53 * @author not attributable
54 * @version $Revision: 1392 $ $Date: 2007-12-05 16:47:13 +0200 $
55 */
56
57 public class WSMORuntime {
58
59
60 public static final String DEFAULT_WSML_PARSER_IMPL = "org.deri.wsmo4j.io.parser.wsml.ParserImpl";
61 public static final String DEFAULT_WSML_SERIALIZER_IMPL = "org.deri.wsmo4j.io.serializer.wsml.SerializerImpl";
62
63 public static IRI NFP_STUDIO = null;
64 public static String NS_STUDIO_LOCAL = "wsmostudio";
65
66 private static WSMORuntime wsmoRuntime = null;
67 private static WSMOCache wsmoCache = null;
68 private WsmoFactory wsmoFactory;
69 private LogicalExpressionFactory leFactory;
70 private DataFactory dataFactory;
71 private Parser wsmlParser;
72 private Serializer wsmlSerializer;
73
74 private WsmlValidator wsmlValidator;
75
76 private WorkspaceLocator wsLocator;
77
78 private static WorkspaceIOManager ioManager;
79
80 private HashMap<TopEntity,IPath> wsmo2file;
81
82 private static void createRuntime() {
83 if (wsmoRuntime != null) {
84 return;
85 }
86 wsmoRuntime = new WSMORuntime();
87 wsmoRuntime.initRuntime();
88 ioManager = new WorkspaceIOManager(wsmoRuntime);
89 wsmoRuntime.wsLocator = new WorkspaceLocator();
90 Factory.getLocatorManager().addLocator(wsmoRuntime.wsLocator);
91 NFP_STUDIO = wsmoRuntime.getWsmoFactory().createIRI("http://www.wsmostudio.org#version");
92 }
93
94 public static WSMORuntime getRuntime() {
95 if (wsmoRuntime == null) {
96 createRuntime();
97 }
98 return wsmoRuntime;
99 }
100
101 public static void forceEarlyInit() {
102 createRuntime();
103 WsmoImageRegistry.declareStudioImages();
104 }
105
106 public WsmoFactory getWsmoFactory() {
107 return wsmoFactory;
108 }
109
110 public Parser getWsmlParser() {
111 return wsmlParser;
112 }
113
114 public WsmlValidator getWsmlValidator() {
115 return wsmlValidator;
116 }
117
118 public Serializer getWsmlSerializer() {
119 return wsmlSerializer;
120 }
121
122 public LogicalExpressionFactory getLogExprFactory() {
123 return leFactory;
124 }
125
126 public DataFactory getDataFactory() {
127 return dataFactory;
128 }
129
130 public void initRuntime() {
131
132 wsmo2file = new HashMap<TopEntity,IPath>();
133 boolean initError = false;
134 Map<String, String> extImplData = WSMOImplManager.getWsmoImplData();
135
136
137 Map<String, Object> factoryProps = null;
138 if (extImplData.containsKey(WSMOImplManager.ATTR_WSMO_FACTORY)) {
139 factoryProps = new HashMap<String, Object>();
140 factoryProps.put(Factory.PROVIDER_CLASS, extImplData.get(WSMOImplManager.ATTR_WSMO_FACTORY));
141 }
142 try {
143 wsmoFactory = Factory.createWsmoFactory(factoryProps);
144 }
145 catch(RuntimeException re) {
146 LogManager.logError(re);
147 initError = true;
148 wsmoFactory = Factory.createWsmoFactory(null);
149 }
150
151
152 Map<String, Object> leFactoryProps = null;
153 if (extImplData.containsKey(WSMOImplManager.ATTR_LE_FACTORY)) {
154 leFactoryProps = new HashMap<String, Object>();
155 leFactoryProps.put(Factory.PROVIDER_CLASS, extImplData.get(WSMOImplManager.ATTR_LE_FACTORY));
156 }
157 try {
158 leFactory = Factory.createLogicalExpressionFactory(leFactoryProps);
159 }
160 catch(RuntimeException re) {
161 LogManager.logError(re);
162 initError = true;
163 leFactory = Factory.createLogicalExpressionFactory(null);
164 }
165
166
167 HashMap<String, Object> parserProps = new HashMap<String, Object>();
168 parserProps.put(Factory.WSMO_FACTORY, wsmoFactory);
169 parserProps.put(Factory.LE_FACTORY, leFactory);
170
171 parserProps.put(Parser.CLEAR_MODEL, Boolean.TRUE);
172
173
174
175 parserProps.put("choreography", new ChoreographyFactoryRI());
176
177 if (extImplData.containsKey(WSMOImplManager.ATTR_WSML_PARSER)) {
178 parserProps.put(Factory.PROVIDER_CLASS, extImplData.get(WSMOImplManager.ATTR_WSML_PARSER));
179 }
180 else {
181 parserProps.put(Factory.PROVIDER_CLASS, DEFAULT_WSML_PARSER_IMPL);
182 }
183 try {
184 wsmlParser = Factory.createParser(parserProps);
185 }
186 catch(RuntimeException re) {
187 LogManager.logError(re);
188 initError = true;
189 parserProps.remove(Factory.PROVIDER_CLASS);
190 wsmlParser = Factory.createParser(parserProps);
191 }
192
193
194 Map<String, Object> serializeProps = new HashMap<String, Object>();
195 if (extImplData.containsKey(WSMOImplManager.ATTR_WSML_SERIALIZER)) {
196 serializeProps.put(Factory.PROVIDER_CLASS, extImplData.get(WSMOImplManager.ATTR_WSML_SERIALIZER));
197 }
198 else {
199 serializeProps.put(Factory.PROVIDER_CLASS, DEFAULT_WSML_SERIALIZER_IMPL);
200 }
201 try {
202 wsmlSerializer = Factory.createSerializer(serializeProps);
203 }
204 catch(RuntimeException re) {
205 LogManager.logError(re);
206 initError = true;
207 wsmlSerializer = Factory.createSerializer(null);
208 }
209
210 dataFactory = Factory.createDataFactory(null);
211
212 reinitWsmlValidator();
213
214 if (true == initError) {
215 MessageDialog.openError(Display.getCurrent().getActiveShell(),
216 "WSMO-API Implementation Error",
217 "An error appeared during initialization (for details see log file)."
218 +"\nProblematic components might be replaced with defaults:"
219 +"\nWsmoFactory - " + wsmoFactory.getClass().getName()
220 +"\nLogicalExpressionFactory - " + leFactory.getClass().getName()
221 +"\nWSML Parser - " + wsmlParser.getClass().getName()
222 +"\nWSML Serializer - " + wsmlSerializer.getClass().getName());
223 }
224
225 LogManager.addLogListener(new ILogListener() {
226 public void logging(IStatus status, String plugin) {
227 String logType = null;
228 switch (status.getSeverity()) {
229 case IStatus.ERROR:
230 logType = "Error: ";
231 break;
232 case IStatus.WARNING:
233 logType = "Warning: ";
234 break;
235 default:
236 logType = "Message: ";
237 }
238 System.out.println(logType + status.getMessage());
239 if (status.getException() != null) {
240 status.getException().printStackTrace(System.out);
241 }
242 }
243 });
244 }
245
246 public void reinitWsmlValidator() {
247 Map<String, Object> props = new HashMap<String, Object>();
248 boolean validateImports =
249 RuntimePlugin.getDefault().getPreferenceStore().getBoolean(
250 WorkspaceIOManager.VALIDATE_IMPORTS_PROPERTY);
251 props.put(WsmlValidator.VALIDATE_IMPORTS,
252 Boolean.valueOf(validateImports));
253 props.put(Factory.LE_FACTORY, this.leFactory);
254
255 wsmlValidator = Factory.createWsmlValidator(props);
256 }
257
258
259 public void registerEntity(TopEntity entity, IPath filePath) {
260 wsmo2file.put(entity, filePath);
261 }
262
263 public void unregisterEntity(TopEntity entity) {
264 wsmo2file.remove(entity);
265 }
266
267 /***
268 * Searches for an opened for editing entity by its file source path
269 * @param srcFile
270 * @return the corresponding registered entity or null if not registered
271 */
272 public TopEntity findRegisteredEntity(IPath srcFile) {
273 for(TopEntity tEntity : wsmo2file.keySet()) {
274 if (srcFile.equals(wsmo2file.get(tEntity))) {
275 return tEntity;
276 }
277 }
278 return null;
279 }
280
281 public void doUpdateEntity(TopEntity dirty) throws Exception {
282 IPath filePath = (IPath)wsmo2file.get(dirty);
283 if (filePath == null) {
284 SaveResourceWizard wizard = new SaveResourceWizard(dirty);
285 wizard.init(PlatformUI.getWorkbench(), new StructuredSelection());
286 WizardDialog dialog = new WizardDialog(
287 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
288 wizard);
289 dialog.open();
290 if (wsmo2file.get(dirty) == null) {
291 throw new Exception("The resource ("
292 + dirty.getIdentifier().toString()
293 + ") is not synchronized with the workspace resources.");
294 }
295 return;
296 }
297 Utils.updateStudioNFP(dirty);
298 ioManager.saveContent(dirty, filePath);
299 }
300
301 public static WSMOCache getCache() {
302 if (wsmoCache == null) {
303 if (wsmoRuntime == null) {
304 createRuntime();
305 }
306 wsmoCache = new WSMOCache(wsmoRuntime.getWsmoFactory());
307 }
308 return wsmoCache;
309 }
310
311 public static WorkspaceIOManager getIOManager() {
312 return ioManager;
313 }
314
315 }
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392