use of org.exist.Database in project exist by eXist-db.
the class DebuggeeJointImpl method evalution.
@Override
public String evalution(String script) throws Exception {
Database db = compiledXQuery.getContext().getDatabase();
// TODO: account required
try (final DBBroker broker = db.getBroker()) {
XQueryContext context = compiledXQuery.getContext().copyContext();
context.setDebuggeeJoint(null);
context.undeclareGlobalVariable(Debuggee.SESSION);
context.undeclareGlobalVariable(Debuggee.IDEKEY);
XQuery service = broker.getBrokerPool().getXQueryService();
CompiledXQuery compiled = service.compile(broker, context, script);
Sequence resultSequence = service.execute(broker, compiled, null);
SAXSerializer sax = null;
Serializer serializer = broker.getSerializer();
serializer.reset();
try {
sax = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
Properties outputProps = new Properties();
StringWriter writer = new StringWriter();
sax.setOutput(writer, outputProps);
serializer.setSAXHandlers(sax, sax);
for (SequenceIterator i = resultSequence.iterate(); i.hasNext(); ) {
Item next = i.nextItem();
if (Type.subTypeOf(next.getType(), Type.NODE))
serializer.toSAX((NodeValue) next);
else
writer.write(next.getStringValue());
}
return writer.toString();
} finally {
if (sax != null) {
SerializerPool.getInstance().returnObject(sax);
}
}
}
}
use of org.exist.Database in project exist by eXist-db.
the class ScriptRunner method run.
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
final Database db = BrokerPool.getInstance();
db.addStatusObserver(this);
try (final DBBroker broker = db.getBroker()) {
XQuery xquery = broker.getBrokerPool().getXQueryService();
xquery.execute(broker, expression, null);
// XQueryContext context = expression.getContext();
//
// expression.reset();
//
// context.setBroker(broker);
// context.getWatchDog().reset();
//
// //do any preparation before execution
// context.prepare();
//
// context.getProfiler().traceQueryStart();
// broker.getBrokerPool().getProcessMonitor().queryStarted(context.getWatchDog());
// try {
// Sequence result = expression.eval(null);
//
// if(outputProperties != null)
// context.checkOptions(outputProperties); //must be done before context.reset!
//
// //return result;
// } finally {
// context.getProfiler().traceQueryEnd(context);
// expression.reset();
// context.reset();
// broker.getBrokerPool().getProcessMonitor().queryCompleted(context.getWatchDog());
// }
}
} catch (Exception e) {
e.printStackTrace();
exception = e;
}
}
use of org.exist.Database in project exist by eXist-db.
the class PropertyGet method getPropertyString.
protected static StringBuilder getPropertyString(Variable variable, XQueryContext context) {
Sequence value = variable.getValue();
Database db = context.getDatabase();
try (final DBBroker broker = db.getBroker()) {
Serializer serializer = broker.getSerializer();
serializer.reset();
try {
StringBuilder property = new StringBuilder();
if (value.hasOne()) {
String strVal = getPropertyValue(value.itemAt(0), serializer);
String type = Type.subTypeOf(value.getItemType(), Type.NODE) ? "node" : Type.getTypeName(value.getItemType());
property.append("<property name=\"");
property.append(variable.getQName().toString());
property.append("\" fullname=\"");
property.append(variable.getQName().toString());
property.append("\" type=\"");
property.append(type);
property.append("\" size=\"");
property.append(strVal.length());
property.append("\" encoding=\"none\">");
property.append(strVal);
property.append("</property>");
} else {
property.append("<property name=\"");
property.append(variable.getQName().toString());
property.append("\" fullname=\"");
property.append(variable.getQName().toString());
property.append("\" type=\"array\" children=\"true\" numchildren=\"");
property.append(value.getItemCount());
property.append("\">");
int idx = 0;
for (SequenceIterator si = value.iterate(); si.hasNext(); idx++) {
Item item = si.nextItem();
String strVal = getPropertyValue(item, serializer);
String type = Type.subTypeOf(value.getItemType(), Type.NODE) ? "xs:string" : Type.getTypeName(value.getItemType());
property.append("<property name=\"");
property.append(idx);
property.append("\" type=\"");
property.append(type);
property.append("\" size=\"");
property.append(strVal.length());
property.append("\" encoding=\"none\">");
property.append(strVal);
property.append("</property>");
}
property.append("</property>");
}
return property;
} catch (SAXException e) {
e.printStackTrace();
} catch (XPathException e) {
e.printStackTrace();
}
} catch (EXistException e) {
e.printStackTrace();
}
return null;
}
use of org.exist.Database in project exist by eXist-db.
the class SMEvents method runScript.
protected void runScript(Subject subject, String scriptURI, String script, QName functionName, List<Expression> args) {
final Database db = getDatabase();
try (final DBBroker broker = db.get(Optional.ofNullable(subject))) {
final Source source = getQuerySource(broker, scriptURI, script);
if (source == null) {
return;
}
final XQuery xquery = broker.getBrokerPool().getXQueryService();
final XQueryContext context = new XQueryContext(broker.getBrokerPool());
final CompiledXQuery compiled = xquery.compile(context, source);
// Sequence result = xquery.execute(compiled, subject.getName());
final ProcessMonitor pm = db.getProcessMonitor();
// execute the XQuery
try {
final UserDefinedFunction function = context.resolveFunction(functionName, 0);
if (function != null) {
context.getProfiler().traceQueryStart();
pm.queryStarted(context.getWatchDog());
final FunctionCall call = new FunctionCall(context, function);
if (args != null) {
call.setArguments(args);
}
final Sequence contextSequence;
final ContextItemDeclaration cid = context.getContextItemDeclartion();
if (cid != null) {
contextSequence = cid.eval(null);
} else {
contextSequence = NodeSet.EMPTY_SET;
}
call.analyze(new AnalyzeContextInfo());
call.eval(contextSequence);
}
} catch (final XPathException e) {
// XXX: log
e.printStackTrace();
} finally {
if (pm != null) {
context.getProfiler().traceQueryEnd(context);
pm.queryCompleted(context.getWatchDog());
}
compiled.reset();
context.reset();
}
} catch (final Exception e) {
// XXX: log
e.printStackTrace();
}
}
use of org.exist.Database in project exist by eXist-db.
the class GetReleaseBrokerDeadlocks method exterServiceMode.
@Test
@Ignore
public void exterServiceMode() {
try {
Configuration config = new Configuration();
config.setProperty(FunctionFactory.PROPERTY_DISABLE_DEPRECATED_FUNCTIONS, Boolean.FALSE);
BrokerPool.configure(1, 5, config);
Database db = BrokerPool.getInstance();
Thread thread = new Thread(db.getThreadGroup(), new EnterServiceMode());
try (final DBBroker broker = db.getBroker()) {
thread.start();
Thread.sleep(1000);
}
Thread.sleep(1000);
assertFalse(thread.isAlive());
} catch (Exception e) {
fail(e.getMessage());
}
}
Aggregations