📄 extensionstore.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.extensions.store;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessages;
import org.jdom.Element;
import org.jdom.JDOMException;
import com.sslexplorer.agent.AgentExtensionDefinition;
import com.sslexplorer.agent.AgentVerifier;
import com.sslexplorer.boot.ContextHolder;
import com.sslexplorer.boot.RepositoryFactory;
import com.sslexplorer.boot.RepositoryStore;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.BundleActionMessage;
import com.sslexplorer.core.CoreAttributeConstants;
import com.sslexplorer.core.CoreEvent;
import com.sslexplorer.core.CoreEventConstants;
import com.sslexplorer.core.CoreMessageResources;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;
import com.sslexplorer.core.GlobalWarning;
import com.sslexplorer.core.LicenseAgreement;
import com.sslexplorer.extensions.ExtensionBundle;
import com.sslexplorer.extensions.ExtensionDescriptor;
import com.sslexplorer.extensions.ExtensionType;
import com.sslexplorer.extensions.UnknownExtensionTypeException;
import com.sslexplorer.extensions.types.PluginType;
import com.sslexplorer.plugin.Plugin;
import com.sslexplorer.plugin.PluginVersion;
import com.sslexplorer.security.Constants;
import com.sslexplorer.security.User;
import com.sslexplorer.setup.LicenseAgreementCallback;
import com.sslexplorer.util.ZipExtract;
public class ExtensionStore {
private static final String DIRS_TO_REMOVE = "dirsToRemove";
public static final String ARCHIVE_STORE = "archives";
public static final String INSTALLED_CATEGORY = "Installed";
String certHost = null;
File basedir;
File agentLocation;
HashMap extensionBundles;
List extensionBundlesList;
static Log log = LogFactory.getLog(ExtensionStore.class);
ExtensionDescriptor agentDescriptor;
ExtensionStoreDescriptor downloadableExtensions;
Calendar downloadableExtensionsLastUpdated;
int permissionIdx;
static ExtensionStore instance = null;
public final static Preferences PREFS = ContextHolder.getContext().getPreferences().node("extensions");
public final static Preferences STORE_PREF = PREFS.node("store");
public final static Preferences VERSION_PREFS = PREFS.node("versions");
HashMap agentExtensions = new HashMap();
boolean repositoryBacked;
ArrayList unknownExtensions = new ArrayList();
ExtensionStore() {
}
public static ExtensionStore getInstance() {
if (instance == null) {
instance = new ExtensionStore();
}
return instance;
}
public File getExtensionStoreDirectory() {
return basedir;
}
public boolean isRepositoryBacked() {
return repositoryBacked;
}
public void init(File basedir, File vpnClientLocation) throws IOException {
// Get if the application store comes from the repository
repositoryBacked = "true".equals(System.getProperty("sslexplorer.localApplicationStore.repositoryBacked", "true"));
this.basedir = basedir;
this.agentLocation = vpnClientLocation;
this.extensionBundles = new HashMap();
this.extensionBundlesList = new ArrayList();
if (repositoryBacked) {
RepositoryStore store = RepositoryFactory.getRepository().getStore("archives");
/**
* Remove the existing extensions
*/
if (basedir.exists()) {
Util.delTree(basedir);
}
/**
* Now recreate all extensions from the repository
*/
basedir.mkdirs();
String[] archives = store.listEntries();
for (int i = 0; i < archives.length; i++) {
if (log.isInfoEnabled()) {
log.info("Extracting archive " + archives[i]);
}
try {
ZipExtract.extractZipFile(basedir, store.getEntryInputStream(archives[i]));
if (log.isInfoEnabled()) {
log.info("Completed archive extraction for extension " + archives[i]);
}
} catch (IOException ex) {
log.error("Error extracting archive for extension " + archives[i], ex);
Util.delTree(new File(basedir, archives[i]));
}
}
}
//
try {
loadAll();
} catch (Exception e) {
log.error("Failed extract extension bundles from repository.", e);
}
// Now run the installer on all extensions
for (Iterator i = extensionBundlesList.iterator(); i.hasNext();) {
ExtensionBundle bundle = (ExtensionBundle) i.next();
String ver = VERSION_PREFS.get(bundle.getId(), "");
if (ver.equals("") || !ver.equals(bundle.getVersion().toString())) {
//
try {
bundle.getInstaller().doInstall();
VERSION_PREFS.put(bundle.getId(), bundle.getVersion().toString());
} catch (Exception e) {
IOException ioe = new IOException("Installer for extension bundle " + bundle.getId() + " failed.");
ioe.initCause(e);
throw ioe;
}
}
}
/*
* First remove any plugins whose uninstallation may have been deferred
* until
*/
if (!repositoryBacked) {
String dirsToRemove = STORE_PREF.get(DIRS_TO_REMOVE, "");
if (!dirsToRemove.equals("")) {
StringTokenizer t = new StringTokenizer(dirsToRemove, ",");
while (t.hasMoreTokens()) {
File dir = new File(t.nextToken());
if (dir.exists()) {
if (log.isInfoEnabled())
log.info("Removing extension " + dir.getAbsolutePath());
Util.delTree(dir);
}
}
STORE_PREF.remove(DIRS_TO_REMOVE);
}
/*
* Check for extension updates
*/
File updatedExtensionsDir = getUpdatedExtensionsDirectory();
File[] extensions = updatedExtensionsDir.listFiles();
if (extensions != null) {
for (int i = 0; i < extensions.length; i++) {
File destDir = new File(ContextHolder.getContext().getApplicationDirectory(), extensions[i].getName());
if (destDir.exists()) {
if (log.isInfoEnabled())
log.info("Removing extension " + destDir.getAbsolutePath());
if (!Util.delTree(destDir)) {
throw new IOException("Failed to remove old extension " + destDir.getAbsolutePath());
}
}
if (log.isInfoEnabled())
log.info("Moving " + extensions[i].getAbsolutePath() + " to " + destDir.getAbsolutePath());
if (!extensions[i].renameTo(destDir)) {
throw new IOException("Failed to rename extension " + extensions[i].getAbsolutePath() + " to "
+ destDir.getAbsolutePath());
}
}
}
}
/*
* Remove the versions of any extensions that no longer exist
*/
String[] k;
try {
k = VERSION_PREFS.keys();
for (int i = 0; i < k.length; i++) {
File destDir = new File(ContextHolder.getContext().getApplicationDirectory(), k[i]);
if (destDir.exists()) {
VERSION_PREFS.remove(k[i]);
}
}
} catch (BackingStoreException e) {
log.warn("Could not clean up extension versions preferences node.", e);
}
}
public List getAllAvailableExtensionBundles() {
List all = new ArrayList(extensionBundlesList);
try {
ExtensionStoreDescriptor descriptor = getDownloadableExtensionStoreDescriptor(downloadableExtensions != null);
if (descriptor != null && descriptor.getExtensionBundles() != null) {
for (Iterator i = descriptor.getExtensionBundles().iterator(); i.hasNext();) {
ExtensionBundle bundle = (ExtensionBundle) i.next();
// If the app is already installed, remove dont include it
// in the list
if (!extensionBundles.containsKey(bundle.getId())) {
all.add(bundle);
}
}
}
} catch (Exception e) {
log.error("Failed to get downloadable extensions.", e);
}
Collections.sort(all);
return all;
}
public URLConnection downloadExtension(String id) throws IOException {
URL downloadURL = null;
String sslxVersion = System.getProperty("sslexplorer.forceVersion", ContextHolder.getContext().getVersion().toString());
try {
String location = System.getProperty("sslexplorer.downloadableApplications.location",
"http://3sp.com/getApplicationZip.do?id=${id}&version=${version}");
location = location.replaceAll("\\$\\{id\\}", id);
location = location.replaceAll("\\$\\{version\\}", sslxVersion);
downloadURL = new URL(location);
} catch (MalformedURLException murle) {
try {
String path = System.getProperty("sslexplorer.downloadableApplications.location");
path = path.replaceAll("\\$\\{id\\}", id);
downloadURL = new File(path).toURL();
} catch (MalformedURLException e) {
log
.error("Invalid downloadable extension location specified in system property sslexplorer.downloadableApplicationStore.location, '"
+ System.getProperty("sslexplorer.downloadableApplicationStore.location")
+ "'. Must be either a URL or the file path of the store descriptor file.");
}
}
if (downloadURL != null) {
if (log.isInfoEnabled())
log.info("Downloading extension from " + downloadURL.toExternalForm());
URLConnection con = downloadURL.openConnection();
con.connect();
return con;
} else {
throw new IOException("No valid download location for " + id);
}
}
public void resetExtensionStoreUpdate() {
downloadableExtensions = null;
}
public ExtensionStoreDescriptor getDownloadableExtensionStoreDescriptor(boolean connect) throws JDOMException, IOException {
if (downloadableExtensions != null && downloadableExtensionsLastUpdated != null) {
Calendar n = ((Calendar) downloadableExtensionsLastUpdated.clone());
n.add(Calendar.DAY_OF_MONTH, 1);
if (new GregorianCalendar().after(n)) {
if (log.isInfoEnabled())
log.info("Downloadable extensions are out of date, will contact the update site again.");
downloadableExtensions = null;
}
}
if (downloadableExtensions == null && connect) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -