⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extensiondescriptor.java

📁 这是linux下ssl vpn的实现程序
💻 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.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Element;
import org.jdom.JDOMException;

import com.sslexplorer.core.CoreMessageResources;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.core.CoreUtil;

public class ExtensionDescriptor implements Comparable {

    final static Log log = LogFactory.getLog(ExtensionDescriptor.class);

    HashSet files;
    HashMap parameters;
    ExtensionType launcherType;
    String typeName;
    CoreMessageResources messageResources;
    Element element;
    Map tunnels;
    String description;
    String id;
    String smallIcon;
    String largeIcon;
    String name;
    ExtensionBundle bundle;
    int status;
    boolean hidden = false;

    public static final String UNDEFINED_PARAMETER = "UNDEFINED";

    public ExtensionDescriptor() {
        this.files = new HashSet();
        this.parameters = new HashMap();
        this.tunnels = new HashMap();
    }

    public boolean isOptions() {
        return parameters.size() > 0;
    }
    
    public boolean isHidden() {
        return hidden;
    }

    public String getDescription() {
        return description;
    }

    public void load(ExtensionBundle bundle, Element element) throws JDOMException, IOException, UnknownExtensionTypeException {
        this.bundle = bundle;
        this.element = element;

        parameters.clear();
        tunnels.clear();
        files.clear();

        if (log.isInfoEnabled())
        	log.info("Loading application descriptor");

        String rootName = element.getName();
        if (rootName.equals("application")) {
            log.warn("DEPRECATED. Extension descriptor in " + bundle.getId() + " should now have <extension> as the root element, not <application>");
        }

        typeName = element.getAttribute("type").getValue();
        if (typeName == null) {
            throw new JDOMException("<" + rootName + "> element requires attribute 'type'");
        }
        //
        try {
            String clazz = "com.sslexplorer.extensions.types."
                            + (String.valueOf(typeName.charAt(0)).toUpperCase() + typeName.substring(1)) + "Type";
            if (log.isDebugEnabled())
            	log.debug("Loading type class " + clazz);
            launcherType = (ExtensionType) Class.forName(clazz).newInstance();
        } catch (Throwable t) {
            
            if(System.getProperty("sslexplorer.extensionType." + typeName, null)!=null) {
                
                /**
                 * Try to load the type from a system property
                 */
                String clazz = System.getProperty("sslexplorer.extensionType." + typeName);
                
                if(clazz==null)
                    throw new JDOMException("Failed to load extension type " + typeName + ".");
                
                if (log.isDebugEnabled())
                    log.debug("Loading extended type class " + clazz);
                
                try {
                    launcherType = (ExtensionType) Class.forName(clazz).newInstance();
                } catch(Throwable t2) {
                    throw new JDOMException("Failed to load extension type " + typeName + ".");
                }
            } else {
                throw new UnknownExtensionTypeException(bundle);
                //log.info(typeName + " does not appear to be a valid extension type");
                //throw new JDOMException("Failed to load extension type " + typeName + ".");
            }
            
        }

        id = element.getAttributeValue("extension");

        if (id == null) {
            id = element.getAttributeValue("application");
            if (id == null) {
                throw new JDOMException("<" + element.getName() + "> element requires attribute 'application'");
            } else {
                log.warn("DEPRECATED. <" + rootName + ">'s 'application' attribute should now be 'extension'");
            }
        }
        if (!id.matches("^[a-zA-Z0-9_-]*$")) {
            throw new JDOMException("<" + element.getName() + "> attribute '" + id
                            + "' may only contain word characters ([a-zA-Z_0-9]).");
        }
 
        name = element.getAttributeValue("name");
        if (log.isDebugEnabled())
        	log.debug("Application name is " + name);
        if (name == null) {
            throw new JDOMException("<application> element requires the attribute 'name'");
        }
        smallIcon = element.getAttributeValue("smallIcon");
        largeIcon = element.getAttributeValue("largeIcon");

        if(element.getAttribute("hidden")!=null) {
            hidden = element.getAttribute("hidden").getBooleanValue();
        }
        
        messageResources = CoreServlet.getServlet().getExtensionStoreResources();

        for (Iterator it = element.getChildren().iterator(); it.hasNext();) {
            Element e = (Element) it.next();
            if (e.getName().equalsIgnoreCase("description")) {
                description = e.getText();
            } else if (e.getName().equalsIgnoreCase("parameter")) {
                addParameter(e);
            } else if (e.getName().equalsIgnoreCase("messages")) {
                for (Iterator i = e.getChildren().iterator(); i.hasNext();) {
                    Element el = (Element) i.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.");
                    }
                    key = "application." + id + "." + key;
                    messageResources.setMessage(el.getAttributeValue("locale"), key, el.getText());
                }
                messageResources.setMessage("", "application." + getId() + ".name", name);
            } else if (e.getName().equalsIgnoreCase("tunnel")) {
                verifyTunnel(e);
            } else if (e.getName().equalsIgnoreCase("files")) {
                verifyFiles(e);
            } else if (e.getName().equalsIgnoreCase("replacements")) {
                // skip client does the processing
            } else if(e.getName().equalsIgnoreCase("cert")) {
                // Allow the application to download the current SSL-Explorer certificate
            } else if(e.getName().equalsIgnoreCase("trustcacerts")) {
                // Allow the application to download all trusted ca certs
            } else {
                launcherType.load(this, e);
            }
            
            launcherType.verifyRequiredElements();
        }

    }

    public TunnelDescriptor getTunnel(String name) {
        return (TunnelDescriptor) tunnels.get(name);
    }

    public ExtensionType getExtensionType() {
        return launcherType;
    }

    private void addParameter(Element e) throws JDOMException, IOException {
        ApplicationParameterDefinition definition = new ApplicationParameterDefinition(e);
        parameters.put(definition.getName(), definition);
    }

    private void verifyTunnel(Element e) throws JDOMException, IOException {
        String name = e.getAttributeValue("name");
        if (name == null || name.equals("")) {
            throw new JDOMException("name attribute required for <tunnel> element");
        }
        String hostname = e.getAttributeValue("hostname");
        if (hostname == null || hostname.equals("")) {
            throw new JDOMException("hostname attribute required for <tunnel> element");
        }
        String port = e.getAttributeValue("port");
        if (port == null || port.equals("")) {
            throw new JDOMException("port attribute required for <tunnel> element");
        }
        boolean usePreferredPort = !("false".equals(e.getAttributeValue("usePreferredPort")));
        tunnels.put(name, new TunnelDescriptor(name, hostname, port, usePreferredPort));

    }
    
    public String getSmallIcon() {
        return smallIcon;
    }
    
    public String getLargeIcon() {
        return largeIcon;
    }

    public Set getParameters() {
        return parameters.keySet();
    }

    public Map getParametersAndDefaults() {
        return parameters;
    }

    public ApplicationParameterDefinition getParameterDefinition(String parameter) {
        return (ApplicationParameterDefinition) parameters.get(parameter);
    }

    public static int[] getVersion(String version) {

        int idx = 0;
        int pos = 0;
        int[] result = new int[0];
        do {

            idx = version.indexOf('.', pos);
            int v;
            if (idx > -1) {
                v = Integer.parseInt(version.substring(pos, idx));
                pos = idx + 1;
            } else {
                int sub = version.indexOf('_', pos);
                if (sub > -1) {
                    v = Integer.parseInt(version.substring(pos, sub));
                } else {
                    v = Integer.parseInt(version.substring(pos));
                }
            }
            int[] tmp = new int[result.length + 1];
            System.arraycopy(result, 0, tmp, 0, result.length);
            tmp[tmp.length - 1] = v;
            result = tmp;

        } while (idx > -1);

        return result;
    }

    private void verifyFiles(Element element) throws JDOMException, IOException {

        for (Iterator it = element.getChildren().iterator(); it.hasNext();) {
            Element e = (Element) it.next();
            if (e.getName().equalsIgnoreCase("if")) {
                verifyFiles(e);
            } else if (!e.getName().equalsIgnoreCase("file")) {
                throw new JDOMException("Unexpected element <" + e.getName() + "> found in <files>");
            } else {
                processFile(e);
            }
        }

    }
    
    public Set getFiles() {
    	return files;
    }

    public void processFile(Element e) throws IOException {

        String filename = e.getText();

        File entry = new File(bundle.getFile().getParent(), filename);

        if (!entry.exists()) {
            if ("true".equals(System.getProperty("sslexplorer.useDevConfig", "false"))) {
                log.warn("File specified in extension descriptor " + bundle.getFile().getAbsolutePath()
                                + " does not exist. As SSL-Explorer is running in Dev. mode, this will be ignored.");
            } else {
                throw new IOException("File '" + filename + "' specified in extension.xml does not exist! " + entry.getAbsolutePath());
            }
        }
        else {
            e.setAttribute("checksum", String.valueOf(CoreUtil.generateChecksum(entry)));
        }
        e.setAttribute("size", String.valueOf(entry.length()));

        files.add(e.getText());
    }

    public boolean containsFile(String filename) {
        return files.contains(filename);
    }

    public File getFile(String filename) throws IOException {

        if (!containsFile(filename)) {
            throw new IOException(filename + " is not a valid application file");
        }
        return new File(bundle.getFile().getParent(), filename);
    }

    /**
     * @return
     */
    public CoreMessageResources getMessageResources() {
        return messageResources;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setApplicationBundle(ExtensionBundle bundle) {
        this.bundle = bundle;
    }

    public ExtensionBundle getApplicationBundle() {
        return bundle;
    }

    /**
     * @return
     */
    public Element getDescriptorElement() {
        return (Element) element.clone();
    }

    public int compareTo(Object arg0) {
        int c = bundle == null ? 0 : (bundle.getType() - ((ExtensionDescriptor) arg0).getApplicationBundle().getType());
        return c != 0 ? c : name.compareTo(((ExtensionDescriptor) arg0).name);
    }

    public class TunnelDescriptor {
        private String name;
        private String hostname;
        private String port;
        private boolean usePreferredPort;

        public TunnelDescriptor(String name, String hostname, String port, boolean usePreferredPort) {
            this.name = name;
            this.hostname = hostname;
            this.port = port;
            this.usePreferredPort = usePreferredPort;
        }

        public String getName() {
            return name;
        }

        public String getHostname() {
            return hostname;
        }

        public String getPort() {
            return port;
        }

        public boolean isUsePreferredPort() {
            return usePreferredPort;
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -