Skip to content

Commit

Permalink
Make MvnServer Groovy, have it log to the right place and re-refactor…
Browse files Browse the repository at this point in the history
… the IO code again
  • Loading branch information
espenhw committed Mar 5, 2009
1 parent 72890cb commit 5c2b452
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/main/groovy/org/grumblesmurf/malabar/GroovyServer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GroovyServer

static startConsole() {
IO io = new IO();
Utils.setOut(io.out);
Utils.setIO(io);
Binding binding = new Binding();
binding['io'] = io;
new Groovysh(binding, io).run();
Expand Down Expand Up @@ -106,7 +106,7 @@ class GroovySocketServer
Socket client = server.accept();
try {
IO io = new IO(client.inputStream, client.outputStream, client.outputStream);
Utils.setOut(io.out);
Utils.setIO(io);
Binding binding = new Binding();
binding['io'] = io;
new Groovysh(binding, io).run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@

import org.apache.maven.MavenTransferListener;
import org.apache.maven.cli.CLIReportingUtils;
import org.apache.maven.cli.ConsoleDownloadMonitor;
import org.apache.maven.embedder.Configuration;
import org.apache.maven.embedder.ConfigurationValidationResult;
import org.apache.maven.embedder.DefaultConfiguration;
import org.apache.maven.embedder.MavenEmbedder;
import org.apache.maven.embedder.MavenEmbedderConsoleLogger;
import org.apache.maven.embedder.AbstractMavenEmbedderLogger;
import org.apache.maven.embedder.MavenEmbedderException;
import org.apache.maven.embedder.MavenEmbedderLogger;
import org.apache.maven.errors.CoreErrorReporter;
Expand All @@ -34,6 +33,7 @@
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.execution.MavenExecutionResult;
import org.apache.maven.wagon.events.TransferEvent;
import org.codehaus.plexus.logging.AbstractLogEnabled;

import java.util.Arrays;
import java.util.Properties;
Expand All @@ -53,11 +53,14 @@ public enum MvnServer
private MvnServer() {
configuration = buildEmbedderConfiguration();
errorReporter = new DefaultCoreErrorReporter();
logger = new MavenEmbedderConsoleLogger();
logger = new MvnServerLogger();
transferListener = new MvnServerTransferListener();

if (validateConfiguration()) {
try {
org.apache.maven.embedder.MavenEmbedderLoggerManager.metaClass.debug =
Utils.&println;

mavenEmbedder = new MavenEmbedder(configuration);
mavenEmbedder.setLogger(logger);
} catch (MavenEmbedderException e) {
Expand Down Expand Up @@ -121,49 +124,165 @@ public RunDescriptor run(String pomFileName, boolean recursive, String... goals)
return run;
}

private static class MvnServerTransferListener
extends ConsoleDownloadMonitor
{
public void transferError(TransferEvent event) {
System.out.println(event.getException().getMessage());
}
public static void main(String[] args) {
INSTANCE.run(args[0], false, "test").addProperty("maven.test.skip", "true").run();
}
}

public static class RunDescriptor
{
File pom;
boolean recursive;
String[] goals;
Properties properties = new Properties();
class RunDescriptor
{
File pom;
boolean recursive;
String[] goals;
Properties properties = new Properties();

public void setPom(File pom) {
this.pom = pom;
public void setPom(File pom) {
this.pom = pom;
}
public void setRecursive(boolean recursive) {
this.recursive = recursive;
}
public void setGoals(String[] goals) {
this.goals = goals;
}
public RunDescriptor addProperty(String key, String value) {
properties.put(key, value);
return this;
}
public boolean run() {
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
.setBaseDirectory(pom.getParentFile())
.setGoals(Arrays.asList(goals))
.setTransferListener(MvnServer.INSTANCE.transferListener)
.setRecursive(recursive)
.setProperties(properties);

PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
try {
if (Utils._io.get()) {
System.setOut(new PrintStream(Utils._io.get().outputStream));
System.setErr(new PrintStream(Utils._io.get().errorStream));
}

MavenExecutionResult result = MvnServer.INSTANCE.mavenEmbedder.execute(request);
CLIReportingUtils.logResult(request, result, MvnServer.INSTANCE.logger);
return !result.hasExceptions();
} finally {
System.setOut(oldOut);
System.setErr(oldErr);
}
public void setRecursive(boolean recursive) {
this.recursive = recursive;
}
}

public class MvnServerLogger
extends AbstractMavenEmbedderLogger
{
private void log(String level, String message, Throwable t) {
Utils.print("[${level}] ");
Utils.println(message);
if (t)
t.printStackTrace(Utils.getOut());
}

public void debug(String message, Throwable t) {
if (isDebugEnabled()) {
log("DEBUG", message, t);
}
public void setGoals(String[] goals) {
this.goals = goals;
}

public void info(String message, Throwable t) {
if (isInfoEnabled()) {
log("INFO", message, t);
}
public RunDescriptor addProperty(String key, String value) {
properties.put(key, value);
return this;
}

public void warn(String message, Throwable t) {
if (isWarnEnabled()) {
log("WARNING", message, t);
}
public boolean run() {
MavenExecutionRequest request = new DefaultMavenExecutionRequest()
.setBaseDirectory(pom.getParentFile())
.setGoals(Arrays.asList(goals))
.setTransferListener(INSTANCE.transferListener)
.setRecursive(recursive)
.setProperties(properties);

MavenExecutionResult result = INSTANCE.mavenEmbedder.execute(request);
CLIReportingUtils.logResult(request, result, INSTANCE.logger);
return !result.hasExceptions();
}

public void error(String message, Throwable t) {
if (isErrorEnabled()) {
log("ERROR", message, t);
}
}

public void fatalError(String message, Throwable t) {
error(message, t);
}

public void close() {
}
}

class MvnServerTransferListener
extends AbstractLogEnabled
implements MavenTransferListener
{
private boolean showChecksumEvents = false;

public static void main(String[] args) {
INSTANCE.run(args[0], false, "test").addProperty("maven.test.skip", "true").run();
protected boolean showEvent(TransferEvent event) {
if (!event.resource) {
return true;
}

String resource = event.resource.name;

if (!resource) {
return true;
}

if (resource.endsWith(".sha1") || resource.endsWith(".md5")) {
return showChecksumEvents;
}

return true;
}

public void transferInitiated(TransferEvent transferEvent) {
if (!showEvent(transferEvent)) {
return;
}

String message =
transferEvent.requestType == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";

String url = transferEvent.wagon.repository.url;

Utils.println(message + ": " + url + "/" + transferEvent.resource.name);
}

public void transferStarted(TransferEvent transferEvent) {
}

public void transferProgress(TransferEvent transferEvent, byte[] buffer, int length) {
}

public void transferCompleted(TransferEvent transferEvent) {
long contentLength = transferEvent.resource.contentLength;
if (contentLength != WagonConstants.UNKNOWN_LENGTH) {
String type = (transferEvent.requestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded");
String l = contentLength >= 1024 ? (contentLength / 1024) + "K" : contentLength + "b";
Utils.println(l + " " + type);
}
}

public void transferError(TransferEvent event) {
Utils.println(event.getException().getMessage());
}

public void debug(String message) {
}

public boolean isShowChecksumEvents()
{
return showChecksumEvents;
}

public void setShowChecksumEvents(boolean showChecksumEvents)
{
this.showChecksumEvents = showChecksumEvents;
}
}
12 changes: 7 additions & 5 deletions src/main/groovy/org/grumblesmurf/malabar/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.grumblesmurf.malabar
package org.grumblesmurf.malabar;

import org.codehaus.groovy.tools.shell.IO;

class Utils
{
static ThreadLocal<PrintWriter> _out = new ThreadLocal<PrintWriter>();
static ThreadLocal<IO> _io = new ThreadLocal<IO>();

static getOut() {
return _out.get() ?: System.out
return _io.get()?.out ?: System.out
}

static setOut(PrintWriter out) {
_out.set(out);
static setIO(IO io) {
_io.set(io);
}

static print(Object v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ClasspathTest
@Before
void setIO() {
out = new ByteArrayOutputStream();
Utils.setOut(new IO(System.in, out, out).out)
Utils.setIO(new IO(System.in, out, out));
}

@AfterClass
Expand Down

0 comments on commit 5c2b452

Please sign in to comment.