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

📄 bugreportmanager.java

📁 orcale9i-java-programming外文书籍源码,结合orcale数据库的java的程序,内有jdbc等非常丰富的内容,好东西
💻 JAVA
字号:
package wrox.bugreports;

import oracle.sqlj.runtime.Oracle;
import sqlj.runtime.ref.DefaultContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;

public class BugReportManager {

    /** Static constants */
    static final String FILE_NAME = "bugReport";
    static File ROOT_DIR;

    static final String ROOT_DIR_PATH = "C:\\temp";
    static {

        // Assume this directory exists.
        ROOT_DIR = new File(ROOT_DIR_PATH);
    }

    public static BugReport readReport(long bugID) throws Exception {

        ReadAction ra = new ReadAction(bugID);
        AccessController.doPrivileged(ra);
        BugReport report = ra.getBugReport();

        if (report != null) {
            BugReportPermission perm =
                new BugReportPermission(report.getBugClass(),"read");
            SecurityManager sman = System.getSecurityManager();
            sman.checkPermission(perm);
        }
        return report;
    }

    public static boolean writeReport(BugReport report) throws Exception {
        BugReportPermission perm =
            new BugReportPermission(report.getBugClass(),"write");
        SecurityManager sman = System.getSecurityManager();
        sman.checkPermission(perm);

        WriteAction wa = new WriteAction(report);
        AccessController.doPrivileged(wa);
        return wa.wasWritten();
    }

    public static boolean deleteReport(BugReport report) throws Exception {
        BugReportPermission perm =
            new BugReportPermission(report.getBugClass(),"delete");
        SecurityManager sman = System.getSecurityManager();
        sman.checkPermission(perm);

        DeleteAction da = new DeleteAction(report);
        AccessController.doPrivileged(da);
        return da.wasDeleted();
    }

static class ReadAction implements PrivilegedAction {

        private BugReport report;
        private Exception stored;
        private long bugID;

        public ReadAction(long bugID) {
            this.bugID = bugID;
        }
        public BugReport getBugReport() throws Exception {
            if (stored != null) {
                throw stored;
            }
            return report;
        }
        public Object run() {

            FileInputStream fis = null;
            ObjectInputStream ois = null;
            try {
                File f = new File(ROOT_DIR,FILE_NAME + bugID);
                if (f.exists()) {
                    fis = new FileInputStream(f);
                    ois = new ObjectInputStream(fis);
                    report = (BugReport) ois.readObject();
                }
            } catch (Exception e) {
                stored = e;
            } finally {
                try {
                    if (fis != null) fis.close();
                } catch (Exception ignored) {
                }
            }
            return null;
        }
    }

    static class WriteAction implements PrivilegedAction {

        private boolean wasWritten;
        private BugReport report;
        private Exception stored;

        public WriteAction(BugReport report) {
            this.report = report;
            wasWritten = false;
        }

        public boolean wasWritten() throws Exception {
            if (stored != null) {
                throw stored;
            }
            return wasWritten;
        }

        public Object run() {

            FileOutputStream fout = null;
            ObjectOutputStream out = null;
            try {

                // Get a new report ID if this is a new report.
                if (report.getBugID() == -1) {
                    report.setBugID((new Date()).toString().hashCode());
                    report.setBugDate(new Date());
                }

                // Write the object to file.
                File f = new File(ROOT_DIR,FILE_NAME + report.getBugID());
                if (!f.exists()) {
                    f.createNewFile();
                }
                fout = new FileOutputStream(f);
                out = new ObjectOutputStream(fout);
                out.writeObject(report);
                wasWritten = true;

            } catch (Exception e) {
                stored = e;
            } finally {
                try {
                    if (out != null) out.flush();
                } catch (Exception ignored) {
                }
                try {
                    if (fout != null) fout.close();
                } catch (Exception ignored) {
                }
            }
            return null;
        }
    }

    static class DeleteAction implements PrivilegedAction {

        private boolean wasDeleted;
        private BugReport report;
        private Exception stored;

        public DeleteAction(BugReport report) {
            this.report = report;
            wasDeleted = false;
        }

        public boolean wasDeleted() throws Exception {
            if (stored != null) {
                throw stored;
            }
            return wasDeleted;
        }

        public Object run() {

            try {

                File f = new File(ROOT_DIR,FILE_NAME + report.getBugID());
                if (f.exists()) {
                    wasDeleted = f.delete();
                }

            } catch (Exception e) {
                stored = e;
            }
            return null;
        }
    }
}

⌨️ 快捷键说明

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