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.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      // the default implementations for the Studio, NOT for the wsmo4j
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         // Custom WSMOFactory implementation initialization
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); //reset to default
149         }
150 
151         // Custom LogicalExpressionFactory implementation initialization
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); //reset to default
164         }
165         
166         // Custom WSML parser implementation initialization
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         // this property is needed for a choreography aware parser implementation
174         // the rest will irnore it
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);//reset to default
191         }
192 
193         // Custom WSML serializer implementation initialization
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  * $Log$
319  * Revision 1.31  2007/04/17 14:34:09  alex_simov
320  * migration to the latest wsmo4j (java 5)
321  *
322  * Revision 1.30  2007/02/13 15:50:12  alex_simov
323  * fixes
324  *
325  * Revision 1.29  2006/12/14 17:25:19  alex_simov
326  * On save, WSMO entities are marked (by a reserved nfp) with the current
327  * version of WSMO Studio
328  *
329  * Revision 1.28  2006/11/20 17:11:06  alex_simov
330  * bugfix[159807]: On loading entities the wsml parser cleans-up the previous
331  * in-memory definition (if such is presented)
332  *
333  * Revision 1.27  2006/11/02 17:14:18  alex_simov
334  * 1) rfe [1588567]: Workspace's lightweight WSML parser registered as a Locator
335  * in the wsmo4j runtime environment
336  * 2) imported ontologies validation disabled in the wsmo4j's WSML validator
337  *
338  * Revision 1.26  2006/02/27 12:25:13  alex_simov
339  * the default wsmo-api implementation for the studio is now set to be
340  * choreography enabled
341  *
342  * Revision 1.25  2006/01/13 12:45:08  alex_simov
343  * wsmo4j refactoring: constants moved to Factory class
344  *
345  * Revision 1.24  2006/01/09 12:51:12  alex_simov
346  * Copyright message in header updated
347  *
348  * Revision 1.23  2005/12/21 09:52:28  alex_simov
349  * Choreography aware parser set as wsmo implementation extension
350  *
351  * Revision 1.22  2005/12/07 15:36:40  alex_simov
352  * wsml validator enabled to mark invalid resources
353  *
354  * Revision 1.21  2005/12/06 14:20:51  alex_simov
355  * imports fix
356  *
357  * Revision 1.20  2005/12/06 12:23:15  alex_simov
358  * A 'Save as' functionality added for resources for which the link
359  * to the workspace is missing (not a normal case)
360  *
361  * Revision 1.19  2005/11/25 14:22:48  alex_simov
362  * runtime code refactoring
363  *
364  * Revision 1.18  2005/11/09 16:11:06  alex_simov
365  * no message
366  *
367  * Revision 1.17  2005/11/04 13:37:49  alex_simov
368  * bugfix : invalid type cast in doUpdateEntity() hid showing error message on
369  * failure
370  *
371  * Revision 1.16  2005/09/19 15:09:48  alex_simov
372  * wsmo4j DataFactory support added
373  *
374  * Revision 1.15  2005/09/16 14:25:11  alex_simov
375  * Identifier.asString() removed, use Object.toString() instead
376  *
377  * Revision 1.14  2005/09/08 16:46:24  alex_simov
378  * Migrating to Java 1.5
379  *
380  * Revision 1.13  2005/08/11 07:59:36  alex_simov
381  * wsmo-api implementation extension point
382  *
383  * Revision 1.12  2005/07/28 07:36:23  alex_simov
384  * unregisterEntity() added
385  *
386  * Revision 1.11  2005/07/25 12:59:19  alex_simov
387  * WSML serializer now accepts an array of TopEntities
388  *
389  * Revision 1.10  2005/07/21 14:44:40  alex_simov
390  * added javadoc: class description, footer
391  *
392  */