📄 extensionbundle.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;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import antlr.collections.impl.Vector;
import com.sslexplorer.boot.VersionInfo;
import com.sslexplorer.boot.VersionInfo.Version;
/**
* @author brett
*/
public class ExtensionBundle extends ArrayList implements Comparable {
final static Log log = LogFactory.getLog(ExtensionBundle.class);
public final static int TYPE_UPDATEABLE = 0;
public final static int TYPE_INSTALLED = 1;
public final static int TYPE_INSTALLABLE = 2;
public final static int TYPE_CONFIGUREABLE = 3;
public final static int TYPE_PENDING_REMOVAL = 4;
public static final int TYPE_PENDING_INSTALLATION = 5;
public static final int TYPE_PENDING_UPDATE = 6;
private File descriptor;
private Document doc;
protected String description;
protected String license;
protected String productURL;
protected String instructionsURL;
protected VersionInfo.Version version;
private int type;
private String id;
private String name;
private String licenseFilePath;
private VersionInfo.Version requiredHostVersion;
private ExtensionInstaller installer;
private String depends;
private String category;
public ExtensionBundle(File descriptor) {
this.descriptor = descriptor;
}
/**
* @param version2
* @param i
* @param id
* @param name
* @param description2
* @param license2
* @param productURL2
* @param instructionsURL2
*/
public ExtensionBundle(Version version, int type, String id, String name, String description, String license,
String productURL, String instructionsURL, VersionInfo.Version requiredHostVersion, String depends, String category) {
this.version = version;
this.type = type;
this.id = id;
this.name = name;
this.description = description;
this.license = license;
this.productURL = productURL;
this.instructionsURL = instructionsURL;
this.requiredHostVersion = requiredHostVersion;
this.depends = depends;
this.category = category;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public File getFile() {
return descriptor;
}
public String getCategory() {
return category;
}
public Enumeration getDependencies() {
if(depends==null || depends.equals("")) {
return new Vector().elements();
} else {
return new StringTokenizer(depends, ",");
}
}
public void load() throws JDOMException, IOException, UnknownExtensionTypeException {
clear();
installer = new ExtensionInstaller(this);
SAXBuilder sax = new SAXBuilder();
doc = sax.build(descriptor);
if (doc.getRootElement().getName().equals("bundle")) {
Attribute a = doc.getRootElement().getAttribute("id");
id = a == null ? null : a.getValue();
if (id == null) {
throw new JDOMException("<bundle> element requires attribute 'id'");
}
if (log.isDebugEnabled())
log.debug("Application bundle id is " + id);
name = doc.getRootElement().getAttribute("name").getValue();
if (log.isDebugEnabled())
log.debug("Application bundle name is " + name);
licenseFilePath = doc.getRootElement().getAttributeValue("licenseAgreement");
if (name == null) {
throw new JDOMException("<bundle> element requires the attribute 'name'");
}
for (Iterator i = doc.getRootElement().getChildren().iterator(); i.hasNext();) {
Element e = (Element) i.next();
if (e.getName().equalsIgnoreCase("description")) {
description = e.getText();
}
else if(e.getName().equalsIgnoreCase("install")) {
processInstall(e);
}
else if (e.getName().equalsIgnoreCase("messages")) {
// processed later
} else if (e.getName().equals("application") || e.getName().equals("extension")) {
ExtensionDescriptor desc = new ExtensionDescriptor();
desc.load(this, e);
add(desc);
} else {
throw new JDOMException(
"<bundle> element may only contain <description> or <extension> (or the deprecated <application>) elements.");
}
}
// Look for the bundle messages first. These will be added to every single application in the bundle
for (Iterator i = doc.getRootElement().getChildren().iterator(); i.hasNext();) {
Element e = (Element) i.next();
if (e.getName().equalsIgnoreCase("messages")) {
for (Iterator i2 = e.getChildren().iterator(); i2.hasNext();) {
Element el = (Element) i2.next();
if (!el.getName().equals("message")) {
throw new JDOMException("<messages> element may only contain <message> elements.");
}
String key = el.getAttributeValue("key");
if (key == null) {
throw new JDOMException("<message> element must have a key attribute.");
}
for(Iterator i3 = iterator(); i3.hasNext(); ) {
ExtensionDescriptor descriptor = (ExtensionDescriptor)i3.next();
String aKey = "application." + descriptor.getId() + "." + key;
if(descriptor.getMessageResources() != null) {
if(!descriptor.getMessageResources().isPresent(key)) {
descriptor.getMessageResources().setMessage(el.getAttributeValue("locale"), aKey, el.getText());
}
}
}
}
}
}
} else if (doc.getRootElement().getName().equals("application") || doc.getRootElement().getName().equals("extension")) {
ExtensionDescriptor desc = new ExtensionDescriptor();
desc.load(this, doc.getRootElement());
id = desc.getId();
name = desc.getName();
description = desc.getDescription();
add(desc);
} else {
throw new JDOMException(
"Application bundle root element must be <bundle> or <extension> (or the deprecated <application>) elements.");
}
setType(TYPE_INSTALLED); // All we know is that the application is
// installed until the application store is
// available
license = doc.getRootElement().getAttributeValue("license");
license = license == null || license.equals("") ? "Unknown" : license;
if (log.isDebugEnabled())
log.debug("Application bundle license is " + license);
productURL = doc.getRootElement().getAttributeValue("productURL");
instructionsURL = doc.getRootElement().getAttributeValue("instructionsURL");
String ver = doc.getRootElement().getAttributeValue("version");
if (ver == null || ver.equals("")) {
throw new JDOMException("<applications> element requires the attribute 'version'.");
}
version = new VersionInfo.Version(ver);
}
public ExtensionInstaller getInstaller() {
return installer;
}
public void processInstall(Element installElement) throws JDOMException, IOException {
for (Iterator i = installElement.getChildren().iterator(); i.hasNext();) {
Element e = (Element) i.next();
if (e.getName().equalsIgnoreCase("mkdir")) {
String dir = e.getText();
if(dir == null || dir.equals("")) {
throw new JDOMException("<mkdir> contents must be the name of a directory to create.");
}
installer.addOp(new ExtensionInstaller.MkdirInstallOp(dir));
}
else if (e.getName().equalsIgnoreCase("cp")) {
String from = e.getText();
String to = e.getAttributeValue("to");
String toDir = e.getAttributeValue("toDir");
if(from == null || from.equals("") || ( ( to == null || to.equals("") ) && ( toDir == null || toDir.equals("") ) ) ) {
throw new JDOMException("<cp> content must be the source path and the tag must have either a to or toDir attribute.");
}
installer.addOp(new ExtensionInstaller.CpInstallOp(from, to, toDir));
}
else if (e.getName().equalsIgnoreCase("rm")) {
String path = e.getText();
if(path == null || path.equals("")) {
throw new JDOMException("<rm> must a path as its content.");
}
installer.addOp(new ExtensionInstaller.RmInstallOp(path));
}
/*
* for (Iterator i2 = e.getChildren().iterator(); i2.hasNext();) {
Element el = (Element) i2.next();
if (!el.getName().equals("message")) {
throw new JDOMException("<messages> element may only contain <message> elements.");
}
String key = el.getAttributeValue("key");
if (key == null) {
throw new JDOMException("<message> element must have a key attribute.");
}
for(Iterator i3 = iterator(); i3.hasNext(); ) {
ExtensionDescriptor descriptor = (ExtensionDescriptor)i3.next();
String aKey = "application." + descriptor.getId() + "." + key;
if(descriptor.getMessageResources() != null) {
if(!descriptor.getMessageResources().isPresent(key)) {
descriptor.getMessageResources().setMessage(el.getAttributeValue("locale"), aKey, el.getText());
}
}
}
}
*/
}
}
public String getInstructionsURL() {
return instructionsURL;
}
public String getProductURL() {
return productURL;
}
public VersionInfo.Version getVersion() {
return version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setLicense(String license) {
this.license = license;
}
public String getLicense() {
return license;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public File getBaseDir() {
return getFile() == null ? null : getFile().getParentFile();
}
/**
* @param application
* @return
*/
public boolean containsApplication(String application) {
for (Iterator i = iterator(); i.hasNext();) {
if (((ExtensionDescriptor) i.next()).getId().equals(application)) {
return true;
}
}
return false;
}
/**
* @param id2
* @return
*/
public ExtensionDescriptor getApplicationDescriptor(String id) {
for (Iterator i = iterator(); i.hasNext();) {
ExtensionDescriptor app = (ExtensionDescriptor) i.next();
if (app.getId().equals(id)) {
return app;
}
}
return null;
}
public int compareTo(Object arg0) {
int c = getType() - ((ExtensionBundle) arg0).getType();
return c != 0 ? c : name.compareTo(((ExtensionBundle) arg0).name);
}
public File getLicenseFile() {
File baseDir = getBaseDir();
return baseDir == null || licenseFilePath == null ? null : new File(baseDir, licenseFilePath);
}
public VersionInfo.Version getRequiredHostVersion() {
return requiredHostVersion;
}
public void setRequiredHostVersion(VersionInfo.Version requiredHostVersion) {
this.requiredHostVersion = requiredHostVersion;
}
public void setCategory(String category) {
this.category = category;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -