Remove and replace Util.sout and .serr

This commit is contained in:
Winston Li 2016-04-18 11:03:55 +01:00
parent a409a15f07
commit d582e3e1cf
11 changed files with 91 additions and 71 deletions

View file

@ -5,11 +5,11 @@ import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.bridge.WLBridgedProject;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InternalErrorException;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.File;
import java.io.IOException;
@ -36,8 +36,16 @@ public class SnapshotRepositoryBuilder {
repository = new FileRepositoryBuilder().setWorkTree(repositoryDirectory).build();
new WLBridgedProject(repository, name, bridgeAPI).buildRepository(oauth2);
} catch (IOException e) {
Util.printStackTrace(e);
throw new ServiceMayNotContinueException(new InternalErrorException().getDescriptionLines().get(0));
Log.warn(
"IOException when trying to get repo: " +
name +
", at: "
+ rootDirectory.getAbsolutePath(),
e
);
throw new ServiceMayNotContinueException(
new InternalErrorException().getDescriptionLines().get(0)
);
}
return repository;
}

View file

@ -11,7 +11,7 @@ import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.snapshot.base.Request;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -37,14 +37,13 @@ public class ResourceFetcher {
contents = fetch(projectName, url, path);
fetchedUrls.put(url, contents);
} else {
Util.sout("Found (" + projectName + "): " + url);
Util.sout("At (" + projectName + "): " + path);
Log.info("Found (" + projectName + "): " + url);
Log.info("At (" + projectName + "): " + path);
contents = fetchedUrls.get(url);
if (contents == null) {
RawFile rawFile = new RepositoryObjectTreeWalker(repository).getDirectoryContents().getFileTable().get(path);
if (rawFile == null) {
Util.sout(
"WARNING: " +
Log.warn(
"File " + path + " was not in the current commit, or the git tree, yet path was not null. " +
"File url is: " + url
);
@ -59,7 +58,7 @@ public class ResourceFetcher {
private byte[] fetch(String projectName, final String url, String path) throws FailedConnectionException {
byte[] contents;
Util.sout("GET -> " + url);
Log.info("GET -> " + url);
try {
contents = Request.httpClient.prepareGet(url).execute(new AsyncCompletionHandler<byte[]>() {
@ -75,16 +74,32 @@ public class ResourceFetcher {
public byte[] onCompleted(Response response) throws Exception {
byte[] data = bytes.toByteArray();
bytes.close();
Util.sout(response.getStatusCode() + " " + response.getStatusText() + " (" + data.length + "B) -> " + url);
Log.info(response.getStatusCode() + " " + response.getStatusText() + " (" + data.length + "B) -> " + url);
return data;
}
}).get();
} catch (InterruptedException e) {
Util.printStackTrace(e);
Log.warn(
"Interrupted when fetching project: " +
projectName +
", url: " +
url +
", path: " +
path,
e
);
throw new FailedConnectionException();
} catch (ExecutionException e) {
Util.printStackTrace(e);
Log.warn(
"ExecutionException when fetching project: " +
projectName +
", url: " +
url +
", path: " +
path,
e
);
throw new FailedConnectionException();
}
persistentStore.addURLIndexForProject(projectName, url, path);

View file

@ -7,11 +7,11 @@ import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import org.eclipse.jgit.transport.resolver.RepositoryResolver;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import uk.ac.ic.wlgitbridge.application.config.Oauth2;
import uk.ac.ic.wlgitbridge.data.SnapshotRepositoryBuilder;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.server.Oauth2Filter;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.util.Log;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.http.HttpServletRequest;
@ -36,7 +36,7 @@ public class WLRepositoryResolver implements RepositoryResolver<HttpServletReque
try {
return snapshotRepositoryBuilder.getRepositoryWithNameAtRootDirectory(Util.removeAllSuffixes(name, "/", ".git"), rootGitDirectory, oauth2);
} catch (RepositoryNotFoundException e) {
Util.printStackTrace(e);
Log.info("Repository not found: " + name);
throw e;
/*
} catch (ServiceNotAuthorizedException e) {
@ -47,7 +47,7 @@ public class WLRepositoryResolver implements RepositoryResolver<HttpServletReque
} catch (ServiceMayNotContinueException e) { /* Such as FailedConnectionException */
throw e;
} catch (RuntimeException e) {
Util.printStackTrace(e);
Log.warn("Runtime exception when trying to open repo", e);
throw new ServiceMayNotContinueException(e);
} catch (ForbiddenException e) {
throw new ServiceNotAuthorizedException();

View file

@ -7,16 +7,15 @@ import org.eclipse.jgit.transport.ReceiveCommand;
import org.eclipse.jgit.transport.ReceiveCommand.Result;
import org.eclipse.jgit.transport.ReceivePack;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.ForcedPushException;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.WrongBranchException;
import uk.ac.ic.wlgitbridge.data.filestore.RawDirectory;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.snapshot.base.ForbiddenException;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InternalErrorException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.OutOfDateException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.IOException;
import java.util.Collection;
@ -49,7 +48,7 @@ public class WriteLatexPutHook implements PreReceiveHook {
} catch (SnapshotPostException e) {
handleSnapshotPostException(receivePack, receiveCommand, e);
} catch (Throwable t) {
Util.printStackTrace(t);
Log.warn("Throwable on pre receive: ", t);
handleSnapshotPostException(receivePack, receiveCommand, new InternalErrorException());
}
}

View file

@ -4,9 +4,9 @@ import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.MultiMap;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InvalidPostbackKeyException;
import uk.ac.ic.wlgitbridge.util.Log;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ -29,7 +29,7 @@ public class FileServlet extends ResourceHandler {
String method = baseRequest.getMethod();
if (method.equals("GET")) {
HttpURI uri = baseRequest.getUri();
Util.sout(method + " <- " + uri);
Log.info(method + " <- " + uri);
MultiMap<String> multimap = new MultiMap<String>();
uri.decodeQueryTo(multimap);
String[] pathSections = uri.getPath().split("/");

View file

@ -6,6 +6,7 @@ import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
import uk.ac.ic.wlgitbridge.util.Log;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.ServletException;
@ -37,7 +38,7 @@ public class PostbackHandler extends AbstractHandler {
}
String projectName = parts[1];
String postbackKey = parts[2];
Util.sout(baseRequest.getMethod() + " <- " + baseRequest.getUri());
Log.info(baseRequest.getMethod() + " <- " + baseRequest.getUri());
PostbackContents postbackContents = new PostbackContents(bridgeAPI, projectName, postbackKey, contents);
JsonObject body = new JsonObject();
@ -56,13 +57,24 @@ public class PostbackHandler extends AbstractHandler {
baseRequest.setHandled(true);
}
} catch (IOException e) {
Util.printStackTrace(e);
Log.warn(
"IOException when handling postback to target: " + target,
e
);
throw e;
} catch (ServletException e) {
Util.printStackTrace(e);
Log.warn(
"ServletException when handling postback to target: " +
target,
e
);
throw e;
} catch (RuntimeException e) {
Util.printStackTrace(e);
Log.warn(
"RuntimeException when handling postback to target: " +
target,
e
);
throw e;
}
}

View file

@ -6,7 +6,6 @@ import com.ning.http.client.AsyncHttpClient;
import uk.ac.ic.wlgitbridge.snapshot.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.util.Instance;
import uk.ac.ic.wlgitbridge.util.Log;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@ -88,7 +87,7 @@ public abstract class Request<T extends Result> {
}
private void performGetRequest() {
Util.sout("GET -> " + url);
Log.info("GET -> " + url);
try {
HttpRequest request = Instance.httpRequestFactory.buildGetRequest(
new GenericUrl(url)
@ -101,7 +100,7 @@ public abstract class Request<T extends Result> {
}
private void performPostRequest() {
Util.sout("POST -> " + url);
Log.info("POST -> " + url);
try {
HttpRequest request = Instance.httpRequestFactory.buildPostRequest(
new GenericUrl(url),

View file

@ -5,7 +5,7 @@ import org.eclipse.jetty.server.handler.AbstractHandler;
import uk.ac.ic.wlgitbridge.snapshot.servermock.exception.InvalidAPICallException;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.SnapshotResponse;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.SnapshotResponseBuilder;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.util.Log;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ -34,7 +34,7 @@ public class MockSnapshotRequestHandler extends AbstractHandler {
} catch (InvalidAPICallException e) {
handled = false;
} catch (RuntimeException e) {
Util.printStackTrace(e);
Log.warn("Runtime exception when handling request", e);
handled = true;
}
baseRequest.setHandled(handled);

View file

@ -6,7 +6,7 @@ import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.ResourceHandler;
import uk.ac.ic.wlgitbridge.snapshot.servermock.response.SnapshotResponseBuilder;
import uk.ac.ic.wlgitbridge.snapshot.servermock.state.SnapshotAPIState;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.File;
@ -42,7 +42,7 @@ public class MockSnapshotServer {
try {
server.start();
} catch (Exception e) {
Util.printStackTrace(e);
Log.warn("Exception when trying to start server", e);
}
port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
}

View file

@ -3,7 +3,7 @@ package uk.ac.ic.wlgitbridge.snapshot.servermock.server;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ning.http.client.AsyncHttpClient;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.util.Log;
import java.io.IOException;
import java.io.Reader;
@ -29,11 +29,29 @@ public class PostbackThread extends Thread {
try {
new AsyncHttpClient().preparePost(url).setBody(postback).execute().get().getResponseBody();
} catch (IOException e) {
Util.printStackTrace(e);
Log.warn(
"IOException on postback, url: " +
url +
", postback: " +
postback,
e
);
} catch (InterruptedException e) {
Util.printStackTrace(e);
Log.warn(
"Interrupted on postback, url: " +
url +
", postback: " +
postback,
e
);
} catch (ExecutionException e) {
Util.printStackTrace(e);
Log.warn(
"ExecutionException on postback, url: " +
url +
", postback: " +
postback,
e
);
}
}

View file

@ -89,37 +89,6 @@ public class Util {
return POSTBACK_URL;
}
private static void println(PrintStream ps, String ln) {
Log.info(ln);
}
public static void sout(String ln) {
println(System.out, ln);
}
public static void serr(String ln) {
println(System.err, ln);
}
private static StringBuilder getStringBuilder() {
StringBuilder sb = new StringBuilder("[");
sb.append(dateFormat.format(new Date()));
sb.append("] ");
return sb;
}
public static void serr() {
serr("");
}
public static void serr(Object obj) {
serr(obj.toString());
}
public static void printStackTrace(Throwable t) {
Log.warn("Exception", t);
}
public static void deleteDirectory(File directory) {
if (directory != null) {
deleteInDirectory(directory);
@ -195,9 +164,9 @@ public class Util {
if (codeElement == null) {
String error = "Unexpected error";
serr("Unexpected response from API:");
serr(json.toString());
serr("End of response");
Log.warn("Unexpected response from API:");
Log.warn(json.toString());
Log.warn("End of response");
JsonElement statusElement = json.get("status");
if (statusElement != null) {
String status = statusElement.getAsString();