Exemplo n.º 1
0
 // Compiler for parsing only, needed by netbeans
 public Compiler(JspCompilationContext ctxt, JspServletWrapper jsw) {
   this.jsw = jsw;
   this.ctxt = ctxt;
   this.jspcMode = false;
   this.options = ctxt.getOptions();
   this.log = Logger.getLogger(Compiler.class.getName());
   this.smapUtil = new SmapUtil(ctxt);
   this.errDispatcher = new ErrorDispatcher(jspcMode);
   this.javaCompiler = new NullJavaCompiler();
   javaCompiler.init(ctxt, errDispatcher, jspcMode);
 }
Exemplo n.º 2
0
 public Compiler(JspCompilationContext ctxt, JspServletWrapper jsw, boolean jspcMode)
     throws JasperException {
   this.jsw = jsw;
   this.ctxt = ctxt;
   this.jspcMode = jspcMode;
   this.options = ctxt.getOptions();
   this.log = Logger.getLogger(Compiler.class.getName());
   if (jspcMode) {
     log.setLevel(Level.OFF);
   }
   this.smapUtil = new SmapUtil(ctxt);
   this.errDispatcher = new ErrorDispatcher(jspcMode);
   initJavaCompiler();
 }
Exemplo n.º 3
0
  private ServletWriter setupContextWriter(String javaFileName)
      throws FileNotFoundException, JasperException {
    ServletWriter writer;
    // Setup the ServletWriter
    String javaEncoding = ctxt.getOptions().getJavaEncoding();
    OutputStreamWriter osw = null;

    try {
      osw = new OutputStreamWriter(new FileOutputStream(javaFileName), javaEncoding);
    } catch (UnsupportedEncodingException ex) {
      errDispatcher.jspError("jsp.error.needAlternateJavaEncoding", javaEncoding);
    }

    writer = new ServletWriter(new PrintWriter(osw));
    ctxt.setWriter(writer);
    return writer;
  }
Exemplo n.º 4
0
  /**
   * Determine if a compilation is necessary by checking the time stamp of the JSP page with that of
   * the corresponding .class or .java file. If the page has dependencies, the check is also
   * extended to its dependeants, and so on. This method can by overidden by a subclasses of
   * Compiler.
   *
   * @param checkClass If true, check against .class file, if false, check against .java file.
   */
  public boolean isOutDated(boolean checkClass) {

    String jsp = ctxt.getJspFile();

    if (jsw != null && (ctxt.getOptions().getModificationTestInterval() > 0)) {

      if (jsw.getLastModificationTest() + (ctxt.getOptions().getModificationTestInterval() * 1000)
          > System.currentTimeMillis()) {
        return false;
      } else {
        jsw.setLastModificationTest(System.currentTimeMillis());
      }
    }

    long jspRealLastModified = 0;
    // START PWC 6468930
    File targetFile;

    if (checkClass) {
      targetFile = new File(ctxt.getClassFileName());
    } else {
      targetFile = new File(ctxt.getServletJavaFileName());
    }

    // Get the target file's last modified time. File.lastModified()
    // returns 0 if the file does not exist.
    long targetLastModified = targetFile.lastModified();

    // Check cached class file
    if (checkClass) {
      JspRuntimeContext rtctxt = ctxt.getRuntimeContext();
      String className = ctxt.getFullClassName();
      long cachedTime = rtctxt.getBytecodeBirthTime(className);
      if (cachedTime > targetLastModified) {
        targetLastModified = cachedTime;
      } else {
        // Remove from cache, since the bytecodes from the file is more
        // current, so that JasperLoader won't load the cached version
        rtctxt.setBytecode(className, null);
      }
    }

    if (targetLastModified == 0L) return true;

    // Check if the jsp exists in the filesystem (instead of a jar
    // or a remote location). If yes, then do a File.lastModified()
    // to determine its last modified time. This is more performant
    // (fewer stat calls) than the ctxt.getResource() followed by
    // openConnection(). However, it only works for file system jsps.
    // If the file has indeed changed, then need to call URL.OpenConnection()
    // so that the cache loads the latest jsp file
    if (jsw != null) {
      File jspFile = jsw.getJspFile();
      if (jspFile != null) {
        jspRealLastModified = jspFile.lastModified();
      }
    }
    if (jspRealLastModified == 0 || targetLastModified < jspRealLastModified) {
      // END PWC 6468930
      try {
        URL jspUrl = ctxt.getResource(jsp);
        if (jspUrl == null) {
          ctxt.incrementRemoved();
          return false;
        }
        URLConnection uc = jspUrl.openConnection();
        jspRealLastModified = uc.getLastModified();
        uc.getInputStream().close();
      } catch (Exception e) {
        e.printStackTrace();
        return true;
      }
      // START PWC 6468930
    }
    // END PWC 6468930
    /* PWC 6468930
    long targetLastModified = 0;
    File targetFile;

    if( checkClass ) {
        targetFile = new File(ctxt.getClassFileName());
    } else {
        targetFile = new File(ctxt.getServletJavaFileName());
    }

    if (!targetFile.exists()) {
        return true;
    }

    targetLastModified = targetFile.lastModified();
    */
    if (checkClass && jsw != null) {
      jsw.setServletClassLastModifiedTime(targetLastModified);
    }

    if (targetLastModified < jspRealLastModified) {
      // Remember JSP mod time
      jspModTime = jspRealLastModified;
      if (log.isLoggable(Level.FINE)) {
        log.fine("Compiler: outdated: " + targetFile + " " + targetLastModified);
      }
      return true;
    }

    // determine if source dependent files (e.g. includes using include
    // directives) have been changed.
    if (jsw == null) {
      return false;
    }

    List depends = jsw.getDependants();
    if (depends == null) {
      return false;
    }

    Iterator it = depends.iterator();
    while (it.hasNext()) {
      String include = (String) it.next();
      try {
        URL includeUrl = ctxt.getResource(include);
        if (includeUrl == null) {
          return true;
        }

        URLConnection includeUconn = includeUrl.openConnection();
        long includeLastModified = includeUconn.getLastModified();
        includeUconn.getInputStream().close();

        if (includeLastModified > targetLastModified) {
          // START GlassFish 750
          if (include.endsWith(".tld")) {
            ctxt.clearTaglibs();
            ctxt.clearTagFileJarUrls();
          }
          // END GlassFish 750
          return true;
        }
      } catch (Exception e) {
        e.printStackTrace();
        return true;
      }
    }

    return false;
  }
Exemplo n.º 5
0
  /** Compile the jsp file into equivalent servlet in java source */
  private void generateJava() throws Exception {

    long t1, t2, t3, t4;
    t1 = t2 = t3 = t4 = 0;

    if (log.isLoggable(Level.FINE)) {
      t1 = System.currentTimeMillis();
    }

    // Setup page info area
    pageInfo =
        new PageInfo(new BeanRepository(ctxt.getClassLoader(), errDispatcher), ctxt.getJspFile());

    JspConfig jspConfig = options.getJspConfig();
    JspProperty jspProperty = jspConfig.findJspProperty(ctxt.getJspFile());

    /*
     * If the current uri is matched by a pattern specified in
     * a jsp-property-group in web.xml, initialize pageInfo with
     * those properties.
     */
    pageInfo.setELIgnored(JspUtil.booleanValue(jspProperty.isELIgnored()));
    pageInfo.setScriptingInvalid(JspUtil.booleanValue(jspProperty.isScriptingInvalid()));
    pageInfo.setTrimDirectiveWhitespaces(JspUtil.booleanValue(jspProperty.getTrimSpaces()));
    pageInfo.setDeferredSyntaxAllowedAsLiteral(JspUtil.booleanValue(jspProperty.getPoundAllowed()));
    pageInfo.setErrorOnUndeclaredNamespace(
        JspUtil.booleanValue(jspProperty.errorOnUndeclaredNamespace()));

    if (jspProperty.getIncludePrelude() != null) {
      pageInfo.setIncludePrelude(jspProperty.getIncludePrelude());
    }
    if (jspProperty.getIncludeCoda() != null) {
      pageInfo.setIncludeCoda(jspProperty.getIncludeCoda());
    }
    if (options.isDefaultBufferNone() && pageInfo.getBufferValue() == null) {
      // Set to unbuffered if not specified explicitly
      pageInfo.setBuffer(0);
    }

    String javaFileName = ctxt.getServletJavaFileName();
    ServletWriter writer = null;

    try {
      // Setup the ServletWriter
      Writer javaWriter =
          javaCompiler.getJavaWriter(javaFileName, ctxt.getOptions().getJavaEncoding());
      writer = new ServletWriter(new PrintWriter(javaWriter));
      ctxt.setWriter(writer);

      // Reset the temporary variable counter for the generator.
      JspUtil.resetTemporaryVariableName();

      // Parse the file
      ParserController parserCtl = new ParserController(ctxt, this);
      pageNodes = parserCtl.parse(ctxt.getJspFile());

      if (ctxt.isPrototypeMode()) {
        // generate prototype .java file for the tag file
        Generator.generate(writer, this, pageNodes);
        writer.close();
        writer = null;
        return;
      }

      // Validate and process attributes
      Validator.validate(this, pageNodes);

      if (log.isLoggable(Level.FINE)) {
        t2 = System.currentTimeMillis();
      }

      // Collect page info
      Collector.collect(this, pageNodes);

      // Compile (if necessary) and load the tag files referenced in
      // this compilation unit.
      tfp = new TagFileProcessor();
      tfp.loadTagFiles(this, pageNodes);

      if (log.isLoggable(Level.FINE)) {
        t3 = System.currentTimeMillis();
      }

      // Determine which custom tag needs to declare which scripting vars
      ScriptingVariabler.set(pageNodes, errDispatcher);

      // Optimizations by Tag Plugins
      TagPluginManager tagPluginManager = options.getTagPluginManager();
      tagPluginManager.apply(pageNodes, errDispatcher, pageInfo);

      // Optimization: concatenate contiguous template texts.
      TextOptimizer.concatenate(this, pageNodes);

      // Generate static function mapper codes.
      ELFunctionMapper.map(this, pageNodes);

      // generate servlet .java file
      Generator.generate(writer, this, pageNodes);
      writer.close();
      writer = null;

      // The writer is only used during the compile, dereference
      // it in the JspCompilationContext when done to allow it
      // to be GC'd and save memory.
      ctxt.setWriter(null);

      if (log.isLoggable(Level.FINE)) {
        t4 = System.currentTimeMillis();
        log.fine(
            "Generated "
                + javaFileName
                + " total="
                + (t4 - t1)
                + " generate="
                + (t4 - t3)
                + " validate="
                + (t2 - t1));
      }

    } catch (Exception e) {
      if (writer != null) {
        try {
          writer.close();
          writer = null;
        } catch (Exception e1) {
          // do nothing
        }
      }
      // Remove the generated .java file
      javaCompiler.doJavaFile(false);
      throw e;
    } finally {
      if (writer != null) {
        try {
          writer.close();
        } catch (Exception e2) {
          // do nothing
        }
      }
    }

    // JSR45 Support
    if (!options.isSmapSuppressed()) {
      smapUtil.generateSmap(pageNodes);
    }

    // If any proto type .java and .class files was generated,
    // the prototype .java may have been replaced by the current
    // compilation (if the tag file is self referencing), but the
    // .class file need to be removed, to make sure that javac would
    // generate .class again from the new .java file just generated.
    tfp.removeProtoTypeFiles(ctxt.getClassFileName());
  }
Exemplo n.º 6
0
 public void init(JspCompilationContext ctxt, JspServletWrapper jsw) {
   this.jsw = jsw;
   this.ctxt = ctxt;
   this.options = ctxt.getOptions();
 }
Exemplo n.º 7
0
  /**
   * Determine if a compilation is necessary by checking the time stamp of the JSP page with that of
   * the corresponding .class or .java file. If the page has dependencies, the check is also
   * extended to its dependents, and so on. This method can by overridden by a subclasses of
   * Compiler.
   *
   * @param checkClass If true, check against .class file, if false, check against .java file.
   */
  public boolean isOutDated(boolean checkClass) {

    String jsp = ctxt.getJspFile();

    if (jsw != null && (ctxt.getOptions().getModificationTestInterval() > 0)) {

      if (jsw.getLastModificationTest() + (ctxt.getOptions().getModificationTestInterval() * 1000)
          > System.currentTimeMillis()) {
        return false;
      }
      jsw.setLastModificationTest(System.currentTimeMillis());
    }

    long jspRealLastModified = 0;
    try {
      URL jspUrl = ctxt.getResource(jsp);
      if (jspUrl == null) {
        ctxt.incrementRemoved();
        return true;
      }
      URLConnection uc = jspUrl.openConnection();
      if (uc instanceof JarURLConnection) {
        jspRealLastModified = ((JarURLConnection) uc).getJarEntry().getTime();
      } else {
        jspRealLastModified = uc.getLastModified();
      }
      uc.getInputStream().close();
    } catch (Exception e) {
      if (log.isDebugEnabled()) log.debug("Problem accessing resource. Treat as outdated.", e);
      return true;
    }

    long targetLastModified = 0;
    File targetFile;

    if (checkClass) {
      targetFile = new File(ctxt.getClassFileName());
    } else {
      targetFile = new File(ctxt.getServletJavaFileName());
    }

    if (!targetFile.exists()) {
      return true;
    }

    targetLastModified = targetFile.lastModified();
    if (checkClass && jsw != null) {
      jsw.setServletClassLastModifiedTime(targetLastModified);
    }
    if (targetLastModified < jspRealLastModified) {
      if (log.isDebugEnabled()) {
        log.debug("Compiler: outdated: " + targetFile + " " + targetLastModified);
      }
      return true;
    }

    // determine if source dependent files (e.g. includes using include
    // directives) have been changed.
    if (jsw == null) {
      return false;
    }

    List<String> depends = jsw.getDependants();
    if (depends == null) {
      return false;
    }

    Iterator<String> it = depends.iterator();
    while (it.hasNext()) {
      String include = it.next();
      try {
        URL includeUrl = ctxt.getResource(include);
        if (includeUrl == null) {
          return true;
        }

        URLConnection iuc = includeUrl.openConnection();
        long includeLastModified = 0;
        if (iuc instanceof JarURLConnection) {
          includeLastModified = ((JarURLConnection) iuc).getJarEntry().getTime();
        } else {
          includeLastModified = iuc.getLastModified();
        }
        iuc.getInputStream().close();

        if (includeLastModified > targetLastModified) {
          return true;
        }
      } catch (Exception e) {
        if (log.isDebugEnabled()) log.debug("Problem accessing resource. Treat as outdated.", e);
        return true;
      }
    }

    return false;
  }