📄 gatewaymanager.java
字号:
* @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.");
}
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 filter Gateway to add to the gateeway 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 void saveGateways(boolean restartImports) {
// Delete the entire context.
deleteContext();
// 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
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 properties.
Map gatewayProps = BeanUtils.getProperties(gateways[i]);
for (Iterator iter = gatewayProps.keySet().iterator(); iter.hasNext(); ) {
String name = (String)iter.next();
String value = (String)gatewayProps.get(name);
properties.setProperty(gatewayContext + "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 intenral Jive classes.
*/
public void exportData(ForumMessage message) {
if (gateways.length == 0) {
return;
}
TaskEngine.addTask(new GatewayExportTask(message));
}
/**
* Deletes all properties for this context. This method is
* only intended to be called by other internal Jive classes.
*/
public void deleteContext() {
properties.deleteProperty(context.substring(0, context.length()-1));
}
/**
* 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 (!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.
properties = new XMLProperties(gatewayXML);
}
}
/**
* 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) {
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -