⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adminclient.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        }
        if (users.isEmpty()) {
            out.println("No users found.");
        } else {
            for (DxIterator it = users.iterator(); it.next() != null;) {
                User user = (User) it.object();

                out.println("user name: " + user.name());
                out.println("    id: " + user.id());
            }
        }

    }


    protected void newGroup(String[] _args) throws Exception {
        String name = null;
        int id = -1;

        // check command line arguments
        for (int i = 1; i < _args.length; i++) {
            String arg = _args[i];

            if (arg.startsWith("-name=")) {
                name = arg.substring(6);
                verboseOut.println("name=" + name);
            } else if (arg.startsWith("-id=")) {
                id = Integer.parseInt(arg.substring(4));
                verboseOut.println("id=" + id);
            } else {
                out.println("Unknow argument: " + arg);
            }
        }
        try {
            admin.newGroup(name, id);
        } catch (UserManagerException e) {
            out.println("Unable to create new group: " + e.getMessage());
        }
    }


    protected void removeGroup(String[] _args) throws Exception {
        String name = null;

        // check command line arguments
        for (int i = 1; i < _args.length; i++) {
            String arg = _args[i];

            if (arg.startsWith("-name=")) {
                name = arg.substring(6);
                verboseOut.println("name=" + name);
            } else {
                out.println("Unknow argument: " + arg);
            }
        }
        try {
            admin.removeGroup(name);
        } catch (UserManagerException e) {
            out.println("Unable to remove group: " + e.getMessage());
        }
    }


    protected void allGroups(String[] _args) throws Exception {
        DxCollection groups = null;
        try {
            groups = admin.allGroups();
        } catch (UserManagerException e) {
            out.println("Unable to retrieve all groups: " + e.getMessage());
            return;
        }
        if (groups.isEmpty()) {
            out.println("No groups found.");
        } else {
            for (DxIterator it = groups.iterator(); it.next() != null;) {
                Group group = (Group) it.object();

                out.println("group name: " + group.name());
                out.println("    id: " + group.id());
                out.println("    user ID count: " + group.usersCount());

                DxIterator it2 = group.userIDs().iterator();
                int c = 1;
                String line = "    user IDs: ";
                while (it2.next() != null) {
                    line += (c > 1 ? ", " : "");
                    line += it2.object().toString();
                    if (c++ % 10 == 0) {
                        out.println(line);
                        line = "              ";
                    }
                }
                out.println(line);
            }
        }
    }


    protected void addUser2Group(String[] _args) throws Exception {
        String username = null;
        String groupname = null;

        // check command line arguments
        for (int i = 1; i < _args.length; i++) {
            String arg = _args[i];

            if (arg.startsWith("-username=")) {
                username = arg.substring(10);
                verboseOut.println("user name=" + username);
            } else if (arg.startsWith("-groupname=")) {
                groupname = arg.substring(11);
                verboseOut.println("group name=" + groupname);
            } else {
                out.println("Unknow argument: " + arg);
            }
        }
        try {
            admin.addUser2Group(username, groupname);
        } catch (UserManagerException e) {
            out.println("Unable to add user to group: " + e.getMessage());
        }
    }


    protected void numberOfTxs() throws Exception {
        out.println(admin.numberOfTxs());
    }


    protected void shutdown() throws Exception {
        admin.shutdown();
    }

    protected void startGarbageCollection() {
        admin.startGarbageCollection();
    }


    protected void restore(String[] _args) throws Exception {
        String filename = "-";
        boolean compress = false;

        // check command line arguments
        for (int i = 1; i < _args.length; i++) {
            String arg = _args[i];

            if (arg.equals("-compress")) {
                compress = true;
                verboseOut.println("compress=" + compress);
            } else if (arg.startsWith("-infile=")) {
                filename = arg.substring(8);
                verboseOut.println("filename=" + filename);
            } else {
                out.println("Unknow argument: " + arg);
            }
        }

        try {
            admin.beginRestore();

            InputStream in = System.in;
            if (!filename.equals("-")) {
                in = new FileInputStream(filename);
            }
            if (compress) {
                in = new GZIPInputStream(in, 4096);
            } else {
                in = new BufferedInputStream(in, 4096);
            }
            InputSource xmlSource = new InputSource(in);

            SAXChunkProducer producer = new SAXChunkProducer(this);

            SAXParserFactory parserFactory = SAXParserFactory.newInstance();
            SAXParser parser = parserFactory.newSAXParser();
            ParserAdapter adapter = new ParserAdapter(parser.getParser());
            adapter.setContentHandler(producer);
            adapter.parse(xmlSource);
        } catch (Exception e) {
            out.println("\nAn error occured while restoring the database.");
            out.println("The database is not entirely restored and is probably not usable!");
            out.println("Please completely install and restore again.");
            out.println("");
            e.printStackTrace(out);
        } finally {
            // signal end
            admin.processRestoreChunk(null);
        }
    }


    /**
     * This method is inherited from SAXChunkProducerDelegate. It is called
     * by the producer when a chunk is ready to be processed.
     */
    public void processChunk(SAXChunkProducer _producer) throws Exception {
        admin.processRestoreChunk(_producer.chunkStream().toByteArray());
    }


    protected void backup(String[] _args) throws Exception {
        String filename = "-";
        boolean indent = false;
        boolean raw = false;
        boolean compress = false;

        // check command line arguments
        for (int i = 1; i < _args.length; i++) {
            String arg = _args[i];

            if (arg.equals("-compress")) {
                compress = true;
                verboseOut.println("compress=" + compress);
            } else if (arg.equals("-indent")) {
                indent = true;
                verboseOut.println("indent=" + indent);
            } else if (arg.equals("-raw")) {
                raw = true;
                verboseOut.println("raw=" + raw);
            } else if (arg.startsWith("-outfile=")) {
                filename = arg.substring(9);
                verboseOut.println("filename=" + filename);
            } else {
                out.println("Unknow argument: " + arg);
            }
        }

        ExternalTransaction tx = db.newTransaction();
        tx.begin();
        try {
            admin.beginBackup();

            OutputStream out = System.out;
            if (!filename.equals("-")) {
                out = new FileOutputStream(filename);
            }
            if (compress) {
                out = new GZIPOutputStream(out, 4096);
            } else {
                out = new BufferedOutputStream(out, 4096);
            }

            XMLSerializer serializer = new XMLSerializer(out, new OutputFormat("xml", "UTF-8", indent));
            SAXChunkConsumer consumer = new SAXChunkConsumer(serializer.asContentHandler());

            byte[] bytes = null;
            while ((bytes = admin.nextBackupChunk()) != null) {
                consumer.processChunk(bytes);
            }
            if (out instanceof GZIPOutputStream) {
                ((GZIPOutputStream) out).finish();
            }
            out.close();
        } finally {
            tx.rollback();
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -