gatewaymanager.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 621 行 · 第 1/2 页
JAVA
621 行
/** * $RCSfile: GatewayManager.java,v $ * $Revision: 1.8 $ * $Date: 2002/06/21 05:55:59 $ * * Copyright (C) 1999-2002 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 java.util.*;import java.util.TimerTask;import java.io.*;import java.lang.reflect.Constructor;import com.jivesoftware.jdom.*;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 GatewayManager. 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); gateways[i] = (Gateway)constructor.newInstance( new Object[] { factory, forum }); } catch (NoSuchMethodException e) { gateways[i] = (Gateway)gClass.newInstance(); } // Load import gateway properties. String importContext = gatewayContext + "gatewayImporter."; String [] importPropNames = properties.getChildrenProperties( importContext + "properties"); Map importGatewayProps = new HashMap(); for (int j=0; j<importPropNames.length; j++) { // Get the bean property name, which is everything after // the last '.' in the xml property name. importGatewayProps.put(importPropNames[j], properties.getProperty( importContext + "properties." + importPropNames[j])); } // Set properties on the bean GatewayImporter gatewayImporter = gateways[i].getGatewayImporter(); BeanUtils.setProperties(gatewayImporter, importGatewayProps); // Load export gateway properties. String exportContext = gatewayContext + "gatewayExporter."; String [] exportPropNames = properties.getChildrenProperties( exportContext + "properties"); Map exportGatewayProps = new HashMap(); for (int j=0; j<exportPropNames.length; j++) { // Get the bean property name, which is everything after // the last '.' in the xml property name. exportGatewayProps.put(exportPropNames[j], properties.getProperty( exportContext + "properties." + exportPropNames[j])); } // Set properties on the bean GatewayExporter gatewayExporter = gateways[i].getGatewayExporter(); BeanUtils.setProperties(gatewayExporter, exportGatewayProps); } 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); this.startImportTask(); } /** * 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); saveGateways(); } /** * 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. * * @return the filter at the specified index. */ public Gateway getGateway(int index) { if (index < 0 || index > gateways.length-1) { throw new IllegalArgumentException("Index " + index + " is not valid."); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?