import java.util.jar.Manifest;
import java.util.jar.Attributes.Name;
+import javax.naming.Binding;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
Boolean.valueOf(System.getProperty("org.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES", "true")).booleanValue();
protected class PrivilegedFindResource
- implements PrivilegedAction {
+ implements PrivilegedAction<ResourceEntry> {
protected File file;
protected String path;
this.path = path;
}
- public Object run() {
+ public ResourceEntry run() {
return findResourceInternal(file, path);
}
* The cache of ResourceEntry for classes and resources we have loaded,
* keyed by resource name.
*/
- protected HashMap resourceEntries = new HashMap();
+ protected HashMap<String, ResourceEntry> resourceEntries = new HashMap<String, ResourceEntry>();
/**
* The list of not found resources.
*/
- protected HashMap notFoundResources = new HashMap();
+ protected HashMap<String, String> notFoundResources = new HashMap<String, String>();
/**
* A list of read File and Jndi Permission's required if this loader
* is for a web application context.
*/
- protected ArrayList permissionList = new ArrayList();
+ protected ArrayList<Permission> permissionList =
+ new ArrayList<Permission>();
/**
* The PermissionCollection for each CodeSource for a web
* application context.
*/
- protected HashMap loaderPC = new HashMap();
+ protected HashMap<String, PermissionCollection> loaderPC = new HashMap<String, PermissionCollection>();
/**
*/
public String[] findRepositories() {
- return ((String[])repositories.clone());
+ return (repositories.clone());
}
if (getJarPath() != null) {
try {
- NamingEnumeration enumeration = resources.listBindings(getJarPath());
+ NamingEnumeration<Binding> enumeration =
+ resources.listBindings(getJarPath());
int i = 0;
while (enumeration.hasMoreElements() && (i < length)) {
- NameClassPair ncPair = (NameClassPair) enumeration.nextElement();
+ NameClassPair ncPair = enumeration.nextElement();
String name = ncPair.getName();
// Ignore non JARs present in the lib folder
if (!name.endsWith(".jar"))
}
if (enumeration.hasMoreElements()) {
while (enumeration.hasMoreElements()) {
- NameClassPair ncPair =
- (NameClassPair) enumeration.nextElement();
+ NameClassPair ncPair = enumeration.nextElement();
String name = ncPair.getName();
// Additional non-JAR files are allowed
if (name.endsWith(".jar")) {
*
* @exception ClassNotFoundException if the class was not found
*/
- public Class findClass(String name) throws ClassNotFoundException {
+ public Class<?> findClass(String name) throws ClassNotFoundException {
if (log.isDebugEnabled())
log.debug(" findClass(" + name + ")");
// Ask our superclass to locate this class, if possible
// (throws ClassNotFoundException if it is not found)
- Class clazz = null;
+ Class<?> clazz = null;
try {
if (log.isTraceEnabled())
log.trace(" findClassInternal(" + name + ")");
if (log.isTraceEnabled())
log.debug(" Returning class " + clazz);
- if ((log.isTraceEnabled()) && (clazz != null)) {
+ if (log.isTraceEnabled()) {
ClassLoader cl;
if (Globals.IS_SECURITY_ENABLED){
cl = AccessController.doPrivileged(
URL url = null;
- ResourceEntry entry = (ResourceEntry) resourceEntries.get(name);
+ ResourceEntry entry = resourceEntries.get(name);
if (entry == null) {
entry = findResourceInternal(name, name);
}
*
* @exception IOException if an input/output error occurs
*/
- public Enumeration findResources(String name) throws IOException {
+ public Enumeration<URL> findResources(String name) throws IOException {
if (log.isDebugEnabled())
log.debug(" findResources(" + name + ")");
- Vector result = new Vector();
+ Vector<URL> result = new Vector<URL>();
int jarFilesLength = jarFiles.length;
int repositoriesLength = repositories.length;
// Adding the results of a call to the superclass
if (hasExternalRepositories) {
- Enumeration otherResourcePaths = super.findResources(name);
+ Enumeration<URL> otherResourcePaths = super.findResources(name);
while (otherResourcePaths.hasMoreElements()) {
result.addElement(otherResourcePaths.nextElement());
// Locating the repository for special handling in the case
// of a JAR
if (antiJARLocking) {
- ResourceEntry entry = (ResourceEntry) resourceEntries.get(name);
+ ResourceEntry entry = resourceEntries.get(name);
try {
String repository = entry.codeBase.toString();
if ((repository.endsWith(".jar"))
if (hasExternalRepositories && (stream == null))
stream = url.openStream();
} catch (IOException e) {
- ; // Ignore
+ // Ignore
}
if (stream != null)
return (stream);
*
* @exception ClassNotFoundException if the class was not found
*/
- public Class loadClass(String name) throws ClassNotFoundException {
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
return (loadClass(name, false));
*
* @exception ClassNotFoundException if the class was not found
*/
- public Class loadClass(String name, boolean resolve)
+ public Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException {
if (log.isDebugEnabled())
log.debug("loadClass(" + name + ", " + resolve + ")");
- Class clazz = null;
+ Class<?> clazz = null;
// Log access to stopped classloader
if (!started) {
return (clazz);
}
} catch (ClassNotFoundException e) {
- ;
+ // Ignore
}
}
return (clazz);
}
} catch (ClassNotFoundException e) {
- ;
+ // Ignore
}
// (3) Delegate to parent unconditionally
return (clazz);
}
} catch (ClassNotFoundException e) {
- ;
+ // Ignore
}
}
String codeUrl = codeSource.getLocation().toString();
PermissionCollection pc;
- if ((pc = (PermissionCollection)loaderPC.get(codeUrl)) == null) {
+ if ((pc = loaderPC.get(codeUrl)) == null) {
pc = super.getPermissions(codeSource);
if (pc != null) {
- Iterator perms = permissionList.iterator();
+ Iterator<Permission> perms = permissionList.iterator();
while (perms.hasNext()) {
- Permission p = (Permission)perms.next();
+ Permission p = perms.next();
pc.add(p);
}
loaderPC.put(codeUrl,pc);
protected void clearReferences() {
// Unregister any JDBC drivers loaded by this classloader
- Enumeration drivers = DriverManager.getDrivers();
+ Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
- Driver driver = (Driver) drivers.nextElement();
+ Driver driver = drivers.nextElement();
if (driver.getClass().getClassLoader() == this) {
try {
DriverManager.deregisterDriver(driver);
// Null out any static or final fields from loaded classes,
// as a workaround for apparent garbage collection bugs
if (ENABLE_CLEAR_REFERENCES) {
- Iterator loadedClasses = ((HashMap) resourceEntries.clone()).values().iterator();
+ Iterator<ResourceEntry> loadedClasses = ((HashMap<String, ResourceEntry>) resourceEntries.clone()).values().iterator();
while (loadedClasses.hasNext()) {
- ResourceEntry entry = (ResourceEntry) loadedClasses.next();
+ ResourceEntry entry = loadedClasses.next();
if (entry.loadedClass != null) {
- Class clazz = entry.loadedClass;
+ Class<?> clazz = entry.loadedClass;
try {
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
} else {
Object value = field.get(instance);
if (null != value) {
- Class valueClass = value.getClass();
+ Class<? extends Object> valueClass = value.getClass();
if (!loadedByThisOrChild(valueClass)) {
if (log.isDebugEnabled()) {
log.debug("Not setting field " + field.getName() +
* Determine whether a class was loaded by this class loader or one of
* its child class loaders.
*/
- protected boolean loadedByThisOrChild(Class clazz)
+ protected boolean loadedByThisOrChild(Class<? extends Object> clazz)
{
boolean result = false;
for (ClassLoader classLoader = clazz.getClassLoader();
*
* @return the loaded class, or null if the class isn't found
*/
- protected Class findClassInternal(String name)
+ protected Class<?> findClassInternal(String name)
throws ClassNotFoundException {
if (!validate(name))
if (entry == null)
throw new ClassNotFoundException(name);
- Class clazz = entry.loadedClass;
+ Class<?> clazz = entry.loadedClass;
if (clazz != null)
return clazz;
if ((name == null) || (path == null))
return null;
- ResourceEntry entry = (ResourceEntry) resourceEntries.get(name);
+ ResourceEntry entry = resourceEntries.get(name);
if (entry != null)
return entry;
// Note : Not getting an exception here means the resource was
// found
if (securityManager != null) {
- PrivilegedAction dp =
+ PrivilegedAction<ResourceEntry> dp =
new PrivilegedFindResource(files[i], path);
- entry = (ResourceEntry)AccessController.doPrivileged(dp);
+ entry = AccessController.doPrivileged(dp);
} else {
entry = findResourceInternal(files[i], path);
}
File resourceFile = new File
(loaderDir, jarEntry.getName());
if (!resourceFile.exists()) {
- Enumeration entries = jarFiles[i].entries();
+ Enumeration<JarEntry> entries =
+ jarFiles[i].entries();
while (entries.hasMoreElements()) {
- JarEntry jarEntry2 =
- (JarEntry) entries.nextElement();
+ JarEntry jarEntry2 = entries.nextElement();
if (!(jarEntry2.isDirectory())
&& (!jarEntry2.getName().endsWith
(".class"))) {
// Ensures that all the threads which may be in a race to load
// a particular class all end up with the same ResourceEntry
// instance
- ResourceEntry entry2 = (ResourceEntry) resourceEntries.get(name);
+ ResourceEntry entry2 = resourceEntries.get(name);
if (entry2 == null) {
resourceEntries.put(name, entry);
} else {
*/
protected InputStream findLoadedResource(String name) {
- ResourceEntry entry = (ResourceEntry) resourceEntries.get(name);
+ ResourceEntry entry = resourceEntries.get(name);
if (entry != null) {
if (entry.binaryContent != null)
return new ByteArrayInputStream(entry.binaryContent);
*
* @param name Name of the resource to return
*/
- protected Class findLoadedClass0(String name) {
+ protected Class<?> findLoadedClass0(String name) {
- ResourceEntry entry = (ResourceEntry) resourceEntries.get(name);
+ ResourceEntry entry = resourceEntries.get(name);
if (entry != null) {
return entry.loadedClass;
}
return (true);
JarFile jarFile = new JarFile(jarfile);
for (int i = 0; i < triggers.length; i++) {
- Class clazz = null;
+ Class<?> clazz = null;
try {
if (parent != null) {
clazz = parent.loadClass(triggers[i]);
if(encoded) {
return getURI(realFile);
} else {
- return realFile.toURL();
+ return realFile.toURI().toURL();
}
}
import org.apache.catalina.Container;
import org.apache.catalina.Context;
-import org.apache.catalina.Engine;
import org.apache.catalina.Globals;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleException;
/**
* Repositories that are set in the loader, for JMX.
*/
- private ArrayList loaderRepositories = null;
+ private ArrayList<String> loaderRepositories = null;
// ------------------------------------------------------------- Properties
*/
public ClassLoader getClassLoader() {
- return ((ClassLoader) classLoader);
+ return classLoader;
}
*/
public String[] findRepositories() {
- return ((String[])repositories.clone());
+ return repositories.clone();
}
public String[] getRepositories() {
- return ((String[])repositories.clone());
+ return repositories.clone();
}
/** Extra repositories for this loader
// Register ourself. The container must be a webapp
try {
StandardContext ctx=(StandardContext)container;
- Engine eng=(Engine)ctx.getParent().getParent();
String path = ctx.getPath();
if (path.equals("")) {
path = "/";
setPermissions();
- if (classLoader instanceof Lifecycle)
- ((Lifecycle) classLoader).start();
+ ((Lifecycle) classLoader).start();
// Binding the Webapp class loader to the directory context
- DirContextURLStreamHandler.bind
- ((ClassLoader) classLoader, this.container.getResources());
+ DirContextURLStreamHandler.bind(classLoader,
+ this.container.getResources());
StandardContext ctx=(StandardContext)container;
- Engine eng=(Engine)ctx.getParent().getParent();
String path = ctx.getPath();
if (path.equals("")) {
path = "/";
}
// Throw away our current class loader
- if (classLoader instanceof Lifecycle)
- ((Lifecycle) classLoader).stop();
- DirContextURLStreamHandler.unbind((ClassLoader) classLoader);
+ ((Lifecycle) classLoader).stop();
+ DirContextURLStreamHandler.unbind(classLoader);
try {
StandardContext ctx=(StandardContext)container;
- Engine eng=(Engine)ctx.getParent().getParent();
String path = ctx.getPath();
if (path.equals("")) {
path = "/";
// Validate the source of this event
if (!(event.getSource() instanceof Context))
return;
- Context context = (Context) event.getSource();
// Process a relevant property change
if (event.getPropertyName().equals("reloadable")) {
private WebappClassLoader createClassLoader()
throws Exception {
- Class clazz = Class.forName(loaderClass);
+ Class<?> clazz = Class.forName(loaderClass);
WebappClassLoader classLoader = null;
if (parentClassLoader == null) {
parentClassLoader = container.getParentClassLoader();
}
- Class[] argTypes = { ClassLoader.class };
+ Class<?>[] argTypes = { ClassLoader.class };
Object[] args = { parentClassLoader };
- Constructor constr = clazz.getConstructor(argTypes);
+ Constructor<?> constr = clazz.getConstructor(argTypes);
classLoader = (WebappClassLoader) constr.newInstance(args);
return classLoader;
if (servletContext == null)
return;
- loaderRepositories=new ArrayList();
+ loaderRepositories=new ArrayList<String>();
// Loading the work directory
File workDir =
(File) servletContext.getAttribute(Globals.WORK_DIR_ATTR);
log.info("No work dir for " + servletContext);
}
- if( log.isDebugEnabled())
+ if( log.isDebugEnabled() && workDir != null)
log.debug(sm.getString("webappLoader.deploy", workDir.getAbsolutePath()));
classLoader.setWorkDir(workDir);
// Looking up directory /WEB-INF/lib in the context
try {
- NamingEnumeration enumeration = resources.listBindings(libPath);
+ NamingEnumeration<Binding> enumeration =
+ resources.listBindings(libPath);
while (enumeration.hasMoreElements()) {
- Binding binding = (Binding) enumeration.nextElement();
+ Binding binding = enumeration.nextElement();
String filename = libPath + "/" + binding.getName();
if (!filename.endsWith(".jar"))
continue;
try {
- NamingEnumeration enumeration = srcDir.list("");
+ NamingEnumeration<NameClassPair> enumeration = srcDir.list("");
while (enumeration.hasMoreElements()) {
- NameClassPair ncPair =
- (NameClassPair) enumeration.nextElement();
+ NameClassPair ncPair = enumeration.nextElement();
String name = ncPair.getName();
Object object = srcDir.lookup(name);
File currentFile = new File(destDir, name);
org.apache.juli.logging.LogFactory.getLog( WebappLoader.class );
private ObjectName oname;
- private MBeanServer mserver;
- private String domain;
private ObjectName controller;
public ObjectName preRegister(MBeanServer server,
ObjectName name) throws Exception {
oname=name;
- mserver=server;
- domain=name.getDomain();
-
return name;
}