Skip to content

Commit

Permalink
issue #9: make logging of exceptions optional. By default the stacktr…
Browse files Browse the repository at this point in the history
…ace is logged
  • Loading branch information
matlux committed Aug 28, 2014
1 parent 6cf6b2b commit e618e36
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
14 changes: 8 additions & 6 deletions bootloader/src/main/java/net/matlux/MBeanRegistration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ public final class MBeanRegistration {

private static final Logger LOGGER = Logger.getLogger(MBeanRegistration.class.getSimpleName());

public static void registerNreplServerAsMBean(NreplMBean nreplServer) {
public static void registerNreplServerAsMBean(NreplMBean nreplServer, boolean logExceptionStack) {
try {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
registerMBean(mbs, getObjectName(), nreplServer);
LOGGER.log(Level.SEVERE, "MBean Registration of JVM-breakglass successful");
LOGGER.log(Level.INFO, "MBean Registration of JVM-breakglass successful");
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "MBean Registration of JVM-breakglass not successful", e);
if (logExceptionStack) LOGGER.log(Level.SEVERE, "MBean Registration of JVM-breakglass not successful", e);
else LOGGER.log(Level.INFO, "MBean Registration of JVM-breakglass not successful");
throw new RuntimeException("MBean Registration of JVM-breakglass not successful", e);
}
}

public static void unregisterNreplServerAsMBean() {
public static void unregisterNreplServerAsMBean(boolean logExceptionStack) {
try {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.unregisterMBean(getObjectName());
LOGGER.log(Level.SEVERE, "MBean Unregistration of JVM-breakglass successful");
LOGGER.log(Level.INFO, "MBean Unregistration of JVM-breakglass successful");
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "MBean Unregistration of JVM-breakglass not successful", e);
if (logExceptionStack) LOGGER.log(Level.SEVERE, "MBean Unregistration of JVM-breakglass not successful", e);
else LOGGER.log(Level.INFO, "MBean Unregistration of JVM-breakglass not successful");
throw new RuntimeException("MBean Unregistration of JVM-breakglass not successful", e);
}
}
Expand Down
17 changes: 11 additions & 6 deletions bootloader/src/main/java/net/matlux/NreplServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ public class NreplServer implements Map<String,Object>, NreplMBean
final static private Var SERVER = RT.var("net.matlux.server.nrepl", "server");

private final Map<String, Object> objMap = new HashMap<String, Object>();
private final boolean logExceptionStack;
private final boolean propagateException;
private int port;

public NreplServer(int port, boolean startOnCreation, boolean registerMBeanOnCreation, boolean propagateException) {

public NreplServer(int port, boolean startOnCreation, boolean registerMBeanOnCreation, boolean propagateException, boolean logExceptionStack) {
this.port = port;
this.propagateException = propagateException;
this.logExceptionStack = logExceptionStack;
LOGGER.info("Creating ReplStartup for Port=" + port);
try {
USE.invoke(REPL_SERVER_NS);
Expand All @@ -56,7 +59,7 @@ public NreplServer(int port, boolean startOnCreation, boolean registerMBeanOnCre
}

public NreplServer(int port) {
this(port, true,true,false);
this(port, true,true,false,true);
}

public static void main(String[] args) throws Exception {
Expand All @@ -74,7 +77,8 @@ public boolean start() {
START_REPL_SERVER.invoke(port);
LOGGER.info("Repl started successfully on Port = " + port);
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Repl startup caught an error", t);
if (logExceptionStack) LOGGER.log(Level.SEVERE, "Repl startup caught an error", t);
else LOGGER.log(Level.INFO, "Repl startup caught an error");
if (propagateException) throw new RuntimeException("Repl startup caught an error", t);
return false;
}
Expand All @@ -87,7 +91,8 @@ public boolean stop() {
STOP_REPL_SERVER.invoke();
LOGGER.info("Repl stopped successfully");
} catch (Throwable t) {
LOGGER.log(Level.SEVERE, "Repl stop caught an error", t);
if (logExceptionStack) LOGGER.log(Level.SEVERE, "Repl stop caught an error", t);
else LOGGER.log(Level.INFO, "Repl stop caught an error");
if (propagateException) throw new RuntimeException("Repl stop caught an error", t);
return false;
}
Expand All @@ -110,11 +115,11 @@ public boolean isStarted() {
}

public void registerMBean() {
MBeanRegistration.registerNreplServerAsMBean(this);
MBeanRegistration.registerNreplServerAsMBean(this, logExceptionStack);
}

public void unregisterMBean() {
MBeanRegistration.unregisterNreplServerAsMBean();
MBeanRegistration.unregisterNreplServerAsMBean(logExceptionStack);
}

public Object getObj(String key) {
Expand Down
4 changes: 2 additions & 2 deletions bootloader/src/main/java/net/matlux/NreplServerSpring.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class NreplServerSpring extends NreplServer implements ApplicationContext
@Autowired
private ApplicationContext ctx;

public NreplServerSpring(int port, boolean startOnCreation, boolean registerMBeanOnCreation, boolean propagateException) {
super(port, startOnCreation, registerMBeanOnCreation, propagateException);
public NreplServerSpring(int port, boolean startOnCreation, boolean registerMBeanOnCreation, boolean propagateException, boolean logExceptionStack) {
super(port, startOnCreation, registerMBeanOnCreation, propagateException, logExceptionStack);
}

public NreplServerSpring(int port) {
Expand Down
16 changes: 8 additions & 8 deletions bootloader/src/test/java/net/matlux/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public static void oneTimeTearDown() {

@Before
public void setUp() {
System.out.println("@Before - setUp");
System.out.println("@Before - setUp" + MBeanTest.class.getSimpleName());
}

@After
public void tearDown() {
System.out.println("@After - tearDown");
System.out.println("@After - tearDown" + MBeanTest.class.getSimpleName());
}

final static private Var USE = RT.var("clojure.core", "use");
Expand Down Expand Up @@ -92,7 +92,7 @@ private void connectionOnPortRefused(int port) {
@Test
public void testStartApp()
{
NreplServer server = new NreplServer(1112,false,false,true);
NreplServer server = new NreplServer(1112,false,false,true,false);
setupFixtureDataOnServer(server);
connectionOnPortRefused(1112);
server.start();
Expand All @@ -114,7 +114,7 @@ public void testSpringApp()
@Test
public void testAutoStartStopApp()
{
NreplServer server = new NreplServer(1112,true,false,true);
NreplServer server = new NreplServer(1112,true,false,true,false);
setupFixtureDataOnServer(server);

canSuccessfullyRunRemoteCommands(REMOTE_CODE_FIXTURE, REMOTE_CODE_RESULT_FIXTURE, 1112);
Expand All @@ -126,7 +126,7 @@ public void testAutoStartStopApp()
@Test
public void testAutoRegisterStartStopApp()
{
NreplServer server = new NreplServer(1112,false,true,true); //start server listening onto port number
NreplServer server = new NreplServer(1112,false,true,true,false); //start server listening onto port number
setupFixtureDataOnServer(server);

connectionOnPortRefused(1112);
Expand All @@ -143,7 +143,7 @@ public void testAutoRegisterStartStopApp()
@Test
public void testRegisterStartStopApp()
{
NreplServer server = new NreplServer(1112,false,false,true); //start server listening onto port number
NreplServer server = new NreplServer(1112,false,false,true,false); //start server listening onto port number
try {
server.registerMBean();
setupFixtureDataOnServer(server);
Expand All @@ -164,8 +164,8 @@ public void testRegisterStartStopApp()

public void testSamePortStartTwice()
{
NreplServer server = new NreplServer(1113,true,false,true); //start server listening onto port number
NreplServer server2 = new NreplServer(1113,true,false,true); //start server listening onto port number
NreplServer server = new NreplServer(1113,true,false,true,false); //start server listening onto port number
NreplServer server2 = new NreplServer(1113,true,false,true,false); //start server listening onto port number

server.stop();
server2.stop();
Expand Down
4 changes: 2 additions & 2 deletions bootloader/src/test/java/net/matlux/MBeanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class MBeanTest
@Before
public void setUp() {
System.out.println("@Before - setUp " + MBeanTest.class.getSimpleName());
nreplServer = new NreplServer(1111, false,false,true);
nreplServer = new NreplServer(1111, false,false,true,false);
nreplServer.registerMBean();
}

Expand All @@ -46,7 +46,7 @@ public void testRegisterUnregister() throws Exception {
@Test(expected = RuntimeException.class)
public void testRegisterTwice() {

NreplServer nreplServer2 = new NreplServer(1112, false,false,true);
NreplServer nreplServer2 = new NreplServer(1112, false,false,true,false);
nreplServer2.registerMBean();
}
@Test(expected = RuntimeException.class)
Expand Down

0 comments on commit e618e36

Please sign in to comment.