From 2f5d1569cb08328c5358c919b53faa0cc8835775 Mon Sep 17 00:00:00 2001 From: James Moger Date: Sat, 31 Mar 2012 08:48:10 -0400 Subject: [PATCH] Adjustments to ClientLogger --- groovy/blockpush.groovy | 14 ++-- groovy/jenkins.groovy | 13 ++-- groovy/protect-refs.groovy | 13 ++-- groovy/sendmail.groovy | 13 ++-- src/com/gitblit/utils/ClientLogger.java | 67 ++++++++++++++++--- tests/com/gitblit/tests/GroovyScriptTest.java | 21 +++++- 6 files changed, 102 insertions(+), 39 deletions(-) diff --git a/groovy/blockpush.groovy b/groovy/blockpush.groovy index 79d696c1..186b2427 100644 --- a/groovy/blockpush.groovy +++ b/groovy/blockpush.groovy @@ -51,13 +51,13 @@ import com.gitblit.utils.ClientLogger * chain, "return false" at the appropriate failure points. * * Bound Variables: - * gitblit Gitblit Server com.gitblit.GitBlit - * repository Gitblit Repository com.gitblit.models.RepositoryModel - * user Gitblit User com.gitblit.models.UserModel - * clientLogger Logs messages to client com.gitblit.utils.ClientLogger - * commands JGit commands Collection - * url Base url for Gitblit String - * logger Logger instance org.slf4j.Logger + * gitblit Gitblit Server com.gitblit.GitBlit + * repository Gitblit Repository com.gitblit.models.RepositoryModel + * user Gitblit User com.gitblit.models.UserModel + * commands JGit commands Collection + * url Base url for Gitblit String + * logger Logs messages to Gitblit org.slf4j.Logger + * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger * */ diff --git a/groovy/jenkins.groovy b/groovy/jenkins.groovy index 443e51de..41083d24 100644 --- a/groovy/jenkins.groovy +++ b/groovy/jenkins.groovy @@ -46,12 +46,13 @@ import org.slf4j.Logger * exception handler so it will not crash another script nor crash Gitblit. * * Bound Variables: - * gitblit Gitblit Server com.gitblit.GitBlit - * repository Gitblit Repository com.gitblit.models.RepositoryModel - * user Gitblit User com.gitblit.models.UserModel - * commands JGit commands Collection - * url Base url for Gitblit String - * logger Logger instance org.slf4j.Logger + * gitblit Gitblit Server com.gitblit.GitBlit + * repository Gitblit Repository com.gitblit.models.RepositoryModel + * user Gitblit User com.gitblit.models.UserModel + * commands JGit commands Collection + * url Base url for Gitblit String + * logger Logs messages to Gitblit org.slf4j.Logger + * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger * */ // Indicate we have started the script diff --git a/groovy/protect-refs.groovy b/groovy/protect-refs.groovy index 065cf5d8..48ac60f1 100644 --- a/groovy/protect-refs.groovy +++ b/groovy/protect-refs.groovy @@ -57,12 +57,13 @@ import org.slf4j.Logger * Subsequent scripts, if any, will always be invoked. * * Bound Variables: - * gitblit Gitblit Server com.gitblit.GitBlit - * repository Gitblit Repository com.gitblit.models.RepositoryModel - * user Gitblit User com.gitblit.models.UserModel - * commands JGit commands Collection - * url Base url for Gitblit String - * logger Logger instance org.slf4j.Logger + * gitblit Gitblit Server com.gitblit.GitBlit + * repository Gitblit Repository com.gitblit.models.RepositoryModel + * user Gitblit User com.gitblit.models.UserModel + * commands JGit commands Collection + * url Base url for Gitblit String + * logger Logs messages to Gitblit org.slf4j.Logger + * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger * */ diff --git a/groovy/sendmail.groovy b/groovy/sendmail.groovy index 73a25e57..888f6643 100644 --- a/groovy/sendmail.groovy +++ b/groovy/sendmail.groovy @@ -52,12 +52,13 @@ import org.slf4j.Logger * chain, "return false" at the appropriate failure points. * * Bound Variables: - * gitblit Gitblit Server com.gitblit.GitBlit - * repository Gitblit Repository com.gitblit.models.RepositoryModel - * user Gitblit User com.gitblit.models.UserModel - * commands JGit commands Collection - * url Base url for Gitblit String - * logger Logger instance org.slf4j.Logger + * gitblit Gitblit Server com.gitblit.GitBlit + * repository Gitblit Repository com.gitblit.models.RepositoryModel + * user Gitblit User com.gitblit.models.UserModel + * commands JGit commands Collection + * url Base url for Gitblit String + * logger Logs messages to Gitblit org.slf4j.Logger + * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger * */ diff --git a/src/com/gitblit/utils/ClientLogger.java b/src/com/gitblit/utils/ClientLogger.java index 2e16fd15..7d18f3d6 100644 --- a/src/com/gitblit/utils/ClientLogger.java +++ b/src/com/gitblit/utils/ClientLogger.java @@ -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()); + } + } diff --git a/tests/com/gitblit/tests/GroovyScriptTest.java b/tests/com/gitblit/tests/GroovyScriptTest.java index 8538d645..3d3621df 100644 --- a/tests/com/gitblit/tests/GroovyScriptTest.java +++ b/tests/com/gitblit/tests/GroovyScriptTest.java @@ -23,6 +23,8 @@ import groovy.util.GroovyScriptEngine; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; +import java.io.PrintWriter; +import java.io.StringWriter; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Arrays; @@ -226,8 +228,7 @@ public class GroovyScriptTest { BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript)); - writer.write("import com.gitblit.utils.ClientLogger\n"); - writer.write("clientLogger.sendMessage('this is a test message')\n"); + writer.write("clientLogger.info('this is a test message')\n"); writer.flush(); writer.close(); @@ -308,9 +309,23 @@ public class GroovyScriptTest { class MockClientLogger { List messages = new ArrayList(); - public void sendMessage(String message) { + public void info(String message) { + messages.add(message); + } + + public void error(String message) { messages.add(message); } + + 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); + messages.add(writer.toString()); + } } class MockMail { -- 2.39.5