[Mulgara-svn] r475 - branches/nw-interface/src/jar/itql/java/org/mulgara/itql
pag at mulgara.org
pag at mulgara.org
Thu Oct 11 17:14:53 UTC 2007
Author: pag
Date: 2007-10-11 12:14:53 -0500 (Thu, 11 Oct 2007)
New Revision: 475
Modified:
branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java
Log:
Initial implementation, using TqlAutoInterpreter instead of ItqlInterpreter. This won't work, but it compiles, and the basics should be here.
Modified: branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java
===================================================================
--- branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java 2007-10-11 17:12:20 UTC (rev 474)
+++ branches/nw-interface/src/jar/itql/java/org/mulgara/itql/ItqlInterpreterBean.java 2007-10-11 17:14:53 UTC (rev 475)
@@ -29,6 +29,7 @@
// Java APIs
import java.io.*;
+import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
@@ -43,15 +44,23 @@
import org.jrdf.graph.BlankNode;
// Mulgara packages
+import org.mulgara.connection.Connection;
+import org.mulgara.connection.ConnectionException;
import org.mulgara.itql.lexer.LexerException;
import org.mulgara.itql.parser.ParserException;
+import org.mulgara.parser.Interpreter;
import org.mulgara.parser.MulgaraLexerException;
import org.mulgara.parser.MulgaraParserException;
import org.mulgara.query.Answer;
import org.mulgara.query.Query;
import org.mulgara.query.QueryException;
import org.mulgara.query.TuplesException;
+import org.mulgara.query.operation.Backup;
+import org.mulgara.query.operation.Command;
+import org.mulgara.query.operation.Load;
+import org.mulgara.query.operation.Restore;
import org.mulgara.query.rdf.LiteralImpl;
+import org.mulgara.query.rdf.Mulgara;
import org.mulgara.server.Session;
/**
@@ -76,63 +85,52 @@
*/
public class ItqlInterpreterBean {
- /**
- * the logging category to log to
- */
- private final static Logger log =
- Logger.getLogger(ItqlInterpreterBean.class.getName());
+ /** The logger */
+ private final static Logger log = Logger.getLogger(ItqlInterpreterBean.class.getName());
- /**
- * Get line separator.
- */
+ /** line separator */
private static final String EOL = System.getProperty("line.separator");
/**
* The TQL namespace, used when creating an XML response.
- *
* TODO: Bring this into line with the Mulgara.NAMESPACE, which may break existing client.
*/
private final static String TQL_NS = "http://mulgara.org/tql#";
+
+ /** Dummy load source for RDF files on an input stream. */
+ private final static URI DUMMY_RDF_SOURCE = URI.create(Mulgara.NAMESPACE+"locally-sourced-inputStream.rdf");
- /**
- * The ITQL interpreter Bean.
- */
- private final ItqlInterpreter interpreter =
- new ItqlInterpreter(ItqlInterpreter.getDefaultAliases());
+ /** The ITQL interpreter Bean. */
+ private final TqlAutoInterpreter interpreter = new TqlAutoInterpreter();
+
+ /** A legacy session handle. Only used to remember the session a client asked for. */
+ private Session legacySession = null;
+
+ /** An internal parser. Only used when a legacy client wants to build their own query. */
+ private Interpreter parser = new TqlInterpreter();
+
+ /** Indicates that the last command wanted to quit. */
+ private boolean quit = false;
/**
- * Create the ITQL interpreter.
- *
- * The session will be obtained using a
- * {@link org.mulgara.server.driver.SessionFactoryFinder}.
+ * Create the TQL interpreter.
*/
public ItqlInterpreterBean() {
-
- if (log.isInfoEnabled()) {
-
- log.info("Created an ItqlInterpreterBean");
- }
-
+ if (log.isInfoEnabled()) log.info("Created an ItqlInterpreterBean");
}
/**
- * Create the ITQL interpreter using the given <code>session</code>.
- *
+ * @deprecated
+ * Create the TQL interpreter using the given <code>session</code>.
+ * The Session will be ignored. Security is currently unimplemented, but the domain is recorded.
* @param session the session to use to communicate with the Mulgara server
*/
public ItqlInterpreterBean(Session session, URI securityDomain) {
-
if (log.isInfoEnabled()) {
log.info("Created an ItqlInterpreterBean with a supplied session and security domain");
}
-
- try {
- interpreter.setSession(session, securityDomain);
- } catch (QueryException e) {
-
- interpreter.setLastError(e);
- interpreter.setLastMessage("Couldn't set interpreter session: " + e.getMessage());
- }
+ legacySession = session;
+ interpreter.setSecurityDomain(securityDomain);
}
// executeQueryToMap()
@@ -244,29 +242,25 @@
/**
* Returns the session to use to communicate with the Mulgara server.
- *
+ * @deprecated The user should not be accessing a session through a
+ * command interpreter.
* @return the session to use to communicate with the Mulgara server
*/
public Session getSession() {
-
- try {
- return this.interpreter.getSession();
- } catch (QueryException e) {
- return null;
- }
+ return legacySession;
}
/**
* Returns the session to use to communicate with the specified Mulgara server.
- *
+ * @deprecated The user should not be accessing a session through a
+ * command interpreter.
* @param serverURI URI Server to get a Session for.
* @return the session to use to communicate with the specified Mulgara server
*/
public Session getSession(URI serverURI) {
-
try {
- return this.interpreter.getSession(serverURI);
- } catch (QueryException e) {
+ return interpreter.establishConnection(serverURI).getSession();
+ } catch (Exception e) {
return null;
}
}
@@ -278,25 +272,14 @@
* @return the alias namespace map associated with this bean.
*/
public Map getAliasMap() {
-
- return this.interpreter.getAliasMap();
+ return this.interpreter.getAliasesInUse();
}
/**
* Closes the session underlying this bean.
*/
public void close() {
- try {
- Session session = interpreter.getSession();
- if (session != null) {
- session.close();
- }
- session = null;
- } catch (QueryException e) {
- if (log.isInfoEnabled()) {
- log.info("Couldn't close interpreter session", e);
- }
- }
+ interpreter.close();
}
//
@@ -383,14 +366,14 @@
log.debug("Executing query : " + singleQuery);
}
- interpreter.executeCommand(singleQuery);
+ executeCommand(singleQuery);
Answer answer = null;
try {
answer = this.interpreter.getLastAnswer();
if ((answer == null) || answer.isUnconstrained()) {
- if (this.interpreter.getLastError() == null) {
+ if (this.interpreter.getLastException() == null) {
//Not an error, but a message does exist
Element message =
(Element)query.appendChild(doc.createElementNS(TQL_NS, "message"));
@@ -403,9 +386,9 @@
//Error has occurred at the interpreter
//Generate a SOAP fault
// TODO REMOVE Constants.FAULT_CODE_SERVER,
- log.error("Execute query failed. Returning error", interpreter.getLastError());
+ log.error("Execute query failed. Returning error", interpreter.getLastException());
throw new SOAPException("ItqlInterpreter error - " +
- interpreter.getCause(interpreter.getLastError(), 0));
+ ItqlUtil.getCause(interpreter.getLastException(), 0));
}
// Ensure answer is null.
answer = null;
@@ -605,7 +588,7 @@
/**
* Builds a {@link org.mulgara.query.Query} from the given <var>query</var>.
*
- * @param query PARAMETER TO DO
+ * @param query Command containing a query (<em>select $x $y .....</em>).
* @return a {@link org.mulgara.query.Query} constructed from the given
* <var>query</var>
* @throws IOException if the <var>query</var> can't be buffered
@@ -614,12 +597,12 @@
* correct
*/
public Query buildQuery(String query) throws IOException, MulgaraLexerException, MulgaraParserException {
+ Command cmd = parser.parseCommand(query);
+ if (!(cmd instanceof Query)) throw new MulgaraParserException("Command is valid, but is not a query: " + query + "(" + cmd.getClass().getName() + ")");
+ return (Query)cmd;
+ }
- // defer to the interpreter
- return this.interpreter.parseQuery(query);
- } // buildQuery()
-
/**
* Execute an iTQL "update" statement that returns no results.
* <p>
@@ -635,14 +618,13 @@
public void executeUpdate(String itql) throws ItqlInterpreterException {
try {
-
- interpreter.executeCommand(itql);
+ executeCommand(itql);
} catch (Exception e) {
-
throw new ItqlInterpreterException(e);
}
- ItqlInterpreterException exception = interpreter.getLastError();
+ Exception e = interpreter.getLastException();
+ ItqlInterpreterException exception = (e == null) ? null : new ItqlInterpreterException(e);
Answer answer = interpreter.getLastAnswer();
if (answer != null) {
@@ -669,7 +651,7 @@
* @see ItqlInterpreter#isQuitRequested()
*/
public boolean isQuitRequested() {
- return interpreter.isQuitRequested();
+ return quit;
}
@@ -689,17 +671,16 @@
/**
- * Returns the {@linkplain ItqlInterpreter#getLastError last error} of the
+ * Returns the {@linkplain ItqlInterpreter#getLastException last error} of the
* interpreter.
*
* @return the results of the last command execution, null if the command did
* not set any message
*
- * @see ItqlInterpreter#getLastError()
+ * @see ItqlInterpreter#getLastException()
*/
- public ItqlInterpreterException getLastError() {
-
- return interpreter.getLastError();
+ public ItqlInterpreterException getLastException() {
+ return new ItqlInterpreterException(interpreter.getLastException());
}
@@ -713,21 +694,15 @@
public Answer executeQuery(String itql) throws ItqlInterpreterException {
try {
- interpreter.executeCommand(itql);
+ executeCommand(itql);
} catch (Exception e) {
throw new ItqlInterpreterException(e);
}
- ItqlInterpreterException exception = interpreter.getLastError();
+ Exception exception = interpreter.getLastException();
+ if (exception != null) throw new ItqlInterpreterException(exception);
- if (exception != null) {
-
- throw exception;
- }
-
- Answer answer = interpreter.getLastAnswer();
-
- return answer;
+ return interpreter.getLastAnswer();
}
@@ -748,30 +723,22 @@
* @throws QueryException if the data fails to load or the file does not exist
* on the local file system.
*/
- public long load(File sourceFile, URI destinationURI)
- throws QueryException {
+ public long load(File sourceFile, URI destinationURI) throws QueryException {
+ long numberOfStatements = 0;
- long NumberOfStatements = 0;
- InputStream inputStream = null;
-
// check for the local file
- if ( ! sourceFile.exists() ) {
- throw new QueryException(sourceFile+" does not exist on the local file "+
- "system");
+ if (!sourceFile.exists()) {
+ throw new QueryException(sourceFile+" does not exist on the local file system");
}
try {
- inputStream = sourceFile.toURL().openStream();
- NumberOfStatements = interpreter.load(inputStream, sourceFile.toURI(), destinationURI);
- } catch (IOException ex) {
- throw new QueryException("Unable to read the contents of "+sourceFile, ex );
- } finally {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException ex) {}
- }
+ Load loadCmd = new Load(sourceFile.toURI(), destinationURI, true);
+ numberOfStatements = (Long)loadCmd.execute(interpreter.establishConnection(loadCmd.getServerURI()));
+ } catch (QueryException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new QueryException("Unable to load data: " + ex.getMessage(), ex);
}
- return NumberOfStatements;
+ return numberOfStatements;
}
/**
@@ -783,9 +750,15 @@
* receive the backup contents.
* @throws QueryException if the backup cannot be completed.
*/
- public void backup(URI sourceURI, File destinationFile ) throws QueryException {
-
- interpreter.backup(sourceURI, destinationFile);
+ public void backup(URI sourceURI, File destinationFile) throws QueryException {
+ Backup backup = new Backup(sourceURI, destinationFile.toURI());
+ try {
+ backup.execute(interpreter.establishConnection(backup.getServerURI()));
+ } catch (MalformedURLException e) {
+ throw new QueryException("Bad source graph URI: " + sourceURI, e);
+ } catch (ConnectionException e) {
+ throw new QueryException("Unable to establish connection to: " + backup.getServerURI(), e);
+ }
}
/**
@@ -797,8 +770,13 @@
* @throws QueryException if the backup cannot be completed.
*/
public void backup(URI sourceURI, OutputStream outputStream) throws QueryException {
-
- interpreter.backup(sourceURI, outputStream);
+ URI serverUri = Backup.calcServerUri(sourceURI);
+ try {
+ Connection conn = interpreter.establishConnection(serverUri);
+ Backup.backup(conn, sourceURI, outputStream);
+ } catch (ConnectionException e) {
+ throw new QueryException("Unable to establish connection to: " + serverUri, e);
+ }
}
/**
@@ -814,8 +792,7 @@
* @return number of rows inserted into the destination model
*/
public long load(InputStream inputStream, URI destinationURI) throws QueryException {
-
- return interpreter.load(inputStream, destinationURI);
+ return load(inputStream, DUMMY_RDF_SOURCE, destinationURI);
}
/**
@@ -834,10 +811,18 @@
* @return number of rows inserted into the destination model
* @throws QueryException if the data fails to load
*/
- public long load(InputStream inputStream, URI sourceURI, URI destinationURI)
- throws QueryException {
-
- return interpreter.load(inputStream, sourceURI, destinationURI);
+ public long load(InputStream inputStream, URI sourceURI, URI destinationURI) throws QueryException {
+ long numberOfStatements = 0;
+ try {
+ Load loadCmd = new Load(sourceURI, destinationURI, true);
+ loadCmd.setOverrideStream(inputStream);
+ numberOfStatements = (Long)loadCmd.execute(interpreter.establishConnection(loadCmd.getServerURI()));
+ } catch (QueryException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new QueryException("Unable to load data: " + ex.getMessage(), ex);
+ }
+ return numberOfStatements;
}
/**
@@ -850,7 +835,7 @@
* @throws QueryException if the restore cannot be completed.
*/
public void restore(InputStream inputStream, URI serverURI) throws QueryException {
- interpreter.restore(inputStream, serverURI);
+ restore(inputStream, serverURI, DUMMY_RDF_SOURCE);
}
/**
@@ -864,9 +849,16 @@
* @param sourceURI The URI of the backup file to restore from.
* @throws QueryException if the restore cannot be completed.
*/
- public void restore(InputStream inputStream, URI serverURI, URI sourceURI)
- throws QueryException {
- interpreter.restore(inputStream, serverURI, sourceURI);
+ public void restore(InputStream inputStream, URI serverURI, URI sourceURI) throws QueryException {
+ try {
+ Restore restoreCmd = new Restore(sourceURI, serverURI, true);
+ restoreCmd.setOverrideStream(inputStream);
+ restoreCmd.execute(interpreter.establishConnection(restoreCmd.getServerURI()));
+ } catch (QueryException ex) {
+ throw ex;
+ } catch (Exception ex) {
+ throw new QueryException("Unable to load data: " + ex.getMessage(), ex);
+ }
}
@@ -975,7 +967,7 @@
try {
// get the answer to the query
- interpreter.executeCommand(query);
+ executeCommand(query);
Answer answer = this.interpreter.getLastAnswer();
// log the query response
@@ -993,23 +985,23 @@
} else {
// get the error in an appropriate form
- if (this.interpreter.getLastError() != null) {
+ if (this.interpreter.getLastException() != null) {
// error has occurred at the interpreter
if (log.isDebugEnabled()) {
log.debug("Adding query error to map - " +
- this.interpreter.getLastError());
+ this.interpreter.getLastException());
}
// end if
// set this as the answer
if (keepExceptions) {
- result = this.interpreter.getLastError();
+ result = this.interpreter.getLastException();
} else {
- result = this.interpreter.getLastError().getMessage();
+ result = this.interpreter.getLastException().getMessage();
}
} else {
@@ -1086,30 +1078,13 @@
}
/**
- * Sets the serverURI of the interpreter.
+ * Sets the serverURI of the interpreter. This method now does nothing.
* @param serverURI The new URI of the server for the interpreter
+ * @deprecated Establishing communication with a server now requires a connection.
+ * Connections can be evaluated automatically with the TqlAutoInterpreter.
*/
public void setServerURI(String serverURI) {
-
- try {
-
- // Set the server URI of the interpreter
- interpreter.setServerURI((serverURI == null) ? null : new URI(serverURI));
- } catch (URISyntaxException uriSyntaxException) {
-
- log.error("Could not change server due to bad uri syntax.",
- uriSyntaxException);
-
- // Set the last error to be the syntax exception
- interpreter.setLastError(uriSyntaxException);
- } catch (QueryException queryException) {
-
- log.error("Could not change the server due to a query exception.",
- queryException);
-
- // Set the last error to be a query exception
- interpreter.setLastError(queryException);
- }
+ // Do nothing
}
/**
@@ -1117,18 +1092,17 @@
*
* @param aliasMap the alias map associated with this bean
*/
- public void setAliasMap(HashMap aliasMap) {
-
- this.interpreter.setAliasMap(aliasMap);
+ public void setAliasMap(HashMap<String,URI> aliasMap) {
+ ((TqlInterpreter)parser).setAliasMap(aliasMap);
+ interpreter.setAliasesInUse(aliasMap);
}
/**
* Clears the last error of the interpreter.
*/
public void clearLastError() {
-
// Set the last error to be null
- interpreter.setLastError(null);
+ interpreter.clearLastException();
}
/**
@@ -1148,4 +1122,13 @@
super.finalize();
}
}
+
+
+ /**
+ * Executes the requested command, and records if the command wants to quit.
+ * @param command The command to execute.
+ */
+ private void executeCommand(String command) {
+ quit = !interpreter.executeCommand(command);
+ }
}
More information about the Mulgara-svn
mailing list