1 package com.ontotext.ordi.wsmo4rdf.remote.server;
2
3 import java.util.LinkedList;
4 import java.util.List;
5
6 import javax.xml.parsers.ParserConfigurationException;
7
8 import org.apache.log4j.Logger;
9 import org.omwg.ontology.Ontology;
10 import org.openrdf.query.MalformedQueryException;
11 import org.wsmo.common.IRI;
12 import org.wsmo.common.exception.SynchronisationException;
13 import org.wsmo.mediator.Mediator;
14 import org.wsmo.service.Goal;
15 import org.wsmo.service.WebService;
16
17 import com.ontotext.ordi.exception.ORDIException;
18 import com.ontotext.ordi.wsmo4rdf.WsmoConnection;
19 import com.ontotext.ordi.wsmo4rdf.remote.DOMUtil;
20 import com.ontotext.ordi.wsmo4rdf.remote.Marshaller;
21 import com.ontotext.ordi.wsmo4rdf.remote.exception.NonExistingEntryException;
22 import com.ontotext.ordi.wsmo4rdf.remote.exception.OrdiConfigurationException;
23 import com.ontotext.ordi.wsmo4rdf.remote.exception.UnmarshalException;
24
25 @javax.jws.WebService(endpointInterface = "com.ontotext.ordi.wsmo4rdf.remote.server.WsmoConnectionService", serviceName = "WsmoConnectionService")
26 public class WsmoConnectionServiceImpl implements WsmoConnectionService {
27 private static Logger logger = Logger
28 .getLogger(WsmoConnectionServiceImpl.class);
29
30 public WsmoConnectionServiceImpl() {
31 }
32
33
34
35
36 public String evaluate(int connectionId, String query)
37 throws MalformedQueryException, NonExistingEntryException,
38 OrdiConfigurationException {
39 WsmoConnection wsmoConnection = getWsmoConnectionForId(connectionId);
40 SparqlXmlResultListener srxListener;
41 try {
42 srxListener = new SparqlXmlResultListener(query);
43 } catch (ParserConfigurationException pce) {
44 throw new OrdiConfigurationException(pce);
45 }
46 wsmoConnection.evaluate(query, srxListener);
47
48 return DOMUtil.convert(srxListener.getResult()).toString();
49 }
50
51
52
53
54 public String getDescription(int connectionId)
55 throws NonExistingEntryException {
56 WsmoConnection connection = getWsmoConnectionForId(connectionId);
57 return connection.getDescription();
58 }
59
60 public void setDescription(int connectionId, String desc)
61 throws NonExistingEntryException {
62 WsmoConnection connection = getWsmoConnectionForId(connectionId);
63 connection.setDescription(desc);
64 }
65
66 public String getVersion(int connectionId) throws SynchronisationException,
67 NonExistingEntryException {
68 WsmoConnection connection = getWsmoConnectionForId(connectionId);
69 return connection.getVersion();
70 }
71
72 public void addOntology(int connectionId, String ont)
73 throws SynchronisationException, NonExistingEntryException,
74 UnmarshalException {
75 WsmoConnection connection = getWsmoConnectionForId(connectionId);
76 Ontology onto = Marshaller.unmarshalOntology(ont);
77 logger.debug("Adding Ontology " + onto.getIdentifier());
78 connection.addOntology(onto);
79 }
80
81 public void saveOntology(int connectionId, String ontology)
82 throws SynchronisationException, NonExistingEntryException,
83 UnmarshalException {
84 WsmoConnection connection = getWsmoConnectionForId(connectionId);
85 Ontology onto = Marshaller.unmarshalOntology(ontology);
86 logger.debug("Saving Ontology " + onto.getIdentifier());
87 connection.saveOntology(onto);
88 }
89
90 public String getOntology(int connectionId, String id)
91 throws SynchronisationException, NonExistingEntryException {
92 WsmoConnection connection = getWsmoConnectionForId(connectionId);
93 Ontology onto = connection.getOntology(Marshaller.newIRI(id));
94 logger.debug("Ontology " + onto.getIdentifier() + " was found and will be returned by WS.");
95 return Marshaller.marshal(onto);
96 }
97
98 public void deleteOntology(int connectionId, String iriAsString)
99 throws SynchronisationException, NonExistingEntryException {
100 WsmoConnection connection = getWsmoConnectionForId(connectionId);
101 IRI iri = Marshaller.newIRI(iriAsString);
102 logger.debug("Deleting Ontology " + iriAsString);
103 connection.deleteOntology(iri);
104 }
105
106 public List<String> listOntologies(int connectionId)
107 throws SynchronisationException, NonExistingEntryException {
108 WsmoConnection connection = getWsmoConnectionForId(connectionId);
109 List<IRI> ontoIRIList = connection.listOntologies();
110 logger.debug("Found " + ontoIRIList.size() + "Ontology(s) in the local WsmoConnection.");
111 List<String> result = new LinkedList<String>();
112 for (IRI iri : ontoIRIList) {
113 result.add(iri.toString());
114 }
115 return result;
116 }
117
118 public void addGoal(int connectionId, String goal)
119 throws SynchronisationException, UnmarshalException,
120 NonExistingEntryException {
121 WsmoConnection connection = getWsmoConnectionForId(connectionId);
122 Goal g = Marshaller.unmarshalGoal(goal);
123 logger.debug("Adding Goal " + g.getIdentifier());
124 connection.addGoal(g);
125 }
126
127 public void saveGoal(int connectionId, String goal)
128 throws SynchronisationException, UnmarshalException,
129 NonExistingEntryException {
130 WsmoConnection connection = getWsmoConnectionForId(connectionId);
131 Goal g = Marshaller.unmarshalGoal(goal);
132 logger.debug("Saving Goal " + g.getIdentifier());
133 connection.saveGoal(g);
134 }
135
136 public String getGoal(int connectionId, String iri)
137 throws SynchronisationException, NonExistingEntryException {
138 WsmoConnection connection = getWsmoConnectionForId(connectionId);
139 Goal goal = connection.getGoal(Marshaller.newIRI(iri));
140 logger.debug("Goal " + goal.getIdentifier() + " was found and will be returned by WS.");
141 return Marshaller.marshal(goal);
142 }
143
144 public void deleteGoal(int connectionId, String iriAsString)
145 throws SynchronisationException, NonExistingEntryException {
146 WsmoConnection connection = getWsmoConnectionForId(connectionId);
147 IRI iri = Marshaller.newIRI(iriAsString);
148 logger.debug("Deleting Goal " + iri.toString());
149 connection.deleteGoal(iri);
150 }
151
152 public List<String> listGoals(int connectionId)
153 throws SynchronisationException, NonExistingEntryException {
154 WsmoConnection connection = getWsmoConnectionForId(connectionId);
155 List<IRI> goalIRIList = connection.listGoals();
156 logger.debug("Found " + goalIRIList.size() + "Goal(s) in the local WsmoConnection");
157 List<String> result = new LinkedList<String>();
158 for (IRI iri : goalIRIList) {
159 result.add(iri.toString());
160 }
161 return result;
162 }
163
164 public void addWebService(int connectionId, String webServiceAsString)
165 throws SynchronisationException, NonExistingEntryException, UnmarshalException {
166 WsmoConnection connection = getWsmoConnectionForId(connectionId);
167 WebService ws = Marshaller.unmarshalWebService(webServiceAsString);
168 logger.debug("Adding WebService " + ws.getIdentifier());
169 connection.addWebService(ws);
170 }
171
172 public void saveWebService(int connectionId, String webServiceAsString)
173 throws SynchronisationException, NonExistingEntryException, UnmarshalException {
174 WsmoConnection connection = getWsmoConnectionForId(connectionId);
175 WebService ws = Marshaller.unmarshalWebService(webServiceAsString);
176 logger.debug("Saving WebService " + ws.getIdentifier());
177 connection.saveWebService(ws);
178 }
179
180 public String getWebService(int connectionId, String iriAsString)
181 throws SynchronisationException, NonExistingEntryException {
182 WsmoConnection connection = getWsmoConnectionForId(connectionId);
183 WebService ws = connection.getWebService(Marshaller.newIRI(iriAsString));
184 logger.debug("WebService " + ws.getIdentifier() + " was found and will be returned by WS.");
185 return Marshaller.marshal(ws);
186 }
187
188 public void deleteWebService(int connectionId, String iriAsString)
189 throws SynchronisationException, NonExistingEntryException {
190 WsmoConnection connection = getWsmoConnectionForId(connectionId);
191 IRI iri = Marshaller.newIRI(iriAsString);
192 logger.debug("Deleting WebService " + iri.toString());
193 connection.deleteWebService(iri);
194 }
195
196 public List<String> listWebServices(int connectionId)
197 throws SynchronisationException, NonExistingEntryException {
198 WsmoConnection connection = getWsmoConnectionForId(connectionId);
199 List<IRI> wsIRIList = connection.listWebServices();
200 logger.debug("Found " + wsIRIList.size() + "WebService(s) in the local WsmoConnection");
201 List<String> result = new LinkedList<String>();
202 for (IRI iri : wsIRIList) {
203 result.add(iri.toString());
204 }
205 return result;
206 }
207
208
209 public void addMediator(int connectionId, String mediatorAsString)
210 throws SynchronisationException, NonExistingEntryException, UnmarshalException {
211 WsmoConnection connection = getWsmoConnectionForId(connectionId);
212 Mediator mediator = Marshaller.unmarshalMediatior(mediatorAsString);
213 logger.debug("Adding Mediator " + mediator.getIdentifier());
214 connection.addMediator(mediator);
215 }
216
217 public void saveMediator(int connectionId, String mediatorAsString)
218 throws SynchronisationException, NonExistingEntryException, UnmarshalException {
219 WsmoConnection connection = getWsmoConnectionForId(connectionId);
220 Mediator mediator = Marshaller.unmarshalMediatior(mediatorAsString);
221 logger.debug("Saving Mediator " + mediator.getIdentifier());
222 connection.saveMediator(mediator);
223
224 }
225
226 public void deleteMediator(int connectionId, String iriAsString)
227 throws SynchronisationException, NonExistingEntryException {
228 WsmoConnection connection = getWsmoConnectionForId(connectionId);
229 IRI iri = Marshaller.newIRI(iriAsString);
230 logger.debug("Deleting Mediator " + iri.toString());
231 connection.deleteMediator(iri);
232 }
233
234 public String getMediator(int connectionId, String iriAsString)
235 throws SynchronisationException, NonExistingEntryException {
236 WsmoConnection connection = getWsmoConnectionForId(connectionId);
237 Mediator mediator = connection.getMediator(Marshaller.newIRI(iriAsString));
238 logger.debug("Mediator " + mediator.getIdentifier() + " was found and will be returned by WS.");
239 return Marshaller.marshal(mediator);
240 }
241
242 public List<String> listMediators(int connectionId)
243 throws SynchronisationException, NonExistingEntryException {
244 WsmoConnection connection = getWsmoConnectionForId(connectionId);
245 List<IRI> mediatorIRIList = connection.listMediators();
246 logger.debug("Found " + mediatorIRIList.size() + "Mediator(s) in the local WsmoConnection");
247 List<String> result = new LinkedList<String>();
248 for (IRI iri : mediatorIRIList) {
249 result.add(iri.toString());
250 }
251 return result;
252 }
253
254
255
256
257 public boolean isAutoCommit(int connectionId)
258 throws NonExistingEntryException {
259 WsmoConnection connection = getWsmoConnectionForId(connectionId);
260 return connection.isAutoCommit();
261 }
262
263 public void setAutoCommit(int connectionId, boolean mode)
264 throws ORDIException, NonExistingEntryException {
265 WsmoConnection connection = getWsmoConnectionForId(connectionId);
266 connection.setAutoCommit(mode);
267 }
268
269 public void rollback(int connectionId) throws ORDIException,
270 NonExistingEntryException {
271 WsmoConnection connection = getWsmoConnectionForId(connectionId);
272 connection.rollback();
273 }
274
275 public void commit(int connectionId) throws ORDIException,
276 NonExistingEntryException {
277 WsmoConnection connection = getWsmoConnectionForId(connectionId);
278 logger.debug("Commiting...");
279 connection.commit();
280
281 logger.debug("Listing ontologies in the source:");
282 for (IRI iri : connection.listOntologies()) {
283 logger.debug("\tLooking for ontology: " + iri);
284 logger.debug("\t\tfound:" + connection.getOntology(iri));
285 }
286
287 }
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 public boolean isReadOnly(int connectionId)
304 throws NonExistingEntryException {
305 WsmoConnection connection = getWsmoConnectionForId(connectionId);
306 return connection.isReadOnly();
307 }
308
309 public boolean isOpen(int connectionId) throws NonExistingEntryException {
310 WsmoConnection connection = getWsmoConnectionForId(connectionId);
311 return connection.isOpen();
312 }
313
314 public void close(int connectionId) throws ORDIException,
315 NonExistingEntryException {
316 WsmoConnection connection = RegistryManager.getInstance().getWsmoConnectionRegistry()
317 .unregister(connectionId);
318 if (connection != null) {
319 connection.close();
320 connection = null;
321 }
322
323 }
324
325
326
327
328 public void clearWarnings(int connectionId)
329 throws NonExistingEntryException {
330 WsmoConnection connection = getWsmoConnectionForId(connectionId);
331 connection.clearWarnings();
332 }
333
334
335 private WsmoConnection getWsmoConnectionForId(int connectionId)
336 throws NonExistingEntryException {
337 WsmoConnection wsmoConnection = RegistryManager.getInstance().getWsmoConnectionRegistry()
338 .getById(connectionId);
339 if (wsmoConnection == null) {
340 throw new NonExistingEntryException();
341 }
342 return wsmoConnection;
343 }
344
345 }