Archive

Author Archive

Hot reload / Build on save for Adobe Flex

September 26, 2011 Comments off

If you’re not using Adobe Flash Builder for developing Adobe Flex apps, you might be missing the build on save (or build automatically / hot reload) feature.

Here’s a little Node.js utility that does the same. While working on Flex with gedit (or any other text editor), just keep “hotflex” running in the background. It watches all MXML and AS3 files and rebuilds the project on save.

Screenshots

This slideshow requires JavaScript.

Dependencies

Node.js and NPM are required, refer to – Setup Node.js and NPM on Ubuntu

For notifications on Ubuntu, install libnotify-bin:

    sudo apt-get install libnotify-bin

Install

From NPM:

    sudo npm install hotflex -g

From GitHub

    git clone git://github.com/Srirangan/hotflex.git
    cd hotflex
    sudo npm install -g

Configure

You will need to create a hotflex.json file containing the following parameters:

    { "source": "./src/main/flex/main.mxml"
    , "sourceFolder": "./src"
    , "target": "./bin/main.swf"
    , "lib": "./lib"
    }

The lib parameter is optional while the rest are mandatory.

Run

Execute hotflex from the folder containing the hotflex.json file, preferably your project root.

hotflex

Meta

GitHub repository – https://github.com/Srirangan/hotflex
NPM – http://search.npmjs.org/#/hotflex

To do

  • Integration with Adobe Flex Compiler Shell fcsh for faster builds
  • Integration for Growl for OSX notifications. Currently notifications work for Ubuntu.
Categories: Web Tags: ,

Get started with Django .. django-kick-start!

May 22, 2011 Comments off

The Django Framework is an extremely popular and powerful Python based framework for web application development. The project began way back in 2003 and have for the past few years really matured and has gained mainstream and enterprise adoption.

I recently conducted an intensive training session titled “django-kick-start” at the Inphina iBat dated 21st May 2011.

Download source code


Srirangan is a programmer / senior consultant with Inphina Technologies
Blog   GitHub   LinkedIn   Twitter

Categories: Web Tags: ,

Flex 4.5 with IntelliJIDEA | Screenshot Guided Tour

May 11, 2011 6 comments

I used to be a Windows, Flash Builder, Eclipse user but I have since migrated to Ubuntu 11.04 and IntelliJIDEA. I would have migrated earlier than I eventually did but one of the things holding me back was the lack of a mature Flex development environment on Ubuntu.

This has now changed and am working with Flex 4.5 SDK on IntelliJIDEA + Ubuntu and I feel more productive than ever before. Listed below are steps for you to get started with developing Flex 4.5 applications on IntelliJIDEA:

  1. Assumption – You have downloaded Flex SDK 4.5 and have a working installation on IntelliJIDEA. Additionally you have downloaded the Flash debug player (projector) for Linux.
  2. Add Flex 4.5 SDK to Global SDKs – Launch the Project Structure window through the file menu or Ctrl + Alt + Shift + S. Under Platform Settings select the SDKs option and add the Flex 4.5 SDK. You should know the path where Flex 4.5 SDK has been extracted.
  3. Add Flex 4.5 SDK as a Module Dependency – Under Project Structure select Modules, further select your Flex module and set Flex 4.5 SDK as Module SDK and it to the Dependencies if not already present.
  4. Fast Flex Compiler – Open Settings via the file menu or Ctrl + Alt + S, select Flex Compiler and choose the Flex compiler shell for fast Flex compilations. Using Built-in compiler shell did not work for me with Flex 4.5 SDK.
  5. Edit configuration with Flash debugger – Create a new Flex run configuration and select the Flash Player Debugger as the Launch Application.
  6. All set to go!


Srirangan is a programmer / senior consultant with Inphina Technologies
Blog   GitHub   LinkedIn   Twitter

Categories: Web Tags: , ,

Setting up Scala dev environment on Ubuntu 11.04

April 30, 2011 4 comments

Inphina provides specialized Scala consulting, training and offshoring … Learn more about Scala@Inphina!

Recently moved to Ubuntu 11.04 from Windows 7 and not regretting it one bit. Used Ubuntu Software Center and Synaptic Package Manager to set up much of my development environment without any hassles. However I couldn’t use the Synaptic Package Manager to install Scala as it had an older version published in the repository — Scala 2.7.x.

If, like me, you need to install Scala 2.8.1 or Scala 2.9.1 on Ubuntu, follow the steps below:

  1. Download your desired version of Scala
  2. Extract the package in /opt/
  3. Edit .bashrc with the command gedit .bashrc
  4. Export variable SCALA_HOME: export SCALA_HOME=/opt/scala-2.8.1.final
  5. Update PATH to include SCALA binaries: export PATH=$SCALA_HOME/bin:$PATH
  6. Save .bashrc, log out and log in
  7. Verify the Scala installation: scala -version
  8. Install / configure your favorite IDE

You’re ready to go, start hacking..


Srirangan is a programmer / senior consultant with Inphina Technologies
Blog   GitHub   LinkedIn   Twitter

Categories: Scala Tags: ,

How to introduce Scala in the enterprise

April 22, 2011 2 comments

Inphina provides specialized Scala consulting, training and offshoring … Learn more about Scala@Inphina!

Are you a Scala fanboy enthusiast? Have you been following the Scala community and projects intently for a past couple of years? And have you been itching to implement Scala code, Scala libraries and tools in a real world scenario only to find your team members / customers / IT leadership not receptive to the idea?

If so, here are some things you may want to try:

Effectively communicate Scala’s benefits

Scala has a lot of advantages … you know that and I know that but do they know that?

Does your IT leader know that Scala plays really nicely with the existing JVM (and CLR) environments? Do your fellow programmers know of the conciseness of Scala code? Is the older, hardened Java developer aware that most times Java Code *is* perfectly compatible and compilable Scala code?

Has your customer been told of the rapid development, scalability and maintainability edge Scala brings to the engagement?

If stakeholders see the benefit they will recognize the opportunity. And it may very well be that we have earned new allies in our crusade for Scala adoption.

Share the case-studies

The Scala programming language has been proven in high-scalability environments. Twitter got over its stability woes with its migration to Scala. LinkedIn picked Scalatra, a Scala web framework, for its newer modules. Foursquare is built entirely on the Scala Lift Web Framework while Guardian.co.uk recently spoke about their adoption of Scala.

These and many more case studies are out there available helping our understanding of the realistic challenges of Scala adoption, how they were overcome and what are the benefits that have been derived from the overall migration to Scala.

Start with something small

Take the initiative and start with something small and show them how it is done.

For example you may want to introduce a sub module in your existing enterprise project for testing with ScalaTests, build your project utilities with Scala, or implement a simple website / web application with a Scala based web frameworks instead.

Amaze the business with the rapid developement that you’ve achieved, amaze the geeks with your sexy Scala code. Go out there, implement it anyway and bedazzle them.

If things go wrong, you can always apologize and make use of Git or whatever other version control you have. ;-)

Destroy the “It’s not enterprise ready!” argument

You have great IDE support (IntelliJIDEA, NetBeans 7, Eclipse with ScalaIDE 2.9 beta). It integrates very nicely with Maven. You have SBT for projects from scratch. You have books and now you have training programmes. It works seamlessly on your JVM / CLR based environments.

Given the current economic situation, it is pragmatic for business to consider the availability of Scala shops and especially Scala Offshoring partners. Inphina Technologies has taken the lead and is pioneering the effort to offer Scala Offshoring solutions to its partners.

Ask loudly *why* do they specifically think it is not enterprise ready? Chances are they don’t really have a good explanation. That’s not to say Scala doesn’t have its shortcomings, however, what Scala also has is a very enthusiastic and effective community.


Srirangan is a programmer / senior consultant with Inphina Technologies
Blog   GitHub   LinkedIn   Twitter

Categories: Scala Tags:

Simple Database Migrations with Scala and Querulous

April 12, 2011 3 comments

Inphina provides specialized Scala consulting, training and offshoring … Learn more about Scala@Inphina!

(Cross posted on my personal blog)

We hate doing it, but one time or the other, each of us ends up writing a quick-and-dirty database migration utility for a project we’re working on. I recently had to do the same, and surprisingly the process was really smooth .. largely thanks to Scala goodness and Querulous — a very cool, simple and lightweight database library created by some folks at Twitter.

Let’s start by creating a Mavenized Scala project, check out Getting Started – Maven Archetype for Scala.

Does the Scala project build successfully? Good.

The next step, add the Querulous dependencies to your project pom.xml file:

<dependencies>
    ..
    <dependency>
      <groupId>com.twitter</groupId>
      <artifactId>querulous</artifactId>
      <version>2.0.1</version>
    </dependency>
    ..
</dependencies>
..
  <repositories>
    ..
    <repository>
      <id>twitter.com</id>
      <url>http://maven.twttr.com</url>
    </repository>
    ..
  </repositories>

In our example, we will migrate a source database boysDb to a destination database girlsDb. No real migration business logic is being applied, think of it as a one-to-one migration.

We start off by declaring QueryEvaluators for our source and destination database schemas:

  val sourceDb = QueryEvaluator("localhost/boysdb", "root", "")
  val destinationDb = QueryEvaluator("localhost/girlsdb", "root", "")

QueryEvaluator object allows us to execute SQL queries on a referenced database.

Next step, get boys from the source database:

val boys = sourceDb.select("select id, name from boys") {
      row => (row.getInt(1), row.getString(2))
    }

boys is a collection of Tuple2[Int, String] containing the id and name attributes of a boy.

For each boy, insert a girl:

boys.foreach(boy => insertGirls(boy))

Let’s now define our insertGirls() method

  def insertGirls(boy: Tuple2[Int, String]): Unit = {
    destinationDb.execute("insert into girls (id, name) values (?, ?)", boy._1, boy._2)
  }

..and we’re done.

Scala + Querulous makes it easy to connect to multiple database instances, makes querying extremely straightforward by exposing SQL directly without unnecessary ORM’ing or configuration / property file mess.

Full source code has been published on GitHub – https://github.com/Srirangan/scala-querulous-simple-migrations

For serious database migrations, do consider Scala Migrations.


Srirangan is a programmer / senior consultant with Inphina Technologies
Blog   GitHub   LinkedIn   Twitter

Categories: Scala Tags: ,

Getting Started – Scala Persistence with Squeryl

March 18, 2011 2 comments

Inphina provides specialized Scala consulting, training and offshoring … Learn more about Scala@Inphina!

(Cross posted on my personal blog)

Recently I had to implement simple entity persistence in Scala. I used Squeryl which defines itself as “a Scala ORM and DSL for talking with Databases with minimum verbosity and maximum type safety”. I tried it out and it was extremely easy to get started with.

This blog post will cover the basics of Scala persistence using Squeryl and the target database system is MySQL.

You will need a Scala project to work with. If you don’t have one already, you can create a Scala project with the Maven archetype.

Step 1 – Define dependencies

Our first dependency is Squeryl. As I blog this, the latest Squeryl version is “0.9.4-RC6″. We need to make this available to our code. Additionally, we need to make available the MySQL Connector as this example persists data into a MySQL database. MySQL Connector is a run-time dependency internally consumed by Squeryl.

    <dependency>
      <groupId>org.squeryl</groupId>
      <artifactId>squeryl_2.8.1</artifactId>
      <version>0.9.4-RC6</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.15</version>
    </dependency>

If you are using Apache Maven, add the code shown above to the “dependencies” element of your project “pom.xml” file. If you don’t use Maven, make sure the artifacts / JARs are available in your project.

Step 2 – Define entities

Squeryl entities are basic Scala objects.. should we call them POSO’s? Here I’ve defined a BaseEntity and extended it to define a User entity.

package net.srirangan.opensource.squerylexample.entities

import java.sql.Timestamp
import org.squeryl.KeyedEntity

class BaseEntity extends KeyedEntity[Long] {

  val id:Long = 0
  var lastModified = new Timestamp(System.currentTimeMillis)

}

All entities will extend and build on the BaseEntity. The BaseEntity introduces two fields “id” and “lastModified”.

package net.srirangan.opensource.squerylexample.entities

class User(var email:String, var password:String) extends BaseEntity {

  // Zero argument constructor required
  // Squeryl Roadmap says 0.9.5 will not need them :-) 
  def this() = this("", "")
  
}

We’ve now defined the User extending the BaseEntity. In addition to the fields “id” and “lastModified”, a User instance will have “email” and “password” attributes. In addition, we have had to define a zero-argument constructor, however, the Squeryl road-map says that this would not be required from version “0.9.5″ onwards.

Step 3 – Define schema

Squeryl requires us to define a Schema object. Our entity classes (i.e. User) needs to be mapped as tables inside the Schema. Table column properties (unique, index, auto_increment etc.) are also defined here.


package net.srirangan.opensource.squerylexample.schema

import org.squeryl._
import org.squeryl.PrimitiveTypeMode._
import net.srirangan.opensource.squerylexample.entities.User

object Schema extends Schema {

  val users = table[User]

  on(users)(user => declare(
      user.id is (autoIncremented),
      user.email is (unique)
    ))

}

In the code above, we map the User entity to the User table and we’ve declared the email column as unique and the id to be auto incremented. Custom table and column names are possible to define through Squeryl annotations provided, check out the Squeryl docs.

Step 4 – Start database session

Needless to mention, we will need an active database session if we are to perform CRUD operations on the database. Here’s how I’ve done it:

  import org.squeryl.Session
  import org.squeryl.SessionFactory
  import org.squeryl.adapters.MySQLAdapter
  
  val databaseUsername = "squeryl-example"
  val databasePassword = "squeryl-example"
  val databaseConnection = "jdbc:mysql://localhost:3306/squeryl-example"
  
  def startDatabaseSession():Unit = {
    Class.forName("com.mysql.jdbc.Driver")
      SessionFactory.concreteFactory = Some(() => Session.create(
          java.sql.DriverManager.getConnection(databaseConnection, databaseUsername, databasePassword),
          new MySQLAdapter)
        )
  }

Step 5 – Generate schema

Once we’ve initialized the database session, we can generate the database schema as shown below:

  import org.squeryl.PrimitiveTypeMode._
  import net.srirangan.opensource.squerylexample.schema.Schema

  startDatabaseSession()
    
  transaction {
    Schema.create
    println("Created the schema")
  }

Step 6 – Insert and Update

  import net.srirangan.opensource.squerylexample.entities.User

  transaction {
    val user1:User = new User("user1@domain.com", "oldPassword")
    Schema.users.insert(user1)
    println("Inserted user1")
    
    user1.password = "newPassword"
    Schema.users.update(user1)
    println("Updated user1")
  }

Step 7 – Select

    transaction {
      val queriedUser:User = Schema.users.where(user => user.id === 2L).single
      println(queriedUser.id + " -- " + queriedUser.email)
    }

The entire squeryl-example source code is available at github.com/Srirangan/squeryl-example

More examples on Squeryl.org.


Srirangan is a programmer / senior consultant with Inphina Technologies
Blog   GitHub   LinkedIn   Twitter

Categories: Scala Tags: , , ,

Build a simple web crawler with Scala and GridGain

March 10, 2011 4 comments

Inphina provides specialized Scala consulting, training and offshoring … Learn more about Scala@Inphina!

(Cross posted on my personal blog)

Recently, as a proof-of-concept, I had to build a crawler. Of course I cannot share much details about that project other than to state that it’s an absolute privilege to be part of. :-)

I set out to build this crawler.

Prior experience of pairing with Narinder and Vikas had made me aware of distributed computing technologies such as Hadoop and GridGain, so I knew there was my start. Based on past experiences, I immediately picked GridGain over Hadoop. Pretty obvious reasons too: More examples, better support etc.

My next choice was a programming language. Java was the obvious choice but I took a risk and chose Scala. GridGain’s support for Scala and abundance of examples made this choice a bit easier. A quick, unofficial definition for those unaware: Scala is an Objective-Functional programming language that is very attractive to programmers and has proved itself in high-scalability situations (Twitter, LinkedIn, FourSquare etc.)

Note – I am new to Scala and my Scala code may look more Java like than Functional. I’m still learning and future examples should be better. “Awesomeness of Scala code” not a valid parameter to judge this blog post!

Professional etiquette (and NDAs + lawyers) will not allow me to share exact details of this crawler. After all, it is not my intellectual property. But for the sake of this example I will consider my target to be a simple web crawler that would be used by search engines to index the content on the internet.

What would our web crawler do?

  1. Start at some base URL
  2. Index content of this URL
  3. Search for more URLs to index
  4. Repeat 2 & 3 for these new URLs

This blog post will not get into the operational logic of loading a URL, extracting keywords, adding to index, extracting URLs etc. That I believe has been done to death. Alternatively I will look at how to scale up the crawling process using Scala and GridGain.

Those already familiar with GridGain, for the sake of this example I would request you to merge the concepts of a GridTask and a GridJob. Here we will create custom GridTasks which have one corresponding, unique custom GridJob.

Our GridTask-GridJob Pairs will be:

  • LoadUrlDataTask, LoadUrlDataJob
  • IndexKeywordsTask, IndexKeywordsJob

Much of the game is being played in LoadUrlDataJob. Its role is envisioned as follows:

  1. Make HTTP request to URL
  2. Gather response data from URL
  3. Trigger IndexKeywordsTask for URL data
  4. Fetch new URLs from URL data
  5. Trigger LoadUrlDataTask for new URLs

While the rest have simple roles:

  • LoadUrlDataTask = Return one LoadUrlDataJob
  • IndexKeywordsTask = Return one IndexKeywordsJob
  • IndexKeywordsJob = Parse data and index keywords

In other words, an IndexKeywords job would index keywords and die. In contrast, a LoadUrlData job would trigger exactly one IndexKeyword job and trigger potentially multiple LoadUrlData jobs.

Let’s look at the sources:

package net.srirangan.simplewebcrawler.tasks

import java.lang.String
import java.util.{List,ArrayList}
import org.gridgain.grid._
import net.srirangan.simplewebcrawler.jobs.LoadUrlJob

class LoadUrlTask extends GridTaskNoReduceSplitAdapter[String] {

  def split(gridSize:Int, url:String):List[GridJob] = {
    val jobs:List[GridJob] = new ArrayList[GridJob]()
    val job:GridJob = new LoadUrlJob(url)
    jobs.add(job)
    jobs
  }
  
}
package net.srirangan.simplewebcrawler.jobs

import java.lang.String
import java.util.{List,ArrayList}
import org.gridgain.grid.GridJobAdapterEx
import org.gridgain.scalar.scalar._
import net.srirangan.simplewebcrawler.tasks.{LoadUrlTask,IndexKeywordsTask}

class LoadUrlJob(url:String) extends GridJobAdapterEx {
  def execute():Object = {
    println("load url for - " + url)

    val data:String = "this is data for " + url
    val urls:List[String] = new ArrayList[String]()

    //
    // .. actual parser logic comes here
    // .. data:String will contain the contents of url:String
    // .. urls:List is a list of all new URLs found in data:String
    //
    
    // Start indexing keywords for data:String from url:String
    grid.execute(classOf[IndexKeywordsTask], data).get
    
    // adding dummy url in urls:List
    urls.add(url + ".1")

    // start load url for urls:List
    while( urls.iterator.hasNext() ) {
      val url:String = urls.iterator.next()
      grid.execute(classOf[LoadUrlTask], url).get
    }

    data
  }
}
package net.srirangan.simplewebcrawler.tasks

import java.lang.String
import java.util.{List,ArrayList}
import org.gridgain.grid.GridJob
import org.gridgain.grid.GridTaskNoReduceSplitAdapter
import net.srirangan.simplewebcrawler.jobs.IndexKeywordsJob

class IndexKeywordsTask extends GridTaskNoReduceSplitAdapter[String] {

  protected def split( gridSize:Int, url:String):List[GridJob] = {
    val jobs:List[GridJob] = new ArrayList[GridJob]()
    val job:GridJob = new IndexKeywordsJob(url)
    jobs.add(job)
    jobs
  }
  
}
package net.srirangan.simplewebcrawler.jobs

import java.lang.String
import org.gridgain.grid.GridJobAdapterEx
import org.gridgain.scalar.scalar._

class IndexKeywordsJob(data:String) extends GridJobAdapterEx {
  def execute():Object = {
    println(data)
    // .. actual indexing logic comes here
    null
  }
}

Complete Mavenized sources for Scala GridGain SimpleWebCrawler can be found on GitHub.com – https://github.com/Srirangan/simplewebcrawler

A quick look at the role of LoadUrlDataJob and we know that this needs to scale and scale big. Here is a visualization showing three levels of LoadUrlData wherein each LoadUrlDataJob spawns three other LoadUrlDataJobs and one IndexKeywords Job.

GridGain takes care of this seamlessly and divides the tasks among available nodes without any configuration or instruction. Here are screenshots showing three nodes of GridGain, one inside my IDE while other two on the console.

Is this a perfect web crawler? No. Far from it. For one, you need to control its spawn-rate else your machine will die. :-)

But it is an example that does showcase the power of GridGain and the ease with which Scala / Scalar can leverage it.


Srirangan is a programmer / senior consultant with Inphina Technologies
Blog   GitHub   LinkedIn   Twitter

Getting Started – Maven Archetype for Scala

March 5, 2011 2 comments

This is a two minute tutorial to get start with Scala development with Maven. Things are easy as there is a cool archetype to automate most of the project creation  .. ;-)

But first .. What is Scala? Here’s how Scala-lang.org defines it:

“Scala is a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive. Code sizes are typically reduced by a factor of two to three when compared to an equivalent Java application.”

Let’s get started. First, you need Maven pre-installed and configured. If you don’t have it ready, stop right here.

Start the console and enter the following archetype:generate command:

$ mvn archetype:generate -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple

This enters the interactive mode and asks you to enter the project groupId, artifactId, version and other project coordinates. After which it generates a Scala project.

This should have been it, but unfortunately it is not. You need to edit the generated pom.xml and uncomment the repositories and pluginRepositories elements.

<repositories>
<repository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url&gt;
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>scala-tools.org</id>
<name>Scala-Tools Maven2 Repository</name>
<url>http://scala-tools.org/repo-releases</url&gt;
</pluginRepository>
</pluginRepositories>

Don’t ask me why they are commented in the first place, but as of March 5th 2011 they are and it sucks that they are.

By now we have avoided “dependency not found, build error”  and thankfully this is the only modification required.

You should now be able to successfully build the project..

$ mvn compile

..and get started with real programming.

Categories: Scala Tags: ,

Send JSON objects over Adobe Flex HTTPService call

March 3, 2011 2 comments

It was either incredibly difficult to locate decent references for, or me being incredibly stupid     <– look me indulging in self-degradation and not pompous bragging as often accused by some team members ;-) but I ended up spending all day trying to figure out how to send a JSON object / string over an HTTPService call. It turned out to be quite simple in the end and a thank you goes to Wetfeetblog.com and Tomas Mazukna.

Here’s how it is done:

var httpService:HTTPService = new HTTPService();
httpService.contentType = “application/json”;
httpService.resultFormat = HTTPService.RESULT_FORMAT_TEXT
httpService.send(“…json..string..here…”);

You may end up using AS3Corelib‘s JSONObject to encode your objects as a JSON string:

httpService.send(JSON.encode(..yourObject..));

..and that is all. It is now ready to be consumed by your RESTful service and converted into a plain old object and / or entity depending on what you’re using back there.

PS: Yes, my Flex skills are a bit rusty :-p

Categories: Web Tags: , , ,
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: