Skip to content

Commit

Permalink
Add missing uncaught exception handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Mar 21, 2013
1 parent 06a51f2 commit 4bd6b8e
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ Example provided by [hepin1989](https://github.com/hepin1989). Thank you!

### How To Use

TODO
1. Start

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.lang.Thread.UncaughtExceptionHandler;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
Expand All @@ -18,7 +19,7 @@
*
* @author Bartosz Firyn (SarXos)
*/
public class WebcamViewerExample extends JFrame implements Runnable, WebcamListener, WindowListener {
public class WebcamViewerExample extends JFrame implements Runnable, WebcamListener, WindowListener, UncaughtExceptionHandler {

private static final long serialVersionUID = -2831291292491395695L;

Expand Down Expand Up @@ -56,6 +57,7 @@ public void run() {
}
};
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.start();
}

Expand Down Expand Up @@ -115,4 +117,10 @@ public void windowIconified(WindowEvent e) {
System.out.println("webcam viewer paused");
viewer.pause();
}

@Override
public void uncaughtException(Thread t, Throwable e) {
System.err.println(String.format("Exception in thread %s", t.getName()));
e.printStackTrace();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.sarxos.webcam;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
Expand All @@ -19,11 +18,11 @@
import org.slf4j.LoggerFactory;


public class WebcamDiscoveryService implements Runnable, UncaughtExceptionHandler {
public class WebcamDiscoveryService implements Runnable {

private static final Logger LOG = LoggerFactory.getLogger(WebcamDiscoveryService.class);

private static final class WebcamsDiscovery implements Callable<List<Webcam>>, ThreadFactory, UncaughtExceptionHandler {
private static final class WebcamsDiscovery implements Callable<List<Webcam>>, ThreadFactory {

private final WebcamDriver driver;

Expand All @@ -40,14 +39,9 @@ public List<Webcam> call() throws Exception {
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "webcam-discovery-service");
t.setDaemon(true);
t.setUncaughtExceptionHandler(this);
t.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
return t;
}

@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
}
}

private final WebcamDriver driver;
Expand Down Expand Up @@ -331,8 +325,8 @@ public synchronized void start() {
// start discovery service runner

runner = new Thread(this, "webcam-discovery-service");
runner.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
runner.setDaemon(true);
runner.setUncaughtExceptionHandler(this);
runner.start();
}

Expand Down Expand Up @@ -366,9 +360,4 @@ protected synchronized void shutdown() {
WebcamDeallocator.unstore();
}
}

@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.github.sarxos.webcam;

import java.lang.Thread.UncaughtExceptionHandler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class WebcamExceptionHandler implements UncaughtExceptionHandler {

private static final Logger LOG = LoggerFactory.getLogger(WebcamExceptionHandler.class);

private static final WebcamExceptionHandler INSTANCE = new WebcamExceptionHandler();

private WebcamExceptionHandler() {
// singleton
}

@Override
public void uncaughtException(Thread t, Throwable e) {
LOG.error(String.format("Exception in thread %s", t.getName()), e);
System.err.println(String.format("Exception in thread %s", t.getName()));
e.printStackTrace();
}

protected static final WebcamExceptionHandler getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private static final class DetectorThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable runnable) {
Thread t = new Thread(runnable, "motion-detector-" + (++number));
t.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
t.setDaemon(true);
return t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import javax.swing.JPanel;

Expand Down Expand Up @@ -170,6 +172,20 @@ public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
}
}

private static final class PanelThreadFactory implements ThreadFactory {

private static final AtomicInteger number = new AtomicInteger(0);

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, String.format("webcam-panel-scheduled-executor-%d", number.incrementAndGet()));
t.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
t.setDaemon(true);
return t;
}

}

/**
* S/N used by Java to serialize beans.
*/
Expand All @@ -190,10 +206,12 @@ public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
*/
private static final double MAX_FREQUENCY = 50; // 50 frames per second

private static final PanelThreadFactory THREAD_FACTORY = new PanelThreadFactory();

/**
* Scheduled executor acting as timer.
*/
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
private ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, THREAD_FACTORY);

/**
* Repainter updates panel when it is being started.
Expand All @@ -203,8 +221,9 @@ public void paintImage(WebcamPanel owner, BufferedImage image, Graphics2D g2) {
private class Repainter extends Thread {

public Repainter() {
setDaemon(true);
setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
setName(String.format("repainter-%s", webcam.getName()));
setDaemon(true);
}

@Override
Expand Down Expand Up @@ -344,6 +363,9 @@ public void run() {
*/
private Painter painter = new DefaultPainter();

/**
* Preferred panel size.
*/
private Dimension size = null;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ private static final class ProcessorThreadFactory implements ThreadFactory {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "atomic-processor");
t.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
t.setDaemon(true);
return t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,46 @@
import java.awt.Dimension;


/**
* Various resolutions.
*
* @author Bartosz Firyn (sarxos)
*/
public enum WebcamResolution {

/**
* 176x144
* Size 176x144
*/
QQVGA(176, 144),

/**
* 320x240
* Size 320x240
*/
QVGA(320, 240),

/**
* 352x288
* Size 352x288
*/
CIF(352, 288),

/**
* Size 480x400
*/
HVGA(480, 400),

/**
* Size 640x480
*/
VGA(640, 480),

/**
* Size 768x576
*/
PAL(768, 576),

/**
* Size 800x600
*/
SVGA(800, 600),

/**
Expand All @@ -31,20 +51,49 @@ public enum WebcamResolution {
XGA(1024, 768),

/**
* 1280x720
* Size 1280x720 also known as HD 720p.
*/
HD720(1280, 720),

/**
* Size 1280x768
*/
WXGA(1280, 768),

/**
* Size 1280x1024
*/
SXGA(1280, 1024),

/**
* Size 1600x1200
*/
UXGA(1600, 1200),

/**
* Size 2048x1536
*/
QXGA(2048, 1536);

/**
* Resolution size.
*/
private Dimension size = null;

/**
*
* @param width the resolution width
* @param height the resolution height
*/
private WebcamResolution(int width, int height) {
this.size = new Dimension(width, height);
}

/**
* Get resolution size.
*
* @return Dimension object
*/
public Dimension getSize() {
return size;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public final class WebcamShutdownHook extends Thread {
protected WebcamShutdownHook(Webcam webcam) {
super("shutdown-hook-" + (++number));
this.webcam = webcam;
this.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public void start() {
@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, String.format("streamer-thread-%s", number++));
thread.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance());
thread.setDaemon(true);
return thread;
}
Expand Down
Loading

0 comments on commit 4bd6b8e

Please sign in to comment.