Thursday, May 10, 2012

Worklog part1: Up and running with authentication

Intention of this session: create a User object and use google to log in.

First I need to create my grails app from the command line(assuming I have installed it first)
# grails create-app webtimer

Then enter interactive-mode by typing
# grails 

To create a domain-class for we simply type
# create-domain-class worklog.User

Now, we need to add some properties to the user, a unique field called email of type String.

String email

So to check that we have a running application, we create some views and run our app locally to see what we have:

# generate-all worklog.User
# run-app 
(notice that you get autocompletion by pressing "tab" in interactive-mode)

The app should now be running on localhost:8080/worklog :
Voila, the app is running.


Now we get down to business. I need authentication! Luckily there are some grails-plugins for that, lets see... I kind of like the sound of the oauth-plugin, so I think I will try that. Installation: always install it by adding it to the BuildConfig-file(as opposed to installing it command-line-style). That way it´s easy to keep track of what you have installed, and upgrade will be less painful. At the time, the version is 1.0, so I add this to my BuildConfig in the plugin-section (current version is 2.0.1, check if it has been updated):

    compile ':oauth:2.0.1'
Thats it? Nope. We need to configure it to authenticate with google. In my Config.groovy, I need to add


oauth {
    providers {
        google {
            api = GoogleApi
            key = '1057869129521.apps.googleusercontent.com'
            secret = 'zaOsvnGrxyZ0kRciJJew0xbb'
            successUri = '/authenticate/success'
            failureUri = '/authenticate/failure'
            scope = 'https://www.googleapis.com/auth/userinfo.email'
            callback = "${grails.serverURL}/oauth/callback"
        }
    }
}



Now,  replace the key and with some sane value and same for the secret (my settings wont work well for you, and will change in a bit). Obtain this from google (go to the apis console link). Of course, now we need to create the success and failure uris...

# create-controller worklog.auth.Authenticate
Wow. Ok, lets see if we can poke around with this. In the index.gsp, we add some code:

<oauth:connect provider="google">Log in with google</oauth:connect>
And in our Authenticate-controller, we add a service and a method for the "authenticate/success"
OauthService oauthService 
 
def success() { 
    Token googleAccessToken = session[oauthService.findSessionKeyForAccessToken('google')] 
    def userInfo = oauthService.getGoogleResource(googleAccessToken, 'https://www.googleapis.com/oauth2/v1/userinfo') 
    def mail = JSON.parse(userInfo.body) 
    render "Authenticated as $mail" 
}
Test and check, heres my result;


This concludes the goal of this session, next time I will work more on domain-objects and setting up some test-data (users) for the app.

Creating an advanced webapp: "work-log" with grails

I have played with the idea of creating my own time-tracker app, and after some prototyping I think I have some useful ideas. So I will hopefully have a series of blog posts where I describe the different 

First of all: my #1 tool is grails. I´m a java-dude, but after playing around with grails for a while I know that java cannot compete with the productivity of grails. 

Second: for authentication I need to use google(it would be nice to integrate with their marketplace for google-apps if this goes anywhere). The users have to be divided into 1) uber-users(me, or the application-maintainer), 2) super-users for a company(CEO) 3) department-managers 4) normal users

Third: I´m no design guru, so I need some good css styling for free. I´m thinking maybe twitter-bootstrap can help me out, but i´m not sure yet. 

Fourth: my domain-model will fit the needs of a multi-department organisations with local managers and company-wide managers. 

Fifth: I need to export data as csv and pdf to create some invoices. Maybe with customized logos. Don´t know how I will do this yet, but I have some experience with itext and "flying saucer".

I will post all my progress on github, and have (hopefully) frequent deployments to cloudfoundry. 

more to come....

Wednesday, April 18, 2012

A hobby paying its own expenses.


I have 2 hobbies: programming and bicycles. I don't make money on bicycling, but programming on the other hand...

Last year I developed a simple slot-machine-game for android, simulating an old norwegian slot-machine from the 80's. The application works ok until some memory leak crashes the application. The application comes in 2 versions: 1) ad-version, user clicks the ads and I get some pennies, 2) a pay version, I get a dollar or so per application.

With the bugs and all, the application was well recieved in Norway and has had 56K downloads of the ad-version, where 97% of the downloaders are norwegian according to the google-stats. The pay-version has 91 downloads.

In total I have recieved about 500usd from ad-clicks and about 100usd from selling the ad-free version. Not enough to quit my day-job and live happily ever after, but enough to buy some gadgets for bicycling and more programming. So here are my next investments:


  • ContourROAM action cam: 300 USD
  • Arduino UNO r3 + accessories : 100USD
  • Stuff I need for building a quadrocopter based on arduino 200USD (++?)
Looking good, now I only need dx.com to deliver the parts, assembly a quadrocopter and make the arduino board-control the beast.

Tuesday, April 3, 2012

Gradle + cobertura + sonar

At my current client we use gradle for building, so i thought I should give sonar+cobertura a go. Here is my config.
subprojects {
    apply plugin: 'java'
    apply plugin: 'sonar'

    // add support for the cobertura task 
    def coberturaPluginBase = 'https://raw.github.com/valkolovos/gradle_cobertura/master/ivy'
    apply from: "${coberturaPluginBase}/gradle_cobertura/gradle_cobertura/1.0-rc4/coberturainit.gradle"

    // configure sonar to pick up the cobertura test-reports
    sonar {
        project {
            coberturaReportPath = file('build/reports/cobertura/coverage.xml')
        }
    }
}
Now you can run cobertura and sonar with the command:
 # gradle cobertura sonarAnalyze

You do need a running instance of sonar to make use of this. Download it here and configure the plugin like this.

Sources:
Sonar-plugin(1.0-m08)
http://gradle.org/docs/current/userguide/sonar_plugin.html
Cobertura-plugin(1.0-rc4)
https://github.com/valkolovos/gradle_cobertura

Wednesday, October 19, 2011

Gradle remote debug for java

Type:
export GRADLE_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=y"


Run your gradle commands as normal and hook up on port 4000 to remote debug your app.

E.g. ./gradlew jettyRun

Monday, June 6, 2011

Map library for gwt

I recently have been working with a hobby project where I want to show a map and create an overlay(path) on the map. I also want to change the overlay of the map in runtime. To do this I´ve discovered these options, but only found one of them to be working for my purposes:

The Official Google gwt-google-apis maps version

the good
  • It works
  • Its the official thing, meaning that most of the functionality in google maps is supported
  • probably works on any version of the gwt
the bad
  • Its version 1.1 of the maps api, the latest version is 3. So you get an old look and feel on the map
  • You need an api key
  • Displaying several maps at once seems to be a problem

gwt-google-maps-v3

the good
  • works very well
  • version 3
the bad:
  • not official, not everything works
  • DOES NOT WORK ON LATEST GWT version!!!
the good
  • simple to use
  • version 3
  • works on any known gwt version
the bad
  • advanced functionality is not implemented yet

For me, the winner is gwt-maps3. It has the features I need(markers, polygon, polyline etc) and it works with the latest gwt version.

Friday, May 6, 2011

Mocking jndi for junit tests

SimpleNamingContextBuilder was the perfect match for me when i wanted to mock some jndi-references for my appctx in my junit-tests

@BeforeClass
    public static void mockJndi() throws IllegalStateException, NamingException {
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        builder.bind("java.naming.factory.initial", new org.jnp.interfaces.NamingContextFactory());
        builder.bind("ConnectionFactory", new QueueConnectionFactoryBean());
        builder.bind("queue/someFileQueue", new ActiveMQQueue());
        builder.activate();
    }.

Thanks to http://formatmysourcecode.blogspot.com/ :)