/** * Opens the specified database. * * @param name name of database * @param ctx database context * @return data reference * @throws IOException I/O exception */ public static Data open(final String name, final Context ctx) throws IOException { synchronized (ctx.dbs) { Data data = ctx.dbs.pin(name); if (data == null) { // check if database exists if (!ctx.globalopts.dbexists(name)) throw new BaseXException(dbnf(name)); data = new DiskData(name, ctx); ctx.dbs.add(data); } // check permissions if (!ctx.perm(Perm.READ, data.meta)) { Close.close(data, ctx); throw new BaseXException(PERM_REQUIRED_X, Perm.READ); } return data; } }
/** * Executes the command, prints the result to the specified output stream and returns a success * flag. * * @param ctx database context * @param os output stream * @return success flag. The {@link #info()} method returns information on a potential exception */ private boolean exec(final Context ctx, final OutputStream os) { // check if data reference is available final Data dt = ctx.data(); if (dt == null && data) return error(NO_DB_OPENED); // check permissions if (!ctx.perm(perm, dt != null ? dt.meta : null)) return error(PERM_REQUIRED_X, perm); // set updating flag updating = updating(ctx); try { // register process ctx.register(this); // run command and return success flag return run(ctx, os); } finally { // guarantee that process will be unregistered ctx.unregister(this); } }