[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Patch for CTRL-C handling (was: Re: Ozone should exit cleanly upon control-C)



> Not all users have a 1.3 JVM. Some time ago some users tried to come up with a
> JVM independent solution. Unfortunately they did not get to far.

If you're referring to the discussion around christmas, the whole thing died
because I was extremely lazy :(

Attached is a patch against org.ozoneDB.core.Server that causes a shutdown
hook to be registered upon start-up if it is supported by the VM. The
response to a signal is to invoke env.shutdown().

One problem: It uses System.out.println() for reporting success/failure, rather
than whichever logging mechanism is used by the rest of initialization
procedure.

The patch was generated using "cvs diff Server.java" on a freshly update
tree.

(Oh; and I didn't cross post to -dev because I understand there's some kind
of problem with cross posting on these lists?)

-- 
/ Peter Schuller, InfiDyne Technologies HB

PGP userID: 0x5584BD98 or 'Peter Schuller <peter.schuller@infidyne.com>'
Key retrival: Send an E-Mail to getpgpkey@scode.infidyne.com
E-Mail: peter.schuller@infidyne.com Web: http://scode.infidyne.com

Index: Server.java
===================================================================
RCS file: /raid/Repository/ozone/org/ozoneDB/core/Server.java,v
retrieving revision 1.42
diff -r1.42 Server.java
10a11
> import java.lang.reflect.*;
92a94,98
> 	    /* This is a good place to set up our shutdown hook. Doing it
> 	     * earlier would case a NullPointerException in race conditions.
> 	     * Doing it now is fine in terms of preventing database corruption
> 	     * because request processing has no yet begun. */
> 	   setupShutdownHook();
162c168,209
<     } 
---
>     }
>     
>     private static void setupShutdownHook() {
>         try {
>            /* We will now attempt to add the shutdown hook
> 	    * using reflection. If we're running in a VM that does
> 	    * not support shutdown hooks (i.e., pre-1.3), it will yield
> 	    * a NoSuchMethodException, which we quietly ignore. */
> 	   Class runtimeClass = Runtime.class;
>            Method hookMethod = runtimeClass.getMethod("addShutdownHook", new Class[]{Thread.class});
>            Runtime rt = Runtime.getRuntime();
> 	   hookMethod.invoke(rt, new Object[]{createShutdownHook()});
> 	   
> 	   System.out.println("Shutdown hook successfully added. Process kills are now handled.");
> 	} catch (NoSuchMethodException nsme) {
> 	   /* Pre-1.3 VM. */
> 	   System.out.println("Running on pre-1.3 VM; no shutdown hook added.");
> 	} catch (SecurityException se) {
> 	   /* Either a screwy setup, or permission deliberately denied. */
> 	   System.out.println("WARNING: Shutdown hook not added; permission denied.");
> 	} catch (InvocationTargetException ite) {
> 	   /* Runtime.addShutdownHook() failed */
> 	   Throwable te = ite.getTargetException();
> 	   System.out.println("WARNING: Shutdown hook addition failed: "
> 	      + te.getClass().getName() + ": " + te.getMessage());
> 	} catch (IllegalAccessException iae) {
> 	   /* The VM is FUBAR. */
> 	   System.out.println("WARNING: Runtime.addShutdownHook() not public? VM bug?");
> 	}
>     }
>    
>    /**
>     * Constructs and returns a Thread to be used as a shutdown hook. It
>     * invokes Env.shutdown().
>     */
>    private static Runnable createShutdownHook() {
>       return new Thread() {
> 	    public void run() {
> 	       env.shutdown();
> 	    }
>          };
>    }