]> source.dussan.org Git - gitblit.git/commitdiff
Adjustments to ClientLogger
authorJames Moger <james.moger@gitblit.com>
Sat, 31 Mar 2012 12:48:10 +0000 (08:48 -0400)
committerJames Moger <james.moger@gitblit.com>
Sat, 31 Mar 2012 12:48:10 +0000 (08:48 -0400)
groovy/blockpush.groovy
groovy/jenkins.groovy
groovy/protect-refs.groovy
groovy/sendmail.groovy
src/com/gitblit/utils/ClientLogger.java
tests/com/gitblit/tests/GroovyScriptTest.java

index 79d696c1da6f0079c8fc02ccff4a0f5ea84a66d6..186b2427920fd2c9788e961a03e08c346e8a6ffb 100644 (file)
@@ -51,13 +51,13 @@ import com.gitblit.utils.ClientLogger
  * chain, "return false" at the appropriate failure points.\r
  *\r
  * Bound Variables:\r
- *  gitblit                    Gitblit Server                  com.gitblit.GitBlit\r
- *  repository         Gitblit Repository              com.gitblit.models.RepositoryModel\r
- *  user                       Gitblit User                    com.gitblit.models.UserModel\r
- *  clientLogger       Logs messages to client com.gitblit.utils.ClientLogger\r
- *  commands           JGit commands                   Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- *     url                             Base url for Gitblit    String\r
- *  logger                     Logger instance                 org.slf4j.Logger\r
+ *  gitblit                    Gitblit Server                          com.gitblit.GitBlit\r
+ *  repository         Gitblit Repository                      com.gitblit.models.RepositoryModel\r
+ *  user                       Gitblit User                            com.gitblit.models.UserModel\r
+ *  commands           JGit commands                           Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ *     url                             Base url for Gitblit            String\r
+ *  logger                     Logs messages to Gitblit        org.slf4j.Logger\r
+ *  clientLogger       Logs messages to Git client     com.gitblit.utils.ClientLogger\r
  *  \r
  */\r
 \r
index 443e51de6d706355b41a64424902df9e9cdaf70a..41083d2499a641eae9871ca5a720426f0ddce8e3 100644 (file)
@@ -46,12 +46,13 @@ import org.slf4j.Logger
  * exception handler so it will not crash another script nor crash Gitblit.\r
  * \r
  * Bound Variables:\r
- *  gitblit            Gitblit Server                  com.gitblit.GitBlit\r
- *  repository Gitblit Repository              com.gitblit.models.RepositoryModel\r
- *  user               Gitblit User                    com.gitblit.models.UserModel\r
- *  commands   JGit commands                   Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- *     url                     Base url for Gitblit    String\r
- *  logger             Logger instance                 org.slf4j.Logger\r
+ *  gitblit                    Gitblit Server                          com.gitblit.GitBlit\r
+ *  repository         Gitblit Repository                      com.gitblit.models.RepositoryModel\r
+ *  user                       Gitblit User                            com.gitblit.models.UserModel\r
+ *  commands           JGit commands                           Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ *     url                             Base url for Gitblit            String\r
+ *  logger                     Logs messages to Gitblit        org.slf4j.Logger\r
+ *  clientLogger       Logs messages to Git client     com.gitblit.utils.ClientLogger\r
  *  \r
  */\r
 // Indicate we have started the script\r
index 065cf5d88031f1816420eca79a7dd2dda94eba7b..48ac60f1932d9971bfa26786a65311da51eda1c0 100644 (file)
@@ -57,12 +57,13 @@ import org.slf4j.Logger
  * Subsequent scripts, if any, will always be invoked.\r
  *\r
  * Bound Variables:\r
- *  gitblit            Gitblit Server                  com.gitblit.GitBlit\r
- *  repository Gitblit Repository              com.gitblit.models.RepositoryModel\r
- *  user               Gitblit User                    com.gitblit.models.UserModel\r
- *  commands   JGit commands                   Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- *  url                        Base url for Gitblit    String\r
- *  logger             Logger instance                 org.slf4j.Logger\r
+ *  gitblit                    Gitblit Server                          com.gitblit.GitBlit\r
+ *  repository         Gitblit Repository                      com.gitblit.models.RepositoryModel\r
+ *  user                       Gitblit User                            com.gitblit.models.UserModel\r
+ *  commands           JGit commands                           Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ *     url                             Base url for Gitblit            String\r
+ *  logger                     Logs messages to Gitblit        org.slf4j.Logger\r
+ *  clientLogger       Logs messages to Git client     com.gitblit.utils.ClientLogger\r
  *  \r
  */\r
 \r
index 73a25e57ec23e8083d84742434710a9bed63210f..888f6643c6ff2fee6c8f05d281b0305292b95ecb 100644 (file)
@@ -52,12 +52,13 @@ import org.slf4j.Logger
  * chain, "return false" at the appropriate failure points.\r
  * \r
  * Bound Variables:\r
- *  gitblit            Gitblit Server                  com.gitblit.GitBlit\r
- *  repository Gitblit Repository              com.gitblit.models.RepositoryModel\r
- *  user               Gitblit User                    com.gitblit.models.UserModel\r
- *  commands   JGit commands                   Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
- *     url                     Base url for Gitblit    String\r
- *  logger             Logger instance                 org.slf4j.Logger\r
+ *  gitblit                    Gitblit Server                          com.gitblit.GitBlit\r
+ *  repository         Gitblit Repository                      com.gitblit.models.RepositoryModel\r
+ *  user                       Gitblit User                            com.gitblit.models.UserModel\r
+ *  commands           JGit commands                           Collection<org.eclipse.jgit.transport.ReceiveCommand>\r
+ *     url                             Base url for Gitblit            String\r
+ *  logger                     Logs messages to Gitblit        org.slf4j.Logger\r
+ *  clientLogger       Logs messages to Git client     com.gitblit.utils.ClientLogger\r
  *  \r
  */\r
 \r
index 2e16fd15ffa9a1f1f1e06c2bea03998fdd61f177..7d18f3d6cf4797390191bf1c38df45a2cd5864ef 100644 (file)
@@ -1,32 +1,77 @@
+/*
+ * Copyright 2012 John Crygier
+ * Copyright 2012 gitblit.com
+ *
+ * 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.gitblit.utils;
 
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
 import org.eclipse.jgit.transport.ReceivePack;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Class to log messages to the pushing client.  Intended to be used by
- * the Groovy Hooks.
+ * Class to log messages to the pushing Git client. Intended to be used by the
+ * Groovy Hooks.
+ * 
+ * @author John Crygier
  * 
- * @author jcrygier
- *
  */
 public class ClientLogger {
-       
-       static final Logger logger = LoggerFactory.getLogger(ClientLogger.class);       
+
+       static final Logger logger = LoggerFactory.getLogger(ClientLogger.class);
        private ReceivePack rp;
-       
+
        public ClientLogger(ReceivePack rp) {
                this.rp = rp;
        }
-       
+
        /**
-        * Sends a message to the git client.  Useful for sending INFO / WARNING messages.
+        * Sends an info/warning message to the git client.
         * 
         * @param message
         */
-       public void sendMessage(String message) {
+       public void info(String message) {
                rp.sendMessage(message);
        }
-       
+
+       /**
+        * Sends an error message to the git client.
+        * 
+        * @param message
+        */
+       public void error(String message) {
+               rp.sendError(message);
+       }
+
+       /**
+        * Sends an error message to the git client with an exception.
+        * 
+        * @param message
+        * @param t
+        *            an exception
+        */
+       public void error(String message, Throwable t) {
+               PrintWriter writer = new PrintWriter(new StringWriter());
+               if (!StringUtils.isEmpty(message)) {
+                       writer.append(message);
+                       writer.append('\n');
+               }
+               t.printStackTrace(writer);
+               rp.sendError(writer.toString());
+       }
+
 }
index 8538d645e4772a5dca372aae61120830d335b8a7..3d3621dfac9925ba5866884d0805fb60379a2f14 100644 (file)
@@ -23,6 +23,8 @@ import groovy.util.GroovyScriptEngine;
 import java.io.BufferedWriter;\r
 import java.io.File;\r
 import java.io.FileWriter;\r
+import java.io.PrintWriter;\r
+import java.io.StringWriter;\r
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
 import java.util.Arrays;\r
@@ -226,8 +228,7 @@ public class GroovyScriptTest {
                \r
                BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript));\r
                \r
-               writer.write("import com.gitblit.utils.ClientLogger\n");\r
-               writer.write("clientLogger.sendMessage('this is a test message')\n");\r
+               writer.write("clientLogger.info('this is a test message')\n");\r
                writer.flush();\r
                writer.close();\r
 \r
@@ -308,9 +309,23 @@ public class GroovyScriptTest {
        class MockClientLogger {\r
                List<String> messages = new ArrayList<String>();\r
 \r
-               public void sendMessage(String message) {\r
+               public void info(String message) {\r
+                       messages.add(message);\r
+               }\r
+               \r
+               public void error(String message) {\r
                        messages.add(message);\r
                }\r
+               \r
+               public void error(String message, Throwable t) {\r
+                       PrintWriter writer = new PrintWriter(new StringWriter());\r
+                       if (!StringUtils.isEmpty(message)) {\r
+                               writer.append(message);\r
+                               writer.append('\n');\r
+                       }\r
+                       t.printStackTrace(writer);\r
+                       messages.add(writer.toString());\r
+               }\r
        }\r
 \r
        class MockMail {\r