/**
 * GraphShutdownHandler.java
 * 
 * Provides a handler that is invoked when the program terminates abnormally.
 * This should avoid various problems that you'ld other encounter with neo4j
 * when leaving a database open.
 * 
 * Copyright (c) 2011 IBM Corporation
 *
 * This library was originally developed for a joint research
 * project with the University of Nebraska, Lincoln under terms
 * of the Joint Study Agreement between IBM and UNL.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author Patrick Wagstrom <patrick@wagstrom.net>
 */

package com.ibm.research.govsci.graph;

import java.util.ArrayList;

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

/**
 * Simple handler that calls a set of routines when it is time to shutdown
 * the program. This is commonly used to ensure that graphs are left in a
 * consistent state.
 * 
 * @author Patrick Wagstrom <pwagstro@us.ibm.com>
 */
public class GraphShutdownHandler extends Thread {
	private Logger log = null;
	protected ArrayList<Shutdownable> elems = null;
	
	public GraphShutdownHandler() {
		super();
		log = LoggerFactory.getLogger(GraphShutdownHandler.class);
		elems = new ArrayList<Shutdownable>();
	}
	
	public void addShutdownHandler(Shutdownable sd) {
		log.trace("Addding shutdown handler: {}", sd);
		elems.add(sd);
	}
	
	@Override()
	public void run() {
		log.debug("Invoking Shutdown Handlers");
		for (Shutdownable sd : elems) {
			log.trace("Invoking shutdown handler: {}", sd);
			sd.shutdown();
		}
	}
	
}

@@ -1469,17 +1469,20 @@
 hread %7B%0A
-%09
+    
 private 
@@ -1500,17 +1500,20 @@
 = null;%0A
-%09
+    
 protecte
@@ -1552,19 +1552,21 @@
 = null;%0A
-%09%0A%09
+%0A    
 public G
@@ -1589,18 +1589,24 @@
 ler() %7B%0A
-%09%09
+        
 super();
@@ -1606,18 +1606,24 @@
 uper();%0A
-%09%09
+        
 log = Lo
@@ -1673,18 +1673,24 @@
 class);%0A
-%09%09
+        
 elems = 
@@ -1720,22 +1720,27 @@
 ble%3E();%0A
-%09%7D%0A%09%0A%09
+    %7D%0A%0A    
 public v
@@ -1777,26 +1777,32 @@
 nable sd) %7B%0A
-%09%09
+        
 log.trace(%22A
@@ -1840,31 +1840,155 @@
 d);%0A
-%09%09elems.add(sd);%0A%09%7D%0A%09%0A%09
+        if (sd == null) %7B%0A            log.warn(%22addShutdownHandler passed null%22);%0A        %7D else %7B%0A            elems.add(sd);%0A        %7D%0A    %7D%0A%0A    
 @Ove
@@ -1995,17 +1995,20 @@
 rride()%0A
-%09
+    
 public v
@@ -2019,18 +2019,24 @@
 run() %7B%0A
-%09%09
+        
 log.debu
@@ -2068,18 +2068,24 @@
 lers%22);%0A
-%09%09
+        
 for (Shu
@@ -2108,19 +2108,62 @@
 lems) %7B%0A
-%09%09%09
+            if (sd != null) %7B%0A                
 log.trac
@@ -2206,33 +2206,215 @@
 d);%0A
-%09%09%09sd.shutdown();%0A%09%09%7D%0A%09
+                sd.shutdown();%0A            %7D else %7B%0A                log.warn(%22Shutdown Handler trying to shut down null element%22);%0A            %7D%0A        %7D%0A        elems = new ArrayList%3CShutdownable%3E();%0A    
 %7D%0A
-%09
 %0A%7D%0A

Check for null when running GraphShutdownHandler