gatewaymanager.java

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

JAVA
621 行
字号
        return gateways[index];    }    /**     * Returns the count of currently active gateways for the forum.     *     * @return a count of the currently active filters.     */    public int getGatewayCount() {        return gateways.length;    }    /**     * Adds a new Gateway to the end of the gateway list.     *     * @param gateway Gateway to add to the gateway list.     */    public void addGateway(Gateway gateway) {        addGateway(gateway, gateways.length);    }    /**     * Inserts a new Gateway at specified index in the gateway list.     *     * @param gateway Gateway to add to the gateway list.     * @param index position in gateway list to insert new gateway.     */    public void addGateway(Gateway gateway, int index) {        ArrayList newGateways = new ArrayList(gateways.length+1);        for (int i=0; i<gateways.length; i++) {            newGateways.add(gateways[i]);        }        newGateways.add(index, gateway);        Gateway[] newArray = new Gateway[newGateways.size()];        for (int i=0; i<newArray.length; i++) {            newArray[i] = (Gateway)newGateways.get(i);        }        // Finally, overwrite gateways with the new array        gateways = newArray;        saveGateways();    }    /**     * Removes a Gateway at the specified index in the gateway list.     *     * @param index position in gateway list to remove gateway from.     */    public void removeGateway(int index) {        ArrayList newGateways = new ArrayList(gateways.length);        for (int i=0; i<gateways.length; i++) {            newGateways.add(gateways[i]);        }        newGateways.remove(index);        Gateway[] newArray = new Gateway[newGateways.size()];        for (int i=0; i<newArray.length; i++) {            newArray[i] = (Gateway)newGateways.get(i);        }        //Finally, overwrite gateways with the new array        gateways = newArray;        saveGateways();    }    /**     * Saves all Gateways to the persistent store. This method     * should be called after setting any properties on individual gateways     * that are being managed by this gateway manager.     */    public void saveGateways() {        saveGateways(true);    }    /**     * Saves all Gateways to the persistent store. This method     * should be called after setting any properties on individual gateways     * that are being managed by this gateway manager.     */    public synchronized void saveGateways(boolean restartImports) {        // Delete all properties.        properties.deleteProperty(context.substring(0, context.length()-1));        // Write out basic props.        properties.setProperty(context + "importEnabled", "" + importEnabled);        properties.setProperty(context + "exportEnabled", "" + exportEnabled);        properties.setProperty(context + "importInterval", "" + importInterval);        if (exportFooter != null) {            properties.setProperty(context + "exportFooter", exportFooter);        }        // Write out all data. First, the count.        if (gateways.length > 0) {            properties.setProperty(context + "gatewayCount",                    Integer.toString(gateways.length));            // Next, the lastImport date            if (importEnabled) {                properties.setProperty(context + "lastImport",                    String.valueOf(lastImport));            }        }        // Each gateway...        for (int i=0; i<gateways.length; i++) {            String gatewayContext = context + "gateway" + i + ".";            // Write out class name.            properties.setProperty(gatewayContext+"className",                    gateways[i].getClass().getName());            // Write out all importer properties.            GatewayImporter gatewayImporter = gateways[i].getGatewayImporter();            Map importGatewayProps = BeanUtils.getProperties(gatewayImporter);            String importContext = gatewayContext + "gatewayImporter.";            for (Iterator iter = importGatewayProps.keySet().iterator(); iter.hasNext(); ) {                String name = (String)iter.next();                String value = (String)importGatewayProps.get(name);                properties.setProperty(importContext + "properties." + name, value);            }            // Write out all exporter properties.            GatewayExporter gatewayExporter = gateways[i].getGatewayExporter();            Map exportGatewayProps = BeanUtils.getProperties(gatewayExporter);            String exportContext = gatewayContext + "gatewayExporter.";            for (Iterator iter = exportGatewayProps.keySet().iterator(); iter.hasNext(); ) {                String name = (String)iter.next();                String value = (String)exportGatewayProps.get(name);                properties.setProperty(exportContext + "properties." + name, value);            }        }        // Since the gateway information likely changed, restart the import task.        if (restartImports) {            this.startImportTask();        }    }    /**     * Exports an individual message through all gateways. This method is     * only intended to be called by other internal Jive classes.     */    public void exportData(ForumMessage message) {        if (gateways.length == 0 || exportEnabled == false) {            return;        }        TaskEngine.addTask(TaskEngine.LOW_PRIORITY, new GatewayExportTask(message));    }    /**     * Deletes all gateways for this context by removing their properties and     * stopping them from running.     */    public void delete() {        // Remove all properties.        properties.deleteProperty(context.substring(0, context.length()-1));        // Stop the import task        if (timerTask != null) {            timerTask.cancel();        }    }    /**     * Gets the export footer for a message, which is the footer with all tokens     * replaced with correct values.     */    public String getTranslatedFooter(ForumMessage message) {        if (exportFooter == null) {            return "";        }        String result = exportFooter;        ForumThread thread = message.getForumThread();        Forum forum = thread.getForum();        result = StringUtils.replace(result, "{threadID}", ""+thread.getID());        result = StringUtils.replace(result, "{threadName}", thread.getName());        result = StringUtils.replace(result, "{forumID}", ""+forum.getID());        result = StringUtils.replace(result, "{forumName}", forum.getName());        result = StringUtils.replace(result, "{messageID}", ""+message.getID());        return "\n" + result;    }    /**     * Start/restart the gateway import task     */    private void startImportTask() {        // if we've just disabled importing, disable the import task        if (!importEnabled && timerTask != null) {            timerTask.cancel();            return;        }        else if (!importEnabled) {            return;        }        // Restart timer with new schedule.        if (timerTask != null) {            timerTask.cancel();        }        timerTask = TaskEngine.scheduleTask(                new GatewayImportTask(),importInterval*JiveGlobals.MINUTE,                importInterval*JiveGlobals.MINUTE);    }    /**     * 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 gatewayXML = JiveGlobals.getJiveHome() + File.separator +                "jive_gateways.xml";            // Make sure the file actually exists. If it doesn't, a new file            // will be created.            File file = new File(gatewayXML);            // If it doesn't exists we have to create it.            if (!file.exists()) {                Document doc = new Document(new Element("jiveGateways"));                Element gatewayClasses = new Element("gatewayClasses");                doc.getRootElement().addContent(gatewayClasses);                // Add in default gateway classes.                for (int i=0; i<DEFAULT_GATEWAY_CLASSES.length; i++) {                    Element newClass = new Element("gateway" + i);                    newClass.setText(DEFAULT_GATEWAY_CLASSES[i]);                    gatewayClasses.addContent(newClass);                }                // 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(gatewayXML);            }            catch (IOException ioe) {                ioe.printStackTrace();            }        }    }    /**     * A task that performs an import for all installed gateways.     */    private class GatewayImportTask implements Runnable {        public void run() {            // If importing is turned off, return.            if (!importEnabled || gateways.length < 1) {                return;            }            long now = System.currentTimeMillis();            // Set the cutoff date for imports to be 24 hours before            // the last import. This will give us a good buffer since            // messages can get delayed in transport or other mail            // servers can have their time set wrong. It's still            // possible that we'll loose some messages even with a 24            // hour buffer, but this occurrence should be quite rare.            Date cutoffDate = new Date(lastImport - JiveGlobals.HOUR *24);            for (int i=0; i<gateways.length; i++) {                try {                    gateways[i].importData(cutoffDate);                }                catch (GatewayException ge) {                    ge.printStackTrace();                }            }            // Done with the import so set the new import timestamp.            lastImport = now;            // Save the timestamp.            properties.setProperty(context + "lastImport", String.valueOf(lastImport));        }    }    /**     * A task that exports a message through all installed gateways.     */    private class GatewayExportTask implements Runnable {        ForumMessage message;        GatewayExportTask(ForumMessage message) {            this.message = message;        }        public void run() {            // If exporting is turned off, return.            if (!exportEnabled) {                return;            }            for (int i=0; i<gateways.length; i++) {                try {                    gateways[i].exportData(message);                }                catch (GatewayException ge) {                    ge.printStackTrace();                }            }        }    }}

⌨️ 快捷键说明

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