001 package com.github.sarxos.webcam; 002 003 import org.slf4j.Logger; 004 import 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 */ 013 public 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 } 039 040 @Override 041 public void run() { 042 LOG.info("Automatic {} deallocation", webcam.getName()); 043 webcam.dispose(); 044 } 045 }