Exemplo n.º 1
0
  /**
   * @see
   *     org.apache.velocity.runtime.resource.loader.ResourceLoader#init(com.dotcms.repackage.org.apache.commons.collections.ExtendedProperties)
   */
  public void init(ExtendedProperties configuration) {
    dataSourceName = StringUtils.nullTrim(configuration.getString("resource.datasource"));
    tableName = StringUtils.nullTrim(configuration.getString("resource.table"));
    keyColumn = StringUtils.nullTrim(configuration.getString("resource.keycolumn"));
    templateColumn = StringUtils.nullTrim(configuration.getString("resource.templatecolumn"));
    timestampColumn = StringUtils.nullTrim(configuration.getString("resource.timestampcolumn"));

    if (dataSource != null) {
      if (Logger.isDebugEnabled(this.getClass())) {
        Logger.debug(
            this,
            "DataSourceResourceLoader: using dataSource instance with table \"" + tableName + "\"");
        Logger.debug(
            this,
            "DataSourceResourceLoader: using columns \""
                + keyColumn
                + "\", \""
                + templateColumn
                + "\" and \""
                + timestampColumn
                + "\"");
      }

      Logger.debug(this, "DataSourceResourceLoader initialized.");
    } else if (dataSourceName != null) {
      if (Logger.isDebugEnabled(this.getClass())) {
        Logger.debug(
            this,
            "DataSourceResourceLoader: using \""
                + dataSourceName
                + "\" datasource with table \""
                + tableName
                + "\"");
        Logger.debug(
            this,
            "DataSourceResourceLoader: using columns \""
                + keyColumn
                + "\", \""
                + templateColumn
                + "\" and \""
                + timestampColumn
                + "\"");
      }

      Logger.debug(this, "DataSourceResourceLoader initialized.");
    } else {
      String msg =
          "DataSourceResourceLoader not properly initialized. No DataSource was identified.";
      Logger.error(this, msg);
      throw new RuntimeException(msg);
    }
  }
  /**
   * @see
   *     org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
   */
  public void init(ExtendedProperties configuration) {
    if (log.isTraceEnabled()) {
      log.trace("FileResourceLoader : initialization starting.");
    }

    paths.addAll(configuration.getVector("path"));

    // unicode files may have a BOM marker at the start, but Java
    // has problems recognizing the UTF-8 bom. Enabling unicode will
    // recognize all unicode boms.
    unicode = configuration.getBoolean("unicode", false);

    if (log.isDebugEnabled()) {
      log.debug("Do unicode file recognition:  " + unicode);
    }

    if (log.isDebugEnabled()) {
      // trim spaces from all paths
      StringUtils.trimStrings(paths);

      // this section lets tell people what paths we will be using
      int sz = paths.size();
      for (int i = 0; i < sz; i++) {
        log.debug("FileResourceLoader : adding path '" + (String) paths.get(i) + "'");
      }
      log.trace("FileResourceLoader : initialization complete.");
    }
  }
  /**
   * Get an InputStream so that the Runtime can build a template with it.
   *
   * @param templateName name of template to get
   * @return InputStream containing the template
   * @throws ResourceNotFoundException if template not found in the file template path.
   */
  public InputStream getResourceStream(String templateName) throws ResourceNotFoundException {
    /*
     * Make sure we have a valid templateName.
     */
    if (org.apache.commons.lang3.StringUtils.isEmpty(templateName)) {
      /*
       * If we don't get a properly formed templateName then
       * there's not much we can do. So we'll forget about
       * trying to search any more paths for the template.
       */
      throw new ResourceNotFoundException("Need to specify a file name or file path!");
    }

    String template = StringUtils.normalizePath(templateName);
    if (template == null || template.length() == 0) {
      String msg =
          "File resource error : argument "
              + template
              + " contains .. and may be trying to access "
              + "content outside of template root.  Rejected.";

      log.error("FileResourceLoader : " + msg);

      throw new ResourceNotFoundException(msg);
    }

    int size = paths.size();
    for (int i = 0; i < size; i++) {
      String path = (String) paths.get(i);
      InputStream inputStream = null;

      try {
        inputStream = findTemplate(path, template);
      } catch (IOException ioe) {
        String msg = "Exception while loading Template " + template;
        log.error(msg, ioe);
        throw new VelocityException(msg, ioe);
      }

      if (inputStream != null) {
        /*
         * Store the path that this template came
         * from so that we can check its modification
         * time.
         */
        templatePaths.put(templateName, path);
        return inputStream;
      }
    }

    /*
     * We have now searched all the paths for
     * templates and we didn't find anything so
     * throw an exception.
     */
    throw new ResourceNotFoundException("FileResourceLoader : cannot find " + template);
  }
  /**
   * Overrides superclass for better performance.
   *
   * @since 1.6
   */
  public boolean resourceExists(String name) {
    if (name == null) {
      return false;
    }
    name = StringUtils.normalizePath(name);
    if (name == null || name.length() == 0) {
      return false;
    }

    int size = paths.size();
    for (int i = 0; i < size; i++) {
      String path = (String) paths.get(i);
      try {
        File file = getFile(path, name);
        if (file.canRead()) {
          return true;
        }
      } catch (Exception ioe) {
        String msg = "Exception while checking for template " + name;
        log.debug(msg, ioe);
      }
    }
    return false;
  }
 /**
  * @see
  *     org.apache.velocity.util.RuntimeServicesAware#setRuntimeServices(org.apache.velocity.runtime.RuntimeServices)
  */
 public void setRuntimeServices(RuntimeServices rs) {
   this.rs = rs;
   notfound = StringUtils.nullTrim(rs.getString(PROPERTY_NOT_FOUND, DEFAULT_NOT_FOUND));
 }