Installing PHP Couchbase extensions and libcouchbase on Mac

Following the instructions from http://www.couchbase.com/develop/php/current and http://www.couchbase.com/develop/php/next didn’t work for me. When using the next version, with libcouchbase installed the recommend way (the way I show you not to do it below), I got this error when using their precompiled binary:

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20100525/couchbase.so' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20100525/couchbase.so, 9): Library not loaded: /usr/local/lib/libcouchbase.2.dylib
  Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20100525/couchbase.so
  Reason: image not found in Unknown on line 0

I tried using the latest version of libcouchbase (also shown below); there were no more startup warnings, but the extension was just not showing up in the modules list. Next, I switched to using the precompiled binary from the current version. Again, no PHP startup warnings, but the extension was just not showing up. So, I poked around and found this combination that works on both OS X 10.7 and OS X 10.8.

We simply need to install the latest version of libcouchbase and then compile the Coucbase PHP extension ourselves.

First off, do not do:

brew install libcouchbase

This will installs the wrong version and you’ll see an error like this when you get to the ./configure step.

configure: error:
It appears you are using libcouchbase 1.x
This DP version of the php extension supports libcouchbase-2.0.0beta
or higher. Either use a stable 1.0 version of the php extension,
upgrade your libcouchbase, or specify the location of a
libcouchbase-2.0.0 installation with --with-couchbase

Instead, install the latest version of libcouchbase, and get the couchbase php extension source code and compile it:

brew install https://github.com/couchbase/homebrew/raw/preview/Library/Formula/libcouchbase.rb
git clone https://github.com/couchbase/php-ext-couchbase.git
cd php-ext-couchbase/
phpize
./configure
make
make test
sudo make install

Then, register the couchbase extension by adding the following to your php.in. Mine is /etc/php.ini.

extension=couchbase.so

Now, you can confirm that PHP is loading this new extension by doing:

php -m

You should see couchbase in the list of modules. If you see it there, everything has gone according to plan.  If it doesn’t show up and you see no startup warnings, you can try specifying the extensions path explicitly.

extension=/path/to/couchbase.so

Lastly, restart Apache

sudo apachectl restart

and double check that the couchbase module is being shown when you do a `phpinfo();`

 

Inserting DATETIME’s with Spring’s JdbcTemplate and NamedParameterJdbcTemplate

Ran into another bit of trouble today that cost me another sizable chunk of time. When using Spring’s NamedParameterJdbcTemplate to insert into a DATETIME column, the following doesn’t work:

public class ExampleDao {
    protected NamedParameterJdbcTemplate jdbcTemplate;

    public void insert(ExampleModel model) {
        jdbcTemplate.update(
            "INSERT INTO `sometable` (`some_datetime_column`) VALUES(:datetime)",
            new MapSqlParameterSource().addValue("datetime", new Date(), Types.DATE)
        );
    }
}

Your date will be happily insert, but you’ll be sad to know that the time component has been truncated to something like “2012-08-28 00:00:00.” Using Types.TIMESTAMP has the same effect.

Using Types.TIMESTAMP is actually correct, but the secret is that you have to wrap your java.util.Date in a java.sql.Date.

public class ExampleDao {
    protected NamedParameterJdbcTemplate jdbcTemplate;

    public void insert(ExampleModel model) {
        jdbcTemplate.update(
            "INSERT INTO `sometable` (`some_datetime_column`) VALUES(:datetime)",
            new MapSqlParameterSource().addValue(
                "datetime", 
                new java.sql.Date(new Date().getTime()), 
                Types.TIMESTAMP
            )
        );
    }
}

If you’re reaction is one of disappointment, then you’re feeling the same way I did.

For simple JdbcTemplates, there is sufficient behind the scenes magic that you don’t run into this kind of problem. The following works without any translations or other such boilerplate (making the above that much more … dissatisfying).

public class ExampleDao {
    protected JdbcTemplate jdbcTemplate;

    public void insert(ExampleModel model) {
        jdbcTemplate.update(
            "INSERT INTO `sometable` (`some_datetime_column`) VALUES(?)",
            new Object[] { new Date() }
        );
    }
}

I leave you with a list of SQL types and their Java equivalents, which is how I finally solved my problem with NamedParameterJdbcTemplate.

 

Installing PHP 5.4 with MacPorts

I had a little bit of a run around trying to figure this out. So here’s the full process, all in one place for your convenience. This assumes that you have MacPorts installed, but if you don’t, there are several ways of doing so.

Before we get started, be sure to disable OS X’s default Apache install if you haven’t done so already.  This can be done by opening System Preferences, going to Sharing and disabling Web Sharing.

If you don’t care about the explanation, skip to the bottom.

Let’s get started. Open Terminal, and paste the following:

sudo port install apache2 php54 php54-apache2handler
sudo port load apache

This will install Apache2 and PHP 5.4, and setup Apache to start on boot.

Next, configure Apache to use PHP 5.4. Create the file /opt/local/apache2/conf/extra/php.conf,

 sudo vi /opt/local/apache2/conf/extra/php.conf

And paste the following into it:

LoadModule php5_module modules/mod_php54.so

<IfModule dir_module>
    DirectoryIndex index.php index.phtml index.html
</IfModule>

AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml

This will load the PHP 5.4 module, make index.php the default directory index file and cause files with .php and .phtml extensions to be interrupted by said PHP module.

Next, tell Apache to include your new PHP config file. Open the httpd.conf,

sudo vi /opt/local/apache2/conf/httpd.conf

And add this line to the bottom:

Include conf/extra/php.conf

Lastly, restart Apache.

sudo /opt/local/apache2/bin/apachectl -k restart

You should now have a functional PHP 5.4 environment. Also, don’t forget that you’ll need to reinstall all of your favorite PHP modules. Here’s what I normally roll with. Don’t forget to restart Apache again.

sudo port install memcached php54-memcache php54-memcached php54-apc php54-mysql php54-xdebug php54-mcrypt
sudo port load memcached

TL;DR: If you have su access, you can just copy and paste the whole thing all at once:

port install apache2 php54 php54-apache2handler
port install memcached php54-memcache php54-memcached php54-apc php54-mysql php54-xdebug php54-mcrypt
echo "Include conf/extra/php.conf" >> /opt/local/apache2/conf/httpd.conf
echo "
LoadModule php5_module modules/mod_php54.so

<IfModule dir_module>
    DirectoryIndex index.php index.phtml index.html
</IfModule>

AddType application/x-httpd-php .php
AddType application/x-httpd-php .phtml" > /opt/local/apache2/conf/extra/php.conf
port load apache
port load memcached
sudo /opt/local/apache2/bin/apachectl -k restart
 

Configuring Remote Debugging in Jboss AS 7 and Eclipse

Configure JBoss

First, to enable JBoss to allow remote debugging, edit your <jboss install location>/bin/standalone.conf.    Now, look for # JAVA_OPTS="$JAVA_OPTS -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n" and uncomment it.  It should be near the bottom.

If you are on Windows, you’ll need to edit standalone.conf.bat instead and uncomment rem set "JAVA_OPTS=%JAVA_OPTS% -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

Start Jboss. You should now see something like this at the top of the output

=========================================================================

JBoss Bootstrap Environment

JBOSS_HOME: /opt/jboss-as-7.1.1.Final

JAVA: java

JAVA_OPTS: -d32 -client -Xms64m -Xmx512m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true 
-Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 
-Dsun.rmi.dgc.server.gcInterval=3600000 -Djboss.modules.system.pkgs=org.jboss.byteman 
-Djava.awt.headless=true -Djboss.server.default.config=standalone.xml 
-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n

Configure Eclipse

Configuring Eclipse is easy. First start by creating a new debug configuration by going to Run > Debug Configurations…  In the left side panel, select Remote Java Application, then in the toolbar up top, click on the new icon.  This will give you a new debug configuration.  On the right side, name it however you like.  Then, change the port to 8787 and the host to where ever your JBoss installation is running.  I’m running mine on the same machine that I am debugging on, so I leave it as localhost.  Hit the Debug button.

Lastly, you’ll need to switch to the debug view to see all of your tools. To do so, go to Window > Show View > Other…, type in  “debug” in the filter box and select the debug view.  You should now see a JVM with a list of a bunch threads.  If you don’t see any threads, make sure that JBoss is running and that you followed the steps above correctly.

If all went well, you can now set break points in your code and they should show up automatically in the debugger.

 

Change the Default Domain of a Shared IP in Plesk

Plesk is a great tool (especially compared to other domain management software), but some things aren’t as straight forward to change.  For example, if you setup a handful of domains on a shared IP, the first domain created on that IP will be it’s default.  So if you go directly to the IP in your browser (not the domain), the first domain that you setup is the one that you will see.  Fortunately, changing this is simple. … 

 

Generic Singleton Factory in JavaScript

Author’s note: I originally posted this article while I was working at Zebrakick and have reposted it here for consolidation.

I recently contributed this little tidbit to stackoverflow’s Hidden Features of JavaScript and thought it would be best to elaborate it on it fully. In a recent client project, I needed a generic method to produce singleton instances of interface widgets.  This is the method that I came up with and it works pretty well. Before we begin, these are not singleton classes in the normal sense, but a generic singleton factory. … 

 

Object Literals vs. Constructed Objects in JavaScript

Author’s note: I originally posted this article while I was working at Zebrakick and have reposted it here for consolidation.

Another discussion born from a question at stackoverflow.  Today I’m laying out the differences between object literals and constructed objects, when to use which, how to get the most out of them, and a few other tricks along the way.  Let’s get started. … 

 

Magento Onepage Checkout Template Bug

Author’s note: I originally posted this article while I was working at Zebrakick and have reposted it here for consolidation.

Recently, we rolled out an installation of Magento 1.3.2.3 for one of our clients but ran into small a problem along the way. Being an established open source project, I initially assumed that the error was my own when I found that the onepage checkout process would stop working half way through (step 3 to be exact).  However, after digging around, I found that the problem was actually a bug in Magento’s JavaScript.  The bug has been reported and will hopefully be fixed soon, but in the meantime, here’s the fix and how I found it. … 

 

Semicolon insertion finally caught me

While debugging a caching class today, I realized that I had finally been caught by semicolon insertion.

Here’s the original code cut from a larger object literal

isValid: function(hash) {
    return
        // Is cached AND
        typeof this.entries[hash] != "undefined" &&
        // Cache doesn't expire (e.g.: -1) OR hasn't expired yet
        (this.entries[hash].expires < 0 || this.entries[hash].expires > new Date().getTime());
}

After about 10 minutes and a gratuitous amount of debug output, I realized that this function was returning `undefined`.  “Nonsense,” I said to myself, “it’s returning a boole…wha…wait. Dammit.”

In my effort to document and format at the same time, I dropped the expression to the next line after the `return`.  Being that `return` by itself on a line is a complete and valid expression, JavaScript’s automatic semicolon did its thing and ended the statement there and my conditional expression was never evaluated.

Wrapping the whole conditional expression in parenthesis fixed the problem by invalidating the `return` line as a whole statement.

isValid: function(hash) {
    return (
        // Is cached AND
        typeof this.entries[hash] !== "undefined" &&
        // Cache doesn't expire (e.g.: -1) OR hasn't expired yet
        (this.entries[hash].expires < 0 || this.entries[hash].expires > new Date().getTime())
    );
}

Let that be a lesson to us all. I suppose it was bound to happen at some point.