Search in sources :

Example 16 with Database

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();
}
Also used : DatabaseImpl(org.exist.xmldb.DatabaseImpl) Database(org.xmldb.api.base.Database) Test(org.junit.Test)

Example 17 with Database

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);
}
Also used : Path(java.nio.file.Path) XMLDBException(org.xmldb.api.base.XMLDBException) Properties(java.util.Properties) StartException(org.exist.start.StartException) XMLDBException(org.xmldb.api.base.XMLDBException) IOException(java.io.IOException) Database(org.xmldb.api.base.Database) Collection(org.xmldb.api.base.Collection) Preferences(java.util.prefs.Preferences) File(java.io.File)

Example 18 with Database

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();
}
Also used : Path(java.nio.file.Path) BrokerPool(org.exist.storage.BrokerPool) ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) Subject(org.exist.security.Subject) EXistException(org.exist.EXistException) Path(java.nio.file.Path) XMLDBException(org.xmldb.api.base.XMLDBException) XQueryURLRewrite(org.exist.http.urlrewrite.XQueryURLRewrite) DatabaseManager(org.xmldb.api.DatabaseManager) ServletConfig(javax.servlet.ServletConfig) HttpServlet(javax.servlet.http.HttpServlet) AuthenticationException(org.exist.security.AuthenticationException) Files(java.nio.file.Files) HttpServletResponse(javax.servlet.http.HttpServletResponse) Database(org.xmldb.api.base.Database) IOException(java.io.IOException) HttpAccount(org.exist.security.internal.web.HttpAccount) SecurityManager(org.exist.security.SecurityManager) Logger(org.apache.logging.log4j.Logger) Principal(java.security.Principal) XmldbPrincipal(org.exist.security.XmldbPrincipal) Paths(java.nio.file.Paths) Optional(java.util.Optional) Configuration(org.exist.util.Configuration) DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) ServletException(javax.servlet.ServletException) Configuration(org.exist.util.Configuration)

Example 19 with Database

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;
}
Also used : Database(org.xmldb.api.base.Database) XPathException(org.exist.xquery.XPathException)

Example 20 with Database

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);
}
Also used : CollectionManagementService(org.xmldb.api.modules.CollectionManagementService) InputStream(java.io.InputStream) Database(org.xmldb.api.base.Database) XMLResource(org.xmldb.api.modules.XMLResource) Resource(org.xmldb.api.base.Resource) Collection(org.xmldb.api.base.Collection) Before(org.junit.Before)

Aggregations

Database (org.xmldb.api.base.Database)20 Collection (org.xmldb.api.base.Collection)8 XMLDBException (org.xmldb.api.base.XMLDBException)6 DatabaseImpl (org.exist.xmldb.DatabaseImpl)5 IOException (java.io.IOException)4 StartException (org.exist.start.StartException)4 CollectionManagementService (org.xmldb.api.modules.CollectionManagementService)4 XMLResource (org.xmldb.api.modules.XMLResource)4 InputStream (java.io.InputStream)3 Path (java.nio.file.Path)3 EXistException (org.exist.EXistException)3 Test (org.junit.Test)3 Properties (java.util.Properties)2 ServletException (javax.servlet.ServletException)2 AuthenticationException (org.exist.security.AuthenticationException)2 Subject (org.exist.security.Subject)2 BrokerPool (org.exist.storage.BrokerPool)2 DatabaseConfigurationException (org.exist.util.DatabaseConfigurationException)2 Resource (org.xmldb.api.base.Resource)2 File (java.io.File)1