Search in sources :

Example 16 with Database

use of org.exist.Database in project exist by eXist-db.

the class GetReleaseBrokerDeadlocks method testingGetReleaseCycle.

@Test
@Ignore
public void testingGetReleaseCycle() {
    boolean debug = false;
    try {
        Configuration config = new Configuration();
        config.setProperty(FunctionFactory.PROPERTY_DISABLE_DEPRECATED_FUNCTIONS, new Boolean(false));
        BrokerPool.configure(1, 5, config);
        Database db = BrokerPool.getInstance();
        Thread thread;
        for (int i = 0; i < 1000; i++) {
            thread = new Thread(db.getThreadGroup(), new GetRelease());
            thread.start();
            Thread.sleep(rd.nextInt(250));
            if (ex != null) {
                LOG.error(ex.getMessage(), ex);
                fail(ex.getMessage());
            }
            if (debug && db.countActiveBrokers() == 20) {
                Map<Thread, StackTraceElement[]> stackTraces = Thread.getAllStackTraces();
                StringBuilder sb = new StringBuilder();
                sb.append("************************************************\n");
                sb.append("************************************************\n");
                for (Map.Entry<Thread, StackTraceElement[]> entry : stackTraces.entrySet()) {
                    StackTraceElement[] stacks = entry.getValue();
                    sb.append("THREAD: ");
                    sb.append(entry.getKey().getName());
                    sb.append("\n");
                    for (int n = 0; n < stacks.length; n++) {
                        sb.append(" ");
                        sb.append(stacks[n]);
                        sb.append("\n");
                    }
                }
                if (stackTraces.isEmpty())
                    sb.append("No threads.");
            // System.out.println(sb.toString());
            }
        }
        while (db.countActiveBrokers() > 0) {
            Thread.sleep(100);
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        fail(e.getMessage());
    }
}
Also used : Configuration(org.exist.util.Configuration) Database(org.exist.Database) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with Database

use of org.exist.Database in project exist by eXist-db.

the class DocumentImplTest method isSameNode_differentDoc.

@Test
public void isSameNode_differentDoc() {
    final BrokerPool mockBrokerPool = EasyMock.createMock(BrokerPool.class);
    final Database mockDatabase = EasyMock.createMock(Database.class);
    final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
    final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
    final Group mockCurrentSubjectGroup = EasyMock.createMock(Group.class);
    final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
    // expectations
    expect(mockBrokerPool.getSecurityManager()).andReturn(mockSecurityManager).times(2);
    expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase).times(2);
    expect(mockDatabase.getActiveBroker()).andReturn(mockBroker).times(2);
    expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject).times(2);
    expect(mockCurrentSubject.getUserMask()).andReturn(Permission.DEFAULT_UMASK).times(2);
    expect(mockCurrentSubject.getId()).andReturn(RealmImpl.SYSTEM_ACCOUNT_ID).times(2);
    expect(mockCurrentSubject.getDefaultGroup()).andReturn(mockCurrentSubjectGroup).times(2);
    expect(mockCurrentSubjectGroup.getId()).andReturn(RealmImpl.DBA_GROUP_ID).times(2);
    replay(mockBrokerPool, mockDatabase, mockBroker, mockCurrentSubject, mockCurrentSubjectGroup, mockSecurityManager);
    // test setup
    final DocumentImpl doc = new DocumentImpl(mockBrokerPool, 99);
    final DocumentImpl doc2 = new DocumentImpl(mockBrokerPool, 765);
    assertFalse(doc.isSameNode(doc2));
    verify(mockBrokerPool, mockDatabase, mockBroker, mockCurrentSubject, mockCurrentSubjectGroup, mockSecurityManager);
}
Also used : DBBroker(org.exist.storage.DBBroker) SecurityManager(org.exist.security.SecurityManager) Database(org.exist.Database) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 18 with Database

use of org.exist.Database in project exist by eXist-db.

the class DocumentImplTest method isSameNode_nonDoc.

@Test
public void isSameNode_nonDoc() {
    final BrokerPool mockBrokerPool = EasyMock.createMock(BrokerPool.class);
    final Database mockDatabase = EasyMock.createMock(Database.class);
    final DBBroker mockBroker = EasyMock.createMock(DBBroker.class);
    final Subject mockCurrentSubject = EasyMock.createMock(Subject.class);
    final Group mockCurrentSubjectGroup = EasyMock.createMock(Group.class);
    final SecurityManager mockSecurityManager = EasyMock.createMock(SecurityManager.class);
    // expectations
    expect(mockBrokerPool.getSecurityManager()).andReturn(mockSecurityManager);
    expect(mockSecurityManager.getDatabase()).andReturn(mockDatabase);
    expect(mockDatabase.getActiveBroker()).andReturn(mockBroker);
    expect(mockBroker.getCurrentSubject()).andReturn(mockCurrentSubject);
    expect(mockCurrentSubject.getUserMask()).andReturn(Permission.DEFAULT_UMASK);
    expect(mockCurrentSubject.getId()).andReturn(RealmImpl.SYSTEM_ACCOUNT_ID);
    expect(mockCurrentSubject.getDefaultGroup()).andReturn(mockCurrentSubjectGroup);
    expect(mockCurrentSubjectGroup.getId()).andReturn(RealmImpl.DBA_GROUP_ID);
    replay(mockBrokerPool, mockDatabase, mockBroker, mockCurrentSubject, mockCurrentSubjectGroup, mockSecurityManager);
    // test setup
    final DocumentImpl doc = new DocumentImpl(mockBrokerPool, 99);
    final TextImpl text = new TextImpl("hello");
    assertFalse(doc.isSameNode(text));
    verify(mockBrokerPool, mockDatabase, mockBroker, mockCurrentSubject, mockCurrentSubjectGroup, mockSecurityManager);
}
Also used : DBBroker(org.exist.storage.DBBroker) SecurityManager(org.exist.security.SecurityManager) Database(org.exist.Database) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 19 with Database

use of org.exist.Database in project exist by eXist-db.

the class DebuggeeImpl method start.

@Override
public String start(String uri) throws Exception {
    Database db = null;
    ScriptRunner runner = null;
    try {
        db = BrokerPool.getInstance();
        try (final DBBroker broker = db.getBroker()) {
            // Try to find the XQuery
            Source source = SourceFactory.getSource(broker, "", uri, true);
            if (source == null)
                return null;
            XQuery xquery = broker.getBrokerPool().getXQueryService();
            XQueryContext queryContext = new XQueryContext(broker.getBrokerPool());
            // Find correct script load path
            queryContext.setModuleLoadPath(XmldbURI.create(uri).removeLastSegment().toString());
            CompiledXQuery compiled;
            try {
                compiled = xquery.compile(broker, queryContext, source);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
            String sessionId = String.valueOf(queryContext.hashCode());
            // link debugger session & script
            DebuggeeJointImpl joint = new DebuggeeJointImpl();
            SessionImpl session = new SessionImpl();
            joint.setCompiledScript(compiled);
            queryContext.setDebuggeeJoint(joint);
            joint.continuation(new Init(session, sessionId, "eXist"));
            runner = new ScriptRunner(session, compiled);
            runner.start();
            int count = 0;
            while (joint.firstExpression == null && runner.exception == null && count < 10) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                count++;
            }
            if (runner.exception != null) {
                throw runner.exception;
            }
            if (joint.firstExpression == null) {
                throw new XPathException("Can't run debug session.");
            }
            // queryContext.declareVariable(Debuggee.SESSION, sessionId);
            // XXX: make sure that it started up
            sessions.put(sessionId, session);
            return sessionId;
        }
    } catch (Exception e) {
        if (runner != null)
            runner.stop();
        throw e;
    }
}
Also used : XPathException(org.exist.xquery.XPathException) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQuery(org.exist.xquery.XQuery) CompiledXQuery(org.exist.xquery.CompiledXQuery) XQueryContext(org.exist.xquery.XQueryContext) IOException(java.io.IOException) Source(org.exist.source.Source) IOException(java.io.IOException) XPathException(org.exist.xquery.XPathException) DBBroker(org.exist.storage.DBBroker) Init(org.exist.debuggee.dbgp.packets.Init) Database(org.exist.Database)

Example 20 with Database

use of org.exist.Database in project exist by eXist-db.

the class Source method exec.

@Override
public void exec() {
    if (fileURI == null) {
        return;
    }
    InputStream is = null;
    try {
        if (fileURI.toLowerCase().startsWith("dbgp://")) {
            String uri = fileURI.substring(7);
            if (uri.toLowerCase().startsWith("file:/")) {
                uri = fileURI.substring(5);
                is = Files.newInputStream(Paths.get(uri));
            } else {
                XmldbURI pathUri = XmldbURI.create(URLDecoder.decode(fileURI.substring(15), "UTF-8"));
                Database db = getJoint().getContext().getDatabase();
                try (final DBBroker broker = db.getBroker();
                    final LockedDocument resource = broker.getXMLResource(pathUri, LockMode.READ_LOCK)) {
                    if (resource.getDocument().getResourceType() == DocumentImpl.BINARY_FILE) {
                        is = broker.getBinaryResource((BinaryDocument) resource.getDocument());
                    } else {
                        // TODO: xml source???
                        return;
                    }
                } catch (EXistException e) {
                    exception = e;
                }
            }
        } else {
            URL url = new URL(fileURI);
            URLConnection conn = url.openConnection();
            is = conn.getInputStream();
        }
        UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream();
        byte[] buf = new byte[256];
        int c;
        while ((c = is.read(buf)) > -1) {
            // TODO: begin & end line should affect
            baos.write(buf, 0, c);
        }
        source = baos.toByteArray();
        success = true;
    } catch (PermissionDeniedException | IOException e) {
        exception = e;
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                if (exception == null) {
                    exception = e;
                }
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) EXistException(org.exist.EXistException) IOException(java.io.IOException) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) URL(java.net.URL) URLConnection(java.net.URLConnection) BinaryDocument(org.exist.dom.persistent.BinaryDocument) DBBroker(org.exist.storage.DBBroker) Database(org.exist.Database) LockedDocument(org.exist.dom.persistent.LockedDocument) PermissionDeniedException(org.exist.security.PermissionDeniedException) XmldbURI(org.exist.xmldb.XmldbURI)

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