use of org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper in project sling by apache.
the class JspScriptEngineFactory method callErrorPageJsp.
/**
* Call the error page
* @param bindings The bindings
* @param scriptHelper Script helper service
* @param context The script context
* @param scriptName The name of the script
*/
private void callErrorPageJsp(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context, final String scriptName) {
final SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, SlingScriptConstants.SLING_SCOPE);
if (resolver == null) {
resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
}
final SlingIOProvider io = this.ioProvider;
final JspFactoryHandler jspfh = this.jspFactoryHandler;
// abort if JSP Support is shut down concurrently (SLING-2704)
if (io == null || jspfh == null) {
logger.warn("callJsp: JSP Script Engine seems to be shut down concurrently; not calling {}", scriptHelper.getScript().getScriptResource().getPath());
return;
}
final ResourceResolver oldResolver = io.setRequestResourceResolver(resolver);
jspfh.incUsage();
try {
final JspServletWrapper errorJsp = getJspWrapper(scriptName, slingBindings);
errorJsp.service(slingBindings);
// The error page could be inside an include.
final SlingHttpServletRequest request = slingBindings.getRequest();
final Throwable t = (Throwable) request.getAttribute("javax.servlet.jsp.jspException");
final Object newException = request.getAttribute("javax.servlet.error.exception");
// t==null means the attribute was not set.
if ((newException != null) && (newException == t)) {
request.removeAttribute("javax.servlet.error.exception");
}
// now clear the error code - to prevent double handling.
request.removeAttribute("javax.servlet.error.status_code");
request.removeAttribute("javax.servlet.error.request_uri");
request.removeAttribute("javax.servlet.error.status_code");
request.removeAttribute("javax.servlet.jsp.jspException");
} finally {
jspfh.decUsage();
io.resetRequestResourceResolver(oldResolver);
}
}
use of org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper in project sling by apache.
the class JspScriptEngineFactory method callJsp.
/**
* Call a JSP script
* @param bindings The bindings
* @param scriptHelper Script helper service
* @param context The script context
* @throws SlingServletException
* @throws SlingIOException
*/
private void callJsp(final Bindings bindings, final SlingScriptHelper scriptHelper, final ScriptContext context) {
ResourceResolver resolver = (ResourceResolver) context.getAttribute(SlingScriptConstants.ATTR_SCRIPT_RESOURCE_RESOLVER, SlingScriptConstants.SLING_SCOPE);
if (resolver == null) {
resolver = scriptHelper.getScript().getScriptResource().getResourceResolver();
}
final SlingIOProvider io = this.ioProvider;
final JspFactoryHandler jspfh = this.jspFactoryHandler;
// abort if JSP Support is shut down concurrently (SLING-2704)
if (io == null || jspfh == null) {
logger.warn("callJsp: JSP Script Engine seems to be shut down concurrently; not calling {}", scriptHelper.getScript().getScriptResource().getPath());
return;
}
final ResourceResolver oldResolver = io.setRequestResourceResolver(resolver);
jspfh.incUsage();
try {
final SlingBindings slingBindings = new SlingBindings();
slingBindings.putAll(bindings);
final JspServletWrapper jsp = getJspWrapper(scriptHelper, slingBindings);
// create a SlingBindings object
jsp.service(slingBindings);
} finally {
jspfh.decUsage();
io.resetRequestResourceResolver(oldResolver);
}
}
use of org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper in project sling by apache.
the class JspRuntimeContext method handleModification.
/**
* Handle jsp modifications
*/
public boolean handleModification(final String scriptName, final boolean isRemove) {
if (log.isDebugEnabled()) {
log.debug("Handling modification " + scriptName);
}
final JspServletWrapper wrapper = jsps.remove(scriptName);
if (wrapper == null && isRemove) {
boolean removed = false;
final Path path = new Path(scriptName);
final Iterator<Map.Entry<String, JspServletWrapper>> iter = jsps.entrySet().iterator();
while (iter.hasNext()) {
final Map.Entry<String, JspServletWrapper> entry = iter.next();
if (path.matches(entry.getKey())) {
iter.remove();
removed |= handleModification(entry.getKey(), entry.getValue());
}
}
return removed;
} else {
return handleModification(scriptName, wrapper);
}
}
use of org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper in project sling by apache.
the class TagFileProcessor method loadTagFile.
/**
* Compiles and loads a tagfile.
*/
private void loadTagFile(Compiler compiler, String tagFilePath, Node.CustomTag n, PageInfo parentPageInfo) throws JasperException {
JspCompilationContext ctxt = compiler.getCompilationContext();
JspRuntimeContext rctxt = ctxt.getRuntimeContext();
rctxt.lockTagFileLoading(tagFilePath);
try {
JspServletWrapper wrapper = rctxt.getWrapper(tagFilePath);
if (wrapper == null) {
wrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, n.getTagInfo(), ctxt.getRuntimeContext(), compiler.getDefaultIsSession(), ctxt.getTagFileJarUrl(tagFilePath));
wrapper = rctxt.addWrapper(tagFilePath, wrapper);
// Use same classloader and classpath for compiling tag files
//wrapper.getJspEngineContext().setClassLoader(ctxt.getClassLoader());
//wrapper.getJspEngineContext().setClassPath(ctxt.getClassPath());
} else {
// Make sure that JspCompilationContext gets the latest TagInfo
// for the tag file. TagInfo instance was created the last
// time the tag file was scanned for directives, and the tag
// file may have been modified since then.
wrapper.getJspEngineContext().setTagInfo(n.getTagInfo());
}
Class tagClazz;
int tripCount = wrapper.incTripCount();
try {
if (tripCount > 0) {
final String postfix = "_" + String.valueOf(tripCount);
// When tripCount is greater than zero, a circular
// dependency exists. The circularily dependant tag
// file is compiled in prototype mode, to avoid infinite
// recursion.
final String tempTagFilePath = tagFilePath + postfix;
final TagInfo tempTagInfo = new JasperTagInfo(n.getTagInfo().getTagName(), n.getTagInfo().getTagClassName() + postfix, n.getTagInfo().getBodyContent(), n.getTagInfo().getInfoString(), n.getTagInfo().getTagLibrary(), n.getTagInfo().getTagExtraInfo(), n.getTagInfo().getAttributes(), n.getTagInfo().getDisplayName(), n.getTagInfo().getSmallIcon(), n.getTagInfo().getLargeIcon(), n.getTagInfo().getTagVariableInfos(), ((JasperTagInfo) n.getTagInfo()).getDynamicAttributesMapName());
JspServletWrapper tempWrapper = new JspServletWrapper(ctxt.getServletContext(), ctxt.getOptions(), tagFilePath, tempTagInfo, ctxt.getRuntimeContext(), compiler.getDefaultIsSession(), ctxt.getTagFileJarUrl(tempTagFilePath));
tagClazz = tempWrapper.loadTagFilePrototype();
tempVector.add(tempWrapper.getJspEngineContext().getCompiler());
String name = JspUtil.getCanonicalName(tagClazz);
final int underscorePos = name.lastIndexOf(postfix);
if (underscorePos > -1) {
n.setTagHandlerClassName(name.substring(0, underscorePos));
}
} else {
tagClazz = wrapper.loadTagFile();
}
} finally {
wrapper.decTripCount();
}
// can only be obtained from the tag instance.
try {
Object tagIns = tagClazz.newInstance();
if (tagIns instanceof JspSourceDependent) {
Iterator iter = ((List) ((JspSourceDependent) tagIns).getDependants()).iterator();
while (iter.hasNext()) {
parentPageInfo.addDependant((String) iter.next());
}
}
} catch (Exception e) {
// ignore errors
}
n.setTagHandlerClass(tagClazz);
} finally {
rctxt.unlockTagFileLoading(tagFilePath);
}
}
use of org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper in project sling by apache.
the class JspScriptEngineFactory method getJspWrapper.
private JspServletWrapper getJspWrapper(final String scriptName, final SlingBindings bindings) throws SlingException {
JspRuntimeContext rctxt = this.getJspRuntimeContext();
JspServletWrapper wrapper = rctxt.getWrapper(scriptName);
if (wrapper != null) {
if (wrapper.isValid()) {
return wrapper;
}
synchronized (this) {
rctxt = this.getJspRuntimeContext();
wrapper = rctxt.getWrapper(scriptName);
if (wrapper != null) {
if (wrapper.isValid()) {
return wrapper;
}
this.renewJspRuntimeContext();
rctxt = this.getJspRuntimeContext();
}
}
}
wrapper = new JspServletWrapper(servletConfig, options, scriptName, false, rctxt, defaultIsSession);
wrapper = rctxt.addWrapper(scriptName, wrapper);
return wrapper;
}
Aggregations