|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Copyright 1997-2004 The Apache Sofware Foundation.
*
* 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.apache.tomcat.util.compat;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* This is a JDK1.1 equivalent of URLClassLoader. It have no dependency on
* tomcat or any other api - just standard java.
*
* Based on AdaptiveClassLoader from JServ1, with the dependency check
* and reloading features removed ( and moved to an external component)
* This is based on the fact that class loading and dependency checking can
* be separated and we want to support multiple forms of class loaders.
*
* The interface also changed to match URLClassLoader.
* This class should be used _only_ with JDK1.1, for JDK1.2 you
* should use a class loader that is aware of Permissions and the
* new rules ( sealing, etc )
*
* This class loader respects the standard order defined in the ClassLoader
* documentation - for a different order you can plug in a different
* class loader ( in the configurations ).
*
* Since this class loader will be visible to applications we need
* to prevent exploits - we'll minimize the public method usage.
*
* The class path can be set only when the object is constructed.
*/
public class SimpleClassLoader extends ClassLoader {
static org.apache.commons.logging.Log logger =
org.apache.commons.logging.LogFactory.getLog(SimpleClassLoader.class);
/**
* The classpath which this classloader searches for class definitions.
* Each element of the vector should be either a directory, a .zip
* file, or a .jar file.
*
* It may be empty when only system classes are controlled.
*/
protected URL urls[];
/**
* A parent class loader for delegation of finding a class definition.
* JDK 1.2 contains parent class loaders as part of java.lang.ClassLoader,
* the parent being passed to a constructor, and retreived with
* getParent() method. For JDK 1.1 compatibility, we'll duplicate the
* 1.2 private member var.
*/
protected ClassLoader parent;
/** Reserved names - this class loader will not allow creation of
classes that start with one of those strings.
*/
protected String reserved[];
SecurityManager sm;
//------------------------------------------------------- Constructors
public SimpleClassLoader( URL urls[]) {
super(); // will check permissions
this.urls=urls;
sm=System.getSecurityManager();
checkURLs();
}
public SimpleClassLoader( URL urls[], ClassLoader parent ) {
super(); // will check permissions
this.urls=urls;
this.parent=parent;
sm=System.getSecurityManager();
checkURLs();
}
/** This is the prefered constructor to be used with this class loader
*/
public SimpleClassLoader( URL urls[], ClassLoader parent,
String reserved[] ) {
super(); // will check permissions
this.urls=urls;
this.parent=parent;
this.reserved=reserved;
sm=System.getSecurityManager();
checkURLs();
}
/** We can't declare a method "getParent" since it'll not compile in
JDK1.2 - the method is final. But we don't have to - this will
be used via JdkCompat
*/
public ClassLoader getParentLoader() {
return parent;
}
private void checkURLs() {
int cnt=0;
for( int i=0; i= 0) {
sm.checkPackageAccess(name.substring(0,i));
sm.checkPackageDefinition(name.substring(0,i));
}
}
// make sure the class is not in a "reserved" package.
if( reserved!=null ) {
for( int i=0; i
* 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( logger.isDebugEnabled() )
logger.debug( "getResourceAsStream() " + name );
// InputStream s = getSystemResourceAsStream(name);
InputStream s = null;
if (parent != null) {
s = parent.getResourceAsStream(name);
if( logger.isDebugEnabled() )
logger.debug( "Found resource in parent " + s );
if (s != null)
return s;
}
// Get this resource from system class loader
s = getSystemResourceAsStream(name);
if( logger.isDebugEnabled() )
logger.debug( "System resource " + s );
if (s != null) {
return s;
}
Resource r=doFindResource( name );
if( r==null ) return null;
if( r.file!=null ) {
if( logger.isDebugEnabled() ) logger.debug( "Found " + r.file);
try {
InputStream res=new FileInputStream(r.file);
return res;
} catch (IOException shouldnothappen) {
logger.error("No File: " + r.file, shouldnothappen);
return null;
}
} else if( r.zipEntry != null ) {
if( logger.isDebugEnabled() ) logger.debug( "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 0) && ((nRead = in.read(buf,count,length)) != -1)) {
count += nRead;
length -= nRead;
}
return buf;
}
public URL[] getURLs() {
return urls;
}
}
|