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

📄 gatewaymanager.java

📁 jive 2.1.1 的源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * $RCSfile: GatewayManager.java,v $
 * $Revision: 1.11 $
 * $Date: 2001/09/22 17:43:18 $
 *
 * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved.
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */

package com.jivesoftware.forum.gateway;

import com.jivesoftware.forum.*;
import com.jivesoftware.forum.util.*;
import com.jivesoftware.util.*;
import com.jivesoftware.util.TimerTask;

import java.util.*;
import java.io.*;
import java.lang.reflect.Constructor;

import com.jivesoftware.jdom.*;
import com.jivesoftware.jdom.input.*;
import com.jivesoftware.jdom.output.*;

/**
 * Manages gateways for a forum, which synchronize the forum with external
 * data sources such as an NNTP newsgroup or email account.
 *
 * Notes: The default GatewayManager implementation stores configuration
 * properties in a file called <tt>gateway_config.xml</tt> under the
 * </tt>jiveHome</tt> directory. The default implementation also only knows how
 * to instantiate Gateway implementations that have either a default constructor
 * or one that accepts a ForumFactory and Forum as arguments.
 *
 * @see Gateway
 */
public class GatewayManager {

    /**
     * The list of the class names of the gateways that are available for
     * installation by default. These values are automatically added into
     * the <tt>jive_gateways.xml</tt> file when it is first created. Further
     * classes can be defined by editing the <tt>jiveGateways.gatewayClasses</tt>
     * property.
     */
    public static final String [] DEFAULT_GATEWAY_CLASSES = new String [] {
            "com.jivesoftware.forum.gateway.EmailGateway",
            "com.jivesoftware.forum.gateway.NewsgroupGateway"
        };

    private static XMLProperties properties = null;

    private Gateway[] gateways;


    /**
     * Context for this manager instance in the XML property file.
     */
    private String context = null;
    /**
     * The scheduled task for gateway imports.
     */
    private TimerTask timerTask = null;
    private long lastImport;

    private boolean importEnabled = true;
    private boolean exportEnabled = true;
    private String exportFooter = null;
    private int importInterval = 15;

    /**
     * Creates a new DbGatewayManager. It will load existing values from the
     * persistent store.
     */
    public GatewayManager(ForumFactory factory, Forum forum) {
        loadProperties();
        String name = "forum" + forum.getID();
        context = name + ".";

        // load up gateways for this manager.
        String gCount = properties.getProperty(context + "gatewayCount");
        if (gCount == null) {
            gCount = "0";
        }
        int gatewayCount = 0;
        try {
            gatewayCount = Integer.parseInt(gCount);
        }
        catch (NumberFormatException nfe) { }

        // See if the time that the last import was performed is stored.
        String importTimestamp = properties.getProperty(context + "lastImport");
        if (importTimestamp != null) {
            try {
                lastImport = Long.parseLong(importTimestamp);
            }
            catch (NumberFormatException nfe) {
                lastImport = System.currentTimeMillis();
            }
        }
        // Not stored, so create new timestamp.
        else {
            lastImport = System.currentTimeMillis();
        }

        // Load up all gateways
        gateways = new Gateway[gatewayCount];

        for (int i=0; i<gatewayCount; i++) {
            try {
                String gatewayContext = context + "gateway" + i + ".";
                String className = properties.getProperty(gatewayContext + "className");
                Class gClass = Class.forName(className);
                // First, attempt to instantiate the Gateway with a ForumFactory
                // and Forum as paramaters. If that doesn't work, we'll try
                // the default constructor.
                try {
                    Class [] params = new Class [] { ForumFactory.class,
                            Forum.class};
                    Constructor constructor = gClass.getConstructor(params);
                    // Intantiate the gateway object. We assume that
                    gateways[i] = (Gateway)constructor.newInstance(
                            new Object[] { factory, forum });
                }
                catch (NoSuchMethodException e) {
                    gateways[i] = (Gateway)gClass.newInstance();
                }
                // Load gateway properties.
                String [] propNames = properties.getChildrenProperties(
                        gatewayContext + "properties");
                Map gatewayProps = new HashMap();
                for (int j=0; j<propNames.length; j++) {
                    // Get the bean property name, which is everything after
                    // the last '.' in the xml property name.
                    gatewayProps.put(propNames[j], properties.getProperty(
                            gatewayContext + "properties." + propNames[j]));
                }
                // Set properties on the bean
                BeanUtils.setProperties(gateways[i], gatewayProps);
            }
            catch (Exception e) {
                System.err.println("Error loading gateway " + i + " for context "
                        + context);
                e.printStackTrace();
            }
        }

        // Now load general properties
        try {
            String propValue = properties.getProperty(context + "importEnabled");
            if (propValue != null) {
                this.importEnabled = Boolean.valueOf(propValue).booleanValue();
            }
            propValue = properties.getProperty(context + "exportEnabled");
            if (propValue != null) {
                this.exportEnabled = Boolean.valueOf(propValue).booleanValue();
            }
            propValue = properties.getProperty(context + "importInterval");
            if (propValue != null) {
                this.setImportInterval(Integer.parseInt(propValue));
            }
            exportFooter = properties.getProperty(context + "exportFooter");
        }
        catch (NumberFormatException nfe) { /* ignore */ }

        this.startImportTask();
    }

    /**
     * Returns true if gateway importing is turned on. When importing is on,
     * the importData method of each installed gateway will be invoked at the
     * specified import interval. By default, importing is enabled.
     *
     * @return true if gateway imports are enabled.
     */
    public boolean isImportEnabled() {
        return importEnabled;
    }

    /**
     * Toggles gateway importing on or off. When importing is on,
     * the importData method of each installed gateway will be invoked at the
     * specified import interval. By default, importing is enabled.
     *
     * @param importEnabled true if gateway importing should be enabled.
     */
    public void setImportEnabled(boolean importEnabled) {
        this.importEnabled = importEnabled;
        properties.setProperty(context + "importEnabled", "" + importEnabled);
    }

    /**
     * Returns true if gateway exporting is turned on. When exporting is on,
     * the exportData method of each installed gateway will be invoked on each
     * new message that is created in the forum. By default, exporting is
     * enabled.
     *
     * @return true if gateway exports are enabled.
     */
    public boolean isExportEnabled() {
        return exportEnabled;
    }

    /**
     * Toggles gateway exporting on or off. When exporting is on,
     * the exportData method of each installed gateway will be invoked on each
     * new message that is created in the forum. By default, exporting is
     * enabled.
     *
     * @param exportEnabled true if gateway exporting should be enabled.
     */
    public void setExportEnabled(boolean exportEnabled) {
        this.exportEnabled = exportEnabled;
        properties.setProperty(context + "exportEnabled", "" + exportEnabled);
    }

    /**
     * Returns the footer that will be appended to messages as they're exported.
     * By default, this value is <tt>null</tt>, which means that no footer will
     * be used.<p>
     *
     * A number of tokens can be inserted into the filter. Each token will be
     * dynamically replaced as the message is sent out with the real value.
     * Valid tokens are:<ul>
     *      {threadID}, {threadName}, {forumID}, {forumName}, {messageID}
     * </ul>
     *
     * @return the footer that will be appended to messages being exported.
     */
    public String getExportFooter() {
        return exportFooter;
    }

    /**
     * Sets the footer that will be appended to messages as they're exported.
     * By default, this value is <tt>null</tt>, which means that no footer will
     * be used.<p>
     *
     * A number of tokens can be inserted into the filter. Each token will be
     * dynamically replaced as the message is sent out with the real value.
     * Valid tokens are:<ul>
     *      {threadID}, {threadName}, {forumID}, {forumName}, {messageID}
     * </ul>
     *
     * @param exportFooter the footer that will be appended to messages being
     *      exported.
     */
    public void setExportFooter(String exportFooter) {
        this.exportFooter = exportFooter;
        // If we are setting the value to null, we should delete the property.
        if (exportFooter == null) {
            properties.deleteProperty(context + "exportFooter");
        }
        // Otherwise, set the property with the new value.
        else {
            properties.setProperty(context + "exportFooter", exportFooter);
        }
    }

    /**
     * Returns the number of minutes that the manager waits between each
     * gateway import. Default is 15 minutes.
     *
     * @return the number of minutes between automatic index updates.
     */
    public int getImportInterval() {
        return importInterval;
    }

    /**
     * Sets the number of minutes that the manager waits between each
     * gateway import. Default is 15 minutes.
     */
    public void setImportInterval(int importInterval) {
        this.importInterval = importInterval;
        properties.setProperty(context + "importInterval", "" + importInterval);
    }

    /**
     * Returns the Gateway at the specified index.
     *

⌨️ 快捷键说明

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