use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.
the class WebappClassLoader method getResource.
/**
* Find the resource with the given name. A resource is some data
* (images, audio, text, etc.) that can be accessed by class code in a
* way that is independent of the location of the code. The name of a
* resource is a "/"-separated path name that identifies the resource.
* If the resource cannot be found, return <code>null</code>.
* <p>
* This method searches according to the following algorithm, returning
* as soon as it finds the appropriate URL. If the resource cannot be
* found, returns <code>null</code>.
* <ul>
* <li>If the <code>delegate</code> property is set to <code>true</code>,
* call the <code>getResource()</code> method of the parent class
* loader, if any.</li>
* <li>Call <code>findResource()</code> to find this resource in our
* locally defined repositories.</li>
* <li>Call the <code>getResource()</code> method of the parent class
* loader, if any.</li>
* </ul>
*
* @param name Name of the resource to return a URL for
*/
@Override
public URL getResource(String name) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "getResource({0})", name);
}
URL url = null;
/*
* (1) Delegate to parent if requested, or if the requested resource
* belongs to one of the packages that are part of the Java EE platform
*/
if (isResourceDelegate(name)) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, "{0} Delegating to parent classloader ", parent);
}
ClassLoader loader = parent;
if (loader == null) {
loader = system;
}
url = loader.getResource(name);
if (url != null) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, " --> Returning ''{0}''", url.toString());
}
return (url);
}
}
// (2) Search local repositories
url = findResource(name);
if (url != null) {
if (antiJARLocking) {
// Locating the repository for special handling in the case
// of a JAR
ResourceEntry entry = resourceEntries.get(name);
try {
String repository = entry.codeBase.toString();
if ((repository.endsWith(".jar")) && !(name.endsWith(".class")) && !(name.endsWith(".jar"))) {
// Copy binary content to the work directory if not present
File resourceFile = new File(loaderDir, name);
url = resourceFile.toURI().toURL();
}
} catch (Exception e) {
logger.log(Level.FINEST, null, e);
}
}
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, " --> Returning ''{0}''", url.toString());
}
return (url);
}
// (3) Delegate to parent unconditionally if not already attempted
if (!delegate) {
ClassLoader loader = parent;
if (loader == null) {
loader = system;
}
url = loader.getResource(name);
if (url != null) {
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, " --> Returning ''{0}''", url.toString());
}
return (url);
}
}
// (4) Resource was not found
if (logger.isLoggable(Level.FINER)) {
logger.log(Level.FINER, " --> Resource not found, returning null");
}
return null;
}
use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.
the class WebappClassLoader method findClassInternal.
/**
* Find specified class in local repositories.
*
* @param name
* @return the loaded class, or null if the class isn't found
* @throws java.lang.ClassNotFoundException
*/
protected ResourceEntry findClassInternal(String name) throws ClassNotFoundException {
if (!validate(name)) {
throw new ClassNotFoundException(name);
}
String tempPath = name.replace('.', '/');
String classPath = tempPath + ".class";
ResourceEntry entry = findResourceInternal(name, classPath);
if (entry == null) {
throw new ClassNotFoundException(name);
}
synchronized (this) {
Class<?> clazz = entry.loadedClass;
if (clazz != null) {
return entry;
}
if (entry.binaryContent == null) {
throw new ClassNotFoundException(name);
}
}
// Looking up the package
definePackage(name, entry);
return entry;
}
use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.
the class WebappClassLoader method clearReferencesStaticFinal.
private void clearReferencesStaticFinal() {
Collection<ResourceEntry> values = resourceEntries.values();
Iterator<ResourceEntry> loadedClasses = values.iterator();
/*
* Step 1: Enumerate all classes loaded by this WebappClassLoader
* and trigger the initialization of any uninitialized ones.
* This is to prevent the scenario where the initialization of
* one class would call a previously cleared class in Step 2 below.
*/
while (loadedClasses.hasNext()) {
ResourceEntry entry = loadedClasses.next();
Class<?> clazz = null;
synchronized (this) {
clazz = entry.loadedClass;
}
if (clazz != null) {
try {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())) {
field.get(null);
break;
}
}
} catch (Throwable t) {
// Ignore
}
}
}
/**
* Step 2: Clear all loaded classes
*/
loadedClasses = values.iterator();
while (loadedClasses.hasNext()) {
ResourceEntry entry = loadedClasses.next();
Class<?> clazz = null;
synchronized (this) {
clazz = entry.loadedClass;
}
if (clazz != null) {
try {
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
int mods = field.getModifiers();
if (field.getType().isPrimitive() || (field.getName().indexOf('$') != -1)) {
continue;
}
if (Modifier.isStatic(mods)) {
try {
setAccessible(field);
if (Modifier.isFinal(mods)) {
if (!((field.getType().getName().startsWith("java.")) || (field.getType().getName().startsWith("javax.")))) {
nullInstance(field.get(null));
}
} else {
field.set(null, null);
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Set field {0} to null in class {1}", new Object[] { field.getName(), clazz.getName() });
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Could not set field " + field.getName() + " to null in class " + clazz.getName(), t);
}
}
}
}
} catch (Throwable t) {
ExceptionUtils.handleThrowable(t);
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, "Could not clean fields for class " + clazz.getName(), t);
}
}
}
}
}
use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.
the class WebappClassLoader method findResourceInternalFromJars.
/**
* Attempts to load the requested resource from this classloader's
* JAR files.
*
* @return The requested resource, or null if not found
*/
private ResourceEntry findResourceInternalFromJars(String name, String path) {
ResourceEntry entry = null;
JarEntry jarEntry = null;
int contentLength = -1;
InputStream binaryStream = null;
if (!openJARs()) {
return null;
}
int jarFilesLength = jarFiles.length;
for (int i = 0; (entry == null) && (i < jarFilesLength); i++) {
jarEntry = jarFiles[i].getJarEntry(path);
if (jarEntry != null) {
entry = new ResourceEntry();
try {
entry.codeBase = getURL(jarRealFiles[i]);
String jarFakeUrl = getURI(jarRealFiles[i]).toString();
jarFakeUrl = "jar:" + jarFakeUrl + "!/" + path;
entry.source = new URL(jarFakeUrl);
entry.lastModified = jarRealFiles[i].lastModified();
} catch (MalformedURLException e) {
return null;
}
contentLength = (int) jarEntry.getSize();
try {
entry.manifest = jarFiles[i].getManifest();
binaryStream = jarFiles[i].getInputStream(jarEntry);
} catch (IOException e) {
return null;
}
// Extract resources contained in JAR to the workdir
if (antiJARLocking && !(path.endsWith(".class"))) {
File resourceFile = new File(loaderDir, jarEntry.getName());
if (!resourceFile.exists()) {
extractResources();
}
}
}
}
if (entry != null) {
readEntryData(entry, name, binaryStream, contentLength, jarEntry);
}
return entry;
}
use of org.glassfish.api.deployment.ResourceEntry in project Payara by payara.
the class WebappClassLoader method findResourceInternal.
/**
* Attempts to find the specified resource in local repositories.
*
* @return the loaded resource, or null if the resource isn't found
*/
protected ResourceEntry findResourceInternal(String name, String path) {
if (!started) {
throw new IllegalStateException(getString(LogFacade.NOT_STARTED, name));
}
if ((name == null) || (path == null)) {
return null;
}
ResourceEntry entry = resourceEntries.get(name);
if (entry != null) {
return entry;
} else if (notFoundResources.containsKey(name)) {
return null;
}
entry = findResourceInternalFromRepositories(name, path);
if (entry == null) {
synchronized (jarFilesLock) {
entry = findResourceInternalFromJars(name, path);
}
}
if (entry == null) {
notFoundResources.put(name, name);
return null;
}
// Add the entry in the local resource repository
// 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 = resourceEntries.putIfAbsent(name, entry);
if (entry2 != null) {
entry = entry2;
}
return entry;
}
Aggregations