use of org.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class StandardContext method getResource.
/**
* Return the URL to the resource that is mapped to a specified path.
* The path must begin with a "/" and is interpreted as relative to the
* current context root.
*/
@Override
public java.net.URL getResource(String path) throws MalformedURLException {
if (path == null || !path.startsWith("/")) {
String msg = MessageFormat.format(rb.getString(LogFacade.INCORRECT_PATH), path);
throw new MalformedURLException(msg);
}
path = RequestUtil.normalize(path);
if (path == null)
return (null);
String libPath = "/WEB-INF/lib/";
if ((path.startsWith(libPath)) && (path.endsWith(".jar"))) {
File jarFile = null;
if (isFilesystemBased()) {
jarFile = new File(getBasePath(docBase), path);
} else {
jarFile = new File(getWorkPath(), path);
}
if (jarFile.exists()) {
return jarFile.toURL();
} else {
return null;
}
} else {
DirContext resources = null;
if (alternateDocBases == null || alternateDocBases.isEmpty()) {
resources = context.getResources();
} else {
AlternateDocBase match = AlternateDocBase.findMatch(path, alternateDocBases);
if (match != null) {
resources = ContextsAdapterUtility.unwrap(match.getResources());
} else {
// None of the url patterns for alternate doc bases matched
resources = getResources();
}
}
if (resources != null) {
String fullPath = getName() + path;
String hostName = getParent().getName();
try {
resources.lookup(path);
return new java.net.URL(// START SJAS 6318494
"jndi", "", 0, getJNDIUri(hostName, fullPath), // END SJSAS 6318494
new DirContextURLStreamHandler(resources));
} catch (Exception e) {
// do nothing
}
}
}
return (null);
}
use of org.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class StandardContext method getResourceAsStream.
/**
* Return the requested resource as an <code>InputStream</code>. The
* path must be specified according to the rules described under
* <code>getResource</code>. If no such resource can be identified,
* return <code>null</code>.
*/
@Override
public InputStream getResourceAsStream(String path) {
if (path == null || !path.startsWith("/"))
return (null);
path = RequestUtil.normalize(path);
if (path == null)
return (null);
DirContext resources = null;
if (alternateDocBases == null || alternateDocBases.isEmpty()) {
resources = getResources();
} else {
AlternateDocBase match = AlternateDocBase.findMatch(path, alternateDocBases);
if (match != null) {
resources = ContextsAdapterUtility.unwrap(match.getResources());
} else {
// None of the url patterns for alternate doc bases matched
resources = getResources();
}
}
if (resources != null) {
try {
Object resource = resources.lookup(path);
if (resource instanceof Resource)
return (((Resource) resource).streamContent());
} catch (Exception e) {
// do nothing
}
}
return (null);
}
use of org.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class StandardContext method start.
/**
* Start this Context component.
*
* @exception LifecycleException if a startup error occurs
*/
@Override
public synchronized void start() throws LifecycleException {
if (started) {
if (log.isLoggable(Level.INFO)) {
log.log(Level.INFO, LogFacade.CONTAINER_ALREADY_STARTED_EXCEPTION, logName());
}
return;
}
long startupTimeStart = System.currentTimeMillis();
if (!initialized) {
try {
init();
} catch (Exception ex) {
throw new LifecycleException("Error initializaing ", ex);
}
}
if (log.isLoggable(Level.FINE)) {
log.log(Level.FINE, "Starting " + ("".equals(getName()) ? "ROOT" : getName()));
}
// Set JMX object name for proper pipeline registration
preRegisterJMX();
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
setAvailable(false);
setConfigured(false);
// Add missing components as necessary
if (webappResources == null) {
// (1) Required by Loader
if (log.isLoggable(Level.FINE)) {
log.log(Level.FINE, "Configuring default Resources");
}
try {
if ((docBase != null) && (docBase.endsWith(".war")) && (!(new File(docBase).isDirectory())))
setResources(new WARDirContext());
else
setResources(new WebDirContext());
} catch (IllegalArgumentException e) {
throw new LifecycleException(rb.getString(LogFacade.INIT_RESOURCES_EXCEPTION), e);
}
}
resourcesStart();
// Add alternate resources
if (alternateDocBases != null && !alternateDocBases.isEmpty()) {
for (AlternateDocBase alternateDocBase : alternateDocBases) {
String docBase = alternateDocBase.getDocBase();
if (log.isLoggable(Level.FINE)) {
log.log(Level.FINE, "Configuring alternate resources");
}
try {
if (docBase != null && docBase.endsWith(".war") && (!(new File(docBase).isDirectory()))) {
setAlternateResources(alternateDocBase, new WARDirContext());
} else {
setAlternateResources(alternateDocBase, new FileDirContext());
}
} catch (IllegalArgumentException e) {
throw new LifecycleException(rb.getString(LogFacade.INIT_RESOURCES_EXCEPTION), e);
}
}
alternateResourcesStart();
}
if (getLoader() == null) {
createLoader();
}
// Initialize character set mapper
getCharsetMapper();
// Post work directory
postWorkDirectory();
// Validate required extensions
try {
ExtensionValidator.validateApplication(getResources(), this);
} catch (IOException ioe) {
String msg = MessageFormat.format(rb.getString(LogFacade.DEPENDENCY_CHECK_EXCEPTION), this);
throw new LifecycleException(msg, ioe);
}
// Reading the "catalina.useNaming" environment variable
String useNamingProperty = System.getProperty("catalina.useNaming");
if ((useNamingProperty != null) && ("false".equals(useNamingProperty))) {
useNaming = false;
}
if (isUseNaming()) {
if (namingContextListener == null) {
namingContextListener = new NamingContextListener();
namingContextListener.setDebug(getDebug());
namingContextListener.setName(getNamingContextName());
addLifecycleListener(namingContextListener);
}
}
// Binding thread
// START OF SJSAS 8.1 6174179
// ClassLoader oldCCL = bindThread();
ClassLoader oldCCL = null;
try {
started = true;
// Start our subordinate components, if any
if ((loader != null) && (loader instanceof Lifecycle))
((Lifecycle) loader).start();
if ((logger != null) && (logger instanceof Lifecycle))
((Lifecycle) logger).start();
// Unbinding thread
// START OF SJSAS 8.1 6174179
// unbindThread(oldCCL);
// END OF SJSAS 8.1 6174179
// Binding thread
oldCCL = bindThread();
if ((realm != null) && (realm instanceof Lifecycle))
((Lifecycle) realm).start();
if ((resources != null) && (resources instanceof Lifecycle))
((Lifecycle) resources).start();
// Start our child containers, if any
for (Container child : findChildren()) {
if (child instanceof Lifecycle) {
((Lifecycle) child).start();
}
}
// if any
if (pipeline instanceof Lifecycle)
((Lifecycle) pipeline).start();
// START SJSAS 8.1 5049111
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(START_EVENT, null);
// END SJSAS 8.1 5049111
} catch (Throwable t) {
throw new LifecycleException(t);
} finally {
// Unbinding thread
unbindThread(oldCCL);
}
if (!getConfigured()) {
String msg = MessageFormat.format(rb.getString(LogFacade.STARTUP_CONTEXT_FAILED_EXCEPTION), getName());
throw new LifecycleException(msg);
}
// Store some required info as ServletContext attributes
postResources();
if (orderedLibs != null && !orderedLibs.isEmpty()) {
getServletContext().setAttribute(ServletContext.ORDERED_LIBS, orderedLibs);
context.setAttributeReadOnly(ServletContext.ORDERED_LIBS);
}
// Initialize associated mapper
mapper.setContext(getPath(), welcomeFiles, ContextsAdapterUtility.wrap(resources));
// Binding thread
oldCCL = bindThread();
try {
// Set up the context init params
mergeParameters();
// Notify our interested LifecycleListeners
lifecycle.fireLifecycleEvent(AFTER_START_EVENT, null);
// Support for pluggability : this has to be done before
// listener events are fired
callServletContainerInitializers();
// Configure and call application event listeners
contextListenerStart();
// Start manager
if ((manager != null) && (manager instanceof Lifecycle)) {
((Lifecycle) getManager()).start();
}
// Start ContainerBackgroundProcessor thread
super.threadStart();
// Configure and call application filters
filterStart();
// Load and initialize all "load on startup" servlets
loadOnStartup(findChildren());
} catch (Throwable t) {
log.log(Level.SEVERE, LogFacade.STARTUP_CONTEXT_FAILED_EXCEPTION, getName());
try {
stop();
} catch (Throwable tt) {
log.log(Level.SEVERE, LogFacade.CLEANUP_FAILED_EXCEPTION, tt);
}
throw new LifecycleException(t);
} finally {
// Unbinding thread
unbindThread(oldCCL);
}
// Set available status depending upon startup success
if (log.isLoggable(Level.FINEST)) {
log.log(Level.FINEST, "Startup successfully completed");
}
setAvailable(true);
// JMX registration
registerJMX();
startTimeMillis = System.currentTimeMillis();
startupTime = startTimeMillis - startupTimeStart;
// Send j2ee.state.running notification
if (getObjectName() != null) {
Notification notification = new Notification("j2ee.state.running", this, sequenceNumber++);
sendNotification(notification);
}
// of files on startup
if (getLoader() instanceof WebappLoader) {
((WebappLoader) getLoader()).closeJARs(true);
}
}
use of org.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class ADBAwareHttpHandler method addAlternateDocBase.
/**
* Add {@link AlternateDocBase} to be checked for requested resources.
*
* @param urlPattern
* @param docBase absolute path
*/
public void addAlternateDocBase(final String urlPattern, final String docBase) {
if (urlPattern == null) {
throw new IllegalArgumentException("The urlPattern argument can't be null");
} else if (docBase == null) {
throw new IllegalArgumentException("The docBase argument can't be null");
}
AlternateDocBase alternateDocBase = new AlternateDocBase();
alternateDocBase.setUrlPattern(urlPattern);
alternateDocBase.setDocBase(docBase);
alternateDocBase.setBasePath(getBasePath(docBase));
alternateDocBases.add(alternateDocBase);
}
use of org.glassfish.grizzly.http.server.util.AlternateDocBase in project Payara by payara.
the class ADBAwareHttpHandler method lookupInADB.
private File lookupInADB(final String uri) {
final AlternateDocBase adb = AlternateDocBase.findMatch(uri, alternateDocBases);
if (adb != null) {
File file = new File(adb.getBasePath(), uri);
boolean exists = file.exists();
boolean isDirectory = file.isDirectory();
if (exists && isDirectory) {
file = new File(file, "/index.html");
exists = file.exists();
isDirectory = file.isDirectory();
}
if (exists && !isDirectory) {
return file;
}
}
return null;
}
Aggregations