// Copyright (C) 2011 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.mail;

import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;

import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeInstance;

import java.util.Properties;

/** Configures Velocity template engine for sending email. */
public class VelocityRuntimeProvider implements Provider<RuntimeInstance> {
  private final SitePaths site;

  @Inject
  VelocityRuntimeProvider(SitePaths site) {
    this.site = site;
  }

  public RuntimeInstance get() {
    String rl = "resource.loader";
    String pkg = "org.apache.velocity.runtime.resource.loader";

    Properties p = new Properties();
    p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
        "org.apache.velocity.runtime.log.SimpleLog4JLogSystem" );
    p.setProperty("runtime.log.logsystem.log4j.category", "velocity");

    if (site.mail_dir.isDirectory()) {
      p.setProperty(rl, "file, class");
      p.setProperty("file." + rl + ".class", pkg + ".FileResourceLoader");
      p.setProperty("file." + rl + ".path", site.mail_dir.getAbsolutePath());
      p.setProperty("class." + rl + ".class", pkg + ".ClasspathResourceLoader");
    } else {
      p.setProperty(rl, "class");
      p.setProperty("class." + rl + ".class", pkg + ".ClasspathResourceLoader");
    }

    RuntimeInstance ri = new RuntimeInstance();
    try {
      ri.init(p);
    } catch (Exception err) {
      throw new ProvisionException("Cannot configure Velocity templates", err);
    }
    return ri;
  }
}

@@ -910,16 +910,174 @@
 nstance;
+%0Aimport org.apache.velocity.runtime.RuntimeServices;%0Aimport org.apache.velocity.runtime.log.LogChute;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1598,63 +1598,37 @@
     
-%22org.apache.velocity.runtime.log.SimpleLog4JLogSystem%22 
+Slf4jLogChute.class.getName()
 );%0A 
@@ -2338,16 +2338,16 @@
 ;%0A    %7D%0A
-
     retu
@@ -2356,11 +2356,1122 @@
  ri;%0A  %7D
+%0A%0A  /** Connects Velocity to sfl4j. */%0A  public static class Slf4jLogChute implements LogChute %7B%0A    private static final Logger log = LoggerFactory.getLogger(%22velocity%22);%0A%0A    @Override%0A    public void init(RuntimeServices rs) %7B%0A    %7D%0A%0A    @Override%0A    public boolean isLevelEnabled(int level) %7B%0A      switch (level) %7B%0A        default:%0A        case DEBUG_ID:%0A          return log.isDebugEnabled();%0A        case INFO_ID:%0A          return log.isInfoEnabled();%0A        case WARN_ID:%0A          return log.isWarnEnabled();%0A        case ERROR_ID:%0A          return log.isErrorEnabled();%0A      %7D%0A    %7D%0A%0A    @Override%0A    public void log(int level, String message) %7B%0A      log(level, message, null);%0A    %7D%0A%0A    @Override%0A    public void log(int level, String msg, Throwable err) %7B%0A      switch (level) %7B%0A        default:%0A        case DEBUG_ID:%0A          log.debug(msg, err);%0A          break;%0A        case INFO_ID:%0A          log.info(msg, err);%0A          break;%0A        case WARN_ID:%0A          log.warn(msg, err);%0A          break;%0A        case ERROR_ID:%0A          log.error(msg, err);%0A          break;%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

Connect Velocity to slf4j