KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > Dispatcher


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.server;
7
8 import java.util.* ;
9 import org.apache.commons.logging.* ;
10
11 import org.joseki.vocabulary.*;
12 import org.joseki.server.module.*;
13
14 import com.hp.hpl.jena.rdf.model.* ;
15 import com.hp.hpl.jena.vocabulary.* ;
16
17 /** The main operation engine class. The dispatcher route requests to
18  * operation processors and keeps the mapping from URI to model.
19  *
20  * @author Andy Seaborne
21  * @version $Id: Dispatcher.java,v 1.9 2004/04/30 14:13:13 andy_seaborne Exp $
22  */

23 public class Dispatcher
24 {
25     private static Log logger = LogFactory.getLog(Dispatcher.class.getName()) ;
26     
27     ModelSet modelSet = new ModelSet() ;
28
29     Dispatcher parent = null ;
30     Loader loader = new Loader() ;
31     
32     /** Create a Dispatcher */
33     public Dispatcher() { }
34     
35     /** Create a Dispatcher that will call another
36      * if it can not handle the request
37      * @param queryDispatcher Parent dispatcher
38      */

39     
40     public Dispatcher(Dispatcher queryDispatcher) { parent = queryDispatcher ; }
41
42     public ModelSet getModelSet() { return modelSet ; }
43     
44     public Loader getLoader() { return loader ; }
45
46     /** Find target model, choose processor and make an operation.
47      * The operation will need connection-specific completion for argument transport.
48      */

49     
50     public Request createOperation(String JavaDoc uri, String JavaDoc url, String JavaDoc opName) throws ExecutionException
51     {
52         return createRequest(uri, url, opName, true) ;
53     }
54     
55     public synchronized Request createRequest(String JavaDoc uri, String JavaDoc url, String JavaDoc opName, boolean modelMustExist) throws ExecutionException
56     {
57         ModelSource aModel = findModel(uri);
58         Processor proc = null ;
59         if ( aModel == null )
60         {
61             if (modelMustExist)
62                 throw new ExecutionException(ExecutionError.rcNoSuchURI, "Not found: " + uri);
63         }
64         else
65         {
66             proc = findProcessor(aModel, opName) ;
67             if ( proc == null )
68                   throw new ExecutionException(ExecutionError.rcOperationNotSupported, "Request not found: " + opName);
69         }
70         Request req = new RequestImpl(uri, url, opName, this, aModel, proc) ;
71         return req ;
72     }
73     
74
75     public Request createQueryRequest(String JavaDoc uri, String JavaDoc url, String JavaDoc langName) throws ExecutionException
76     {
77         QueryProcessor qProc = null;
78         ModelSource aModel = null;
79         synchronized (this)
80         {
81             aModel = findModel(uri);
82             if (aModel == null)
83                 throw new ExecutionException(ExecutionError.rcNoSuchURI, "Not found: " + uri);
84             qProc = findQueryProcessor(aModel, langName) ;
85         }
86
87         if ( qProc == null )
88         {
89             if ( langName == null )
90                 throw new ExecutionException(ExecutionError.rcQueryExecutionFailure, "Null query language name") ;
91             throw new ExecutionException(ExecutionError.rcNoSuchQueryLanguage, "No such query language: "+langName) ;
92         }
93         Request req = new RequestImpl(uri, url, "query", this, aModel, qProc) ;
94         req.setParam("lang", langName) ;
95         return req ;
96     }
97
98     /** Actually do something!
99      * @param request Request to perform
100      * @throws ExecutionException
101      */

102     
103     public Model exec(Request request) throws ExecutionException
104     {
105         return request.getProcessor().exec(request) ;
106     }
107     
108     private Processor findProcessor(ModelSource aModel, String JavaDoc opName)
109     {
110         Processor proc = aModel.getProcessorRegistry().findProcessor(opName);
111         return proc ;
112     }
113
114     private QueryProcessor findQueryProcessor(ModelSource aModel, String JavaDoc langName)
115     {
116         QueryProcessor qProc = aModel.getProcessorRegistry().findQueryProcessor(langName);
117         return qProc ;
118     }
119
120     public ModelSource findModel(String JavaDoc uri)
121     {
122         ModelSource aModel = modelSet.findModel(uri);
123         return aModel ;
124     }
125     
126     // -------- Meta data about this dispatcher
127

128     Model optionsModel = null ;
129     public Model getOptionsModel(String JavaDoc baseName) throws ExecutionException
130     {
131         if ( optionsModel == null )
132             optionsModel = calcOptionsModel(baseName) ;
133         return optionsModel ;
134     }
135     
136     private synchronized Model calcOptionsModel(String JavaDoc baseName) throws ExecutionException
137     {
138         try {
139             Model optModel = ModelFactory.createDefaultModel() ;
140             // Just put in all models.
141
for ( Iterator iter = modelSet.sourceURIs() ; iter.hasNext() ; )
142             {
143                 String JavaDoc uri = (String JavaDoc)iter.next() ;
144                 uri = baseName+uri ;
145                 logger.debug("Server options request: URI = "+uri) ;
146                 Resource r = optModel.createResource(uri) ;
147                 optModel.add(r, RDF.type, JosekiVocab.AttachedModel) ;
148             }
149             return optModel ;
150         } catch (RDFException rdfEx)
151         {
152             logger.warn("RDFException", rdfEx) ;
153             throw new ExecutionException(ExecutionError.rcInternalError, "Failed to create options model") ;
154         }
155         //return null ;
156
}
157     
158     // -------- Meta data about a source
159

160     public Model getOptionsModel(ModelSource aModel, String JavaDoc baseName) throws ExecutionException
161     {
162         return calcOptionsModel(aModel, baseName) ;
163     }
164         
165     private Model calcOptionsModel(ModelSource aModel, String JavaDoc baseName) throws ExecutionException
166     {
167          // Nested locking is currently broken.
168
// try {
169
// aModel.startOperation(true) ;
170
try {
171                 Model optModel = ModelFactory.createDefaultModel() ;
172                 // We recreate from the internal datastructures,
173
// not just read/copy from the original configuration data
174
//Model model = modelSet.getConfigurationModel() ;
175

176                 // The "URI" is dependent on the request and the
177
// the webapplication mount point.
178
String JavaDoc configURI = ModelSet.serverRootURI+aModel.getServerURI() ;
179
180                 // Model as bNode as we do not know its true URI (URL)
181
// It has several, depending on how it is addressed!
182

183                 Resource optionRes = optModel.createResource() ;
184                 optModel.add(optionRes, RDF.type, JosekiVocab.AttachedModel) ;
185
186                 aModel.getProcessorRegistry().toRDF(optModel, optionRes) ;
187
188                 // Add prefixes
189
Map prefixes = aModel.getPrefixes() ;
190                 for ( Iterator iter = prefixes.keySet().iterator() ; iter.hasNext() ; )
191                 {
192                     String JavaDoc prefix = (String JavaDoc)iter.next() ;
193                     String JavaDoc nsURI = (String JavaDoc)prefixes.get(prefix) ;
194                     Resource r = optModel.createResource() ;
195                     r.addProperty(JosekiVocab.nsURI, nsURI) ;
196                     r.addProperty(JosekiVocab.prefix, prefix) ;
197                     optModel.add(optionRes, JosekiVocab.namespacePrefix, r) ;
198                 }
199                 
200                 
201                 return optModel ;
202             } catch (RDFException rdfEx)
203             {
204                 logger.warn("RDFException", rdfEx) ;
205                 throw new ExecutionException(ExecutionError.rcInternalError, "Failed to create options model for "+aModel.getServerURI()) ;
206             }
207 // } finally { aModel.endOperation() ; }
208
}
209     
210     
211     // -------- Operations to provide programmatic configuration of a dispatcher
212

213     public synchronized void addModelSource(ModelSource aModel, String JavaDoc uri)
214     {
215         if ( uri == null )
216         {
217             logger.warn("No URI supplied for the model source") ;
218             return ;
219         }
220         modelSet.addModel(uri, aModel) ;
221     }
222
223     public synchronized void removeModelSource(String JavaDoc uri)
224     {
225         if ( uri == null )
226         {
227             logger.warn("No URI supplied for the model source") ;
228             return ;
229         }
230         modelSet.removeModel(uri) ;
231     }
232
233     /** Add an operation processor.
234      * @param aModelURI URI for the source the processor is specific to.
235      * @param shortName Short name for the operation
236      * @param proc The processor
237      */

238     public synchronized void addProcessor(String JavaDoc aModelURI, String JavaDoc shortName, Processor proc)
239     {
240         if ( aModelURI == null )
241         {
242             logger.warn("No URI supplied for the attached model") ;
243             return ;
244         }
245
246         ModelSource aModel = modelSet.findModel(aModelURI) ;
247         if ( aModel != null )
248             aModel.getProcessorRegistry().registerProcessor(shortName, proc) ;
249     }
250
251     /** Add a query operation processor.
252      * @param aModelURI Model the processor is specific to.
253      * @param langName Language name
254      * @param queryProc The query processor
255      */

256     public synchronized void addQueryProcessor(String JavaDoc aModelURI, String JavaDoc langName, QueryProcessor queryProc)
257     {
258         if ( aModelURI == null )
259         {
260             logger.warn("No URI supplied for the attached model") ;
261             return ;
262         }
263
264         ModelSource aModel = modelSet.findModel(aModelURI) ;
265         if ( aModel != null )
266             aModel.getProcessorRegistry().registerQueryProcessor(langName, queryProc) ;
267     }
268
269     /** Remove a processor.
270      * @param aModelURI The attached model URI that the processor is specific to.
271      * @param processor The processor
272      */

273     public synchronized void removeProcessor(String JavaDoc aModelURI, Processor processor)
274     {
275         if ( aModelURI == null )
276         {
277             logger.warn("No URI supplied for the attached model") ;
278             return ;
279         }
280         
281         ModelSource aModel = modelSet.findModel(aModelURI) ;
282         if ( aModel != null )
283             aModel.getProcessorRegistry().remove(processor) ;
284     }
285 }
286
287
288 /*
289  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
290  * All rights reserved.
291  *
292  * Redistribution and use in source and binary forms, with or without
293  * modification, are permitted provided that the following conditions
294  * are met:
295  * 1. Redistributions of source code must retain the above copyright
296  * notice, this list of conditions and the following disclaimer.
297  * 2. Redistributions in binary form must reproduce the above copyright
298  * notice, this list of conditions and the following disclaimer in the
299  * documentation and/or other materials provided with the distribution.
300  * 3. The name of the author may not be used to endorse or promote products
301  * derived from this software without specific prior written permission.
302  *
303  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
304  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
305  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
306  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
307  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
308  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
309  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
310  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
311  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
312  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
313  */

314
Popular Tags