Search in sources :

Example 1 with JspC

use of org.apache.jasper.JspC in project Openfire by igniterealtime.

the class PluginServlet method handleDevJSP.

/**
     * Handles a request for a JSP page in development mode. If development mode is
     * not enabled, this method returns false so that normal JSP handling can be performed.
     * If development mode is enabled, this method tries to locate the JSP, compile
     * it using JSPC, and then return the output.
     *
     * @param pathInfo the extra path info.
     * @param request  the request object.
     * @param response the response object.
     * @return true if this page request was handled; false if the request was not handled.
     */
private boolean handleDevJSP(String pathInfo, HttpServletRequest request, HttpServletResponse response) {
    String jspURL = pathInfo.substring(1);
    // Handle pre-existing pages and fail over to pre-compiled pages.
    int fileSeperator = jspURL.indexOf("/");
    if (fileSeperator != -1) {
        String pluginName = jspURL.substring(0, fileSeperator);
        Plugin plugin = pluginManager.getPlugin(pluginName);
        PluginDevEnvironment environment = pluginManager.getDevEnvironment(plugin);
        // If development mode not turned on for plugin, return false.
        if (environment == null) {
            return false;
        }
        File webDir = environment.getWebRoot();
        if (webDir == null || !webDir.exists()) {
            return false;
        }
        File pluginDirectory = pluginManager.getPluginDirectory(plugin);
        File compilationDir = new File(pluginDirectory, "classes");
        compilationDir.mkdirs();
        String jsp = jspURL.substring(fileSeperator + 1);
        int indexOfLastSlash = jsp.lastIndexOf("/");
        String relativeDir = "";
        if (indexOfLastSlash != -1) {
            relativeDir = jsp.substring(0, indexOfLastSlash);
            relativeDir = relativeDir.replaceAll("//", ".") + '.';
        }
        File jspFile = new File(webDir, jsp);
        String filename = jspFile.getName();
        int indexOfPeriod = filename.indexOf(".");
        if (indexOfPeriod != -1) {
            filename = "dev" + StringUtils.randomString(4);
        }
        JspC jspc = new JspC();
        if (!jspFile.exists()) {
            return false;
        }
        try {
            jspc.setJspFiles(jspFile.getCanonicalPath());
        } catch (IOException e) {
            Log.error(e.getMessage(), e);
        }
        jspc.setOutputDir(compilationDir.getAbsolutePath());
        jspc.setClassName(filename);
        jspc.setCompile(true);
        jspc.setClassPath(getClasspathForPlugin(plugin));
        jspc.execute();
        try {
            Object servletInstance = pluginManager.loadClass(plugin, "org.apache.jsp." + relativeDir + filename).newInstance();
            HttpServlet servlet = (HttpServlet) servletInstance;
            servlet.init(servletConfig);
            servlet.service(request, response);
            return true;
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
        }
    }
    return false;
}
Also used : HttpServlet(javax.servlet.http.HttpServlet) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) JasperException(org.apache.jasper.JasperException) JspC(org.apache.jasper.JspC)

Example 2 with JspC

use of org.apache.jasper.JspC in project Payara by payara.

the class AllJSPsMustBeCompilable method compile.

protected List<JasperException> compile(WebBundleDescriptor descriptor) {
    String archiveUri = getAbstractArchiveUri(descriptor);
    File outDir = getVerifierContext().getOutDir();
    logger.log(Level.INFO, "Compiling JSPs in [ " + new File(archiveUri).getName() + " ]");
    JspC jspc = new JspC();
    jspc.setUriroot(new File(URI.create(archiveUri)).getAbsolutePath());
    jspc.setCompile(true);
    jspc.setOutputDir(outDir.getAbsolutePath());
    jspc.setFailOnError(false);
    setClassPath(jspc);
    jspc.setSchemaResourcePrefix("/schemas/");
    jspc.setDtdResourcePrefix("/dtds/");
    if (logger.isLoggable(Level.FINEST))
        jspc.setVerbose(1);
    try {
        jspc.execute();
    } catch (JasperException je) {
        List<JasperException> errors = jspc.getJSPCompilationErrors();
        errors.add(je);
        return errors;
    }
    return jspc.getJSPCompilationErrors();
}
Also used : JasperException(org.apache.jasper.JasperException) List(java.util.List) File(java.io.File) JspC(org.apache.jasper.JspC)

Example 3 with JspC

use of org.apache.jasper.JspC in project Payara by payara.

the class JSPCompiler method compile.

// //////////////////////////////////////////////////////////////////////////
public static void compile(File inWebDir, File outWebDir, WebBundleDescriptor wbd, String classpath, ServerContext serverContext) throws DeploymentException {
    JspC jspc = new JspC();
    if (classpath != null && classpath.length() > 0) {
        jspc.setClassPath(classpath);
    }
    // START SJSAS 6311155
    String appName = wbd.getApplication().getName();
    // so far, this is not segragated per web bundle, all web-bundles will get the
    // same sysClassPath
    String sysClassPath = ASClassLoaderUtil.getModuleClassPath(serverContext.getDefaultServices(), appName, null);
    jspc.setSystemClassPath(sysClassPath);
    // END SJSAS 6311155
    verify(inWebDir, outWebDir);
    configureJspc(jspc, wbd);
    jspc.setOutputDir(outWebDir.getAbsolutePath());
    jspc.setUriroot(inWebDir.getAbsolutePath());
    jspc.setCompile(true);
    logger.log(Level.INFO, LogFacade.START_MESSAGE);
    try {
        jspc.execute();
    } catch (Exception je) {
        throw new DeploymentException("JSP Compilation Error: " + je, je);
    } finally {
        // bnevins 9-9-03 -- There may be no jsp files in this web-module
        // in such a case the code above will create a useless, and possibly
        // problematic empty directory.	 If the directory is empty -- delete
        // the directory.
        String[] files = outWebDir.list();
        if (files == null || files.length <= 0) {
            if (!outWebDir.delete()) {
                logger.log(Level.FINE, LogFacade.CANNOT_DELETE_FILE, outWebDir);
            }
        }
        logger.log(Level.INFO, LogFacade.FINISH_MESSAGE);
    }
}
Also used : DeploymentException(org.glassfish.deployment.common.DeploymentException) DeploymentException(org.glassfish.deployment.common.DeploymentException) JspC(org.apache.jasper.JspC)

Aggregations

JspC (org.apache.jasper.JspC)3 File (java.io.File)2 JasperException (org.apache.jasper.JasperException)2 IOException (java.io.IOException)1 List (java.util.List)1 HttpServlet (javax.servlet.http.HttpServlet)1 DeploymentException (org.glassfish.deployment.common.DeploymentException)1 SAXException (org.xml.sax.SAXException)1