/*
 * Copyright (c) 2016, Ronald DeSantis
 *
 *	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.
 */

package com.hauldata.dbpa;

import java.util.Arrays;

import com.hauldata.dbpa.process.Context;
import com.hauldata.dbpa.process.ContextProperties;
import com.hauldata.dbpa.process.DbProcess;

/**
 * RunDbp - Run Database Process.
 * Command line utility to invoke DbProcess
 */
public class RunDbp {

	static final String programName = "RunDbp";
	
	public static void main(String[] args) {

		ContextProperties contextProps = null;
		Context context = null;

		int status = 0;
		try {
			String processID = args[0];
			String[] processArgs = Arrays.copyOfRange(args, 1, args.length);

			contextProps = new ContextProperties(programName);

			context = contextProps.createContext(processID);

			DbProcess process = context.loader.load(processID);
			
			process.run(processArgs, context);
		}
		catch (Exception ex) {
			System.err.println(ex.getMessage());
			status = 1;
		}
		finally {
			try { if (context != null) context.close(); } catch (Exception ex) {}
			
			System.exit(status);
		}
	}
}

@@ -1344,19 +1344,39 @@
 essID);%0A
+%0A
 %09%09%09
+hookProgramExit();%0A
 %0A%09%09%09proc
@@ -1579,19 +1579,686 @@
  ex) %7B%7D%0A
+%0A
 %09%09%09
+exitProgram(status);%0A%09%09%7D%0A%09%7D%0A%0A%09private static Thread processThread;%0A%09private static boolean isProcessInterrupted;%0A%09private static Thread hookThread;%0A%0A%09private static void hookProgramExit() %7B%0A%09%09processThread = Thread.currentThread();%0A%09%09isProcessInterrupted = false;%0A%0A%09%09hookThread = new Thread() %7B%0A%09%09%09public void run() %7B%0A%09%09%09%09isProcessInterrupted = true;%0A%09%09%09%09processThread.interrupt();%0A%09%09%09%09try %7B%0A%09%09%09%09%09processThread.join();%0A%09%09%09%09%7D%0A%09%09%09%09catch (InterruptedException ex) %7B%7D%0A%09%09%09%7D%0A%09%09%7D;%0A%0A%09%09Runtime.getRuntime().addShutdownHook(hookThread);%0A%09%7D%0A%09%0A%09private static void exitProgram(int status) %7B%0A%09%09if (!isProcessInterrupted) %7B%0A%09%09%09Runtime.getRuntime().removeShutdownHook(hookThread);%0A
 %0A%09%09%09Syst

Handle Ctrl+C cleanly in RunDbp