lørdag 29. november 2014

TheArtOfBeingTheBest.com – Be the Best Web Developer you always wanted to be.

Being the best means being interested. It means knowing a lot. Being the best means you have passion. You will read articles about how to become passionate and you will read about web development. You will read everything you need to know about to be the best Web Developer.

mandag 18. november 2013

How I created testdata for a Clojure based statistic application

Currently I am working on a Clojure based statistic application showing the usage of a certain service we have in our netbank. 

The first step I wanted to have solved was to become independent from the database inside the bank. So I had to get me testdata. There were two options for me. 
The first one was to write a simple testdata generator. The disadvantage with that was that I would have spend time on building an application to be able to build the real target application. It would have cost me too much time even if I have plenty of that :) 

But I found my solution - the other option :) http://www.generatedata.com.
For my project the online testdata generator worked perfectly right in that sense that I could easily genate some testdata I can use for building my application and test it roughly.

søndag 27. oktober 2013

Starting Clojure - Web Application Intro with REST (Sven Malvik)

How to develop a Clojure Web Application with REST support. This demo uses Ring, Compojure and Hiccup for the Web Development part and Leiningen as the Project Manager. It is a short tutorial and meant to be a starting point for those who are new to Clojure.

All the code to this video are available on my GitHub account.

Here comes a step by step tutorial for this video. It is the result of my notes for it.

How to write a Standalone Clojure based Web Application with an embedded Jetty Server

This blog entry is the result of my notes for the video "Starting Clojure - Web Application Intro with REST". It is meant to be a starting point of web application development with the Clojure language.

The Clojure syntax

Clojure is a dynamic programming language that targets the Java Virtual Machine. Clojure treats code as data and we write it in form of a list. Let's say we want to calculate (3 + 4). What most compilers do is to convert code expressions to an Abstract Syntax Tree (AST).  
The result of (3 + 4) would look like this.

Abstract Syntax Tree (AST)
In Clojure we write that AST directly in form of a list. (3 + 4) becomes (+ 3 4). So, if we would transform ((3 + 4) - (5 * 9)) into a Clojure list we would write it that way:
(- (+ 3 4) (* 5 9))

Web Application

Before we start we need leiningen. Leiningen is for automating Clojure projects (https://github.com/technomancy/leiningen).

- Create a project

Switch to your workspace and create a project using leiningen

> lein new mybank

I will just create an idea project as well.

- Add a web server

Inside your newly created project you will find a project.clj file where all the dependencies will be declared.

Here is how project.clj looks like after we created it.

(defproject mybank "0.1.0-SNAPSHOT"
 :description "FIXME: write description"
 :url "http://example.com/FIXME"
 :license {:name "Eclipse Public License"
           :url "http://www.eclipse.org/legal/epl-v10.html"}
 :dependencies [[org.clojure/clojure "1.5.1"]])

Check the version of Clojure if it is the one you want.

- Add now ring and compojure to your project file by adding these two libs to the key :dependencies
[ring/ring "1.2.0"]
[compojure "1.2.0-SNAPSHOT"]

Ring is a Clojure web applications library inspired by Python's WSGI and Ruby's Rack. By abstracting the details of HTTP into a simple, unified API, Ring allows web applications to be constructed of modular components that can be shared among a variety of applications, web servers, and web frameworks. (https://github.com/ring-clojure/ring)

Compojure is a small routing library for Ring that allows web applications to be composed of small, independent parts. (https://github.com/weavejester/compojure)

Leiningen offers a ring-plugin that automates common Ring task like starting a development web server
- Add ring-lein configurations to your project file.

:plugins [[lein-ring "0.8.7"]]
:ring {:handler mybank.core/my_routes
      :auto-reload? true
      :auto-refresh? false}

We just declared the my_routes as the entry point for our web server in the namespace core.
- Now we have to define my_routes in core.clj.

That’s how core.clj looks like before our changes.

- Define my_routes. Whenever we request for / we call the function foo.
(defroutes my_routes
 (GET "/" [] (foo "Jim"))
 (route/resources "/"))

The foo function does not work properly yet because it prints just to the console. 
- Let foo return a string by replacing println with str.

Here is a simpler version of foo.
(defn foo [x] (str "Hello, " x))
The namespace route is still unknown in core
We can change that by adding them the key :use in :ns and tell clojure that we only require compojure.route

(ns mybank.core
 (:use compojure.core)
 (:require [compojure.route :as route]))

- Resolve our new dependencies by executing
> lein deps

- Start the server.
> lein ring server 3000

- Replace the whole foo function call with (view/index-page) and
add [mybank.view :as view] in :require

- Delete the foo function

- Create a new file mybank.view.clj
(ns mybank.view)
(defn index-page [] "Bla")

- Restart the server
lein ring server 3000

- Include hiccup deps in the project
[hiccup "1.0.4"]

- Add this to :ns in view
 (:use hiccup.page hiccup.element)

(defn index-page []
    [:body "bla"]]))

- Restart the server lein ring server 3000

Want to add a REST service?

- Add [clojure.data.json :as json] in :require in core
- Add ring.middleware.params in :use in core
- Add [org.clojure/data.json "0.2.1"] in project
- Add this to my_routes
(GET "/rest" [] (json/json-str {:email “sven@malvik.de”}))

- To make it work we have to wrap my_routes
(def app (wrap-params my_routes))

- Let’s inform the project as well and change.
:ring {:handler mybank.core/my_routes to
:ring {:handler mybank.core/app

Ok, let the party begin.
- Restart the server.
> lein ring server 3000

Everything is ok? Stop the server again and let’s move on and deploy our very first clojure webapp.

- Stop the server for now.
Now, try lein run in the your console and you will get an error saying that you have to declare a main function. No :main namespace specified in project.clj.

- Let’s therefore add the main function in core.clj
(defn -main [& args] (run-jetty my_routes {:port 8080}))

- We have to tell the project where the main function is located by adding this to the project file :main mybank.core

- Add ring.adapter.jetty to :use in core as well.
(:use compojure.core ring.adapter.jetty)

- Try
> lein run

- Add (:gen-class :main true) to :ns in core.

lein clean & lein compile & lein uberjar
java -jar target/mybank-0.1.0-SNAPSHOT-standalone.jar


For the full code you can visit me on GitHub. The GitHub version also includes HTML templating with hiccup and AngularJS and how to connect to a database: https://github.com/svenmalvik/ClojureWebAppTemplate

torsdag 12. januar 2012

JBoss in 5 minutes (from Installing to Deployment)

In this 5 minute JBoss Quickstart Demo I will show how to install, setup, start, stop your JBoss AS7 in JBoss Developer Studio and deploy a simple dynamic web application.

onsdag 11. januar 2012

Migration my Playframework 1.0 project to version 2.0

I just worked on the migration from my little CMS4Play project which is based on the playframework  version 1.0 to the beta version 2.0. It was not as easy as exspected, unfortunately. Classes were moved, parameters became methods and of course the different template engine. All in all I can say that I would follow the advice Guillaume Bort gave:
"Remember: Play 1.2.3 is still the best way to write Web applications in Java while Play 2.0 is still under development, and will continue to be maintained and developed by the core team and contributors. Play 2.0 is an opportunity to move to for your next project, or the next major rewrite of your application."

fredag 6. januar 2012

Seam via JBoss ESB Quickstart

This Quickstart Demo shall demonstrate how to get access to a Seam Web Application through the JBoss Enterprise Service Bus.

torsdag 22. desember 2011


I just was wondering whether going for SOAP or REST. So I did a short research. Here my conclusions:

SOAP has standards and is designed to promote:

  • Interoperability (WS-I),
  • Security (WS-Security) and
  • Reliability (WS-Reliability).

When interacting with a SOAP service you know what to expect - it's always the same: WSDL, XML, SOAP envelopes/headers.

REST promotes no standards that describes the format of data being exchanged.
POST? GET? Query string? JSON? XML? TEXT?
Interface is never the same. Data formats are never the same.

SOAP helps preventing the flu. If you do get the flu, then you need REST.
Writing an iPhone application to interface with my bank I would need to go with SOAP.
Else, REST is just fine.

External Resources:
SOAP vs REST: The war between simplicity and standards

onsdag 14. desember 2011

JBoss SOA-P Quickstart Demo with Seam

Just made a JBoss Enterprise SOA Platform Quickstart Demo with a Seam Application.

søndag 28. august 2011

Farlig med iPad

... og så kom engelen, svevende, hvisker at det er på tide å våkne for en ny dag og nye spennende muligheter. Jeg snudde meg mot vinduet hvor ipaden min lå, og slå av engellyden på wekkerklokke-appen.
Deilig å sveve i drømmehimmelen.

Jeg går inn på kjøkkenet, lager meg en kopp kaffe, og åpner Smart Coveren på ipaden. Tre sekunder senere ser jeg Googles akjsekurs med +1.5%. Det er gode nyheter tenker jeg. For se om vg.no og E24.no har like gode nyheter. En drøy kvarter senere forlatter jeg huset med retning mot kolbotn tog stasjonen. Jeg var sent ute. Burde kanskje ikke ha sett hele den ti minuters filmen da jeg var på badet. En venn av meg som jobber for IBM tipset meg om den på Google+.
Kjekt å leve et sosialt liv.

På toget tar jeg ipaden ut av vesken min for å jobbe litt med presentasjonen jeg skal holde i ettermiddag. Når den ligger på fanget føles det som å holde en 100 siders hardcover bok. Størreslsen er på 18,57 cm ganger 24,12 cm. Vekten er på 613g og er dermed omtrent like tung en eller annen del av raketbilen ThrustSSC som ha den enorme hastigheten på opptil 1228 km/t.
Avslappende å bære nesten ingenting.

Ipadens enorme hastighet skyldes bl. a. Apples raske A5 prosessoren. En annen grunn er sikkert også at man ikke får muliheten til å kjøre widgets. Widgets er små, delvis nyttige programmer, som du kjenner fra desktop-en din og viser en f. eks. nyeste Facebook meldinger, værmeldinger eller klokke. Tenk deg at du i tilleg også kjører flere apps som epost-klient, nettleser og et tekst programm i parallell. Hvert eneste programm som er aktivt trenger spiser en del ressurser. Ipadens hastighet ville i det verste fall går ned mot null.
Sunt å spise lite.

90.000 apps finnes for tiden i Apple sin AppSore. Det er så enkelt å få nyeste programmvare på ipaden. Er man engang misfornoyd, så er det bare å slette den igjen. Det tar et sekund.
Interesant å bruke tid på viktige ting.

Ipaden er forholdsvis sikker mot virus. Grunnen til det er at brukeren ikke får administrasjonstilgang til systemet. Det er altså ikke mulig å installere programvare som endrer systemet. Tenk deg et skadelig program du nettopp har lastet ned. Uten administrasjonsrettigheter vil den ikke kunne endre og skade ditt system. Ulempen med det er at brukeren ikke får installert programvare heller som trenger disse rettighetene og ikke er skadelig. Vil du f. eks. kunne lese hva som foregår på nettverket du surfer i, må du jailbreake ipaden - for altså lov til å gjøre alt du vil på din ipad.
Har du etterpå fått deg tilgang til systemet, må du være værsom. Du har nettopp mistet garantien.
Godt å leve farlig.

Ipaden kan og er et apparat som kan og skal underholde deg. Ta på øreklokkene, sette deg på tog og begyn å lage musikk. Det finnes mange, gode musikk-apps. Du kan også se film på den eller male bilder, lese dokumentasjon, redigere pdf'er, skrive mail, osv. Men først og fremst kan man surfe med den og lese om verden. De fleste har selvfølgelig også egne apps som er tilpasset til din ipad. Når du elsker å spille, så er det bare å kjøre på med f. eks. flysimulator, racerspill, sjakk, osv.
Befriende å leke seg.

Å jobbe på ipaden er som å sitte foran den stasjonære pc. Med Citrix appen er akkurat dette mulig. Oppløsningen og tastaturen er greie nok for å kunne være nesten like produktivt. Når jeg jobber via Citrix pleier jeg å sjkulle vinduene og sidepanelene som ikke nødvendigvis trengs. Appen har en rekke gestures som la deg styre datamaskinen din veldig kjapt. Det finnes også mange andre apps som la deg fjernstyre enkelte programmer på din stasjonære datamaskin.
Smart å lære hvor som helst.

Ipaden er et fint apparat, og jeg vil ikke miste det. Snart, og det er jeg overbevist om, vil den erstatte hjemme pc-en.