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

📄 customwebapplicationcontext.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.server.jetty;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mortbay.http.ResourceCache;
import org.mortbay.jetty.servlet.WebApplicationContext;
import org.mortbay.util.Resource;

import com.sslexplorer.server.Main;

/**
 * <p>
 * An extension to the standard Jetty
 * {@link org.mortbay.jetty.servlet.WebApplicationContext} that allows resources
 * to be loaded from multiple {@link org.mortbay.http.ResourceCache}s.
 * 
 * <p>
 * This is necessary for the plugin architecture so that plugins may register
 * their own <b>webapp</b> directories which can then be overlaid onto the
 * namespace of the main SSL-Explorer webapp.
 * 
 * <p>
 * Other SSL-Explorer specific webapp configuration is also performed here,
 * including setting up the special /defaultStyle.css alias that is used to
 * workaround the change to the way the CSS file is load. If this alias was not
 * set up, upgraders would have lost their CSS as /defaultStyle.css no longer
 * really exists.
 * 
 * <p>
 * Plugins also register new classpaths here so that any Java bytecode they may
 * require (be it in .CLASS format or .JAR format) may be loaded in the same
 * Class Loader as the main webapp.
 * 
 * @author Brett Smith <brett@3sp.com>
 */
public class CustomWebApplicationContext extends WebApplicationContext {

    final static Log log = LogFactory.getLog(CustomWebApplicationContext.class);
    private List resourceCaches;
    private String additionalClasspath;
    private Map resourceCacheCache;

    /**
     * Constructor
     * 
     * @param useDevConfig <code>true</code> if running in development mode
     * @throws Exception on any error
     */
    public CustomWebApplicationContext(boolean useDevConfig) throws Exception {
        super("webapp");
        additionalClasspath = "";
        resourceCacheCache = new HashMap();
        setContextPath("/");
        setDefaultsDescriptor("/com/sslexplorer/boot/webdefault.xml");
        setDisplayName("SSL-Explorer");
        setTempDirectory(new File("tmp"));

        setResourceAlias("/defaultStyle.css", "/css/defaultStyle.jsp");

        if (useDevConfig) {
            setClassLoader(Main.class.getClassLoader());
        }
        setWelcomeFiles(new String[] { "showHome.do" });
        resourceCaches = new ArrayList();
    }

    /**
     * <p>
     * Add a new Resource Cache. Whenever a resource is requested, this handler
     * will search all registered resource caches until one can locate it.
     * 
     * <p>
     * This shouldn't be called directly, but through
     * {@link com.sslexplorer.boot.Context#addResourceBase(URL)}
     * 
     * @param cache cache to add
     */
    public void addResourceCache(ResourceCache cache) {
        resourceCaches.add(cache);
    }

    /**
     * <p>
     * Remove a Resrouce Cache. Whenever a resource is requested, this handler
     * will no longer use this cache.
     * 
     * <p>
     * This shouldn't be called directly, but through
     * {@link com.sslexplorer.boot.Context#removeResourceBase(URL)}
     * 
     * @param cache cache to remove
     */
    public void removeResourceCache(ResourceCache cache) {
        resourceCaches.remove(cache);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.mortbay.http.ResourceCache#getResource(java.lang.String)
     */
    public Resource getResource(String pathInContext) throws IOException {
        /*
         * See if it has been accessed before, if so just check in that resource
         * cache
         */
        ResourceCache o = (ResourceCache) resourceCacheCache.get(pathInContext);
        if (log.isDebugEnabled())
        	log.debug("Getting resource " + pathInContext + ", checking in plugins");
        if (o == null) {
            // First try all the plugins with the path for an overidden resource
            for (Iterator i = resourceCaches.iterator(); i.hasNext();) {
                ResourceCache cache = (ResourceCache) i.next();
                Resource r = cache.getResource(pathInContext);
                if (r != null && r.exists() && !r.isDirectory()) {
                	if (log.isDebugEnabled())
                		log.debug("    Found in " + cache.getBaseResource().toString());
                    resourceCacheCache.put(pathInContext, cache);
                    return r;
                }
            }
            if (log.isDebugEnabled())
            	log.debug("   Not found");
        } else {
            Resource r = o.getResource(pathInContext);
            if (r != null && r.exists() && !r.isDirectory()) {
            	if (log.isDebugEnabled())
            		log.debug("    Found in " + o.getBaseResource().toString());
                return r;
            }
        }

        // No the
        if (log.isDebugEnabled())
        	log.debug("Checking for alias");

        // Now try all the plugins for an overidden resource with the resource
        // alias if there is one
        String resourceAlias = getResourceAlias(pathInContext);
        if (resourceAlias != null) {
        	if (log.isDebugEnabled())
        		log.debug("Found alias of " + resourceAlias + ", checking in plugins");
            for (Iterator i = resourceCaches.iterator(); i.hasNext();) {
                ResourceCache cache = (ResourceCache) i.next();
                Resource r = cache.getResource(resourceAlias);
                if (r != null && r.exists() && !r.isDirectory()) {
                	if (log.isDebugEnabled())
                		log.debug("    Found in " + cache.getBaseResource());
                    resourceCacheCache.put(pathInContext, cache);
                    return r;
                }
            }
            if (log.isDebugEnabled())
            	log.debug("   Not found");
        }

        // Get from the main webapp
        if (log.isDebugEnabled())
        	log.debug("Passsing to main webapp");
        return super.getResource(pathInContext);
    }

    /**
     * <p>
     * Add a new location to the webapp Class Loader. This is used when plugins
     * are loaded and they have one or more JARS or CLASS directories containing
     * any bytecode they require.
     * 
     * <p>
     * The URL supplied should have a protocol of <b>file</code>.
     * 
     * <p>
     * This should be called directly, but through
     * {@link com.sslexplorer.boot.Context#addContextLoaderURL(URL)}.
     * 
     * @param url url to add
     */
    public void addContextLoaderURL(URL url) {
        if (url.getProtocol().equals("file")) {
            additionalClasspath = additionalClasspath
                            + (additionalClasspath.length() != 0 ? System.getProperty("path.separator") : "") + url.getFile();
        }
        doAddContextLoaderURL(url);
    }

    private void doAddContextLoaderURL(URL u) {
        try {
            URLClassLoader sysloader = (URLClassLoader) getClassLoader();
            Class sysclass = URLClassLoader.class;
            Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
            method.setAccessible(true);
            method.invoke(sysloader, new Object[] { u });
        } catch (Exception e) {
            log.error("Failed to add to classpath.", e);
        }
    }

}

⌨️ 快捷键说明

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