Welcome to my website. I am always posting links to photo albums, art, technology and other creations. Everything that you will see on my numerous personal sites is powered by the formVistaTM Website Management Engine.

icon.linkedin.jpgicon.twitter.jpg

  • Subscribe to this RSS Feed
  • Adding a ShutdownHook to a Java Program to Enable Clean Exit on CTRL-C
    09/02/2011 10:06AM

    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
    09/01/2011 10:25AM

    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
    08/29/2011 5:05PM

    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


  • Fixing Metadata file does not match checksum
    08/29/2011 4:38PM

    Sent to me by my friend, Wendy:

    When attempting to run a yum (search/install) if you get a the "Metadata file does not match checksum" error, try the following:

    # yum clean all

    # yum makecache

    # yum update

  • Random String Generator
    08/04/2011 4:21PM

    Very handy random string generator:

    http://www.random.org/strings/

Advanced Search

Categories

Archives