Scala in Business | Knoldus Newsletter – January 2015


Hello Folks

We are back again with January 2015, Newsletter. Here is this Scala in Business | Knoldus Newsletter – January 2015

In this news letter, you will see how functional programming is getting much attention, why should Akka be used in production, how Scala is getting popularity in Italy and many more.

So, if you haven’t subscribed to the newsletter yet then make it hurry and click on Subscribe Monthly Scala News Letter

Screenshot from 2015-01-27 10:37:34

Posted in Akka, Future, Java, News, Non-Blocking, Play Framework, Reactive, Scala, Spark, Web | Leave a comment

Working Effortlessly with Java null in Scala


We would not repeat why null is a billion dollar mistake of Java. In our earlier post, we have already discussed how to handle nulls of Java. In this post we would take it a step further, but first a bit of recap.

Say that you have a function in Java which can potentially throw a null.

def getUser:User = null

Now, the way we have been handling this in Scala has been with fold. So what we would do is

Option(getUser).fold("Sky")(u=>processUser(u))
def processUser(u:User):String= u.name 

Hence, if the Option(getUser) results in None then we give the default value which is “Sky” else if it is Some then we pass it to a method called processUser which brings us back the name. Hence it is of the type User=>String

Now what is the problem with this approach, actually none but it just leads to quite some boilerplating if we are working with quite a few Java methods which can return null because everytime we would be writing statements like

Option(getUser).fold(“Sky”)(u=>processUser(u))

Let us get it into a common object.

 object SValue {
    def apply[T, P](a: T, f: T => P)(implicit defaultValue: P) = Option(a).fold(defaultValue)(f)
  }

and now how we would use it for the above scenario

SValue(getUser,processUser)("Sky")

No folds, no options and just SValue (Scala Value). You can pass the defautValue explicitly or you can let it be picked up from the implicit scope. The gist for the code is present here.

Ok, so far so good. But what if we would like to defer the function for later. We want to do currying it. How do we do that

object SValue {
    def apply[T, P ](a: T,defaultValue: P)( f: T => P) = Option(a).fold(defaultValue)(f)
}

Now, how do we call it this time. We know that if the value is null what would be the default vlaue but if the value is not null then we would like to apply a function to process later.

val f = SValue1(getUser,"Sky") _   

This gives us f which is a partially applied function. Now how do we apply it later

f(processUser)
f(_.name)   

If you notice, the beauty of the above code is now that Scala is Type inferring our datatype and hence we are just able to call _.name on it. Enjoy, the gist is here

Posted in Best Practices | Tagged | 2 Comments

How to Implement Cross Cutting Concern using AOP in Scala


In this blog, I have explained that how easily we can implement Cross Cutting Concern such as logging using Aspect Oriented Programming in Scala. These concerns have nothing to do with core business logic. Aspect Oriented Programming is used for encapsulating cross-cutting concerns into re-usable modules. Here we used AspectJ weaver to weave precompiled aspects into project output

First we defined an annotation style aspect scala code:-

/**
   * around execution of Sum
   */
  @Around("execution(@com.<wbr />knoldus.aspect.Loggable * *.*(..))")
  def aroundSum(joinPoint: ProceedingJoinPoint): Object = {
    val result = joinPoint.proceed
    println("Method:-" + joinPoint.toShortString() + " Input:-" + joinPoint.getArgs().toList.<wbr />mkString(",") + " Result:-" + result)
    result
  }

This aspect will print Method, its input and result detail, when a method is entered annotated with the @Loggable annotation and finished execution.

Let’s define the @Loggable annotation Java code

@Retention(RetentionPolicy.<wbr />RUNTIME)
@Target({ ElementType.METHOD })
public @interface Loggable {
}

Finally define a Sum class to see usage of Aspect

class Sum {
  @Loggable
  def checkSum(a: Int, b: Int): Int = a + b
}

We also need to add META-INF/aop.xml to configure aspect weaver.

<aspectj>

    <aspects>
        <!-- declare existing aspects to the weaver -->
        <aspect name="com.knoldus.aspect.AnnotationBasedAspect"/>

    </aspects>

    <weaver options="-verbose">
        <dump within="com.knoldus.*"/>
    </weaver>

</aspectj>

Complete source code can be found here CrossCuttingConcern_Scala.

Now run sbt test. You will see below message on console.

Method:-execution(Sum.checkSum(..)) Input:-2,3 Result:-5

Posted in Agile, Java, Scala | Tagged , , | 3 Comments

Spray authenticate directive : A decoupled way to authenticate your API


Organization exposes critical business data or user’s private data through API. It is required to protect API against any unauthorized access, attack or security breach. In this blog I am not going to discuss API security mechanism. Here I want to detailed out the separation of API authentication from underlying RESTful Api.

Authentication / Authorization is cross cutting concern and it must be decoupled from business logic. In this post I am going to discuss Spray’s authenticate directive which handle authentication/authorization in decoupled way.

From Spray’s documentation.
“Directives” are small building blocks of which you can construct arbitrarily complex route structures.

A directive does one or more of the following:

  • Transform the incoming RequestContext before passing it on to its inner Route
  • Filter the RequestContext according to some logic, i.e. only pass on certain requests and reject all others
  • Extract values from the RequestContext and make them available to its inner Route as “extractions”
  • Complete the request

 authenticate directive is nothing but a special directive with takes a special function which would be used to authenticate inbound requests. At lowest level authenticate takes Future[Authentication[T]] or ContextAuthention[T] as argument.
Continue reading

Posted in Scala | 1 Comment

How To Implement logRequestResponse in Spray


Spray has the feature to log request and response of every route through logRequestResponse. We needn’t to add log in each route because it could be done from a single place. This also helps to maintain clean code.
Please see below example:-

class HubServices extends Actor with HubRoutes {
  def actorRefFactory: ActorContext = context
  // logs just the request method and response status at info level
  def requestMethodAndResponseStatusAsInfo(req: HttpRequest): Any => Option[LogEntry] = {
    case res: HttpResponse => Some(LogEntry(req.method + ":" + req.uri + ":" + res.message.status, InfoLevel))
    case _ => None // other kind of responses
  }

  def routeWithLogging = logRequestResponse(requestMethodAndResponseStatusAsInfo _)(hubRoutes)

  def receive: Receive = runRoute(routeWithLogging)
}

Now execute sbt run, run the http://localhost:8080/module/BANG from browser and see the log in console.

[INFO] [01/08/2015 22:00:50.925] [ForkJoinPool-3-worker-7] [akka://HubActorSystem/user/hubServices] GET:http://localhost:8080/module/BANG:200 OK

We could change log format and level, depending on the requirement. For complete example, you can pull the code from spray-akka-starter.

Posted in Agile, Akka, Scala, Web | Tagged , , , | 3 Comments

Configure TURN server for webRTC with Firefox


Initially, I was using turn server for webRTC in my application only with Chrome but later i got the requirement of using turn server with Firefox as well.

I tried to run my application on Firefox with the same code as for Chrome,but i was getting the below error :

uncaught exception: buildPeerConnection failed, call not completed

Actually, as i mentioned in my previous blog Configure turn server for WebRTC on Amazon EC2 for configuring turn server that add below code in JavaScript file to use turn server:


var pc_config = {“iceServers”: [{“url”: “stun:stun.l.google.com:19302″},
                                {“url”:”turn:my_username@’amazon ec2 PUBLIC IP address’“, 
                                 “credential”:”my_password”}]};

Above code will run perfectly on Chrome but if you want to run on Firefox then change the above code as :


var pc_config = {“iceServers”: [{“url”: “stun:stun.l.google.com:19302″},
                                {“url”:”turn:’amazon ec2 PUBLIC ip address’ “, 
                                 "username":"my_username",
                                 “credential”:”my_password”}]};

Above code will run for both Chrome and Firefox.

In older format, Firefox cannot find the username, and will not attempt to authenticate itself to the TURN server.

Cheers !

Posted in AJAX, Amazon EC2, Cloud, JavaScript, jQuery, Web | 1 Comment

FunHop: Working with Exceptions in Scala – Problem Statement


If you look at the earlier posts in the FunHop series, you would notice that Exceptions are side effects. Exceptions by their very nature also break referential transparency and are context dependent. You would have got an idea about referential transparency from our earlier post. If not, it might be a good idea to review it once.

Let us consider the code sequence below

def fetchEmployeeName(id: Int) = {
    val z = throw new Exception("Employee not found")
    try {
      if (id > 0) "Jack" else z
    } catch {
      case ex: Exception => "John"
    }
  }

What would happen when we call this method with fetchEmployeeName(-1) ?

It would throw the exception “Exception in thread “main” java.lang.Exception: Employee not found”

Now, let us apply Referential Transparency to the above code. The new avatar of the code becomes

def fetchEmployeeName(id: Int) = {
    try {
      if (id > 0) "Jack" else throw new Exception("Employee not found")
    } catch {
      case ex: Exception => "John"
    }
  }def divide[A:Numeric](divident: A, divisor: A, div:(A,A)=>A): A = try {
    div(divident,divisor)
  } catch {
    case ex: Exception => 0
  }

What would be the output of fetchEmployeeName(-1) now? It would be “John”

Clearly exceptions are not referentially transparent. They are also context dependent. As you would notice in the above example, changing the context of the exception and bringing it inside the try block, changes the behavior of the logic.
Continue reading

Posted in Best Practices | Tagged , | 2 Comments

Scala in Business | Knoldus Christmas Newsletter – December 2014


Hello Folks

We are back again with December 2014, Newsletter. Here is this Scala in Business | Knoldus Newsletter – December 2014

In this news letter, you will see that how fast organisations are adopting Typesafe Reactive Platform to get best solution, how functional programming is getting into mainstream and how functional programming is becoming popular choice among the developers.

So, if you haven’t subscribed to the newsletter yet then make it hurry and click on Subscribe Monthly Scala News Letter

Screenshot from 2014-12-26 10:39:41

Merry Christmas and Happy New Year 2015 to you and your family from entire Knoldus Family. Happy Holidays !

ws_christmas_tree_1024x7681-copy1 (1)

Posted in Java, Agile, News, Cloud, Scala, LiftWeb, Akka, Spark, Amazon EC2, MongoDB, Clojure, Play Framework, Reactive, Non-Blocking, Future, NoSql | 1 Comment

Continuous Integration : Integrating BuildKite with Your Scala Project


In a very broad sense, BuildKite (earlier called BuildBox) is a continuous integration server that allows you to keep working on your code while there is a CI box which is reporting about any issues. The problem with most web based CI servers is that they have to do a lot of magic beneath the hood to let you build your custom system on their environment.

What does this mean? This means that if your product uses the Riak database and mine uses PostgreSQL then a web based CI system would have to give me default installations of both of them so that both our products can be supported. Remember this is just 2 databases that we talked about. Now, bring in more databases, more external integration’s and to cap it, different versions of all of these for different products. Suddenly having a hosted CI is not really the space you want to be :)

Contrary to this, BuildKite seems to have pushed down these problems back to the development team. Ok, this is not necessarily as bad as it sounds. What this means is that now you have control over your build environment. You know exactly what instances and what versions you would be working with.

Conceptually, it is quite similar to any other CI environment that you would have worked with.
You have a Project on which you want to run CI. This could be your GitHub, Bitbucket or any other repository project. You need to specify a file which would get executed when the CI would trigger. For example in our case the file is build.sh It is IMPORTANT to note that this should be an executable script and buildkite should have rights to execute this script.

Selection_011
Now comes the interesting part. Which is the Agents. Agent is responsible for running the CI job on your CI server. The agent polls Buildkite looking for work. When a new job is ready to run, the agent will run the bootstrap.sh script with all the environment variables required to run the job.
This script is responsible for creating the build directory, cloning the repo, running the build script, and uploading artifacts.

Agent would do the following for any repository that it works with as a part of setting up the environment.

$ mkdir -p "/home/vikas/.buildbox/builds/vikas-dev-agent/knoldus/akka-roller"
$ cd "/home/vikas/.buildbox/builds/vikas-dev-agent/knoldus/akka-roller"
$ git clean -fdq
$ git fetch -q
$ git reset --hard origin/master
HEAD is now at 87763ca Update README.md
$ git checkout -qf "87763ca0bd0bdf3e905d6bd545693a14350779fd"

Once it has downloaded the latest code onto your CI box then it would run the script that you have mentioned. In our case, it ends up being src/main/resources/build.sh

For example, refer to the diagram below. We have 2 projects and 2 CI servers. On one CI server we have the agent running for Project 1 and on the other, we have the agent running for Project 2. Now when BuildKite gets the trigger for building then it invokes the running agent on either of the CI servers as necessary.
Selection_012

What I like?

  1. Controlled environment – We can set up our environment
  2. Ease of use
  3. Has Bells and whistles like build passing badges etc

What I dont like?

  1. It should be free for public projects on Github

Our public project which has been integrated with BuildBox is present here.

Posted in Architecture, Devops | 2 Comments

Knox Session : Working With Slick 2.1.0


In this session I am going to discuss  how  can   effectively work  with slick 2.1.0

 

 

Working code

Enjoy With Slick !!!!

Posted in Scala | 2 Comments