Monday, March 12, 2012

Deploying Existing Play! Application to Jelastic


If you had an existing play application and you want to put it on Jelastic, the easiest way is to use Jelastic Plugin on Play!.

The Requirement:
  • You need to have an existing Jelastic Envorinment (if you’re Play! application used a database, your environment must have database enable and also configured (means you have your database create with appropriate user assigned to it)
  • Your application must use Play! version must be 1.2.x.


Steps:
  1. On your application.conf add this jelastic configuration

jelastic.api.login=login@domain.com
jelastic.api.password=yourpassword
jelastic.api.context=defaultLocationOnEnvironment
jelastic.api.environment=’environmentName
jelastic.api.apihoster=api.jelastic.com
Example:
jelastic.api.login=barramedalb@gmail.com
jalestic.api.password=6680539
jelastic.api.context=ROOT
jelastic.api.environment=aims-jam
jelastic.api.apihoster=app.jelastic.servint.net

jelastic.api.login and password are your credentials to log onto jelastic app controller. Jelastic.api.context specifies where the application will be deployed, in the example ROOT means it is deployed and accessible by the domain name which is aims-jam.jelastic.servint.net. If I put app on jelastic.api.context, my application will be deploy on app context and can be access on aims-jam.jelastic.servint.net/app. jelastic.api.environment which is the name of your environment defined on your apihoster.

Moreover, jelastic.api.apihoster can only have two value which are: app.jelastic.com (host on Europe), and app.jelastic.servint.net (host in North America).

  1. Configure your jdbc on application.conf.

db.url=jdbc:mysql://environmentName.yourapihoster/dbName
db.driver=com.mysql.jdbc.Driver
db.user=dbUserName
db.pass=dbUserPassword

Example:
db.url=jdbc:mysql://aims-jam.app.jelastic.servint.net/aims-jam
db.driver=com.mysql.jdbc.Driver
db.user=getch123
db.pass=123456789

You can define this on the phpMyAdmin of jelastic.
  1. Increase your db.pool.timeout to 10000ms of application.conf

db.pool.timeout=10000

  1. Deploy your application to jelastic cloud by (must be on the application directory):

play jelastic:deploy


This will take a while depending on your internet upload speed, this will compile your application and create a war file and then will upload it to jelastic. If it is finish, you can now access your application. In my example, I can access my application on aims-jam.jelastic.servint.net.

It seems that jelastic plugin can only work the default environment specified on application.conf, so I created a new environment that I named localhost for my localhost testing environment.

If I am testing my application locally, I will execute: play run --%localhost

Hope this can help you. :)

Resources:




Tuesday, March 6, 2012

Maven with Scala and Lift

  1. Use this lift-archetype-basic archetype which is hosted on http://scala-tools.org/repo-release.

mvn archetype:generate -DarchetypeGroupId=net.liftweb -DarchetypeArtifactId=lift-archetype-basic_2.9.1 -DarchetypeRepository=http://scala-tools.org/repo-releases -DremoteRepositories=http://scala-tools.org/repo-releases -DgroupId=com.dynamicobjx -DartifactId=lift-chat -Dversion=1.0
  1. This will prompt if you want the default version and configuration. Then you should change scala version to latest version. As of now the version is 2.9.1, that also change lift version which is 2.4.

  1. If you had error on dependency life-mapper, change the dependency definition by changing artifactId to life-mapper_2.9.1.

<dependency>
<groupId>net.liftweb</groupId>
<artifactId>lift-mapper_2.9.1</artifactId>
<version>2.4</version>
</dependency>
  1. If you had an error on test classes, this is due to its incompatibility to scala 2.9.1. Therefore, you have to change its artifact id of org.scala-tools.testing from specs to specs_2.9.1 and make the version to 1.6.9.

<dependency>
<groupId>org.scala-tools.testing</groupId>
<artifactId>specs_2.9.1</artifactId>
<version>1.6.9</version>
</dependency>

Now, resolve all the dependencies and make the project an eclipse project (eclipse is my prefered IDE) by executing:
mvn dependency:resolve eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true

This will download all the dependencies you defined on pom. You explicitly also say to download sources and javadocs using -DdownloadSources=true -DdownloadJavadocs=true (parameter for eclipse:eclipse). eclipse:eclipse is the command that let the maven project to be open on eclipse, this goal create files like .project, .classpath, .wtpmodels and .components that is needed by eclipse.

After that you can now import it to your eclipse. The structure of the project must be like this:

The project name will be the artifactid- the source file is categorized into two: one source file for testing and another for your actual source code. It will also generates a src/main/webapp directory, that contains all the *.html and public files like image, css and javascript.

To run the project you use:
mvn jetty:run

To run, with auto-compile if source code is change use this:
mvn scala:cc

If you want an on-the-fly reloading of changes made on Java class file, try to use JRebel. Right now, I don’t add it to my dependencies.

After running jetty:run goal, the output project can be seen on 0.0.0.0:9080.

At the end of today’s experience of mine on scala and lift, I found out that the Boot.scala file is the main Class of the project and its main page is index.html based on the sitemap() method of Boot class.

index.html
<lift:surround with="default" at="content">
<h2>Welcome to your project!</h2>
<p>
    <lift:helloWorld.howdy>
      <span>Welcome to lift-chat at <b:time/></span>
    </lift:helloWorld.howdy>
  </p>
</lift:surround>

This means that this html will be surrounded by the default.html content, where the enclosing content of list:surround will be placed on default.html on the place where lift:bind name=”content” is defined.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:lift="http://liftweb.net/">
<head></head>
<body>
  <div class="container">
    <div class="column span-12 last" style="text-align: right">
      <h1 class="alt">lift-chat<img alt="" id="ajax-loader" style="display:none; margin-bottom: 0px; margin-left: 5px" src="/images/ajax-loader.gif"/></h1>
    </div>

    <hr/>

    <div class="column span-6 colborder sidebar">
      <hr class="space" />
      <lift:Menu.builder />
      <div>
        <lift:Msgs showAll="true"/>
        <hr class="space" />
      </div>
    </div>

    <div class="column span-17 last">
      <lift:bind name="content" /> <!-- this is where the content of index.html be injected -->
    </div>

    <hr />
    <div class="column span-23 last" style="text-align: center">
      <h4 class="alt">
        <a href="http://www.liftweb.net"><i>Lift</i></a>
       is Copyright 2007-2010 WorldWide Conferencing, LLC.  Distributed under an Apache 2.0 License.</h4>
    </div>
    
  </div>
</body>
</html>



(On index.html) Also <b: time> is also a binding tags, in here the value of b:time is defined on Helloworld.howdy. The binding tags is <lift:hellowWOrld.howdy>. in howdy method, they bind the b:time tags to the current time and date.

Resources: