Adding a ShutdownHook to a Java Program to Enable Clean Exit on CTRL-C

Often times I will be building a Java program that will either be run from a command-line or as a system service.  In most cases, during development, I’ll be running it directly from the command-line and will want to kill it by pressing control c on the keyboard.

When I do that I want to make sure that my program cleanly exits, closing any open files, socket connections, database connects, or what have you.

To do so, you must invoke the addShutdownHook(Thread) method on the Runtime.getRuntime() instance for a given process.

Following is a quick how-to on setting that up.

You need the following 4 things:

  1. The class that you want to be able to shutdown cleanly, we will call this the Parent.
  2. A class that extends Thread, we will call this the ShutdownThread class.  This class will call a method in the Parent
  3. An interface that the Parent will implement, called the ShutdownInterface.
  4. The invocation of the Runtime.getRuntime().addShutdownHook(ShutdownInstance) in the Parent.

The classes:

/** ***************************************************
 *
 * The ShutdownInterface
 */


public interface IShutdownThreadParent {
    public void shutdown();
}


/** ***************************************************
 *
 * The ShutdownThread
 */


public class ShutdownThread extends Thread {

    private IShutdownThreadParent mShutdownThreadParent;

    public ShutdownThread(IShutdownThreadParent mShutdownThreadParent) {
        this.mShutdownThreadParent = mShutdownThreadParent;
    }

    @Override
    public void run() {
        this.mShutdownThreadParent.shutdown();
    }
}

 /** ***************************************************
 *
 * The Parent
 */

public class Parent implements IShutdownThreadParent{

    private  ShutdownThread fShutdownThread;

    public Parent() {
        // Instantiate a new ShutdownThread instance and invoke the addShutdownHook method.
        fShutdownThread = new ShutdownThread(this);
        Runtime.getRuntime().addShutdownHook(fShutdownThread);
    }

    @Override
    public void shutdown() {
        // code to cleanly shutdown your Parent instance.
    }

    public static void main(String[] args) {
        Parent parent = new Parent();
    }
}

When instantiated the Parent class instantiates a new ShutdownThread instance, passing it a reference to itself.  The Parent class then invokes the addShutdownHook() method passing it the reference to the ShudownThread instance.

When CTRL-C is pressed on the keyboard, the JVM will invoke the run method in the ShutdownThread class which will then invoke the shutdown() method in the parent class enabling you to cleanly exit before the JVM exits.

Replacing Smart Quotes (<91> and <92>) in vi

Smart quotes will be displayed as <91> and <92> in vi. A number of other special characters will also be displayed in similar fashion <96>, etc. . . .

To replaced them use do the following:

  1. Type:? ‘:’ 1,%s/
  2. Then, since it is a special character, you can’t simply type ‘<91>’ or that string will not be matched, so:
  1. Press: CTRL and V
  2. Then type: [space] x [space] 91
  3. [Enter]

Configuring CentOS to run SELinux in Strict Mode

I am in the process of setting up some CentOS/RHEL 6 servers to run SELinux in strict mode. What follows are notes, links to online resources and things that I am discovering along the way. Once I am finished I will go back and re-write it to follow more of a how-to/guide type format. In the meantime, it might seem a bit disjointed.

Links/Resources:

  • http://wiki.centos.org/HowTos/SELinux
  • http://fedoraproject.org/wiki/SELinux
  • http://www.centos.org/docs/5/html/Deployment_Guide-en-US/rhlcommon-chapter-0001.html
  • http://www.nsa.gov/research/selinux/index.shtml

MaintLog Notes:

  • Make sure that the selinux-policy-strict package (and deps) are installed:
  • # yum install selinux-policy-strict
  • After installing the policy I was unable to reboot as I hadn’t relabeled the file system properly. If having problems booting try:
  • # genhomedircon
  • # touch /.autorelabel
  • # reboot
  • After successfully booting with strict mode enabled you will not be able to do the things that you would normally expect as a root user.This is because your root shell does not have access to the system administrator role. To do so invoke the newrole command:
  • # newrole -r sysadmin_r
  • LEFTOFF: it seems semanage isn’t installed. I’ll need to restart with selinux disabled to install it so that I can sort out running newrole properly:? see: http://www.spinics.net/lists/selinux/msg09681.html
  • Make sure that the semanage package is installed: # yum install libsemanage

Getting JavaScript CSS Dropdown Menu to Render Over a Flash Component

If you are having trouble getting a DHTML drop-down menu to display on top of a Flash movie try adding the following to the <object> tag that embeds the .swf file in your .html.

Add the following as a child to the <object> tag:

<param name=”wmode” value=”transparent” />

Add the following as an attribute to the <object> tag:

wmode=”transparent”

I was having trouble getting a JavaScript dropdown to render over a Flash movie under IE and after adding the aforementioned tags it works just fine.

Compiling .jsp Fragments in Eclipse

The ability to generate HTML or .JSP fragments that you can include on multiple pages of your site enables you to re-use code and increase maintainability.

However, if you need to include functions (or methods) and fields (or variables) in one fragment that is included on a page and then utilize them in another fragment included in the same page Eclipse will give you compile errors because it concludes that your references are out of scope.

The solution is quite simple:? Instead of naming the fragments .jsp, use some other extension (.jnc, .jinc, etc) for the fragements that contain references to variables or functions that are included on the parent page and use the <%@ include %> directive as this includes the content of the fragment at compile time.

Accessing JSON Data Under IE7

Given the following JSON object:

var var_affectedAreas = {
? ? ‘areaCount’ : 6,
? ? ‘areasList’: [
? ? ? {‘enum’: ‘FILE’, ‘val’: ‘F’},
? ? ? {‘enum’: ‘REGISTRY’, ‘val’: ‘R’},
? ? ? {‘enum’: ‘PROCESS’, ‘val’: ‘P’},
? ? ? {‘enum’: ‘MEMORY’, ‘val’: ‘M’},
? ? ? {‘enum’: ‘NETWORK’, ‘val’: ‘N’},
? ? ? {‘enum’: ‘OTHER’, ‘val’: ‘O’}
? ? ]
};

You would think (and rightly so, at least under Firefox) that you would be able to access data in this object via the following:

areaCell = var_affectedAreas.areasList[var_areasCount].enum;
areaCell = eval(“var_affectedAreas.areasList[” + var_areasCount + “].enum”);
areaCell = eval(“var_affectedAreas.areasList[‘” + var_areasCount + “‘].enum”);

However, IE7 does not like that syntax and throws syntax errors.

The following syntax works under both IE7 and Firefox.

areaCell = var_affectedAreas[“areasList”][var_areasCount][“enum”];

Setting up Cygwin, Linux Command Line, on a Windows Box

For all of you Linux (and Unix) users who are used to the Linux command line there is an application called Cygwin. Not only does it allow you to use the familiar command line commands but it also includes the ability to execute bash and csh shell scripts on a Windows machine.

When installing it, here are a few additional packages you’ll probably want to include:

vim, ncurses, tcsh

Simply type them into the search field and then check off those packages.