001package com.github.sarxos.webcam; 002 003import org.slf4j.Logger; 004import org.slf4j.LoggerFactory; 005 006 007/** 008 * Shutdown hook to be executed when JVM exits gracefully. This class intention 009 * is to be used internally only. 010 * 011 * @author Bartosz Firyn (sarxos) 012 */ 013public final class WebcamShutdownHook extends Thread { 014 015 /** 016 * Logger. 017 */ 018 private static final Logger LOG = LoggerFactory.getLogger(WebcamShutdownHook.class); 019 020 /** 021 * Number of shutdown hook instance. 022 */ 023 private static int number = 0; 024 025 /** 026 * Webcam instance to be disposed / closed. 027 */ 028 private Webcam webcam = null; 029 030 /** 031 * Create new shutdown hook instance. 032 * 033 * @param webcam the webcam for which hook is intended 034 */ 035 protected WebcamShutdownHook(Webcam webcam) { 036 super("shutdown-hook-" + (++number)); 037 this.webcam = webcam; 038 this.setUncaughtExceptionHandler(WebcamExceptionHandler.getInstance()); 039 } 040 041 @Override 042 public void run() { 043 LOG.info("Automatic {} deallocation", webcam.getName()); 044 webcam.dispose(); 045 } 046}