Search in sources :

Example 11 with Database

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);
            }
        }
    }
}
Also used : CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) DBBroker(org.exist.storage.DBBroker) StringWriter(java.io.StringWriter) Database(org.exist.Database) SAXSerializer(org.exist.util.serializer.SAXSerializer) SAXSerializer(org.exist.util.serializer.SAXSerializer) Serializer(org.exist.storage.serializers.Serializer)

Example 12 with Database

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;
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) Database(org.exist.Database) EXistException(org.exist.EXistException)

Example 13 with Database

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;
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) Database(org.exist.Database) EXistException(org.exist.EXistException) Serializer(org.exist.storage.serializers.Serializer) SAXException(org.xml.sax.SAXException)

Example 14 with Database

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();
    }
}
Also used : Sequence(org.exist.xquery.value.Sequence) ProcessMonitor(org.exist.storage.ProcessMonitor) StringSource(org.exist.source.StringSource) Source(org.exist.source.Source) DBSource(org.exist.source.DBSource) PermissionDeniedException(org.exist.security.PermissionDeniedException) DBBroker(org.exist.storage.DBBroker) Database(org.exist.Database)

Example 15 with Database

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());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Configuration(org.exist.util.Configuration) Database(org.exist.Database) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Database (org.exist.Database)42 DBBroker (org.exist.storage.DBBroker)34 Test (org.junit.Test)29 EXistException (org.exist.EXistException)6 SecurityManager (org.exist.security.SecurityManager)5 BrokerPool (org.exist.storage.BrokerPool)4 XmldbURI (org.exist.xmldb.XmldbURI)4 XQuery (org.exist.xquery.XQuery)4 IOException (java.io.IOException)3 Random (java.util.Random)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 CompiledXQuery (org.exist.xquery.CompiledXQuery)3 Ignore (org.junit.Ignore)3 StringWriter (java.io.StringWriter)2 Collection (org.exist.collections.Collection)2 Configuration (org.exist.config.Configuration)2 StreamListener (org.exist.indexing.StreamListener)2 SecurityManagerImpl (org.exist.security.internal.SecurityManagerImpl)2 Serializer (org.exist.storage.serializers.Serializer)2 Configuration (org.exist.util.Configuration)2