📄 resource.java
字号:
// ========================================================================// Copyright 1996-2005 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// ========================================================================package org.mortbay.resource;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.Serializable;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.text.DateFormat;import java.util.Arrays;import java.util.Date;import org.mortbay.log.Log;import org.mortbay.util.IO;import org.mortbay.util.Loader;import org.mortbay.util.StringUtil;import org.mortbay.util.URIUtil;/* ------------------------------------------------------------ *//** Abstract resource class. * * @author Nuno Pregui�a * @author Greg Wilkins (gregw) */public abstract class Resource implements Serializable{ public static boolean __defaultUseCaches = true; Object _associate; /** * Change the default setting for url connection caches. * Subsequent URLConnections will use this default. * @param useCaches */ public static void setDefaultUseCaches (boolean useCaches) { __defaultUseCaches=useCaches; } public static boolean getDefaultUseCaches () { return __defaultUseCaches; } /* ------------------------------------------------------------ */ /** Construct a resource from a url. * @param url A URL. * @return A Resource object. */ public static Resource newResource(URL url) throws IOException { return newResource(url, __defaultUseCaches); } /* ------------------------------------------------------------ */ /** * Construct a resource from a url. * @param url the url for which to make the resource * @param useCaches true enables URLConnection caching if applicable to the type of resource * @return */ public static Resource newResource(URL url, boolean useCaches) { if (url==null) return null; String url_string=url.toExternalForm(); if( url_string.startsWith( "file:")) { try { FileResource fileResource= new FileResource(url); return fileResource; } catch(Exception e) { Log.debug(Log.EXCEPTION,e); return new BadResource(url,e.toString()); } } else if( url_string.startsWith( "jar:file:")) { return new JarFileResource(url, useCaches); } else if( url_string.startsWith( "jar:")) { return new JarResource(url, useCaches); } return new URLResource(url,null,useCaches); } /* ------------------------------------------------------------ */ /** Construct a resource from a string. * @param resource A URL or filename. * @return A Resource object. */ public static Resource newResource(String resource) throws MalformedURLException, IOException { return newResource(resource, __defaultUseCaches); } /* ------------------------------------------------------------ */ /** Construct a resource from a string. * @param resource A URL or filename. * @param useCaches controls URLConnection caching * @return A Resource object. */ public static Resource newResource (String resource, boolean useCaches) throws MalformedURLException, IOException { URL url=null; try { // Try to format as a URL? url = new URL(resource); } catch(MalformedURLException e) { if(!resource.startsWith("ftp:") && !resource.startsWith("file:") && !resource.startsWith("jar:")) { try { // It's a file. if (resource.startsWith("./")) resource=resource.substring(2); File file=new File(resource).getCanonicalFile(); url=new URL(URIUtil.encodePath(file.toURL().toString())); URLConnection connection=url.openConnection(); connection.setUseCaches(useCaches); FileResource fileResource= new FileResource(url,connection,file); return fileResource; } catch(Exception e2) { Log.debug(Log.EXCEPTION,e2); throw e; } } else { Log.warn("Bad Resource: "+resource); throw e; } } // Make sure that any special characters stripped really are ignorable. String nurl=url.toString(); if (nurl.length()>0 && nurl.charAt(nurl.length()-1)!=resource.charAt(resource.length()-1)) { if ((nurl.charAt(nurl.length()-1)!='/' || nurl.charAt(nurl.length()-2)!=resource.charAt(resource.length()-1)) && (resource.charAt(resource.length()-1)!='/' || resource.charAt(resource.length()-2)!=nurl.charAt(nurl.length()-1) )) { return new BadResource(url,"Trailing special characters stripped by URL in "+resource); } } return newResource(url); } /* ------------------------------------------------------------ */ /** Construct a system resource from a string. * The resource is tried as classloader resource before being * treated as a normal resource. */ public static Resource newSystemResource(String resource) throws IOException { URL url=null; // Try to format as a URL? ClassLoader loader=Thread.currentThread().getContextClassLoader(); if (loader!=null) { url=loader.getResource(resource); if (url==null && resource.startsWith("/")) url=loader.getResource(resource.substring(1)); } if (url==null) { loader=Resource.class.getClassLoader(); if (loader!=null) { url=loader.getResource(resource); if (url==null && resource.startsWith("/")) url=loader.getResource(resource.substring(1)); } } if (url==null) { url=ClassLoader.getSystemResource(resource); if (url==null && resource.startsWith("/")) url=loader.getResource(resource.substring(1)); } if (url==null) return null; return newResource(url); } /* ------------------------------------------------------------ */ /** Find a classpath resource. */ public static Resource newClassPathResource(String resource) { return newClassPathResource(resource,true,false); } /* ------------------------------------------------------------ */ /** Find a classpath resource. * The {@java.lang.Class#getResource} method is used to lookup the resource. If it is not * found, then the {@link Loader#getResource(Class, String, boolean)} method is used. * If it is still not found, then {@link ClassLoader#getSystemResource(String)} is used. * Unlike {@link #getSystemResource} this method does not check for normal resources. * @param name The relative name of the resouce * @param useCaches True if URL caches are to be used. * @param checkParents True if forced searching of parent classloaders is performed to work around * loaders with inverted priorities * @return Resource or null */ public static Resource newClassPathResource(String name,boolean useCaches,boolean checkParents) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -