Setting up a Grails app on Cloud Foundry

I was lucky enough to get access to cloud foundry. I have heard a lot about how easy it is to deploy a grails app on cloudfoundry. I had to try it myself and here is my experience trying to do so….

Building An App
Lets just create a simple grails app called “HelloCloudFoundry”. to do so, we can create a grails app by running the “grails create-app” command like follows:

   /home/manijshrestha/workspace/$grails create-app HelloCloudFoundry

Once the app is created lets just dummy up a index.gsp, I usally delete the default one grails creates and replace it with my own with body text “HelloWorld”. For this example I replaced the content of the index.gsp with simple html as follows

<html>
    <head>
        <title>Hello Cloud Foundry</title>
    </head>
    <body>
        Hello From the Cloud!
    </body>
</html>

Installing cloud foundry plugin
I was pretty impressed that grails already had a cloupowershellpowershelld foundry plugin, to install lets just run our typical grails install as follows

/home/manijshrestha/workspace/HelloCloudFoundry$ grails install-plugin cloud-foundry 

Now, it should install the plugin for cloudfoundry

Configuring the App
To set up the app its fairly easy, edit the Config.grooy file found in grails-app/conf/Config.groovy
and set up with your credentials. If you don’t have an account get one via http://www.cloudfoundry.com
You should receive an email with user username and password

grails.plugin.cloudfoundry.username = 'your.email@server.com'
grails.plugin.cloudfoundry.password = 's3kr3t'
grails.plugin.cloudfoundry.appname = 'YOUR-APP-NAME'

thats it!!! we about 90 percent done!

Deploying
Now, lets deploy our app!!! Yes we are there now..

Now lets just run a grails command to push the app to the cloud foundry

from the command line run

NOTE: technically, we should configure our database like mysql or mongodb etc, but for our demo we will just push it, the command will asks us to create one and we will do so.

   $grails prod cf-push

You will see some input dialogs asking you for the app url? and also since, we didn’t configure a service it will ask you if you would like to bind a mysql service, See my responses below:

Application Deployed URL: 'manijshrestha.cloudfoundry.com'? 
manijshrestha.cloudfoundry.com

Would you like to bind the 'mysql-XXXX' service? ([y], n)
y

And BOOM we are done now you can navigate to your application URL
in my case its

http://manijshrestha.cloudfoundry.com/

Hopefully you enjoyed this, feel free to comment!

Helpful Links:
http://www.grails.org/plugin/cloud-foundry
http://grails-plugins.github.com/grails-cloud-foundry/docs/manual/index.html

Advertisements

Mobile Development using FLEX!

Adobe air has been creating buzz about being able to develop flex application in multiple platform pretty similar to Java. Now, with Adobe Air being available in mobile devices, possibility is endless. It will be interesting to see how far this will go. Specially, the devices with lot of hardware like accelerometer, gyroscope, GPS. (Hello Apple, will you someone day support Adobe Air?Ans: No, If you want adobe use Android!)

http://www.adobe.com/devnet/flex/articles/mobile-development-flex-flashbuilder.html

Android SDK – Changing background of a layout at run time

I was working on a android project where I had to change background image in a layout at at run time. For instance, the user selects an image and it becomes the background of the layout. It is fairly simple to do in Android. See below on how I achieved it:

I have a Frame Layout in which I have defined a background in my case the image file name is “wooden.png” so I have, ‘ android:background=”@drawable/wooden” ‘ in the layout definition. By having this line in my layout, Android will use the wooden.png  that is in the “drawable” directory as the background for the activity .

See below for xml extract:


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"	android:id="@+id/main_layout"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@drawable/wooden">

.. ...

</FrameLayout>

 

In the code if I want to change the background I could achieve this by calling “setBackgroundResource” method on the layout.

See below for an example.


...

View mainLayout = findViewById(R.id.main_layout); // getting the layout

mainLayout.setBackgroundResource(R.drawable.stone); // changing the background to 'stone.png'

...