package io.undertow.server.handlers.resource;
import io.undertow.UndertowMessages;
import java.io.IOException;
import java.net.URL;
public class ClassPathResourceManager implements ResourceManager {
private final ClassLoader classLoader;
private final String prefix;
public ClassPathResourceManager(final ClassLoader loader, final Package p) {
this(loader, p.getName().replace(".", "/"));
}
public ClassPathResourceManager(final ClassLoader classLoader, final String prefix) {
this.classLoader = classLoader;
if (prefix.isEmpty()) {
this.prefix = "";
} else if (prefix.endsWith("/")) {
this.prefix = prefix;
} else {
this.prefix = prefix + "/";
}
}
public ClassPathResourceManager(final ClassLoader classLoader) {
this(classLoader, "");
}
@Override
public Resource getResource(final String path) throws IOException {
String modPath = path;
if(modPath.startsWith("/")) {
modPath = path.substring(1);
}
final String realPath = prefix + modPath;
final URL resource = classLoader.getResource(realPath);
if(resource == null) {
return null;
} else {
return new URLResource(resource, path);
}
}
@Override
public boolean isResourceChangeListenerSupported() {
return false;
}
@Override
public void registerResourceChangeListener(ResourceChangeListener listener) {
throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported();
}
@Override
public void removeResourceChangeListener(ResourceChangeListener listener) {
throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported();
}
@Override
public void close() throws IOException {
}
}