use of org.exist.util.StringInputSource in project exist by eXist-db.
the class ImplicitConnectionCloseIT method getJndiConnectionFromModuleIsAutomaticallyClosed.
@Test
public void getJndiConnectionFromModuleIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException, LockException, SAXException {
final String moduleQuery = "module namespace mymodule = \"http://mymodule.com\";\n" + "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "declare function mymodule:get-handle() {\n" + " sql:get-jndi-connection(\"" + JNDI_DS_NAME + "\", \"" + STUB_JDBC_USER + "\", \"" + STUB_JDBC_PASSWORD + "\")\n" + "};\n";
final String mainQuery = "import module namespace mymodule = \"http://mymodule.com\" at \"xmldb:exist:///db/mymodule.xqm\";\n" + "mymodule:get-handle()";
final Source mainQuerySource = new StringSource(mainQuery);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store module
try (final Collection collection = broker.openCollection(XmldbURI.create("/db"), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, XmldbURI.create("mymodule.xqm"), new StringInputSource(moduleQuery.getBytes(UTF_8)), MimeType.XQUERY_TYPE, collection);
}
final Tuple2<XQueryContext, ModuleContext> escapedContexts = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
final XQueryContext mainQueryContext = mainCompiledQuery.getContext();
// get the context of the library module
final Module[] libraryModules = mainQueryContext.getModules("http://mymodule.com");
assertEquals(1, libraryModules.length);
assertTrue(libraryModules[0] instanceof ExternalModule);
final ExternalModule libraryModule = (ExternalModule) libraryModules[0];
final XQueryContext libraryQueryContext = libraryModule.getContext();
assertTrue(libraryQueryContext instanceof ModuleContext);
// execute the query
final Sequence result = executeQuery(broker, mainCompiledQuery);
// check that the handle for the sql connection that was created was valid
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof IntegerValue);
assertEquals(Type.LONG, result.itemAt(0).getType());
final long connectionHandle = result.itemAt(0).toJavaObject(long.class);
assertFalse(connectionHandle == 0);
// intentionally escape the contexts from the lambda
return Tuple(mainQueryContext, (ModuleContext) libraryQueryContext);
});
final XQueryContext escapedMainQueryContext = escapedContexts._1;
final ModuleContext escapedLibraryQueryContext = escapedContexts._2;
assertTrue(escapedMainQueryContext != escapedLibraryQueryContext);
// check the connections were closed in the main module
final int mainConnectionsCount = ModuleUtils.readContextMap(escapedMainQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, mainConnectionsCount);
// check the connections were closed in the library module
final int libraryConnectionsCount = ModuleUtils.readContextMap(escapedLibraryQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, libraryConnectionsCount);
// check the connections from our StubDataSource, they should all be closed
final Deque<StubDataSource> createdDataSources = StubDataSourceFactory.CREATED_DATA_SOURCES;
assertEquals(1, createdDataSources.size());
final StubDataSource stubDataSource = createdDataSources.peek();
final Deque<StubConnection> createdConnections = stubDataSource.createdConnections;
assertEquals(1, createdConnections.size());
final StubConnection stubConnection = createdConnections.peek();
assertTrue(stubConnection.isClosed());
transaction.commit();
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class JndiConnectionIT method getJndiConnectionFromModuleIsAutomaticallyClosed.
@Test
public void getJndiConnectionFromModuleIsAutomaticallyClosed() throws EXistException, XPathException, PermissionDeniedException, IOException, LockException, SAXException {
final String moduleQuery = "module namespace mymodule = \"http://mymodule.com\";\n" + "import module namespace sql = \"http://exist-db.org/xquery/sql\";\n" + "declare function mymodule:get-handle() {\n" + " sql:get-jndi-connection(\"" + JNDI_DS_NAME + "\", \"" + h2Database.getUser() + "\", \"" + h2Database.getPassword() + "\")\n" + "};\n";
final String mainQuery = "import module namespace mymodule = \"http://mymodule.com\" at \"xmldb:exist:///db/mymodule.xqm\";\n" + "mymodule:get-handle()";
final Source mainQuerySource = new StringSource(mainQuery);
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
// store module
try (final Collection collection = broker.openCollection(XmldbURI.create("/db"), Lock.LockMode.WRITE_LOCK)) {
broker.storeDocument(transaction, XmldbURI.create("mymodule.xqm"), new StringInputSource(moduleQuery.getBytes(UTF_8)), MimeType.XQUERY_TYPE, collection);
}
final Tuple2<XQueryContext, ModuleContext> escapedContexts = withCompiledQuery(broker, mainQuerySource, mainCompiledQuery -> {
final XQueryContext mainQueryContext = mainCompiledQuery.getContext();
// get the context of the library module
final Module[] libraryModules = mainQueryContext.getModules("http://mymodule.com");
assertEquals(1, libraryModules.length);
assertTrue(libraryModules[0] instanceof ExternalModule);
final ExternalModule libraryModule = (ExternalModule) libraryModules[0];
final XQueryContext libraryQueryContext = libraryModule.getContext();
assertTrue(libraryQueryContext instanceof ModuleContext);
// execute the query
final Sequence result = executeQuery(broker, mainCompiledQuery);
// check that the handle for the sql connection that was created was valid
assertEquals(1, result.getItemCount());
assertTrue(result.itemAt(0) instanceof IntegerValue);
assertEquals(Type.LONG, result.itemAt(0).getType());
final long connectionHandle = result.itemAt(0).toJavaObject(long.class);
assertFalse(connectionHandle == 0);
// intentionally escape the contexts from the lambda
return Tuple(mainQueryContext, (ModuleContext) libraryQueryContext);
});
final XQueryContext escapedMainQueryContext = escapedContexts._1;
final ModuleContext escapedLibraryQueryContext = escapedContexts._2;
assertTrue(escapedMainQueryContext != escapedLibraryQueryContext);
// check the connections were closed in the main module
final int mainConnectionsCount = ModuleUtils.readContextMap(escapedMainQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, mainConnectionsCount);
// check the connections were closed in the library module
final int libraryConnectionsCount = ModuleUtils.readContextMap(escapedLibraryQueryContext, SQLModule.CONNECTIONS_CONTEXTVAR, Map::size);
assertEquals(0, libraryConnectionsCount);
transaction.commit();
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class DLNStorageTest method setUp.
@BeforeClass
public static void setUp() throws Exception {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
final TransactionManager transact = pool.getTransactionManager();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = transact.beginTransaction()) {
Collection test = broker.getOrCreateCollection(transaction, TEST_COLLECTION);
broker.saveCollection(transaction, test);
broker.storeDocument(transaction, XmldbURI.create("test_string.xml"), new StringInputSource(TEST_XML), MimeType.XML_TYPE, test);
// TODO : unlock the collection here ?
transact.commit(transaction);
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class PersistentDomTest method createCollection.
private static void createCollection(final DBBroker broker, final Txn transaction, final XmldbURI collectionUri, final Tuple2<XmldbURI, String>... docs) throws PermissionDeniedException, IOException, SAXException, LockException, EXistException {
try (final ManagedCollectionLock collectionLock = broker.getBrokerPool().getLockManager().acquireCollectionWriteLock(collectionUri)) {
final Collection collection = broker.getOrCreateCollection(transaction, collectionUri);
broker.saveCollection(transaction, collection);
for (final Tuple2<XmldbURI, String> doc : docs) {
broker.storeDocument(transaction, doc._1, new StringInputSource(doc._2), MimeType.XML_TYPE, collection);
}
}
}
use of org.exist.util.StringInputSource in project exist by eXist-db.
the class AuditTrailSessionListenerTest method storeScripts.
private static void storeScripts() throws EXistException, PermissionDeniedException, IOException, SAXException, LockException {
try (final DBBroker broker = existEmbeddedServer.getBrokerPool().get(Optional.of(existEmbeddedServer.getBrokerPool().getSecurityManager().getSystemSubject()));
final Txn transaction = existEmbeddedServer.getBrokerPool().getTransactionManager().beginTransaction()) {
final Collection testCollection = broker.getOrCreateCollection(transaction, TEST_COLLECTION);
broker.storeDocument(transaction, XmldbURI.create(CREATE_SCRIPT), new StringInputSource("<create/>".getBytes(UTF_8)), MimeType.XQUERY_TYPE, testCollection);
broker.storeDocument(transaction, XmldbURI.create(DESTROYED_SCRIPT), new StringInputSource("</destroyed>".getBytes(UTF_8)), MimeType.XQUERY_TYPE, testCollection);
transaction.commit();
}
}
Aggregations