📄 updatechecker.java
字号:
package com.sslexplorer.core;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sslexplorer.boot.Util;
import com.sslexplorer.boot.VersionInfo;
import com.sslexplorer.extensions.ExtensionBundle;
import com.sslexplorer.extensions.store.ExtensionStore;
import com.sslexplorer.extensions.store.ExtensionStoreDescriptor;
import com.sun.syndication.io.SyndFeedInput;
public class UpdateChecker extends Thread {
final static Log log = LogFactory.getLog(UpdateChecker.class);
private VersionInfo.Version availableVersion;
private Map feeds;
private List updateable;
public UpdateChecker() {
super("UpdateThread");
feeds = new HashMap();
setPriority(Thread.MIN_PRIORITY);
}
public void run() {
do {
if (log.isInfoEnabled())
log.info("Checking for SSL-Explorer and Extension updates");
long checkAgainIn = 1000 * 60 * 60 * 24; // 1 day
try {
// Check for core update
if ("true".equals(CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null,
"updates.checkForCoreUpdates"))) {
checkAvailableVersion();
}
// Check for extensions update
if ("true".equals(CoreServlet.getServlet().getPropertyDatabase().getProperty(0, null,
"updates.checkForExtensionUpdates"))) {
checkAvailableExtensions();
}
} catch (Exception e) {
checkAgainIn = 1000 * 60 * 60; // 1 hour
log.error("Failed to check available version / extension updates. Will try again in 1 hour", e);
}
retrieveFeeds();
try {
if (log.isInfoEnabled())
log.info("Finished checking for updates / feeds, next check will occur at "
+ DateFormat.getDateTimeInstance().format(
new Date(System.currentTimeMillis() + checkAgainIn)));
Thread.sleep(checkAgainIn);
} catch (InterruptedException ie) {
}
} while (true);
}
/**
* Get a list of available RSS feeds as {@link CoreFeed} objects.
*
* @return list of feeds
*/
/*public List getFeeds() {
return feeds;
}*/
public CoreFeed getFeed(String feedName) {
if(System.getProperty("rssFeeds.disable", "false").equalsIgnoreCase("true"))
return null;
synchronized(feeds) {
if(!feeds.containsKey(feedName) && !feedName.equals("${rssFeed}")) {
SyndFeedInput input = new SyndFeedInput();
try {
feeds.put(feedName, new CoreFeed(feedName, input,
new URL(System.getProperty("rssFeeds.baseURL", "http://3sp.com/feeds") + "/" + feedName + ".xml")));
} catch(MalformedURLException ex) { }
}
return (CoreFeed)feeds.get(feedName);
}
}
/**
* Check what version there is a new version of the SSL-Explorer core
* available. This will only be actually retrieved from the 3SP site after a
* restart or if a day has elapsed since the last update.
*
* @return last available core version
*/
public VersionInfo.Version getAvailableCoreVersion() {
return availableVersion;
}
/**
* Returns a list of {@link String}s contain extension bundles names that
* may be updated.
*
* @return list of updateable extensions
* @throws Exception on any error
*/
public List getUpdateableExtensions() {
return updateable;
}
private void retrieveFeeds() {
if (log.isInfoEnabled())
log.info("Getting latest RSS feeds");
HashMap updatedFeeds = new HashMap();
synchronized (feeds) {
for(Iterator it = feeds.keySet().iterator(); it.hasNext();) {
String feedName = (String) it.next();
SyndFeedInput input = new SyndFeedInput();
try {
updatedFeeds.put(feedName, new CoreFeed(feedName, input, new URL("http://3sp.com/feeds/" + feedName + ".xml")));
} catch(MalformedURLException ex) { }
}
feeds.clear();
}
this.feeds = updatedFeeds;
}
private void checkAvailableExtensions() throws Exception {
if (log.isInfoEnabled())
log.info("Checking for available extension updates.");
ExtensionStoreDescriptor descriptor = ExtensionStore.getInstance().getDownloadableExtensionStoreDescriptor(true);
updateable = new ArrayList();
for (Iterator i = descriptor.getExtensionBundles().iterator(); i.hasNext();) {
ExtensionBundle bundle = (ExtensionBundle) i.next();
if (ExtensionStore.getInstance().isExtensionLoaded(bundle.getId())) {
ExtensionBundle installed = ExtensionStore.getInstance().getExtensionBundle(bundle.getId());
if (bundle.getVersion().compareTo(installed.getVersion()) > 0) {
updateable.add(bundle);
}
}
}
}
private void checkAvailableVersion() throws IOException {
if (log.isInfoEnabled())
log.info("Checking for SSL-Explorer core updates.");
URL availableVersionURL = null;
try {
String location = System.getProperty("sslexplorer.availableVersion.location", "http://3sp.com/getAvailableVersion.do");
availableVersionURL = new URL(location);
} catch (MalformedURLException murle) {
try {
availableVersionURL = new File(System.getProperty("sslexplorer.availableVersion.location")).toURL();
} catch (MalformedURLException e) {
log
.error("Invalid available version action location specified in system property sslexplorer.availableVersion.location, '"
+ System.getProperty("sslexplorer.availableVersion.location")
+ "'. Must be either a URL or the file path of the available version document.");
}
}
if (availableVersionURL != null) {
if (log.isInfoEnabled())
log.info("Loading available version from " + availableVersionURL.toExternalForm());
InputStream in = null;
try {
in = availableVersionURL.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
availableVersion = new VersionInfo.Version(br.readLine());
} finally {
Util.closeStream(in);
}
if (log.isInfoEnabled())
log.info("Version " + availableVersion + " is the latest availabble.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -