Author Archive

Capsules for recovering Scala fever.

December 27, 2011 Comments off

Scala is a multi-paradigm programming language that combines the features of object-oriented programming and functional programming. The word “Scala” refers to “Sca(Scalable)+la(Language)” i.e designed to grow with the demands of its users. Scala runs on the Java Virtual Machine.

Here We’ll discuss about some of the basic concepts in Scala that make it distinct from other programming languages.Let us start with some of the following:

1. Simple method in scala:-

object Check {
   def countcharacter(a: String, b: String) = { //Declaring a method in scala
       if (a.length > b.length) println("The large word is" + a) 
       else  println("The large word is" + b) 

  def main(args: Array[String]) {                 // The main method in scala
      countcharacter("Neelkanth","Sachdeva")     //calling the method


2. Scala Array with “for” loop :-

object forloopdemo {
  val name= new Array[String](4)     // Declaring an Array in scala
  def printarrayelem(args:Array[String]):Unit={  

   /*Creating a method named "printarrayelem" with return type "Unit",
   that will print out all the elements in array.  */                                             
  for(arg <- args)      
  /* A sample for loop for printing all elements in array. */                                                  
  def main(args: Array[String]) {    //The main method in scala

3. Constructor in Scala:-

class construct(val name: String, val age: Int) {                
println("This is the example program of primary constructor")

object PrimaryConstructor {
  def main(args: Array[String]) {           //the main method.
    val cons = new construct("Neelkanth Sachdeva", 23) 
     /*passing the values for name and age at the time of object creation. */                                                
    println(      // accessing the value via constructor.

4. Companion Objects. :-

In Java, you often have a class with both instance methods and static methods. In Scala, you achieve this by having a class and a “companion” object
of the same name. The class and its companion object can access each other’s private features. They must be located in the same source file.

class companion{
  private var balance=0.0
  private def deposit(amount:Double){      //Declaring a private method
object companion {
  def main(args: Array[String]) {
  val n=new companion
/*Accessing the private method of class in its companion object.  */

5. Bypass the main method :-
In Scala, you need not to have main method always, you can bypass the main method by extending the “App” trait as follows.

object main_alternate extends App{       //extending the "App" trait 
                                         //to avoid main method.
 println("This is the alternate of main")

6.ArrayBuffer in Scala:-
Most operations on an array buffer have the same speed as for an array, because the operations simply access and modify the underlying array. Additionally, array buffers can have data efficiently added to the end.Array buffers are useful for efficiently building up a large collection whenever the new items are always added to the end.

import scala.collection.mutable.ArrayBuffer
object arraybuffdemo {
  val abuf = new ArrayBuffer[Int]()        //Array buffer
  val array = Array(1, 2, 3, 4, 5)         //Simple array

  def main(args: Array[String]) {

    abuf += 1            //Inserting an element to buffer

    abuf ++= array      //Inserting a whole array to arraybuffer

    abuf.trimStart(1)   //removing one element from 
                         //starting point in arraybuffer

    abuf.trimEnd(1)    //removing one element from 
                         //ending point in arraybuffer

    abuf.insert(1, 80, 81, 82)  //inserting the three element 
                                //say 80,81,82 at index 1. 
    abuf.remove(1, 2)   //removing two elements from the index 1


Enjoiii Scala!!!! :-)

Categories: News

Basic Http authentication in Lift.

December 16, 2011 Comments off

Lift web framework accommodate a no. of conspicuous features that are implemented in a unique and solitary way as compared to other frameworks. Here we’ll discuss about the basic “Http authentication” mechanism for security in Lift.

What is ‘LiftRules’ singleton ?
Nearby all configuration parameters for the http requests and response are defined in LiftRules singleton. The important thing to notice is that we can only change the configuration parameteres of LiftRules only during boot but not at other times. You can find more about LiftRules Here.

Now for our purpose of Http authentication what all you have to do , is to add some Lines of code using the LiftRules singleton present in lift, in to your Boot.scala File and you’ll find all gets done for a basic http authentication.

Let us have a look to the steps for doing it ::

*Firstly in your Boot.scala file make the class extends Loggable(part of “net.liftweb.common”) as :

class Boot extends Loggable {
  def boot {
} }

*Now you have to proceed with the ‘prepend’ method(from LiftRules package) implementation in your Boot(def Boot) method & defining the name of html pages on which you want to implement the authentication as follows:

      case (Req("html_page_name":: Nil,_,_)) => Full(AuthRole("admin"))
      case (Req("securepage":: Nil,_,_)) => Full(AuthRole("admin"))
// Just mention the above code for page on which you
// want to implement the http authentication.   


*Call the authentication(have root in ‘HttpAuthentication.class’) from the LiftRules and mention the username and password for the authentication purpose as :

LiftRules.authentication = HttpBasicAuthentication("Authenticate Yourself") {      
      case("user_name","password",req) => {"Authentication Succeeded...Welcome!!")

* Now each time when user will try to access the page,browser will pop up for the http authentication like: :-)

Http Authentication in Lift

Note:- Don’t forgot the following basic imports:

import common.{Loggable, Full}
import http._
import auth.{userRoles, HttpBasicAuthentication, AuthRole}

::: Want to get start with Lift ? come Here.

Categories: News

Getting Started with Lift web framework : Your first step…!

November 3, 2011 1 comment

Lift is a sophisticated web development framework that runs inside a Java web container & uses the Scala programming language.Lift removes a lot of the burdens that other frameworks place on developers by mixing together the best ideas in the marketplace today & aims to make building real-time, highly interactive, and amazingly scalable applications.Here we are going to introduce you with the Lift web framework in a very easy and understandable manner to give you a better quick start with Lift.

1. First of all you have to install simple-build-tool(sbt) on your machine. (Know more about it Here )

2. After installing simple-build-tool(sbt), Just create a directory for your project say “Test” , enter in to directory and just say “sbt” as:

nsachdeva@nsachdeva-Vostro-3700:~/Test$ sbt

3. “sbt” will prompt you to ask for creating new project, just press “y” & give the Name to your project as shown in Fig. :


4. Now for creating the lift project, you’ll use a processor to generate the structure and default files you’ll need to start working on the application.From the shell, run this command:

*lift is org.lifty lifty 1.6.1

5. Now you have to execute the following command to populate a blank SBT project with Lift web specialities.

> lift create project-blank

6. Just give the name of your main package & the version of Lift you want to use, when it prompt for ask and then you will obtain the following type structure of your project.

7. After obtaining this point, proceed by a “reload” & “update”.This will download all the dependencies required.

8.Start the jetty and check the running application at http://localhost:8080.

Importing the project in “IntellijIdea”:

you can now import the project on any platform with which you want to work e.g for importing the project in intellijIdea just run the following commands. This will generate the “Test.iml” file within the project structure and you will be able to import this project in IntellijIdea.

>  *sbtIdeaRepo at
 > *idea is com.github.mpeltonen sbt-idea-processor 0.4.0
 > update
 > idea
Fig: The generated Project hierarchy

Categories: Lift web framework

Setting up environment to work with scala using play framework.

October 27, 2011 Comments off

Play is an open source web application framework base on model-view controller architecture. Play is written in java but also provides support for the scala programming language. Here we are going to elaborate the steps about the initial environment setup to start working with scala using play framework.

1. Install scala for play using following command.

$ play install scala

2. Create new project as follows (Let us assume that application that is to be created has the name “Test”):

$ play new Test --with scala

3. Make the project able to import in IntellijIdea. (This is completely your choice. You can use your desired Platform to work with e.g Eclipse etc. )

$ play idealize Test

4. Run the Test application using following command:

$ play run Test

5. Check the created application at http://localhost:9000 and verify its working.

Now the created application can’t be directly imported in intellijIdea. Now do it as elaborated follow:

6. Create the new project in IntellijIdea. The location of the project must be the same as the directory where the play project is created.Don’t forget to uncheck the create module option because the created play project itself acts as a module.

7.Now select ‘Project Structure’ option from the file menu. Click the Module option and press the “+” button. Select the “Import existing module” option and then point towards the (.iml) file.

8.Let the Module selected,Then select the ‘tmp/generated’ directory in the right one tree and press the ‘Sources’ button. The HTML template pages are converted to scala source files by Play ,these source files are to be included in the project along with the source files in the Test directory.

9.Select Global Libraries > scala-compiler-2.9.1, Then press the ‘Attach Classes’ button and navigate to the ‘lib’ directory in your Scala 2.9.1 directory. Then select the following files & press Apply.

10. Now add another global library as Global Library > scala-library-2.9.1, containing the files scala-dbc.jar, scala-library.jar and scala-swing.jar. Select these files and press OK.

11.Now go to the Modules > Dependencies, press Add button to add new library called “scala-library-2.9.1″ to dependencies.

12. Go to Module,select the scala under Test branch and set the compiler library to the presented one on your machine e.g on my machine this is the “scala-compiler-2.9.1″.

13.Save the Project Structure by clicking the “Ok” button.Again verify the project at http://localhost:9000. Your Project development environment is now ready.

Categories: Play, Scala

Create web application menus using the Lift web framework

September 30, 2011 1 comment

Lift is a powerful & secure web framework having elegant features and also stresses the importance of security, maintainability, scalability and performance. Lift applications are written in Scala.

Here we will discuss about creating the menus in Lift. These are the approaches to create a menu:

  • Define the url names and routes directly in Boot.scala.
  • Create the seperate Singleton for defining menu items.

Let us see how this works:

Define the url names and routes in Boot.scala

class Boot {
  def boot {
    // where to search snippet

    // Build SiteMap
    val entries =
      Menu(Loc("Home", List("index"), "Home"))::
      Menu(Loc("Items", List("items"), "Items")) ::
      Menu(Loc("Search", List("search"), "Search")) :: Nil

    // Sitemap


And here is the output:

Create the seperate Singleton for defining menu items

We will define the sitemap in Boot.scala. Sitemap will obtained from the Application Singleton

class Boot {
  def boot {
    LiftRules.setSiteMap(SiteMap(Application.sitemap: _*))

Now define your menu items in this seperate singleton say "Application" singleton.

object Application {
  // Sitemap
  val sitemap = List(
    Menu.i("Items") / "items",
    Menu.i("Search") / "search"
  ) ::: User.menus

When working with html pages always provide the links to pages corresponds to as defined in your singleton's sitemap e.g

  <li><a href="/items">Items</a></li>
  <li><a href="/search">Search</a></li>

Lift will search for corresponding HTML pages defined either in Boot.scala or in Application.scala in src/main/webapp folder.

Categories: Scala, Web Tags:

Get every new post delivered to your Inbox.

%d bloggers like this: