/* * Copyright (c) 2020 MarkLogic Corporation */ package com.marklogic.xcc.examples; import java.net.URI; import com.marklogic.xcc.ContentSource; import com.marklogic.xcc.ContentSourceFactory; import com.marklogic.xcc.ModuleSpawn; import com.marklogic.xcc.Request; import com.marklogic.xcc.RequestOptions; import com.marklogic.xcc.ResultSequence; import com.marklogic.xcc.Session; import com.marklogic.xcc.exceptions.RequestException; import com.marklogic.xcc.exceptions.XccConfigException; import com.marklogic.xcc.types.ValueType; /** *
* This is a very simple class that will invoke a named XQuery module on the server and return the * result. *
** Click here for the source code for this class *
** The main() method looks for two command-line args, a URL for the server ( * {@link ContentSourceFactory#newContentSource(java.net.URI)}) and theURI of a module to invoke. * This URI should be relative to the "Library" path set in the XDBC server configuration for the * content server you're connecting to (arg #1). This program invokes the named module and then * prints the result sequence to stdout, one item per line. *
** The class has methods that could be used to set request options and to obtain the results as an * array of Strings or as a real {@link ResultSequence}. *
** If you want to set external variables for a request, you can call {@link #getRequest()} to obtain * a reference to the internal {@link Request} object and set the variable values on it before * calling {@link #invoke(String)}. *
** ToDo: Accept variable names and values on the command line. *
*/ public class ModuleRunner { private final Session session; private final ModuleSpawn request; private RequestOptions options; /** * Construct an instance that will invoke modules on the server represented by the given URI. * Note that the URI will not be validated at this time. * * @param serverUri * A URI that specifies a server per ( * {@link ContentSourceFactory#newContentSource(java.net.URI)}). * @throws XccConfigException * If the URI is not a valid XCC server URL. */ public ModuleRunner(URI serverUri) throws XccConfigException { ContentSource cs = ContentSourceFactory.newContentSource(serverUri); session = cs.newSession(); request = session.newModuleSpawn(null); } /** * Invoke the module with the given URI and return the resulting {@link ResultSequence}. * * @param moduleUri * @return An instance {@link ResultSequence}, possibly with size zero. * @throws RequestException * If an unrecoverable error occurs when submitting or evaluating the request. */ public ResultSequence invoke(String moduleUri) throws RequestException { request.setModuleUri(moduleUri); request.setOptions(options); request.setNewVariable("my-namespace-uri","uri", ValueType.XS_STRING,"28477-moduleInvoke.xml"); request.setNewVariable("my-namespace-uri","doc", ValueType.DOCUMENT,"