📄 updatemanager.java
字号:
/** * $RCSfile$ * $Revision: $ * $Date: $ * * Copyright (C) 2006 Jive Software. All rights reserved. * * This software is published under the terms of the GNU Public License (GPL), * a copy of which is included in this distribution. */package org.jivesoftware.wildfire.update;import org.apache.commons.httpclient.HostConfiguration;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.NameValuePair;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentFactory;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.jivesoftware.util.*;import org.jivesoftware.wildfire.MessageRouter;import org.jivesoftware.wildfire.XMPPServer;import org.jivesoftware.wildfire.container.BasicModule;import org.jivesoftware.wildfire.container.Plugin;import org.xmpp.packet.JID;import org.xmpp.packet.Message;import java.io.*;import java.util.*;/** * Service that frequently checks for new server or plugins releases. By default the service * will check every 48 hours for updates. Use the system property <tt>update.frequency</tt> * to set new values.<p> * <p/> * New versions of plugins can be downloaded and installed. However, new server releases * should be manually installed. * * @author Gaston Dombiak */public class UpdateManager extends BasicModule { protected static DocumentFactory docFactory = DocumentFactory.getInstance(); /** * URL of the servlet (JSP) that provides the "check for update" service. */ private static String updateServiceURL = "http://www.jivesoftware.org/wildfire/versions.jsp"; /** * Information about the available server update. */ private Update serverUpdate; /** * List of plugins that need to be updated. */ private Collection<Update> pluginUpdates = new ArrayList<Update>(); /** * List of plugins available at jivesoftware.org. */ private Map<String, AvailablePlugin> availablePlugins = new HashMap<String, AvailablePlugin>(); /** * Thread that performs the periodic checks for updates. */ private Thread thread; /** * Router to use for sending notitication messages to admins. */ private MessageRouter router; private String serverName; public UpdateManager() { super("Update manager"); } public void start() throws IllegalStateException { super.start(); startService(); } /** * Starts sevice that checks for new updates. */ private void startService() { // Thread that performs the periodic checks for updates thread = new Thread("Update Manager") { public void run() { try { // Sleep for 5 seconds before starting to work. This is required because // this module has a dependency on the PluginManager, which is loaded // after all other modules. Thread.sleep(5000); // Load last saved information (if any) loadSavedInfo(); while (isServiceEnabled()) { waitForNextCheck(); // Check if the service is still enabled if (isServiceEnabled()) { try { // Check for server updates checkForServerUpdate(true); // Refresh list of available plugins and check for plugin updates checkForPluginsUpdates(true); } catch (Exception e) { Log.error("Error checking for updates", e); } // Keep track of the last time we checked for updates. long now = System.currentTimeMillis(); JiveGlobals.setProperty("update.lastCheck", String.valueOf(now)); // As an extra precaution, make sure that that the value // we just set is saved. If not, return to make sure that // no additional update checks are performed until Wildfire // is restarted. if (now != JiveGlobals.getLongProperty("update.lastCheck", 0)) { Log.error("Error: update service check did not save correctly. " + "Stopping update service."); return; } } } } catch (InterruptedException e) { Log.error(e); } finally { // Clean up reference to this thread thread = null; } } private void waitForNextCheck() throws InterruptedException { long lastCheck = JiveGlobals.getLongProperty("update.lastCheck", 0); if (lastCheck == 0) { // This is the first time the server is used (since we added this feature) Thread.sleep(30000); } else { long elapsed = System.currentTimeMillis() - lastCheck; long frequency = getCheckFrequency() * JiveConstants.HOUR; // Sleep until we've waited the appropriate amount of time. while (elapsed < frequency) { Thread.sleep(frequency - elapsed); // Update the elapsed time. This check is necessary just in case the // thread woke up early. elapsed = System.currentTimeMillis() - lastCheck; } } } }; thread.setDaemon(true); thread.start(); } public void initialize(XMPPServer server) { super.initialize(server); router = server.getMessageRouter(); serverName = server.getServerInfo().getName(); } /** * Queries the jivesoftware.org server for new server and plugin updates. * * @param notificationsEnabled true if admins will be notified when new updates are found. * @throws Exception if some error happens during the query. */ public synchronized void checkForServerUpdate(boolean notificationsEnabled) throws Exception { // Get the XML request to include in the HTTP request String requestXML = getServerUpdateRequest(); // Send the request to the server HttpClient httpClient = new HttpClient(); // Check if a proxy should be used if (isUsingProxy()) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(getProxyHost(), getProxyPort()); httpClient.setHostConfiguration(hc); } PostMethod postMethod = new PostMethod(updateServiceURL); NameValuePair[] data = { new NameValuePair("type", "update"), new NameValuePair("query", requestXML) }; postMethod.setRequestBody(data); if (httpClient.executeMethod(postMethod) == 200) { // Process answer from the server String responseBody = postMethod.getResponseBodyAsString(); processServerUpdateResponse(responseBody, notificationsEnabled); } } public synchronized void checkForPluginsUpdates(boolean notificationsEnabled) throws Exception { // Get the XML request to include in the HTTP request String requestXML = getAvailablePluginsUpdateRequest(); // Send the request to the server HttpClient httpClient = new HttpClient(); // Check if a proxy should be used if (isUsingProxy()) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(getProxyHost(), getProxyPort()); httpClient.setHostConfiguration(hc); } PostMethod postMethod = new PostMethod(updateServiceURL); NameValuePair[] data = { new NameValuePair("type", "available"), new NameValuePair("query", requestXML) }; postMethod.setRequestBody(data); if (httpClient.executeMethod(postMethod) == 200) { // Process answer from the server String responseBody = postMethod.getResponseBodyAsString(); processAvailablePluginsResponse(responseBody, notificationsEnabled); } } /** * Download and install latest version of plugin. * * @param url the URL of the latest version of the plugin. * @return true if the plugin was successfully downloaded and installed. */ public boolean downloadPlugin(String url) { boolean installed = false; // Download and install new version of plugin HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); //execute the method try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode == 200) { //get the resonse as an InputStream InputStream in = getMethod.getResponseBodyAsStream(); String pluginFilename = url.substring(url.lastIndexOf("/") + 1); installed = XMPPServer.getInstance().getPluginManager() .installPlugin(in, pluginFilename); in.close(); if (installed) { // Remove the plugin from the list of plugins to update for (Update update : pluginUpdates) { if (update.getURL().equals(url)) { update.setDownloaded(true); } } // Save response in a file for later retrieval saveLatestServerInfo(); } } } catch (IOException e) { Log.warn("Error downloading new plugin version", e); } return installed; } /** * Returns true if the plugin downloaded from the specified URL has been downloaded. Plugins * may be downloaded but not installed. The install process may take like 30 seconds to * detect new plugins to install. * * @param url the URL of the latest version of the plugin. * @return true if the plugin downloaded from the specified URL has been downloaded. */ public boolean isPluginDownloaded(String url) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -