Grails Gotcha – Domain Field Capitalization

You should use standard camelCase names for your fields and not ProperCase.

The following gotcha only reveals itself if you do not use standard camelCase naming conventions for fields. The reason you may want to violate camelCase naming conventions is when you have a service callback where you just want to capture the params that they send to you.

Bad:

class TwilioVoice {
    String AccountSid
    static constraints = {
        AccountSid(nullable: false)
    }
}

Good:

class TwilioVoice {
    String accountSid
    static constraints = {
        accountSid(nullable: false)
    }
}

If your class has a field that starts with a capital letter and you need to use the constraints feature, you will get an error similar to this:

Message: Can not set java.lang.String field TwilioVoice.MyField to java.lang.Class

Grails IDE

Searching for a nice IDE for Grails.  Code completion is the primary criteria I used while evaluating these IDEs.  I limited my evaluation to the following tools: Eclipse, Netbeans, Spring Source’s Groovy/Grails Tool Suite, and IntelliJ IDEA.

Using the following class:

class Customer {
    String firstName
    String lastName
    static hasMany = [phoneNumbers:String]
}

I should have the following code completion helps to me. One, it should give me help in the constructor of my field choices. It should also give me help for the dynamic methods that Grails introduces with hasMany.

Customer c = new Customer(firstName: 'Jason', lastName: 'Rowland')
c.addToPhoneNumbers('3035551212')

Eclipse

I am a fan of Eclipse and it is always my IDE of choice because of it’s many plugins.  In this case though, I couldn’t get the code completion to work with the dynamic properties and methods that Grails creates.  Also, with the Juno version, it kept crashing and not allowing me to save my code.  I have to force quite the application.  So because Eclipse is not really usable, I had to look for others.

Netbeans

Not much to say here except that code completion doesn’t work for dynamic properties or methods.

Grails/Groovy Tool Suite

This IDE from Spring Source get’s closer.  It gives you code completion tips for the constructors of an object.  It fails to add the dynamic method’s addToX for the hasMany functionality.

IntelliJ IDEA

Nice IDE to use for Grails/groovy development.  Dynamic properties and methods are all available for code completion.  It also has other nice features that I’d like to explore more. The biggest is the ability to diagram your domain objects.  I haven’t yet looked into how to do that.  The drawback is the price.  All the other IDEs are free. This has a $199 price tag for an independent developer or a $499 price tag for corporations.

Grails application running on Open-Shift

Install Redhat command line tools.  I followed the standard docs to do this for my mac.  Grails will not run on a small gear.  You need to request an upgraded account for this to work.  I created a openshift-grails-quickstart that can be used.  This work was based on the openshift-tomcat-quickstart on github for the tomcat portion.

 The grails work consists basically of the following:
  1. Copy the tomcat distribution in diy/tomcat directory.
  2. Copy the Grails distribution in the diy/grails directory.
  3. Modify the deploy, start, stop commands for grails.
  4. Fix the “grails war” command to use a HOME directory and ivy-cache to which the openshift user can write.

There is a slight problem that I’ve yet to resolve.  When I run grails run-app locally, it adds “plugins.tomcat=2.1.1″ to the application.properties file.  This allows it to run locally.  But if this gets pushed to the openshift git repo, then it fails with:

remote: ::::::::::::::::::::::::::::::::::::::::::::::
remote: :: UNRESOLVED DEPENDENCIES ::
remote: ::::::::::::::::::::::::::::::::::::::::::::::
remote: :: org.grails.plugins#tomcat;2.1.1: not found
remote: ::::::::::::::::::::::::::::::::::::::::::::::

 

Python with embedded javascript engine

I want a safe way to modify data rows after a schema change.  I first attempted just using python with the eval method.  There is no good way to safely sandbox a python script.  I don’t suppose it really matters if it is sandboxed since Ruby on Rails migrations let you write migrations with full access to do nasty things to your computer.  Might need to rethink this…  In any case, I chose to support a javascript interpreter.  Every developer knows javascript (or every web developer anyway).  If they don’t, they should.

Install PyV8 for OSX

PyV8 is the python binding for google’s V8 javascript engine.  Currently, it’s only released for Windows.  I found a good pre-compiled package to install PyV8 for Mac OSX.  Unfortunately, it doesn’t work out of the box because the directory is all lower case (pyv8) when the import statements expect mixed case (PyV8).  You can either correct the setup, or you can correct it after it installs in the site-packages folder.

cd /opt
pip install -e git://github.com/brokenseal/PyV8-OS-X#egg=pyv8
python setup.py install
cd /usr/local/lib/python2.7/site-packages/PyV8-0.8-py2.7.egg
mv pyv8/PyV8.py .
mv pyv8/_PyV8.so .
mv pyv8 backup

You can verify by trying to import PyV8 from the python interpreter.

Javascript through Python

This is a simple python script that will print “3″ if you have installed everything correctly.

import PyV8
ctxt = PyV8.JSContext()
ctxt.enter()
print(ctxt.eval("1+2"))

I wanted to grant access to a global object where I placed functions that would be available in javascript.  For example, I wanted to give the ability to return the current date in a “now()” function.

import datetime
import PyV8
class Global(PyV8.JSClass):
    def now(self):
        return datetime.datetime.now()

ctx = PyV8.JSContext(Global())
ctx.enter()
d = ctx.eval("now()")
print(d)

Timezone issues

Hmmmm, got a little side tracked with timezone issues. Seems like there is a challenge with timezones between python and V8 engine.  If I create date in javascript through ctx.eval(“new Date()”), I get the date I expect (in UTC).  If I create a date in python and give it to javascript, it comes back to python wrong.  This means if I want to do any data manipulation, I’m going to have to fix all the dates going to javascript.

class Global(PyV8.JSClass):
    def __init__(self):
        # We must calculate the offset here 
        # because timezones are screwy
        self.offset = datetime.datetime.now()
        ctx = PyV8.JSContext(self)
        ctx.enter()
        self.offset = ctx.eval("offset") - self.offset

    def now(self):
        utc = datetime.datetime.utcnow()
        return utc - self.offset

    def out(self, string):
        print(string)

class TestJavascript(unittest.TestCase):

    def test_javascript(self):
        ctxt = PyV8.JSContext()
        ctxt.enter()
        value = ctxt.eval("1+2")
        assert value == 3

    def test_javascript_withglobal(self):
        ctx = PyV8.JSContext(Global())
        ctx.enter()
        js_global_now = ctx.eval("now()")
        js_now = (ctx.eval("new Date()") 
                  + datetime.timedelta(seconds=1))
        py_now = (datetime.datetime.utcnow() 
                  + datetime.timedelta(seconds=2))
        print("js_global_now=%s" % js_global_now)
        print("js_now=%s" % js_now)
        print("py_now=%s" % py_now)

        assert type(js_now) is datetime.datetime
        assert js_global_now

glassfish SSL self signed cert

When trying to access a third party webservice’s staging environment, we experienced a problem with their SSL certificate.  When trying to make a SOAP request against their server, we were getting:
"unable to find valid certification path to requested target".
 In order to troubleshoot the SSL issues, you can turn on debugging in the glassfish container by setting the JVM option.  It shows you every cert that it loads as well as the cacert file that it is using.
-Djavax.net.debug=ssl

openssl

I Also discovered a useful command that would let me investigate the SSL problems and see the cert that was being sent.  I used this to determine that the certificate wasn’t sending the whole certificate chain.
$openssl s_client -connect staging.example.com:443
CONNECTED(00000003)
depth=0 /O=*.example.com/OU=Domain Control Validated/CN=*.example.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 /O=*.example.com/OU=Domain Control Validated/CN=*.example.com
verify error:num=27:certificate not trusted
verify return:1
depth=0 /O=*.example.com/OU=Domain Control Validated/CN=*.example.com
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:/O=*.example.com/OU=Domain Control Validated/CN=*.example.com
 i:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./OU=http://certificates.godaddy.com/repository/CN=Go Daddy Secure Certification Authority/serialNumber=07969287
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIFYTCCBEmgAwIBAgIHKAMC7TRRezANBgkqhkiG9w0BAQUFADCByjELMAkGA1UE

InstallCert.java

Initially, to add the cert to the repository, I found a post that pointed me in the right direction. The InstallCert.java seemed promising.  It creates a jssecacerts file in the directory you run it.  It contains the contents of the JRE_HOME/lib/security/cacerts file, plus the cert you choose to add.  You need to then copy the jssecacerts file on top of the original cacerts file (after you back it up).

java -djava.home=/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home/jre InstallCert www.example.com:443

With the cert I was using, it still kept failing for me.  When I investigated the code of InstallCert.java, it was throwing an UnsupportedOperation exception when I tried running it again to see if it worked.  Since it was old code, I gave up on that tool and decided to do it manually through the keytool.

keytool

After obtaining the cert (using firefox to view the cert and then exporting it), I ran the following command:

keytool -import -trustcacerts -alias example -storepass "changeit" -file example.pem -keystore $GLASSFISH_CONFIG/cacerts.jks

You can find the GLASSFISH directory by looking at the log file in eclipse.  On my Mac, it was:

/Applications/eclipse/plugins/oracle.eclipse.runtime.glassfish312_3.1.2.0/glassfish3/glassfish/domains/domain1/config

You can verify that it was added by running:

keytool -list -v -keystore cacerts.jks

You should see your cert by looking for the alias you created.

 

Upgrade to MySQL 5.5 using Homebrew

Upgrading to 5.5.25a using Homebrew

First, backup all the databases you are interested in keeping.  There is an -A, –all-databases command if you’d like to use that instead.

mysqldump --flush-logs -u root -p mydatabase > backup.sql

Install the latest version of mysql using Homebrew.

brew install mysql

Note that the “_mysql” user below was the same user that my MacPorts install of mysql was already using so I just reused the same account.  Change it to a valid account.

sudo mysql_install_db --verbose --user=_mysql --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

Start the server in console mode.  Below are the start/stop commands:

sudo mysql.server start
sudo mysql.server stop

Change the root passwords.

mysqladmin -u root password
mysqladmin -u root -h localhost password -p

To launch mysql on startup you first need to edit the plist file referred to below.  Change the UserName to be _mysql (or whatever user mysqld is running as).  Then run the following commands:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Now, restore your database:

mysql -u root -p mydatabase < backup.sql

python ide

I think it’s important to understand how your applications work without the aid of an IDE.  But man, oh man.  It sure is nice to have a good IDE while you are trying to be productive in a language.  I installed PyDev today now that I feel very comfortable with how python applications are built and run.  It is so nice to explore what’s available by just typing code.  In my IDE, I can just type “platform.” wait a sec and see all my available options.  I’ve been using TextMate for the better part of two years now.  It’s a great text editor, but it sure is nice to have a full featured IDE to not only write the code, but see warnings and errors very quickly.  It’s nice to see the structure of my code in the outline view.  By far, my favorite feature is the integrated debugging.  Setting breakpoints, stepping through code, and inspecting variables is a beautiful thing.