1 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 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 33 public Dispatcher() { } 34 35 39 40 public Dispatcher(Dispatcher queryDispatcher) { parent = queryDispatcher ; } 41 42 public ModelSet getModelSet() { return modelSet ; } 43 44 public Loader getLoader() { return loader ; } 45 46 49 50 public Request createOperation(String uri, String url, String opName) throws ExecutionException 51 { 52 return createRequest(uri, url, opName, true) ; 53 } 54 55 public synchronized Request createRequest(String uri, String url, String 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 uri, String url, String 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 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 opName) 109 { 110 Processor proc = aModel.getProcessorRegistry().findProcessor(opName); 111 return proc ; 112 } 113 114 private QueryProcessor findQueryProcessor(ModelSource aModel, String langName) 115 { 116 QueryProcessor qProc = aModel.getProcessorRegistry().findQueryProcessor(langName); 117 return qProc ; 118 } 119 120 public ModelSource findModel(String uri) 121 { 122 ModelSource aModel = modelSet.findModel(uri); 123 return aModel ; 124 } 125 126 128 Model optionsModel = null ; 129 public Model getOptionsModel(String baseName) throws ExecutionException 130 { 131 if ( optionsModel == null ) 132 optionsModel = calcOptionsModel(baseName) ; 133 return optionsModel ; 134 } 135 136 private synchronized Model calcOptionsModel(String baseName) throws ExecutionException 137 { 138 try { 139 Model optModel = ModelFactory.createDefaultModel() ; 140 for ( Iterator iter = modelSet.sourceURIs() ; iter.hasNext() ; ) 142 { 143 String uri = (String )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 } 157 158 160 public Model getOptionsModel(ModelSource aModel, String baseName) throws ExecutionException 161 { 162 return calcOptionsModel(aModel, baseName) ; 163 } 164 165 private Model calcOptionsModel(ModelSource aModel, String baseName) throws ExecutionException 166 { 167 try { 171 Model optModel = ModelFactory.createDefaultModel() ; 172 176 String configURI = ModelSet.serverRootURI+aModel.getServerURI() ; 179 180 183 Resource optionRes = optModel.createResource() ; 184 optModel.add(optionRes, RDF.type, JosekiVocab.AttachedModel) ; 185 186 aModel.getProcessorRegistry().toRDF(optModel, optionRes) ; 187 188 Map prefixes = aModel.getPrefixes() ; 190 for ( Iterator iter = prefixes.keySet().iterator() ; iter.hasNext() ; ) 191 { 192 String prefix = (String )iter.next() ; 193 String nsURI = (String )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 } 209 210 211 213 public synchronized void addModelSource(ModelSource aModel, String 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 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 238 public synchronized void addProcessor(String aModelURI, String 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 256 public synchronized void addQueryProcessor(String aModelURI, String 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 273 public synchronized void removeProcessor(String 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 314 | Popular Tags |