My HP7 predictions

Filed Under 42 (Life the Universe & Everything) on September 22, 2005 | 3 Comments

Well, I’ve had a blog for a few months now and have surprised myself by
not having any HP posts at all. Well, it couldn’t last forever could it!

Now, I was not one those sad people who queued up at midnight to get HP6 the
second it came out. No, I was one of those sadder people who waited
till 1:30am to nip down to the 24 hour Tesco’s to pick up a copy on the
night it came out but without the queues!

When I got the book Idid of course read it but then I also went further and re-read all the
books from book one right up to and including book six again. I just
finished this last night so I guess now would be a good time to put my
money where my mouth is and make my predictions for HP7.

  • Firstly, DUMBLEDORE IS DEAD! I actually think I should dedicate an entire ‘rant’ post to this fact because those silly people who insist on deluding themselves that he is still alive annoy me immensely!
  • Secondly, Dumbledore had a ‘big plan’ and Snape killing Dumbledore was a part of that plan.
  • Thirdly, Snape still has a big part to play in this story and he will be instrumental in the final victory of good over evil.
  • Fourthly, Draco Malfoy still has a part to play in the story and he will also help good triumph over evil.
  • Fifthly, Harry will defeat Voldemort and he will live to tell the tale. JK Rowling is not Shakespeare!
  • And finally, Hermione and Ron will stay together and Harry will get back together with Ginny.

So, now we just have to wait two years to see just how wrong I am!

Rumours have been doing the rounds that iTunes would up their prices, it was great to see Steve Jobs stand up for the consumer and say NO!

In fact he went further, he said that the record companies already make more money per tune on iTunes than they do from CD sales because they don’t have to actually manufacture anything so looking for a rise is just the record companies getting greedy. That in it self is a statement worthy of applause, but he went further and said it like it is. He said that people use iTunes because they like getting music legally for a fair price, and that increasing this price significantly would undermine this and drive people to piracy.

I can only speak for myself but I think Steve has hit the nail on the head. I don’t WANT to pirate music but I am not about to bend over and let the record company fat cats fuck me over. I love iTunes because it gives me legal music at a fair price, it is perfect and I have not pirated a single piece of music since I set up my iTunes account. If the record companies force apple to dramatically up the price then I will have zero moral objection to pirating music because the record companies will have proven themselves to the thieving bastards and hence I will have no problems fucking them over in return for them fucking me over, in fact I will take pleasure from it.

Listen up Sony and co. I don’t pirate music or software, don’t force me to by being money grabbing bastards!

I have a feeling some poor guy at Sky might be fired for this but it sure is the truest thing Sky News have said for a LONG time!

One of the worst disasters to hit the US

Following on from my previous post on shelling out in Java.

When shelling out in Java there are only two models you get by default, you can either set the process off and carry on your thread of execution in parallel, or you can halt your thread of execution until the thread you spawned finishes. The questions is, what if you want something in between, what if you want to wait for a given length of time and then simply assume that something has gone horribly wrong and kill the child process and then move on?

Well, I have what I consider to be a good solution!

The following is a Java function that allows you to specify just
about everything you could ever need when shelling out. It has made my
life a lot easier so hopefully it will be of some use to other people
too!

/** The exit code that indicuated success. */
public final static int SUCCESS = 0;
/**
* A static method to execute a given command on the shell and return the exit
* code.
*
* @param cmd              The command to execute.
* @param waitForSecs      How many seconds to wait for the spawned thread to
*      terminate. A value of zero indicated not to wait at all but to let the
*      new process carry on in the background. A negative value indicates
*      that this function should wait indefinitely for the spawned process to
*      finish and any positive value will be interpreted as the number of
*      seconds to wait for the process to terminate before killing it.
* @param workingDir       The working directory for Java to use. If a blank
*      string or null is passed no working directory is set.
* @param env              The environment variables to shell out with. If an
*      empty array or null is passed no environment variables will be sent
*      are sent.
* @return                 Whether or not the spawned process exited without
*      errors. If we are not waiting for the proccess to end it is impossible
*      to know this so false will always be returned.
* @exception Exception  If any exception is thrown in the process of
*      executing the command it is caught and encapsulated in a Exception
*      and then re-thrown.
*/

public static boolean executeCommand(String cmd, String[] env, String workingDir, int waitForSecs) throws Exception {
  try {
    String[] theEnv = env;
    if(theEnv == null) {
      theEnv = new String[0];
    }
    Process proc = null;
    if(workingDir != null && !workingDir.equals("")) {
      proc = Runtime.getRuntime().exec(cmd, theEnv, new File(workingDir));
    } else {
      proc = Runtime.getRuntime().exec(cmd, theEnv);
    }
    //if we are not waiting, return
    if(waitForSecs == 0) {
      return false;
    }

    //if we are waiting indefinitely, do so
    if(waitForSecs < 0) {
      return SUCCESS == proc.waitFor();
    }

    //if we are waiting for a set amound of seconds do so
    int waited = 0;
    do {
      Thread.sleep(1000);
      try {
        boolean ans = SUCCESS == proc.exitValue();
        return ans;
      } catch(IllegalThreadStateException e) {
        //the thread has not finished yet
        waited++;
        if(waited >= waitForSecs) {
          //we have been here too long, kill the process and return false
          proc.destroy();
          return false;
        }
      }
    }while (true);
  } catch(Exception e) {
    throw new Exception("An exception occouted while shelling out for the command " + cmd, e);
  }
}

Tagged with:

As well as containing very useful functions and objects for the creation of GUIs (useful because Swing extends AWT really) Java AWT (Abstract Windowing Toolkit) also provides a bunch of utility methods and object used for non-GUI related image manipulation via the excellent ImageIO classes that were introduced in Java 1.4. This all sounds great but the problems start when you try to use ImageIO in a scenario where the JVM has no access to an X-server or equivalent to do GUI stuff, then ImageIO breaks down because as soon as you access any AWT component java checks for a place to render to and if it finds none it gives a bizarre and really un-informative exception and dies! Now, you might ask why you would need to manipulate images if you have no GUI, well if you think about it that is the kind of thing that web servers have to do all the time, in fact the blogging software I am using to write this post needs to do that kind of thing all the time (but not in Java of course)!

However, do not despair, deep in the bowels of Java there is a solution and since even Google had some trouble pointing me to it I figured I’d share it with the world!

The key is to decapitate AWT, or, to be more formal to force it to run &quot;headless&quot;. If you are just running your application from the command line this is not hard to do at all, you just pass Java a command line argument to tell it to run AWT headless like so:

java xxxxxxx -Djava.awt.headless=true

However, the chances are you won’t be running this kind of application from the command line, in all likelihood you will be trying to do this from within a web server like Tomcat and you can’t just add that -D flag to the Tomcat startup script!

However, after some dissecting of the main tomcat startup script (catalina.sh) I found the solution for Tomcat servers, you need to set up an environment variable called JAVA_OPTS that contains the option and export it in what ever script you are using to start Tomcat. I am running Tomcat on OS X so I did this in my StartupItem for Tomcat (/Library/StartupItems/Tomcat/Tomcat) which now looks like this:

#!/bin/sh

. /etc/rc.common
export JAVA_HOME=&quot;/Library/Java/Home&quot;
export CATALINA_HOME=&quot;/usr/local/tomcat&quot;
export JAVA_OPTS=&quot;-Djava.awt.headless=true&quot;

if [ &quot;${TOMCAT}&quot; = &quot;-YES-&quot; ]; then
ConsoleMessage &quot;Starting Tomcat&quot;
su tomcat - /usr/local/tomcat/bin/startup.sh
fi

So, it really is that simple but I have to say it took me a long to to figure all this out this afternoon when for no apparent reason ImageIO was crashing with some error about a window!

Tagged with:

I was recently forced to switch from MySQL to PostgreSQL for a project I am working on and at first I was very reluctant to change because I had been using MySQL for years, was very familiar with it, and knew my way round the developers section of the MySQL website. However, having worked with PSQL I would now never go back to MySQL, it is simply in a lower league than PSQL!

When using a DB the differences are not all that big but when creating and admining DBs there are some differences that will throw you at first so I’ll list the ones I came across and how to get by them.

User Management

MySQL has a bizarre model of user management, the user bart from say localhost is considered a completely separate user to the user bart from any host or the user bart from a particular host. This makes managing users interesting and TBH I have always hated this aspect of MySQL. PSQL takes a simpler view, it uses the system accounts as its accounts. The user bart is the user bart no matter where he is logging in from and his password is his system password. Also, there is no root account as such on PSQL, if you access PSQL as the user who owns PSQL then you have ‘root’ powers, if you don’t you only have the powers that have been given to you by the DBA with GRANT statements.

Control of access to different databases by different users from different places is all controlled from a text file called pg_hba.conf in which you specify how PSQL should authenticate different users trying to access different DBs from different places. You have a number of choices ranging from ‘trust’ which lets any system user access the DB as any user without a password to ‘password’ which requires the user to supply a password and ‘ident’ which only allows the user to log into the DB as the user they are logged into the system as. pg_hba.conf is well commented with lots of examples so it is really easy to manage client authentication on PSQL and makes MySQL look over complicated and under-powered.

AUTO_INCREMENT

There is no AUTO_INCREMENT keyword in PSQL. This scared the crap out of me at first because I thought I had a real problem, but of course I didn’t, PSQL has a special data type for fields that need to auto increment called SERIAL. This creates an implicit sequence which adds a slight complication in that you need to grant users access to BOTH the table AND the sequence if you want them to be able to add rows to the table. The sequence always has a name with the format: &lt;table&gt;_&lt;field&gt;_seq.

ENUM

Again PSQL does not have an ENUM type which, at first, seems like a problem but you can create fields that are effectively enumerations using the CHECK keyword (and you can do MUCH MUCH more with it too!). To set up an enumeration called user_level with the possible values ‘STUDENT’, ‘MENTOR’ and ‘ADMIN’ you would use the following syntax:

user_level VARCHAR(10) DEFAULT ‘STUDENT’ NOT NULL CHECK (user_level IN (‘STUDENT’, ‘MENTOR’, ‘ADMIN’))

Transactions

PSQL has excellent transaction support and it is stable and works with things like JDBC which is more than can be said for MySQL ATM. And, to make it even better, the syntax is immensely simple!

BEGIN;
-- do work
COMMIT;

Shell

The psql shell to access PSQL is very very different to the mysql shell you access MySQL through. Basically you are just gonna have to RTFM it! Also, when you get stuck (and you will) just remember that \h gives you help and \q exits! The thing I missed most were MySQLs simple keywords SHOW and DESCRIBE, PSQL has the same functionality but just with much less memorable commands, e.g. \dt lists all the tables in the current DB.


Overall I find that PostgreSQL is a more powerful and more mature and robust piece of software than MySQL and since it is also open source I can see no reason to choose MySQL over PostgreSQL. Yes, if you have been using MySQL for years you will have a bit of a learning curve but trust me, it is well worth the bit of time and effort to do that learning!

Tagged with:

« go back