Skip to content

Get the PID of a running Java process using only BASH tools

22-Jul-10

I need to get the PID of a running Java process, in this case Glassfish 3 to see if it is running before I run some asadmin commands.

ps -ax | grep "java -cp /opt/bmam/glassfish" | grep -v "grep" | cut -d " " -f 1
26306

How to force changes to /etc/hosts to take effect in OSX

15-Jul-10

If you make changes to your /etc/hosts file and want the changes to take effect immediately without having to reboot issue the following command at in the Terminal

dscacheutil -flushcache

Migrating Java Projects to Maven 2

13-Jul-10

Looks like Maven 2 is pretty much inline with the generic Ant based build system I have been using for the past 8 years. So I am going to take the plunge and start using Maven 2 so I don't have to keep maintaining my system. Less time maintaining a build system means more time writing code that actually does something unique.

SpiderMonkey in Xcode 3.2.3

28-Jun-10

I am working on a Desktop Application project that will need scripting support, specifically scripting support for people familiar with web development. Even though I have prior experience successfully embedding both Lua and Python in server side applications, I have picked Javascript for this project. Mainly for the low barrier to entry for web developers, and the massive amount of documentation for the language. I am specifically testing out SpiderMonkey right now. I struggled trying to get SpiderMonkey to compile in Xcode 3.2.3 and am documenting how I got it to work. The JSAPI User Guide had some incorrect information, so I updated it with the same information here.

First of I assume you know how to include the headers and the libjs.a library in Xcode 3.2.3. The problem came when I tried to compile the simplest of applications. The fix was you have to include a #define at before the #include "jsapi.h".
Like so:

#define XP_UNIX
#include "jsapi.h"

Also the line:

global = JS_NewGlobalObject(cx, &global_class, NULL, NULL);

was incorrect as well, it should have been:

global = JS_NewObject(cx, &global_class, NULL, NULL);

Here is the corrected code that will compile and run on Xcode 3.2.3

#define XP_UNIX
#include "jsapi.h"

/* The class of the global object. */
static JSClass global_class = {
    "global", JSCLASS_GLOBAL_FLAGS,
    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
    JSCLASS_NO_OPTIONAL_MEMBERS
};

/* The error reporter callback. */
void reportError(JSContext *cx, const char *message, JSErrorReport *report)
{
    fprintf(stderr, "%s:%u:%s\n",
            report->filename ? report->filename : "<no filename>",
            (unsigned int) report->lineno,
            message);
}

int main(int argc, const char *argv[])
{
    /* JS variables. */
    JSRuntime *rt;
    JSContext *cx;
    JSObject  *global;
    
    /* Create a JS runtime. */
    rt = JS_NewRuntime(8L * 1024L * 1024L);
    if (rt == NULL)
        return 1;
    
    /* Create a context. */
    cx = JS_NewContext(rt, 8192);
    if (cx == NULL)
        return 1;
    JS_SetOptions(cx, JSOPTION_VAROBJFIX);
    //JS_SetVersion(cx, JSVERSION_LATEST);
    JS_SetErrorReporter(cx, reportError);
    
    /* Create the global object. */
    global = JS_NewObject(cx, &global_class, NULL, NULL);
    if (global == NULL)
        return 1;
    
    /* Populate the global object with the standard globals,
     like Object and Array. */

    if (!JS_InitStandardClasses(cx, global))
        return 1;
    
    //TODO app goes here
    
    /* Cleanup. */
    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);
    JS_ShutDown();
    return 0;
}

How to see what your site looks like in many browsers on many operating systems

04-Jun-10

I just found this site that lets you see what your pages look like in a bunch of different browsers on a bunch of different operating systems.

MacFuse on 64bit Snow Leopard 10.6.3

28-May-10

I just spent a good bit of time searching Google on how to get MacFuse working my 64bit Snow Leopard (10.6.3) machine.
Here is what I found.

MacFuse for Snow Leopard ( 10.6.3 )

You don't really need MacFusion 2 but it puts a nice interface on MacFuse. It included the SSHFS and FTPFS files systems by default.
MacFusion 2 for Snow Leopard ( 10.6.3 )

The plan is to work on a CouchDBFS implementation.

How to determine how many threads you can create from Java

19-May-10

I was getting Out of Memory Error: can't create native thread errors on a server, and it was definately not running out of memory, so I hacked this little program together to tell me how many threads I should be able to create.

64K is the minimum stack size that Java will allow on an Intel platform. OSX 10.6.3 Snow Leopard has a hard coded limit of 2560 threads. Leopard Server doesn't seem to have this limit.
TestThreadStackSize.java

public class TestThreadStackSizes
{
    public static void main(final String[] args)
    {
        Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(final Thread t, final Throwable e)
            {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        });
        int numThreads = 1000;
        if (args.length == 1)
        {
            numThreads = Integer.parseInt(args[0]);
        }

        for (int i = 0; i < numThreads; i++)
        {
            try
            {
                Thread t = new Thread(new SleeperThread());
                t.start();
            }
            catch (final OutOfMemoryError e)
            {
                throw new RuntimeException(String.format("Out of Memory Error on Thread %d", i), e);
            }
        }
    }

    private static class SleeperThread implements Runnable
    {
        public void run()
        {
            try
            {
                Thread.sleep(1000 * 60 * 60);
            }
            catch (final InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        }
    }
}

Updating all Code blocks to prefer Inconsolata where supported

17-May-10

I am looking at you IE with your non-standard EOT font format. I added an @font-face declaration for my favorite monospace programming font. All Firefox 3.5+, Safari 3.1+, Opera 10+ the latest version of Chrome should now see all the formatted code blocks in the font Inconsolata instead of the default monospace which is what IE will fall back to since I am not going to go to the trouble of support the EOT font format. Sorry IE users complain to Microsoft, they don't even support the format they bought from Apple in their war against Adobe and PostScript fonts!.

Get All Views from All Databases in CouchDB

11-May-10

Here is a little code snippet on how to get a list of all the Views from all the Design Documents from all the Databases in a CouchDB instance with couchdbkit.

#!/usr/bin/env python

from couchdbkit import *

server = Server()
dbs = server.all_dbs()
for dbname in dbs:
    db = server.get_or_create_db(dbname)
    result = db.all_docs(startkey='_design', endkey='_design0')
    for doc in result.all():
       designdoc = db.get(doc['id'])
       if 'views' in designdoc:
           for view in designdoc['views']:
              print '%s/%s/_view/%s' % (dbname, designdoc['_id'], view)

Getting CouchDB to install from source on OSX 10.6.3 Snow Leopard in 64 bit

06-May-10

The problem with getting CouchDB to install and run correctly in 64 bit mode of OSX 10.6.x is that the ICU dependency has a broken configure script that just won't compile in 64 bit mode, even with the correct arguments passed in. The solution is to patch up the configure script and force it to build in 64 bit.
Find the following in the configure script

# Check for potential -arch flags.  It is not universal unless
# there are some -arch flags.  Note that *ppc* also matches
# ppc64.  This check is also rather less than ideal.
case "${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS}" in  #(
  *-arch*ppc*|*-arch*i386*|*-arch*x86_64*) ac_cv_c_bigendian=universal;;
esac

and change it to look like this

# Check for potential -arch flags.  It is not universal unless
# there are some -arch flags.  Note that *ppc* also matches
# ppc64.  This check is also rather less than ideal.
case "${CC} ${CFLAGS} ${CPPFLAGS} ${LDFLAGS}" in  #(
  *-arch*ppc*) ac_cv_c_bigendian=yes;;
esac

now make sure to compile Erlang in 64 bit mode and CouchDB will link to the ICU compiled with 64 bit mode and work.
Here is an already patched ICU 4.4.1 configure script you can just download and use.