Add a JNDI resource to provide a Java Mail Session factory

This way Gerrit can send outgoing messages through SMTP, via the
common Java Mail framework.  Server configuration is supplied by
the servlet container, avoiding the need to edit property files
within the web application.

Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/Documentation/install.txt b/Documentation/install.txt
index 380b320..fbfbaa7 100644
--- a/Documentation/install.txt
+++ b/Documentation/install.txt
@@ -176,7 +176,8 @@
 ====
 
 Edit `$JETTY_HOME/contexts/gerrit.xml` to correctly configure the
-database connection, especially the user and password fields.
+database and outgoing SMTP connections, especially the user and
+password fields.
 
 To start automatically when the system boots, consider a start
 script such as the following in `/etc/init.d/gerrit2-jetty`
@@ -219,6 +220,13 @@
 Don't forget to ensure your JNDI configuration can load the
 necessary JDBC drivers.
 
+('Optional') Configure the JNDI name `mail/Outgoing` for the web
+application context to be a factory for a `javax.mail.Session`,
+with the connection information necessary to send outgoing emails.
+You may need to download and install the Java Mail JARs in your
+container's classpath.  If this is not configured, Gerrit will
+function, but will not be able to send email.
+
 [[apache2]]
 Apache2 Reverse Proxy
 ~~~~~~~~~~~~~~~~~~~~~
diff --git a/appdist/src/main/www/jetty_gerrit.xml b/appdist/src/main/www/jetty_gerrit.xml
index c90cbfa..9b1f943 100644
--- a/appdist/src/main/www/jetty_gerrit.xml
+++ b/appdist/src/main/www/jetty_gerrit.xml
@@ -6,7 +6,8 @@
   so it answers to old-style URLs like "/$changeid" and "/mine".
 
   * Copy this file to $JETTY_HOME/contexts/gerrit.xml
-  * Edit user, password, jdbcUrl as necessary below.
+  * Edit user, password, jdbcUrl as necessary below for database.
+  * Edit mail.smtp.host, user, password as necessary for outgoing SMTP.
 
   * Copy jdbc/c3p0-0.9.1.2.jar       to $JETTY_HOME/lib/plus/
   * Copy jdbc/postgresql-*.jdbc*.jar to $JETTY_HOME/lib/plus/
@@ -44,4 +45,23 @@
       </New>
     </Arg>
   </New>
+
+  <New id="OutgoingSmtp" class="org.mortbay.jetty.plus.naming.Resource">
+    <Arg></Arg>
+    <Arg>mail/Outgoing</Arg>
+    <Arg>
+      <New class="org.mortbay.naming.factories.MailSessionReference">
+        <Set name="properties">
+          <New class="java.util.Properties">
+            <Put name="mail.smtp.host">localhost</Put>
+            <Put name="mail.smtp.port">25</Put>
+          </New>
+        </Set>
+        <!-- Uncomment if you need to authenticate to your SMTP server.
+        <Set name="user">gerrit2</Set>
+        <Set name="password">secretkey</Set>
+             /Uncomment -->
+      </New>
+    </Arg>
+  </New>
 </Configure>
diff --git a/appjar/pom.xml b/appjar/pom.xml
index f5c61d8..ca5f80a 100644
--- a/appjar/pom.xml
+++ b/appjar/pom.xml
@@ -341,6 +341,13 @@
     </dependency>
 
     <dependency>
+      <groupId>javax.mail</groupId>
+      <artifactId>mail</artifactId>
+      <version>1.4.1</version>
+      <scope>provided</scope>
+    </dependency>
+
+    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.4.3</version>
diff --git a/appjar/src/main/java/com/google/gerrit/server/GerritServer.java b/appjar/src/main/java/com/google/gerrit/server/GerritServer.java
index 44df7eb..2d8f932 100644
--- a/appjar/src/main/java/com/google/gerrit/server/GerritServer.java
+++ b/appjar/src/main/java/com/google/gerrit/server/GerritServer.java
@@ -86,10 +86,11 @@
 
   private final Database<ReviewDb> db;
   private SystemConfig sConfig;
-  private PersonIdent gerritPersonIdentTemplate;
+  private final PersonIdent gerritPersonIdentTemplate;
   private final SignedToken xsrf;
   private final SignedToken account;
   private final RepositoryCache repositories;
+  private final javax.mail.Session outgoingMail;
 
   private GerritServer() throws OrmException, XsrfException {
     db = createDatabase();
@@ -127,7 +128,8 @@
       }
     }
     gerritPersonIdentTemplate = new PersonIdent(sConfig.gerritGitName, email);
-
+    outgoingMail = createOutgoingMail();
+    
     Common.setSchemaFactory(db);
     Common.setProjectCache(new ProjectCache());
     Common.setAccountCache(new AccountCache());
@@ -373,6 +375,15 @@
     Common.setGerritConfig(r);
   }
 
+  private javax.mail.Session createOutgoingMail() {
+    final String dsName = "java:comp/env/mail/Outgoing";
+    try {
+      return (javax.mail.Session) new InitialContext().lookup(dsName);
+    } catch (NamingException namingErr) {
+      return null;
+    }
+  }
+
   /** Time (in seconds) that user sessions stay "signed in". */
   public int getSessionAge() {
     return sConfig.maxSessionAge;
@@ -429,6 +440,11 @@
     return repositories;
   }
 
+  /** The mail session used to send messages; null if not configured. */
+  public javax.mail.Session getOutgoingMail() {
+    return outgoingMail;
+  }
+
   /** Get a new identity representing this Gerrit server in Git. */
   public PersonIdent newGerritPersonIdent() {
     return new PersonIdent(gerritPersonIdentTemplate);
@@ -437,5 +453,4 @@
   public boolean isAllowGoogleAccountUpgrade() {
     return sConfig.allowGoogleAccountUpgrade;
   }
-
 }
diff --git a/appwar/src/main/webapp/WEB-INF/web.xml b/appwar/src/main/webapp/WEB-INF/web.xml
index 488df0e..4641453 100644
--- a/appwar/src/main/webapp/WEB-INF/web.xml
+++ b/appwar/src/main/webapp/WEB-INF/web.xml
@@ -6,6 +6,12 @@
     <res-auth>Container</res-auth>
   </resource-ref>
 
+  <resource-ref>
+    <res-ref-name>mail/Outgoing</res-ref-name>
+    <res-type>javax.mail.Session</res-type>
+    <res-auth>Container</res-auth>
+  </resource-ref>
+
   <filter>
     <filter-name>UrlRewrite</filter-name>
     <filter-class>com.google.gerrit.server.UrlRewriteFilter</filter-class>