dbarchivemanager.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 446 行 · 第 1/2 页

JAVA
446 行
字号
        }        else {            return null;        }    }    public boolean isBusy() throws UnauthorizedException {        return busy;    }    public void runArchiver() throws UnauthorizedException {        TaskEngine.addTask(new ArchiveTask());    }    public synchronized void run() {        // Do nothing if auto-archiving is disabled.        if (!isAutoArchiveEnabled()) {            return;        }        // Add a task to the task engine to run archiving.        TaskEngine.addTask(TaskEngine.LOW_PRIORITY, new ArchiveTask());    }    /**     * Loads a property manager for gateway persistence if it isn't already     * loaded. If an XML file for the gateways isn't already created, it will     * attempt to make a file with default value.     */    private static void loadProperties() {        if (properties == null) {            String archivingXML = JiveGlobals.getJiveHome() + File.separator +                "jive_archiving.xml";            // Make sure the file actually exists. If it doesn't, a new file            // will be created.            File file = new File(archivingXML);            // If it doesn't exists we have to create it.            if (!file.exists()) {                Document doc = new Document(new Element("jiveArchiving"));                // Now, write out to the file.                OutputStream out = null;                try {                    // Use JDOM's XMLOutputter to do the writing and formatting.                    XMLOutputter outputter = new XMLOutputter("    ", true);                    out = new BufferedOutputStream(new FileOutputStream(file));                    outputter.output(doc, out);                }                catch (Exception e) {                    e.printStackTrace();                }                finally {                    try {  out.close();  }                    catch (Exception e) { }                }            }            // Finally, create xml properties with the file.            try {                properties = new XMLProperties(archivingXML);            }            catch (IOException ioe) {                ioe.printStackTrace();            }        }    }    /**     * A task to be run in the TaskEngine that executes an archiving run. This does two things:<ul>     *      <li> Identifies threads that aren't already marked as archived that should be and marks     *          them as archived.     *      <li> Takes action on archived threads, which is defined by the archiving mode for     *          each forum.     * </ul>     */    public class ArchiveTask implements Runnable {        public void run() {            try {                // Acquire the busy lock and then set busy to true if another archive task isn't                // already running. If busy is already true, return.                synchronized (busyLock) {                    if (busy) {                        return;                    }                    else {                        busy = true;                    }                }                long archivedDate = System.currentTimeMillis();                properties.setProperty("lastArchivedDate", ""+archivedDate);                // Loop through all forums in the system.                DbForumFactory factory = DbForumFactory.getInstance();                for (Iterator i = factory.getRootForumCategory().recursiveForums(); i.hasNext(); ) {                    Forum forum = (Forum)i.next();                    if (isArchivingEnabled(forum)) {                        // Mark the list of threads in the forum as archived that haven't been                        // modified for at least archiveDays and that aren't already                        // marked as archived .                        Connection con = null;                        PreparedStatement pstmt = null;                        try {                            con = ConnectionManager.getConnection();                            // Get the number of days that threads bust be inactive before                            // they are marked as archived. The deafault is 180 days.                            int archiveDays = getArchiveDays(forum);                            String archiveDate = StringUtils.dateToMillis(new Date(                                    archivedDate - archiveDays*JiveGlobals.DAY));                            // For databases that support sub-selects, use a database query that                            // checks for old threads that aren't already marked as archived directly.                            if (ConnectionManager.supportsSubqueries) {                                pstmt = con.prepareStatement(ARCHIVABLE_THREADS);                                pstmt.setLong(1, forum.getID());                                pstmt.setString(2, archiveDate);                                pstmt.setLong(3, forum.getID());                                ResultSet rs = pstmt.executeQuery();                                while (rs.next()) {                                    try {                                        ForumThread thread = forum.getThread(rs.getLong(1));                                        // Mark the thread as archived.                                        thread.setProperty("archived", "true");                                    }                                    catch (ForumThreadNotFoundException e) { }                                    catch (UnauthorizedException e) { }                                }                            }                            // For databases that don't support sub-selects (such as MySQL), we first                            // have to get a list of all old threads and then individually examine                            // each of them to see if they are already marked as archived.                            else {                                pstmt = con.prepareStatement(ARCHIVABLE_THREADS_NO_SUBQUERIES);                                pstmt.setLong(1, forum.getID());                                pstmt.setString(2, archiveDate);                                ResultSet rs = pstmt.executeQuery();                                while (rs.next()) {                                    try {                                        ForumThread thread = forum.getThread(rs.getLong(1));                                        // If the thread isn't already marked as archived, do so.                                        if (!("true".equals(thread.getProperty("archived")))) {                                            thread.setProperty("archived", "true");                                        }                                    }                                    catch (ForumThreadNotFoundException e) { }                                    catch (UnauthorizedException e) { }                                }                            }                            // Next, we need to process all threads marked as archived depending on                            // the archiving "mode" of the forum -- do nothing, delete old content,                            // or move the content to another forum.                            int archiveMode = getArchiveMode(forum);                            // If the archive mode is MARK_ONLY, we don't need to take further action.                            // Otherwise, we need to delete or move the threads.                            if (archiveMode == ArchiveManager.DELETE_THREADS ||                                    archiveMode == ArchiveManager.MOVE_THREADS)                            {                                pstmt = con.prepareStatement(ARCHIVED_THREADS);                                pstmt.setLong(1, forum.getID());                                // If the archive mode is delete, perform deletes.                                if (archiveMode == ArchiveManager.DELETE_THREADS) {                                    ResultSet rs = pstmt.executeQuery();                                    while (rs.next()) {                                        try {                                            ForumThread thread = forum.getThread(rs.getLong(1));                                            forum.deleteThread(thread);                                        }                                        catch (ForumThreadNotFoundException e) {                                            e.printStackTrace();                                        }                                        catch (UnauthorizedException ue) {                                            ue.printStackTrace();                                        }                                    }                                }                                // Otherwise, we need to move the threads to the archiveForum defined                                // for this forum.                                else {                                    Forum archiveForum = getArchiveForum(forum);                                    // Only attempt the move if the archiveForum is defined correctly.                                    if (archiveForum != null) {                                        ResultSet rs = pstmt.executeQuery();                                        while (rs.next()) {                                            try {                                                ForumThread thread = forum.getThread(rs.getLong(1));                                                forum.moveThread(thread, archiveForum);                                            }                                            catch (ForumThreadNotFoundException e) {                                                e.printStackTrace();                                            }                                            catch (UnauthorizedException ue) {                                                ue.printStackTrace();                                            }                                        }                                    }                                    else {                                        System.err.println("Error! The archive mode for forum " +                                                forum + " is MOVE_THREADS, but an archive forum " +                                                "is not defined.");                                    }                                }                            }                        }                        catch (SQLException sqle) {                            sqle.printStackTrace();                        }                        finally {                            try { pstmt.close(); }                            catch (Exception e) { e.printStackTrace(); }                            try { con.close(); }                            catch (Exception e) { e.printStackTrace(); }                        }                    }                }            }            // Always release the busy lock.            finally {                synchronized (busyLock) {                     busy = false;                }            }        }    }}

⌨️ 快捷键说明

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