Wednesday, October 8, 2008

Getting Started With OSGi

starting a new project at work we discussed the use of OSGi. i had read some articles about OSGi before but never got deep into it. but with gaining inside, i'm pretty amazed about OSGi now, as it makes it possible to write modular, flexible and dynamic applications in a way that wasn't possible before.

at work, the new project is a set of different collaborating web application using our usual stack of spring, hibernate and wicket. so we had a look at different platforms that supports this kind of apps, and we found that springsource dm server fitted our needs best. unfortunately some of our customers require us to run the apps on a websphere application server, so we had to drop springsource dm server and with it the whole OSGi solution was dropped. we all weren't happy, as all other solutions were so far behind OSGi.

anyway, i decided to learn more about OSGi for personal interest. currently i'm reading Neil Bartlett's free book OSGi in Practice. There aren't that many sources about OSGi at the moment, but although Neil's book is currently in draft state, it's quite worth a read. Thanks Neil.

Monday, September 15, 2008

sources of information

to stay up to date of what's going on mainly in the java space, different developers have different strategies. in this post i'll describe mine. my strategy consists of two parts. one part is keeping track of news and information coming from different sources, the other part is storing the important bits and pieces of information for personal use.


my primary source of information is reading news and blogs from the internet. i collect these news as feeds (rss or atom) with google reader. currently i have registered about 20 feeds, which i group in the first place by categories as 'daily feeds' and 'none daily feeds'. i try to read all the incoming 'daily feeds' every day, while i read the others when i get the time to. some of my favorite daily feeds are from dzone, theserverside, javalobby, slashdot, ibm developerworks and sun developer center. many of these news i mainly skip through - probably about 80 percent. of the remaining 20% there are sometimes some items which are especially interesting to me. i store these in google reader by marking them with a star. some posts on special topics (currently for example javafx or scala) i store in separate categories.


another information store is my google mail account. whenever i find any special articles outside of my news feeds or other documents/tutorials (e.g. in pdf format) i store links to these or the documents themselves in my google mail account. sometimes i also create link collections on a special topic. i store a set of keywords with each mail. currently i'm also trying out a personal wiki(mediawiki) for a better structuring of these collected information than it is possible with emails.


another source of information is, of course, books. i don't read books on a regular basis, but i read about three or four books a year. the latest were 'effective java', 'hibernate in action' and 'pojos in action'.


last but not least i listen to the java posse podcast, which is really great.


so all inall this might seem a lot of information. but actually i have a lot of time driving to work and home again by train.

Tuesday, September 2, 2008

Maven Integration Tests with Jetty and Selenium

in this post i'll describe how to configure maven to run integration tests with selenium and the maven-jetty-plugin. although this solution works, there surely are some points where it could be optimized (leave comments!). this solution assumes you have a maven-project configured for a web-application that can be run with the jetty plugin for maven. also, i assume that you have selenium/junit tests set up.

overview
this solution works in the following way:

  1. create two maven modules: one for the webapp, one for the integration tests

  2. install the war-file of the webapp to your maven repository

  3. in the integration test project set up the selenium/junit tests

  4. configure the surefire plugin to run the tests only in the integration-test phase

  5. use the maven-dependency plugin to download and unpack the war-file to the target directory of the integration-test project

  6. run the unpacked webapp with the jetty plugin

  7. start the selenium server with the selenium plugin

  8. run the integration tests



the details



set up the multi-module project

first of all, make your maven project a multi-module project in order to run the integration tests in a separate module apart from you ordinary junit tests. so you'll have three projects: a webapp-parent project, a webapp (war) project and a webapp-integration-tests (jar) project. for more information on setting up a multi-module project see the definitive guide to maven. here are excertps of the projects' poms:

webapp-parent pom.xml:

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.foo.webapp</groupId>
<artifactId>webapp-parent</artifactId>
<packaging>pom</packaging>
<name>WebApp Parent</name>
<version>1.0</version>
<modules>
<module>../webapp</module>
<module>../webapp-integration-tests</module>
</modules>
...


webapp pom.xml. the webapp-integration-tests is like this but packaging is jar:

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>de.foo.webapp</groupId>
<artifactId>webapp</artifactId>
<packaging>war</packaging>
<name>WebApp</name>
<parent>
<groupId>de.foo.webapp</groupId>
<artifactId>webapp-parent</artifactId>
<version>1.0</version>
</parent>
...


unpack the war file

in most cases the maven jetty plugin is used to run the the project it is configured for as a webapp. in this case we'll configure the jetty plugin in the integration-tests project to run the webapp project's generated war-file. to do so, the web app war has to be installed to your local repository (run mvn install). this war file can be fetched and unpacked in the integration-tests project with the maven dependency plugin:


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>dependency-maven-plugin</artifactId>
<executions>
<execution>
<id>get war</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>de.foo.webapp</groupId>
<artifactId>webapp</artifactId>
<version>1.0</version>
<type>war</type>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/webapp/webapp</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
...


set up the jetty plugin

first, tell the jetty plugin to run the unpacked war-file by placing a jetty.xml file in the src/test/resources folder of the integration-tests project which defines the webapp directory. you can use the default jetty.xml and set the webAppDir parameter to point to the target directory:
<Set name="webAppDir">target/webapp</Set>
. then in the pom.xml configure the jetty plugin to run in the pre-integration-test phase.


<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<daemon>true</daemon>
<webApp>target</webApp>
<jettyConfig>target/test-classes/jetty.xml</jettyConfig>
</configuration>
<executions>
<execution>
<id>start jetty server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run-war</goal>
</goals>
</execution>
</executions>
</plugin>
...


you can try maven integration-test or maven jetty:run-war to see if this works as expected (set the daemon parameter to false before trying this).




set up the selenium plugin

the selenium plugin then starts a selenium server in the pre-integration-test phase, too. keep in mind that the configured maven goals run in the order they were defined in the pom.

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>selenium-maven-plugin</artifactId>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>start-server</goal>
</goals>
<configuration>
<background>true</background>
</configuration>
</execution>
<execution>
<id>stop selenium server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop-server</goal>
</goals>
</execution>
</executions>
</plugin>


set up the tests

next, move your integration tests to src/test/java in the integration-tests project. these can be run with the maven surefire plugin. this has to be configured to run the tests only in the integration-test phase.


<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>false</skipTests>
</configuration>
</execution>
</executions>
</plugin>


that's it. call maven integration-test to run the tests.

Tuesday, August 12, 2008

effective java

recently i've put my scala sessions aside for a while, because i've had the chance to get the second editition of 'Effective Java' by Josh Bloch into my hands. Although i had read the first edition, the second edition struck me again. it contains some of the items already in the first edition, but also many (about 20 or so) new items, some of them regarding especially the new features of Java 5 and 6.

this book is still great.

won an eclipse t-shirt

for its latest release (ganymede) the eclipse foundation started a contest. they gave away eclipse t-shirts for the first reviews of the new release and i've won one for my post in this blog.

thanks to eclipse. this is my first developer t-shirt.

Thursday, June 26, 2008

scala functional programming

i continued learning scala reading the 'Scala By Example' tutorial from the scala-lang.org website. next up was functional programming in scala. in scala functions are first class citizens. that means you can declare functions and pass them around as parameters or return them from other functions. let's first see, how to define a function:

def plus1(a : Int) : Int = 1 + a

it takes an argument a of type Int and returns 1 + a (you guessed it). you can call this function like plus1(2). [you could have enclosed the body of the function in curly braces, but it's just one expression, so you don't have to]

so, now let's define a higher-order function. that's a function which takes another function as parameter or returns a function.

def doWithInt(a : Int, f : Int => Int) : Int = f(a)

this is a function that takes an Int as first parameter and a function type as second. the parameter f is a function of type Int => Int which takes an Int as parameter and returns an Int. this function simply applies the function f to the parameter a. you could call this function passing to it a reference to the function plus1 declared before: doWithInt(3, plus1). this will return 4 as it applies plus1 to 3.

so, these are just the basics of functional programming with scala. things can become quite weird when it comes to more complex higher-order functions, e.g. function returning functions, currying or recursion. have a look at 'Scala By Example' for examples.

one thing i forgot to mentioned is some syntactic sugar for the example above. instead of calling doWithInt(3, plus1) you could have equally done

doWithInt(3, 1 +)

this is because + is a member function of the type Int, as seen in a previous post.

so, that's it for this post then. more on functional programming with scala coming soon.

Wednesday, June 25, 2008

eclipse ganymede release

last week i downloaded a release candidate version of the new eclipse ganymede release and today the final version is coming out. i've downloaded the big bundle including modelling tools.

i like the new modelling features, especially because i've been waiting so long for some (free) support in that area in eclipse. all the uml plugins i've tried so far were either quite limited in their functionality or their usability. but now all sorts of uml diagrams are supported, e.g. class diagrams, activity diagrams, use case diagrams, etc. What's missing are some kind of reverse engineering features and sequence diagrams (or i haven't found them yet).

the new breadcrumbs feature shows the path to the current item at the top of the editor. it's like project -> src folder -> package -> class -> class member. each of these crumbs has a context menu which shows other items at the chosen level. i think that's quite nice, as it allows more context-sensitive working like mylyn task focussing, which makes it easier to find items than in the package/project explorer.

javascript support has been greatly improved. it now offers many features known from the java perspective. For example you can follow function calls by pressing F3 oder holding Ctrl and clicking on a method call. you can display the call hierarchy of functions and there is refactoring support, e.g. renaming functions.

one thing i'm still waiting for is a mylyn feature that allows for creating reports, e.g. a report that lists all completed tasks per week with estimated time and actual time. sadly, mylyn hasn't been improved that much, i think. but still it's a great feature.

these are just some of the new features and improvements. there are many smaller ones, like junit test excectution time, enhanced tooltips (e.g. javadoc on types), autocompletion, subversion support, etc.

one feature i haven't tried out so far is the ECF (eclipse communication framework), but that is one i'll try out for sure. from what i've read, it seems to support remote team collaboration, e.g. to share an editor. that sounds interesting.

Friday, June 20, 2008

quicksort source code

andres asked for the source code of the quicksort comparison.

Java
Scala
Groovy

Please give me some feedback

google syntaxhighlighter

recently i've found the google javascript syntaxhighlighter. it looks kind of like this - i can't get the blog to not to convert line breaks in a pre-tag to br-tags. but i think you can guess what it does ... and it's quite cool.

public static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

Tuesday, June 10, 2008

quicksort java scala groovy

today i compared the running times of the quicksort algorithm implemented in java, scala and groovy. i took the scala implementation from 'Scala By Example' [available on scala-lang.org] and based on that i wrote the other two. i ran a test on an array of 1.000.000 integers. the algorithms were not optimized for each particular language [but i compiled the groovy file at least] and i don't really know if these results are quite representative. but anyway, here are the running times:

java: 280 ms
scala: 359 ms
groovy: 535250 ms

Thursday, June 5, 2008

first look at scala

recently people are talking a lot about scala ('the next java'). so i decided to have a look at it. in this post i am not going to give an introduction to scala, but i will give some examples of the language features that show what scala can do for you. the examples are taken from the scala tutorial, which is available at scala-lang.org.

scala runs on the jvm and the syntax should be quite familiar to java developers. hello world in scala looks like this:

object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}

there are some interesting things about this. first, the object declaration means that HelloWorld is a singleton object. that is also why the main 'method' is not declared static. as HelloWorld is a singleton main is 'globally available' and doesn't need to be static. static members (methods) or fields do not exist in scala. second, main doesn't declare a return type. it's called a procedure method.

as said, scala runs on the jvm. because of that scala classes can interact with existing java classes. for example you can import and use java classes:

import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._

these import statements show that you can import multiple classes from one packge with that curly braces notation. and you can import all names from a package or class with the underscore notation. the above import makes members of DateFormat available, so you can call the static method getDateInstance() directly. Why not use the * notation for the imports? * is a valid identifier in scala. you'll see why, soon.

Methods with a single argument can be called in infix syntax in scala that means the method format of DateFormat can be called like in the following example:

val now = new Date
val df = getDateInstance()
val str = df format now

this call is equivalent to df.format(now). at the first look this is not quite an interesting feature but it offers some possibilities, one of which i'll show next. in scala 'everything is an object', e.g. primitive types as well. then remember that * is a valid identifier and the infix
syntax for methods. then look at this code and try to figure out how it works:

2 + 3 * 4 / 2

this is equivalent to

2.+(3.*(4./(2)))

all the integers are objects and the operators are methods of these objects. operators are method calls.

this was my first look at scala. more looks coming soon, for sure ...

Monday, June 2, 2008

staying up to date

so, recently i found some ways to stay up to date. first i'm reading news everyday on some news sites, e.g. theserverside, infoq and slashdot. then i found some interesting blogs on java.net, theserverside blog news, adam bien's blog and some more. and i listen to the javaposse podcast, which is very interesting.

Friday, May 16, 2008

far behind

hi, currently i feel so far behind. There's so much motion in the java community, so many new things i'm currently discovering and I'm trying to catch it all up. There are all these new languages that is talked about so much (e.g. Groovy, (J)Ruby, Scala, ...); some people even say that java is at its end. Then there is for example JavaFX that plays an important role at this years' JavaOne or there is the new SpringSource Application Platform or Google's Android.

This is simply too much. Seems like I have to wait and see what is really important.

Thursday, March 6, 2008

no time

hi, currently me an my family are pretty busy as we are planning to buy a house. so on my way home from work i'm thinking more about financial things than about grails. but i will go on soon, hopefully.

Wednesday, February 27, 2008

javascript is dirty

today a colleague tried to execute javascript with placeholders for a wicket component, no matter if it is rendered in response to an ajax request or in response to a normal request.

one possibility would be to append the javascript to the component markup. this way it would be executed whenever the component is rendered. but what about the placeholders in that script? he said, he could use wicket label components in the javascript that does not render its tag (e.g. a span) but only its body.

after all javascript would be a dirty language anyway (e.g. because of browser incompability), so why not try this dirty approach.

Friday, February 15, 2008

grails orm (gorm)

grails object relational mapping is so great. it`s based on hibernate and one of its coolest features is dynamic finders. a dynamic finder in grails is a method for querying data that does not exist up front but is created at runtime.

consider a domain model class user which has properties birthday and username. you can then query data calling User.findByUsername(uname) or User.findByUsernameLike(uname) or even User.findByUsernameAndBirthdayLessThan(uname, beforeDate) without implementing or even declaring these methods in any way - they are generated dynamically at runtime and this is mainly possible because of groovy`s dynamic features.

checking out grails

one week ago i started trying out grails and it`s really amazing how `convention over configuration` accelerates web development. a grails application consists of three (or four?) layers: a set of views and controllers (one for each domain model class), a service layer and a persistence layer which is integrated into the domain model (this is quite a new approach, i think).

the project starts creating an empty domain model class which actually grails creates for you from the command line. then you need to fill this class with its properties. grails can then create all other components (view, controller, persistence, tests, ...) for you either using scaffolding (i.e. components are generated at runtime) or by generating the according groovy files. the generated artefacts are created based on useful defaults (the basic crud actions are implemented) and can be customized by the developer later on.

all in all i`m very confident with what i`m able to do with grails after only one week. but i think what helped very much is that i`m a bit familiar with the frameworks used by grails (groovy/java, spring, spring mvc, hibernate). the next days will go on learning grails and have a deeper look into the details of the different layers.

beginning groovy

last week i started to learn groovy. i think i heard about groovy for the first time at the beginning or in the middle of last year and it´s getting more and more popular. at a certain point last week my curiosity had reached maximum, so i gave it a try.

groovy is a dynamic programming language that runs on the JVM and it´s quite easy to learn for java programmers. as it runs on the jvm you can use existing java libraries in groovy as well as you can use groovy scripts in your java applications which at the moment seems the main use case to me. mainly due to its dynamic features groovy offers more flexibility and an increased productivity opposed to java in some cases, which is probably the primary goal of groovy. in other cases you will probably miss some of java´s features (mainly static typing).

my first impression of groovy is that with some experience you can do simple things really fast, for example text/data processing. in this case groovy comes with really helpful features like improved support for regular expressions, closures, builders, native support of lists and maps, ranges and groovy strings.

maybe the next days i will get into details of some the coolest features of groovy, e.g. closures, and when i feel experienced enough i`ll try grails, a groovy web application framework.

first post

hi, my name is nick. i`m a software developer from germany and in this blog i plan to talk about web development, related technologies and IT in general.

in the first place i start this blog as a personal blog to record the history of my (hopefully) growing experience in software development. my background: i started web development about one year ago. so my experience in this area is not quite big. but maybe this is why this blog could become interesting.

currently my company is developing web applications using java. we use wicket for the front end, spring for the business tier and hibernate for persistence.