KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > server > processors > QueryProcessorCom


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

5
6 package org.joseki.server.processors;
7
8 import org.apache.commons.logging.*;
9 import org.joseki.server.*;
10 import com.hp.hpl.jena.rdf.model.*;
11 import com.hp.hpl.jena.rdf.model.RDFException;
12
13 /** Query processor framework that returns the whole model.
14  * Queries can be supplied by string (e.g. HTTP GET) or
15  * ad a model (e.g. HTTP POST).
16  *
17  * @author Andy Seaborne
18  * @version $Id: QueryProcessorCom.java,v 1.7 2004/04/30 14:13:11 andy_seaborne Exp $
19  */

20 public abstract class QueryProcessorCom implements QueryProcessor
21 {
22     static final Log logger = LogFactory.getLog(QueryProcessorCom.class.getName()) ;
23
24     boolean readOnlyLock = true ;
25
26     public QueryProcessorCom() { }
27
28     /** @see org.joseki.server.Processor#init(Resource, Resource)
29      */

30     public void init(Resource processor, Resource implementation) { }
31
32     // Not exactly true! This is for when the processor
33
// is invoked via POST
34
public int argsNeeded() { return Processor.ARGS_ONE ; }
35
36     /**
37      * @see org.joseki.server.Processor#exec(Request)
38      */

39     public Model exec(Request request) throws ExecutionException
40     {
41         ModelSource aModel = request.getModelSource();
42         boolean needsEndOperation = false ;
43         try
44         {
45             aModel.startOperation(readOnlyLock);
46             needsEndOperation = true ;
47             try
48             {
49                 // May be null (no string supplied) in which case the query may be a model.
50
String JavaDoc queryString = request.getParam("query");
51                 // Actually this will be "" for a POSTed query - fix.
52
if (queryString != null && queryString.equals(""))
53                     queryString = null;
54
55                 if (queryString != null && request.getDataArgs().size() > 0)
56                     throw new ExecutionException(
57                         ExecutionError.rcQueryExecutionFailure,
58                         "Query has string and model arguments");
59
60                 if (request.getDataArgs().size() > 0)
61                     // Execute via model
62
return execQuery(aModel, (Model) request.getDataArgs().get(0), request);
63
64                 // Execute via query string
65
return execQuery(aModel, queryString, request);
66
67             }
68             catch (RDFException ex)
69             {
70                 needsEndOperation = false;
71                 aModel.abortOperation();
72                 logger.trace("RDFException: " + ex.getMessage());
73                 throw new ExecutionException(ExecutionError.rcInternalError, null);
74             }
75             catch (ExecutionException exEx)
76             {
77                 throw exEx;
78             }
79             catch (Exception JavaDoc ex)
80             {
81                 aModel.abortOperation();
82                 logger.trace("Exception : " + ex.getMessage());
83                 throw new ExecutionException(ExecutionError.rcInternalError, null);
84             }
85         }
86         finally
87         {
88             if ( needsEndOperation )
89             {
90                 needsEndOperation = false ;
91                 aModel.endOperation();
92             }
93         } //return emptyModel ;
94
}
95
96     /** Query processors supply this, rather than the <code>exec</code> method.
97      * The queryString may be null (no string supplied).
98      */

99
100     abstract public Model execQuery(ModelSource target, String JavaDoc queryString, Request request) throws RDFException, QueryExecutionException ;
101     abstract public Model execQuery(ModelSource target, Model queryModel, Request request) throws RDFException, QueryExecutionException ;
102 }
103
104
105 /*
106  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
107  * All rights reserved.
108  *
109  * Redistribution and use in source and binary forms, with or without
110  * modification, are permitted provided that the following conditions
111  * are met:
112  * 1. Redistributions of source code must retain the above copyright
113  * notice, this list of conditions and the following disclaimer.
114  * 2. Redistributions in binary form must reproduce the above copyright
115  * notice, this list of conditions and the following disclaimer in the
116  * documentation and/or other materials provided with the distribution.
117  * 3. The name of the author may not be used to endorse or promote products
118  * derived from this software without specific prior written permission.
119  *
120  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
121  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
122  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
123  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
124  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
125  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
126  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
127  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
128  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
129  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
130  */

131
Popular Tags