use of org.xmldb.api.base.Database in project exist by eXist-db.
the class CopyCollectionRecoveryTest method storeAndReadXmldb.
@Test
public void storeAndReadXmldb() throws DatabaseConfigurationException, XMLDBException, EXistException, IOException {
// initialize xml:db driver
final Database database = new DatabaseImpl();
database.setProperty("create-database", "true");
DatabaseManager.registerDatabase(database);
BrokerPool.FORCE_CORRUPTION = false;
xmldbStore();
existEmbeddedServer.restart();
BrokerPool.FORCE_CORRUPTION = false;
xmldbRead();
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class Main method process.
/**
* Constructor for Main.
*
* @param arguments parsed command line arguments
*/
public static void process(final ParsedArguments arguments) {
final Properties properties = loadProperties();
final Preferences preferences = Preferences.userNodeForPackage(Main.class);
final boolean guiMode = getBool(arguments, guiArg);
final boolean quiet = getBool(arguments, quietArg);
Optional.ofNullable(arguments.get(optionArg)).ifPresent(options -> options.forEach(properties::setProperty));
properties.setProperty(USER_PROP, arguments.get(userArg));
final String optionPass = arguments.get(passwordArg);
properties.setProperty(PASSWORD_PROP, optionPass);
final Optional<String> optionDbaPass = getOpt(arguments, dbaPasswordArg);
final boolean useSsl = getBool(arguments, useSslArg);
if (useSsl) {
properties.setProperty(SSL_ENABLE, "TRUE");
}
final Optional<String> backupCollection = getOpt(arguments, backupCollectionArg);
getOpt(arguments, backupOutputDirArg).ifPresent(backupOutputDir -> properties.setProperty(BACKUP_DIR_PROP, backupOutputDir.getAbsolutePath()));
final Optional<Path> restorePath = getOpt(arguments, restoreArg).map(File::toPath);
final boolean rebuildRepo = getBool(arguments, rebuildExpathRepoArg);
final boolean overwriteApps = getBool(arguments, overwriteAppsArg);
boolean deduplicateBlobs = getBool(arguments, backupDeduplicateBlobs);
// initialize driver
final Database database;
try {
final Class<?> cl = Class.forName(properties.getProperty(DRIVER_PROP, DEFAULT_DRIVER));
database = (Database) cl.newInstance();
database.setProperty(CREATE_DATABASE_PROP, "true");
database.setProperty(SSL_ENABLE, properties.getProperty(SSL_ENABLE, "FALSE"));
if (properties.containsKey(CONFIGURATION_PROP)) {
database.setProperty(CONFIGURATION_PROP, properties.getProperty(CONFIGURATION_PROP));
}
DatabaseManager.registerDatabase(database);
} catch (final ClassNotFoundException | InstantiationException | XMLDBException | IllegalAccessException e) {
reportError(e);
return;
}
// process
if (backupCollection.isPresent()) {
String collection = backupCollection.get();
if (collection.isEmpty()) {
if (guiMode) {
final CreateBackupDialog dialog = new CreateBackupDialog(properties.getProperty(URI_PROP, DEFAULT_URI), properties.getProperty(USER_PROP, DEFAULT_USER), properties.getProperty(PASSWORD_PROP, DEFAULT_PASSWORD), Paths.get(preferences.get("directory.backup", System.getProperty("user.dir"))));
if (JOptionPane.showOptionDialog(null, dialog, "Create Backup", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null) == JOptionPane.YES_OPTION) {
collection = dialog.getCollection();
deduplicateBlobs = dialog.getDeduplicateBlobs();
properties.setProperty(BACKUP_DIR_PROP, dialog.getBackupTarget());
}
} else {
collection = XmldbURI.ROOT_COLLECTION;
}
}
if (!collection.isEmpty()) {
try {
final Backup backup = new Backup(properties.getProperty(USER_PROP, DEFAULT_USER), properties.getProperty(PASSWORD_PROP, DEFAULT_PASSWORD), Paths.get(properties.getProperty(BACKUP_DIR_PROP, DEFAULT_BACKUP_DIR)), XmldbURI.xmldbUriFor(properties.getProperty(URI_PROP, DEFAULT_URI) + collection), properties, deduplicateBlobs);
backup.backup(guiMode, null);
} catch (final Exception e) {
reportError(e);
}
}
}
if (restorePath.isPresent()) {
Path path = restorePath.get();
if (!Files.exists(path) && guiMode) {
final JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if (chooser.showDialog(null, "Select backup file for restore") == JFileChooser.APPROVE_OPTION) {
path = chooser.getSelectedFile().toPath();
}
}
if (Files.exists(path)) {
final String username = properties.getProperty(USER_PROP, DEFAULT_USER);
final String uri = properties.getProperty(URI_PROP, DEFAULT_URI);
try {
final XmldbURI dbUri;
if (!uri.endsWith(XmldbURI.ROOT_COLLECTION)) {
dbUri = XmldbURI.xmldbUriFor(uri + XmldbURI.ROOT_COLLECTION);
} else {
dbUri = XmldbURI.xmldbUriFor(uri);
}
if (guiMode) {
restoreWithGui(username, optionPass, optionDbaPass, path, dbUri, overwriteApps);
} else {
restoreWithoutGui(username, optionPass, optionDbaPass, path, dbUri, rebuildRepo, quiet, overwriteApps);
}
} catch (final Exception e) {
reportError(e);
}
}
}
try {
String uri = properties.getProperty(URI_PROP, XmldbURI.EMBEDDED_SERVER_URI_PREFIX);
if (!(uri.contains(XmldbURI.ROOT_COLLECTION) || uri.endsWith(XmldbURI.ROOT_COLLECTION))) {
uri += XmldbURI.ROOT_COLLECTION;
}
final Collection root = DatabaseManager.getCollection(uri, properties.getProperty(USER_PROP, DEFAULT_USER), optionDbaPass.orElse(optionPass));
shutdown(root);
} catch (final Exception e) {
reportError(e);
}
System.exit(SystemExitCodes.OK_EXIT_CODE);
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class AbstractExistHttpServlet method getOrCreateBrokerPool.
private BrokerPool getOrCreateBrokerPool(final ServletConfig config) throws EXistException, DatabaseConfigurationException, ServletException {
// Configure BrokerPool
if (BrokerPool.isConfigured()) {
getLog().info("Database already started. Skipping configuration ...");
} else {
final String confFile = Optional.ofNullable(config.getInitParameter("configuration")).orElse("conf.xml");
final Optional<Path> dbHome = Optional.ofNullable(config.getInitParameter("basedir")).map(baseDir -> Optional.ofNullable(config.getServletContext().getRealPath(baseDir)).map(rp -> Optional.of(Paths.get(rp))).orElse(Optional.ofNullable(config.getServletContext().getRealPath("/")).map(dir -> Paths.get(dir).resolve("WEB-INF").toAbsolutePath()))).orElse(Optional.ofNullable(config.getServletContext().getRealPath("/")).map(Paths::get));
getLog().info("EXistServlet: exist.home={}", dbHome.map(Path::toString).orElse("null"));
final Path cf = dbHome.map(h -> h.resolve(confFile)).orElse(Paths.get(confFile));
getLog().info("Reading configuration from {}", cf.toAbsolutePath().toString());
if (!Files.isReadable(cf)) {
throw new ServletException("Configuration file " + confFile + " not found or not readable");
}
final Configuration configuration = new Configuration(confFile, dbHome);
final String start = config.getInitParameter("start");
if (start != null && "true".equals(start)) {
doDatabaseStartup(configuration);
}
}
return BrokerPool.getInstance();
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class XMLDBRegisterDatabase method eval.
/*
* (non-Javadoc)
*
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet,
* org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final String driverName = args[0].getStringValue();
final boolean createDatabase = args[1].effectiveBooleanValue();
try {
final Class<?> driver = Class.forName(driverName);
final Database database = (Database) driver.newInstance();
database.setProperty("create-database", createDatabase ? "true" : "false");
DatabaseManager.registerDatabase(database);
} catch (final Exception e) {
logger.error("failed to initiate XMLDB database driver: {}", driverName);
return BooleanValue.FALSE;
}
return BooleanValue.TRUE;
}
use of org.xmldb.api.base.Database in project exist by eXist-db.
the class RemoteQueryTest method setUp.
@Before
public void setUp() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException, URISyntaxException, IOException {
// initialize driver
Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
Database database = (Database) cl.newInstance();
database.setProperty("create-database", "true");
DatabaseManager.registerDatabase(database);
Collection root = DatabaseManager.getCollection(getUri() + XmldbURI.ROOT_COLLECTION, "admin", null);
CollectionManagementService service = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
testCollection = service.createCollection("test");
assertNotNull(testCollection);
Resource xr = testCollection.createResource("hamlet.xml", "XMLResource");
try (final InputStream is = SAMPLES.getHamletSample()) {
xr.setContent(InputStreamUtil.readString(is, UTF_8));
testCollection.storeResource(xr);
}
xmlrpcCollection = service.createCollection("xmlrpc");
assertNotNull(xmlrpcCollection);
Resource br = xmlrpcCollection.createResource(TestConstants.TEST_MODULE_URI.toString(), "BinaryResource");
((EXistResource) br).setMimeType(MimeType.XQUERY_TYPE.getName());
br.setContent(XmlRpcTest.MODULE_DATA);
xmlrpcCollection.storeResource(br);
}
Aggregations