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

📄 simpleclassloader.java

📁 低版本的tomcat 对于有些老版本的应用还真的需要老版的中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            name.replace('.', '/') + ".class";
	// It's '/' not File.Separator - at least that's how URLClassLoader
	// does it and I suppose there are reasons ( costin )


	Resource r=doFindResource( classFileName );
	if( r==null )
	    throw new ClassNotFoundException(name);

	byte[] classData=null;

	if( r.file != null ) {
	    InputStream in=null;
            try {
		in = new FileInputStream(r.file);
                classData=loadBytesFromStream(in, (int) r.file.length());
            } catch (IOException ioex) {
                return null;
            } finally {
                try {
		    if( in!=null) in.close();
		} catch( IOException ex ) {}
            }
	} else if( r.zipEntry != null ) {
	    try {
                classData=loadBytesFromStream(r.zipFile.
					      getInputStream(r.zipEntry),
					      (int)r.zipEntry.getSize());
            } catch (IOException ioex) {
                return null;
	    } finally {
		try {
		    r.zipFile.close();
		} catch ( IOException ignored ) {
		}
	    }
	}

	if (classData != null) {
	    try {
		c = defineClass(name, classData, 0, classData.length );
		if (resolve) resolveClass(c);
		if( debug>0) log( "loadClass() from local repository " +
				  name);
		return c;
	    } catch(Throwable t ) {
		t.printStackTrace();
	    }
	}

        // If not found in any repository
        throw new ClassNotFoundException(name);
    }

    /**
     * Find a resource with a given name.  The return is a URL to the
     * resource. Doing a getContent() on the URL may return an Image,
     * an AudioClip,or an InputStream.
     *
     * @param   name    the name of the resource, to be used as is.
     * @return  an URL on the resource, or null if not found.
     */
    public URL getResource(String name) {
        if( debug > 0 ) log( "getResource() " + name );
	URL u = null;

	if (parent != null) {
	    u = parent.getResource(name);
	    if (u != null)
		return u;
	}

	u = getSystemResource(name);
	if (u != null) {
	    return u;
	}

	Resource r=doFindResource( name );

	if( r==null )
	    return null;

	// Construct a file://-URL if the repository is a directory
	if( r.file != null ) { // Build a file:// URL form the file name
	    try {
		return new URL("file", null,
			       r.file.getAbsolutePath());
	    } catch(java.net.MalformedURLException badurl) {
		badurl.printStackTrace();
		return null;
	    }
	}

	// a jar:-URL *could* change even between minor releases, but
	// didn't between JVM's 1.1.6 and 1.3beta. Tested on JVM's from
	// IBM, Blackdown, Microsoft, Sun @ Windows and Sun @ Solaris
	if( r.zipEntry != null ) {
	    try {
		return new URL("jar:file:" +
			       r.repository.getPath() + "!/" +
			       name);
	    } catch(java.net.MalformedURLException badurl) {
		badurl.printStackTrace();
		return null;
	    } finally {
		try {
		    r.zipFile.close();
		} catch ( IOException ignored ) {
		}
	    }
	}
	// Not found
        return null;

    }

    /**
     * Get an InputStream on a given resource.  Will return null if no
     * resource with this name is found.
     * <p>
     * The JServClassLoader translate the resource's name to a file
     * or a zip entry. It looks for the resource in all its repository
     * entry.
     *
     * @see     java.lang.Class#getResourceAsStream(String)
     * @param   name    the name of the resource, to be used as is.
     * @return  an InputStream on the resource, or null if not found.
     */
    public InputStream getResourceAsStream(String name) {
        // Try to load it from the system class
        if( debug > 0 ) log( "getResourceAsStream() " + name );
	//	InputStream s = getSystemResourceAsStream(name);
	InputStream s = null;

	if (parent != null) {
	    s = parent.getResourceAsStream(name);
	    if( debug>0 ) log( "Found resource in parent " + s );
	    if (s != null)
		return s;
	}

	// Get this resource from system class loader 
	s = getSystemResourceAsStream(name);

        if( debug>0 ) log( "System resource " + s );
	if (s != null) {
	    return s;
	}
		
	Resource r=doFindResource( name );

	if( r==null ) return null;
	
	if( r.file!=null ) {
	    if( debug > 0 ) log( "Found "  + r.file);
	    try {
                InputStream res=new FileInputStream(r.file);
		return res;
            } catch (IOException shouldnothappen) {
		shouldnothappen.printStackTrace();
		return null;
            }
	} else if( r.zipEntry != null ) {
	    if( debug > 0 ) log( "Found "  + r.zipEntry);
	    // workaround - the better solution is to not close the
	    // zipfile !!!!
	    try {
		byte[] data= loadBytesFromStream(r.zipFile.
						 getInputStream(r.zipEntry),
						 (int) r.zipEntry.getSize());
		if(data != null) {
		    InputStream istream = new ByteArrayInputStream(data);
		    return istream;
		}
	    } catch(IOException e) {
	    } finally {
	    // if we close the zipfile bad things will happen -
		// we can't read the stream on some VMs
		if ( r.zipFile != null ) {
		    try {
			r.zipFile.close();
		    } catch ( IOException ignored ) {
		    }
		}
	    }
	}
        return s;
    }

    // -------------------- Private methods --------------------
    /** Private class used to store the result of the search 
     */
    private class Resource {
	/* Repository used to find the resource */
	File repository;
	/* File - if the resource is a file */
	File file;

	/* Zip file and entry if it's in a jar */
	ZipEntry zipEntry;
	ZipFile zipFile;
    }

    // common code to find the resource
    private Resource doFindResource( String name ) {
	Resource r=new Resource();
	
	for( int i=0; i<urls.length; i++ ) {
	    URL cp = urls[i];
	    if( cp==null ) continue;
            String fileN = cp.getFile();
	    File file=new File( fileN );
	    
	    if (fileN.endsWith("/")) {
                String fileName = name.replace('/', File.separatorChar);
                File resFile = new File(file, fileName);
                if (resFile.exists()) {
		    r.file=resFile;
		    r.repository=file;
		    return r;
                }
            } else {
                try {
                    ZipFile zf = new ZipFile(file.getAbsolutePath());
                    ZipEntry ze = zf.getEntry(name);
					
                    if (ze != null) {
			r.zipEntry=ze;
			r.zipFile=zf;
			r.repository=file;
			return r;
                    }
		    zf.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
		    System.out.println("Name= " + name + " " + file );
                    return null;
                }
            }   
        }
	return null;
    }
    
    /**
     * Loads all the bytes of an InputStream.
     */
    private byte[] loadBytesFromStream(InputStream in, int length)
        throws IOException
    {
        byte[] buf = new byte[length];
        int nRead, count = 0;

        while ((length > 0) && ((nRead = in.read(buf,count,length)) != -1)) {
            count += nRead;
            length -= nRead;
        }
        return buf;
    }

    public URL[] getURLs() {
        //TODO:  Add custom implementation.
        return urls;
    }

}

⌨️ 快捷键说明

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