Skip to content
Browse files

some work around exception reporting

  • Loading branch information...
1 parent 1e3d4fe commit 7fce663038dc134c36b9eafc980678642fb64135 @purplefox purplefox committed
Showing with 206 additions and 124 deletions.
  1. +15 −0 src/examples/javascript/scratch/scratch.js
  2. +17 −0 src/examples/ruby/scratch/scratch.rb
  3. +3 −0 src/main/java/org/vertx/java/core/Vertx.java
  4. +4 −0 src/main/java/org/vertx/java/core/VertxImpl.java
  5. +3 −0 src/main/java/org/vertx/java/core/app/VerticleFactory.java
  6. +52 −42 src/main/java/org/vertx/java/core/app/VerticleManager.java
  7. +12 −8 src/main/java/org/vertx/java/core/app/groovy/GroovyVerticleFactory.java
  8. +7 −0 src/main/java/org/vertx/java/core/app/java/JavaVerticleFactory.java
  9. +35 −0 src/main/java/org/vertx/java/core/app/jruby/JRubyVerticleFactory.java
  10. +1 −0 src/main/java/org/vertx/java/core/app/rhino/RhinoVerticle.java
  11. +14 −0 src/main/java/org/vertx/java/core/app/rhino/RhinoVerticleFactory.java
  12. +2 −2 src/main/java/org/vertx/java/core/logging/JULLogDelegate.java
  13. +2 −2 src/main/java/org/vertx/java/core/logging/JULLogDelegateFactory.java
  14. +2 −2 src/main/java/org/vertx/java/core/logging/Log4jLogDelegate.java
  15. +2 −2 src/main/java/org/vertx/java/core/logging/Log4jLogDelegateFactory.java
  16. +1 −1 src/main/java/org/vertx/java/core/logging/LogDelegateFactory.java
  17. +12 −4 src/main/java/org/vertx/java/core/logging/Logger.java
  18. +2 −2 src/main/java/org/vertx/java/core/logging/SLF4JLogDelegate.java
  19. +1 −1 src/main/java/org/vertx/java/core/logging/SLF4JLogDelegateFactory.java
  20. +8 −0 src/main/javascript/core/logger.js
  21. +2 −1 src/main/javascript/vertx.js
  22. +3 −57 src/main/ruby/core/logger.rb
  23. +3 −0 src/tests/javascript/core/http/test_client.js
  24. +3 −0 src/tests/ruby/core/http/test_client.rb
View
15 src/examples/javascript/scratch/scratch.js
@@ -1,3 +1,18 @@
load('vertx.js');
log.println("in scratch");
+
+foo();
+
+function foo() {
+ bar();
+}
+
+function bar() {
+ quux();
+}
+
+
+function quux() {
+ throw "foo"
+}
View
17 src/examples/ruby/scratch/scratch.rb
@@ -0,0 +1,17 @@
+puts "in ruby verticle"
+
+def foo
+ bar
+end
+
+def bar
+ quux
+end
+
+
+def quux
+ raise "Foo"
+end
+
+foo()
+
View
3 src/main/java/org/vertx/java/core/Vertx.java
@@ -17,6 +17,7 @@
package org.vertx.java.core;
import org.vertx.java.core.json.JsonObject;
+import org.vertx.java.core.logging.Logger;
/**
*
@@ -90,4 +91,6 @@
void exit();
JsonObject getConfig();
+
+ Logger getLogger();
}
View
4 src/main/java/org/vertx/java/core/VertxImpl.java
@@ -196,6 +196,10 @@ public JsonObject getConfig() {
return VerticleManager.instance.getConfig();
}
+ public Logger getLogger() {
+ return VerticleManager.instance.getLogger();
+ }
+
// Internal API -----------------------------------------------------------------------------------------
//The worker pool is used for making blocking calls to legacy synchronous APIs
View
3 src/main/java/org/vertx/java/core/app/VerticleFactory.java
@@ -4,6 +4,9 @@
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public interface VerticleFactory {
+
Verticle createVerticle(String main, ClassLoader parentCL) throws Exception;
+ void reportException(Throwable t);
+
}
View
94 src/main/java/org/vertx/java/core/app/VerticleManager.java
@@ -1,5 +1,6 @@
package org.vertx.java.core.app;
+import org.mozilla.javascript.JavaScriptException;
import org.vertx.java.core.Handler;
import org.vertx.java.core.SimpleHandler;
import org.vertx.java.core.Vertx;
@@ -11,8 +12,6 @@
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
-import java.io.File;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
@@ -34,19 +33,22 @@
public static VerticleManager instance = new VerticleManager();
- private Map<String, Deployment> deployments = new HashMap();
- private final Map<Long, String> contextMap = new ConcurrentHashMap<>();
+ private final Map<String, Deployment> deployments = new HashMap();
+ private final Map<Long, String> contextDeploymentNameMap = new ConcurrentHashMap<>();
+ private final Map<Long, VerticleHolder> contextVerticleMap = new ConcurrentHashMap<>();
private CountDownLatch stopLatch = new CountDownLatch(1);
private static class Deployment {
+ final VerticleFactory factory;
final JsonObject config;
final URL[] urls;
final List<VerticleHolder> verticles = new ArrayList<>();
final List<String> childDeployments = new ArrayList<>();
final String parentDeploymentName;
- private Deployment(JsonObject config, URL[] urls, String parentDeploymentName) {
+ private Deployment(VerticleFactory factory, JsonObject config, URL[] urls, String parentDeploymentName) {
+ this.factory = factory;
this.config = config;
this.urls = urls;
this.parentDeploymentName = parentDeploymentName;
@@ -84,7 +86,7 @@ public JsonObject getConfig() {
public String getDeploymentName() {
Long contextID = Vertx.instance.getContextID();
- return contextID == null ? null : contextMap.get(contextID);
+ return contextID == null ? null : contextDeploymentNameMap.get(contextID);
}
public URL[] getDeploymentURLs() {
@@ -98,6 +100,27 @@ public String getDeploymentName() {
return null;
}
+ public Logger getLogger() {
+ VerticleHolder holder = contextVerticleMap.get(Vertx.instance.getContextID());
+ if (holder != null) {
+ return holder.logger;
+ } else {
+ return null;
+ }
+ }
+
+ public void reportException(Throwable t) {
+ String deploymentName = getDeploymentName();
+ if (deploymentName != null) {
+ Deployment deployment = deployments.get(deploymentName);
+ if (deployment != null) {
+ deployment.factory.reportException(t);
+ return;
+ }
+ }
+ log.error("Unhandled exception", t);
+ }
+
public synchronized String deploy(boolean worker, String name, final String main,
final JsonObject config, final URL[] urls,
int instances,
@@ -107,8 +130,6 @@ public synchronized String deploy(boolean worker, String name, final String main
throw new IllegalStateException("There is already a deployment with name: " + name);
}
- //final String path = thePath == null ? "." : thePath;
-
//Infer the main type
VerticleType type = VerticleType.JAVA;
@@ -120,32 +141,6 @@ public synchronized String deploy(boolean worker, String name, final String main
type = VerticleType.GROOVY;
}
- // Convert to URL[]
-
-// String[] parts;
-// if (path.contains(":")) {
-// parts = path.split(":");
-// } else {
-// parts = new String[] { path };
-// }
-// int index = 0;
-// final URL[] urls = new URL[parts.length];
-// for (String part: parts) {
-// File file = new File(part);
-// part = file.getAbsolutePath();
-// if (!part.endsWith(".jar") && !part.endsWith(".zip") && !part.endsWith("/")) {
-// //It's a directory - need to add trailing slash
-// part += "/";
-// }
-// URL url;
-// try {
-// url = new URL("file://" + part);
-// } catch (MalformedURLException e) {
-// throw new IllegalArgumentException("Invalid path: " + path) ;
-// }
-// urls[index++] = url;
-// }
-
final String deploymentName = name == null ? "deployment-" + UUID.randomUUID().toString() : name;
log.debug("Deploying name : " + deploymentName + " main: " + main +
@@ -186,7 +181,7 @@ void started() {
final AggHandler aggHandler = new AggHandler();
String parentDeploymentName = getDeploymentName();
- Deployment deployment = new Deployment(config == null ? null : config.copy(), urls, parentDeploymentName);
+ Deployment deployment = new Deployment(verticleFactory, config == null ? null : config.copy(), urls, parentDeploymentName);
deployments.put(deploymentName, deployment);
if (parentDeploymentName != null) {
Deployment parent = deployments.get(parentDeploymentName);
@@ -214,7 +209,7 @@ public void run() {
addVerticle(deploymentName, verticle);
verticle.start();
} catch (Throwable t) {
- log.error("Unhandled exception in verticle start", t);
+ reportException(t);
internalUndeploy(deploymentName, doneHandler);
}
aggHandler.started();
@@ -266,7 +261,7 @@ public synchronized void undeploy(String name, final Handler<Void> doneHandler)
}
private void internalUndeploy(String name, final Handler<Void> doneHandler) {
- Deployment deployment = deployments.remove(name);
+ final Deployment deployment = deployments.remove(name);
// Depth first - undeploy children first TODO
// for (String childDeployment: deployment.childDeployments) {
@@ -293,9 +288,13 @@ public void run() {
}
// Remove context mapping
- contextMap.remove(holder.contextID);
+ contextDeploymentNameMap.remove(holder.contextID);
+ contextVerticleMap.remove(holder.contextID);
+
+ Logger.removeLogger(holder.loggerName);
}
});
+
}
}
@@ -316,20 +315,31 @@ public void run() {
return map;
}
- // Must be synchronized since called directlyfrom different thread
+ // Must be synchronized since called directly from different thread
private synchronized void addVerticle(String name, Verticle verticle) {
Deployment deployment = deployments.get(name);
- deployment.verticles.add(new VerticleHolder(Vertx.instance.getContextID(), verticle));
- contextMap.put(Vertx.instance.getContextID(), name);
+ String loggerName = name + "-" + deployment.verticles.size();
+ Logger logger = Logger.getLogger(loggerName);
+ Long contextID = Vertx.instance.getContextID();
+ VerticleHolder holder = new VerticleHolder(contextID, verticle,
+ loggerName, logger);
+ deployment.verticles.add(holder);
+ contextDeploymentNameMap.put(contextID, name);
+ contextVerticleMap.put(contextID, holder);
}
private static class VerticleHolder {
final long contextID;
final Verticle verticle;
+ final String loggerName;
+ final Logger logger;
- private VerticleHolder(long contextID, Verticle verticle) {
+ private VerticleHolder(long contextID, Verticle verticle, String loggerName,
+ Logger logger) {
this.contextID = contextID;
this.verticle = verticle;
+ this.loggerName = loggerName;
+ this.logger = logger;
}
}
}
View
20 src/main/java/org/vertx/java/core/app/groovy/GroovyVerticleFactory.java
@@ -2,8 +2,10 @@
import groovy.lang.GroovyClassLoader;
import groovy.lang.GroovyCodeSource;
+import org.mozilla.javascript.JavaScriptException;
import org.vertx.java.core.app.Verticle;
import org.vertx.java.core.app.VerticleFactory;
+import org.vertx.java.core.app.VerticleManager;
import org.vertx.java.core.logging.Logger;
import java.lang.reflect.Method;
@@ -14,8 +16,6 @@
*/
public class GroovyVerticleFactory implements VerticleFactory {
- private static final Logger log = Logger.getLogger(GroovyVerticleFactory.class);
-
public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
URL url = cl.getResource(main);
@@ -48,22 +48,26 @@ public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
return new Verticle() {
public void start() {
try {
- mrun.invoke(verticle, (Object[])null);
- } catch (Exception e) {
- log.error("Failed to run Groovy verticle", e);
- }
+ mrun.invoke(verticle, (Object[])null);
+ } catch (Throwable t) {
+ reportException(t);
+ }
}
public void stop() {
if (mstop != null) {
try {
mstop.invoke(verticle, (Object[])null);
- } catch (Exception e) {
- log.error("Failed to stop Groovy verticle", e);
+ } catch (Throwable t) {
+ reportException(t);
}
}
}
};
}
+
+ public void reportException(Throwable t) {
+ VerticleManager.instance.getLogger().error("Exception in Groovy verticle", t);
+ }
}
View
7 src/main/java/org/vertx/java/core/app/java/JavaVerticleFactory.java
@@ -1,7 +1,10 @@
package org.vertx.java.core.app.java;
+import org.mozilla.javascript.JavaScriptException;
import org.vertx.java.core.app.Verticle;
import org.vertx.java.core.app.VerticleFactory;
+import org.vertx.java.core.app.VerticleManager;
+import org.vertx.java.core.logging.Logger;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
@@ -26,4 +29,8 @@ public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
return verticle;
}
+
+ public void reportException(Throwable t) {
+ VerticleManager.instance.getLogger().error("Exception in Java verticle script", t);
+ }
}
View
35 src/main/java/org/vertx/java/core/app/jruby/JRubyVerticleFactory.java
@@ -1,7 +1,18 @@
package org.vertx.java.core.app.jruby;
+import org.jruby.RubyException;
+import org.jruby.embed.EvalFailedException;
+import org.jruby.exceptions.RaiseException;
+import org.jruby.runtime.backtrace.BacktraceData;
+import org.jruby.runtime.backtrace.RubyStackTraceElement;
+import org.mozilla.javascript.JavaScriptException;
import org.vertx.java.core.app.Verticle;
import org.vertx.java.core.app.VerticleFactory;
+import org.vertx.java.core.app.VerticleManager;
+import org.vertx.java.core.logging.Logger;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
@@ -15,4 +26,28 @@ public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
Verticle app = new JRubyVerticle(main, cl);
return app;
}
+
+ public void reportException(Throwable t) {
+ Logger logger = VerticleManager.instance.getLogger();
+
+ if (t instanceof EvalFailedException) {
+ EvalFailedException je = (EvalFailedException)t;
+ Throwable cause = je.getCause();
+ if (cause instanceof RaiseException) {
+ // Gosh, this is a bit long winded!
+ RaiseException re = (RaiseException)cause;
+ RubyException rbe = re.getException();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ PrintStream ps = new PrintStream(baos);
+ rbe.printBacktrace(ps);
+ ps.flush();
+ String stack = baos.toString();
+ logger.error("Exception in Ruby verticle: " + rbe.message +
+ "\n" + stack);
+ }
+ }
+
+
+ logger.error("Exception in Ruby verticle", t);
+ }
}
View
1 src/main/java/org/vertx/java/core/app/rhino/RhinoVerticle.java
@@ -36,6 +36,7 @@ public static void load(String moduleName) throws Exception {
ScriptableObject scope = scopeThreadLocal.get();
ClassLoader cl = clThreadLocal.get();
Context cx = Context.getCurrentContext();
+ cx.setOptimizationLevel(0);
loadScript(cl, cx, scope, moduleName);
}
View
14 src/main/java/org/vertx/java/core/app/rhino/RhinoVerticleFactory.java
@@ -1,8 +1,11 @@
package org.vertx.java.core.app.rhino;
import org.mozilla.javascript.ContextFactory;
+import org.mozilla.javascript.JavaScriptException;
import org.vertx.java.core.app.Verticle;
import org.vertx.java.core.app.VerticleFactory;
+import org.vertx.java.core.app.VerticleManager;
+import org.vertx.java.core.logging.Logger;
/**
* @author <a href="http://tfox.org">Tim Fox</a>
@@ -17,5 +20,16 @@ public Verticle createVerticle(String main, ClassLoader cl) throws Exception {
Verticle app = new RhinoVerticle(main, cl);
return app;
}
+
+ public void reportException(Throwable t) {
+ Logger logger = VerticleManager.instance.getLogger();
+ if (t instanceof JavaScriptException) {
+ JavaScriptException je = (JavaScriptException)t;
+ logger.error("Exception in JavaScript verticle: " + je.getMessage() +
+ "\n" + je.getScriptStackTrace());
+ } else {
+ logger.error("Exception in JavaScript verticle", t);
+ }
+ }
}
View
4 src/main/java/org/vertx/java/core/logging/JULLogDelegate.java
@@ -23,8 +23,8 @@
public class JULLogDelegate implements LogDelegate {
private final java.util.logging.Logger logger;
- JULLogDelegate(final Class<?> clazz) {
- logger = java.util.logging.Logger.getLogger(clazz.getName());
+ JULLogDelegate(final String name) {
+ logger = java.util.logging.Logger.getLogger(name);
}
public boolean isInfoEnabled() {
View
4 src/main/java/org/vertx/java/core/logging/JULLogDelegateFactory.java
@@ -19,7 +19,7 @@
* @author <a href="kenny.macleod@kizoom.com">Kenny MacLeod</a>
*/
public class JULLogDelegateFactory implements LogDelegateFactory {
- public LogDelegate createDelegate(final Class<?> clazz) {
- return new JULLogDelegate(clazz);
+ public LogDelegate createDelegate(final String name) {
+ return new JULLogDelegate(name);
}
}
View
4 src/main/java/org/vertx/java/core/logging/Log4jLogDelegate.java
@@ -21,8 +21,8 @@
public class Log4jLogDelegate implements LogDelegate {
private final org.apache.log4j.Logger logger;
- Log4jLogDelegate(final Class<?> clazz) {
- logger = org.apache.log4j.Logger.getLogger(clazz);
+ Log4jLogDelegate(final String name) {
+ logger = org.apache.log4j.Logger.getLogger(name);
}
public boolean isInfoEnabled() {
View
4 src/main/java/org/vertx/java/core/logging/Log4jLogDelegateFactory.java
@@ -22,9 +22,9 @@
*/
public class Log4jLogDelegateFactory implements LogDelegateFactory
{
- public LogDelegate createDelegate(final Class<?> clazz)
+ public LogDelegate createDelegate(final String name)
{
- return new Log4jLogDelegate(clazz);
+ return new Log4jLogDelegate(name);
}
}
View
2 src/main/java/org/vertx/java/core/logging/LogDelegateFactory.java
@@ -19,5 +19,5 @@
* @author <a href="kenny.macleod@kizoom.com">Kenny MacLeod</a>
*/
public interface LogDelegateFactory {
- LogDelegate createDelegate(Class<?> clazz);
+ LogDelegate createDelegate(String name);
}
View
16 src/main/java/org/vertx/java/core/logging/Logger.java
@@ -39,7 +39,7 @@
private static volatile LogDelegateFactory delegateFactory;
- private static final ConcurrentMap<Class<?>, Logger> loggers = new ConcurrentHashMap<>();
+ private static final ConcurrentMap<String, Logger> loggers = new ConcurrentHashMap<>();
static {
Logger.initialise();
@@ -74,14 +74,18 @@ public static synchronized void initialise() {
}
public static Logger getLogger(final Class<?> clazz) {
- Logger logger = Logger.loggers.get(clazz);
+ return getLogger(clazz.getCanonicalName());
+ }
+
+ public static Logger getLogger(final String name) {
+ Logger logger = Logger.loggers.get(name);
if (logger == null) {
- LogDelegate delegate = Logger.delegateFactory.createDelegate(clazz);
+ LogDelegate delegate = Logger.delegateFactory.createDelegate(name);
logger = new Logger(delegate);
- Logger oldLogger = Logger.loggers.putIfAbsent(clazz, logger);
+ Logger oldLogger = Logger.loggers.putIfAbsent(name, logger);
if (oldLogger != null) {
logger = oldLogger;
@@ -91,6 +95,10 @@ public static Logger getLogger(final Class<?> clazz) {
return logger;
}
+ public static void removeLogger(String name) {
+ loggers.remove(name);
+ }
+
private final LogDelegate delegate;
Logger(final LogDelegate delegate) {
View
4 src/main/java/org/vertx/java/core/logging/SLF4JLogDelegate.java
@@ -26,8 +26,8 @@
private final Logger logger;
- SLF4JLogDelegate(final Class<?> clazz) {
- logger = LoggerFactory.getLogger(clazz);
+ SLF4JLogDelegate(final String name) {
+ logger = LoggerFactory.getLogger(name);
}
public boolean isInfoEnabled() {
View
2 src/main/java/org/vertx/java/core/logging/SLF4JLogDelegateFactory.java
@@ -18,7 +18,7 @@
*/
public class SLF4JLogDelegateFactory implements LogDelegateFactory
{
- public LogDelegate createDelegate(final Class<?> clazz)
+ public LogDelegate createDelegate(final String clazz)
{
return new SLF4JLogDelegate(clazz);
}
View
8 src/main/javascript/core/logger.js
@@ -0,0 +1,8 @@
+var vertx = vertx || {};
+
+if (!vertx.getLogger) {
+
+ vertx.getLogger = function() {
+ return org.vertx.java.core.Vertx.instance.getLogger();
+ }
+}
View
3 src/main/javascript/vertx.js
@@ -9,4 +9,5 @@ load('core/sockjs.js');
load('core/parse_tools.js');
load('core/shared_data.js');
load('core/filesystem.js');
-load('core/deploy.js');
+load('core/deploy.js');
+load('core/logger.js');
View
60 src/main/ruby/core/logger.rb
@@ -15,62 +15,8 @@
module Vertx
- # @author {http://tfox.org Tim Fox}
- class Logger
-
- def Logger.get_logger(clazz)
- raise "Please provide a class to get_logger method" if !clazz.is_a? Class
- Logger.new(org.vertx.java.core.logging.Logger.getLogger(clazz))
- end
-
- def initialize(j_del)
- @j_del = j_del
- end
-
- def info_enabled?
- @j_del.infoEnabled
- end
-
- def debug_enabled?
- @j_del.debugEnabled
- end
-
- def trace_enabled?
- @j_del.traceEnabled
- end
-
- def fatal
-
- end
-
- def error(msg, ex = nil)
-
- if ex == nil
- @j_del.error(msg)
- elsif ex.is_a? java.lang.Throwable
- @j_del.error(msg, ex)
- elsif ex.is_a? Exception
- @j_del.error(obj.message)
- trace = ''
- obj.backtrace.each { |line| trace << line << "\n" }
- @j_del.error(trace)
- end
- end
-
-
- def warn
-
- end
-
- def info
-
- end
-
- def trace
-
- end
-
-
-
+ def Vertx.logger
+ org.vertx.java.core.Vertx.instance.getLogger
end
+
end
View
3 src/tests/javascript/core/http/test_client.js
@@ -5,6 +5,7 @@ var tu = new TestUtils();
var server = new vertx.HttpServer();
var client = new vertx.HttpClient().setPort(8080);
+var logger = vertx.getLogger();
// This is just a basic test. Most testing occurs in the Java tests
@@ -157,6 +158,8 @@ function testPATCHSSLChunked() {
function httpMethod(ssl, method, chunked) {
+ // logger.info("In method " + method);
+
if (ssl) {
server.setSSL(true);
server.setKeyStorePath('./src/tests/keystores/server-keystore.jks');
View
3 src/tests/ruby/core/http/test_client.rb
@@ -7,6 +7,7 @@
@server = HttpServer.new
@client = HttpClient.new
@client.port = 8080
+@logger = Vertx.logger
# This is just a basic test. Most testing occurs in the Java tests
@@ -159,6 +160,8 @@ def test_patch_ssl_chunked
def http_method(ssl, method, chunked)
+ # @logger.info("in http method #{method}")
+
if ssl
@server.ssl = true
@server.key_store_path = './src/tests/keystores/server-keystore.jks'

0 comments on commit 7fce663

Please sign in to comment.
Something went wrong with that request. Please try again.