In case anyone needs this: https://gist.github.com/1377952
You’ve got to bootstrap it in some bootstrap job like so:
ScalaCacheAccessor.set(new MongoCacheImpl)
It’s only tested with the Press module so far (shared state is evil anyway
)
In case anyone needs this: https://gist.github.com/1377952
You’ve got to bootstrap it in some bootstrap job like so:
ScalaCacheAccessor.set(new MongoCacheImpl)
It’s only tested with the Press module so far (shared state is evil anyway
)
Since I wrote my previous post on how to use Play! Scala with Jerkson, I was asked a number of times how to make use of custom de/serialization for classes. This happens when e.g. the case class you want to de/serialize has a POJO or another complex type that you’d like to have more control over, such as e.g. the BSON ObjectId or Play’s Anorm PK.
Here’s a wrap-up on how you can easily create custom de/serializers and use them within your Play! Scala application. Let’s take the example of the BSON ObjectId, which you will most likely run into if you use MongoDB in the backend.
First, we need to create a Jackon serializer and de-serializer for the org.bson.types.ObjectId type:
import org.codehaus.jackson.map.annotate.JsonCachable
import org.bson.types.ObjectId
import org.codehaus.jackson.map.{DeserializationContext, JsonDeserializer, SerializerProvider, JsonSerializer}
import org.codehaus.jackson.{Version, JsonParser, JsonGenerator}
@JsonCachable
class ObjectIdSerializer extends JsonSerializer[ObjectId] {
def serialize(id: ObjectId, json: JsonGenerator, provider: SerializerProvider) {
json.writeString(id.toString)
}
}
class ObjectIdDeserializer extends JsonDeserializer[ObjectId] {
def deserialize(jp: JsonParser, context: DeserializationContext) = {
if (!ObjectId.isValid(jp.getText)) throw context.mappingException("invalid ObjectId " + jp.getText)
new ObjectId(jp.getText)
}
}
This is pretty straightforward: when serializing, we just print out the string representation of the ObjectId via toString(), and when deserializing we check if we have a valid ObjectId and then create an instance given the text representation (or throw an exception if we don’t get a valid one).
Now all what is left to do is to register this de/serializer couple. For that purpose we can extend Jerkson’s Json trait that gives us access to the mapper and register a new Jackson SimpleModule:
import org.codehaus.jackson.map.module.SimpleModule
object CustomJson extends com.codahale.jerkson.Json {
val module = new SimpleModule("CustomJson", Version.unknownVersion())
module.addSerializer(classOf[ObjectId], new ObjectIdSerializer)
module.addDeserializer(classOf[ObjectId], new ObjectIdDeserializer)
mapper.registerModule(module)
}
And that’s all that is needed. If you are using a custom action in Play! in order to generate Json, make sure to update it so that it uses the CustomJson object we just created:
trait JsonAction { self: Controller =>
def Json(data: AnyRef): Result = new Result() {
def apply(request: Request, response: Response) {
val encoding = getEncoding
setContentTypeIfNotSet(response, "application/json; charset=" + encoding)
response.out.write(CustomJson.generate(data).getBytes(encoding))
}
}
}
Update: If you need to have custom de/serialization of some types, check out this new post
I’ve been working with Play! Scala for a couple of months now, and the experience is really good – the combination of the Scala language and the sound design principles of Play! make web-development a real pleasure.
One of the aspects I have been fighting with for a while (and which had initially put me off writing my first bigger Play! application with Play! Scala) is the lack of simple support for JSON de/serialization. With simple I mean the possibility to turn a case class into a JSON string and back, and to additionally have access to a couple of more advanced features that are often required (e.g. implementing custom serialization of specific types).
From the recent development on the Play! Scala Github repository, it looks as tough sjson will be used as the integrated library with the framework.
I have tried out sjson and my experience with it wasn’t all that good, I outlined the issues I had with it (mainly that it doesn’t offer support for custom de/serialization of Java beans) in this post to the Play! mailing-list.
The alternative I found bears the sexy name of Jerkson and is developed by Coda Hale. It is a scala wrapper for Jackson which supports all kind of features you may want from a JSON library.
The latest release now makes the integration of Jerkson very easy, as it addresses two issues that happened in the Play! context: it finds the custom classloader Play! uses, and allows to deserialize case classes with more than one constructor (this can be problematic in some cases, and Play! adds an empty constructor to case classes, necessary for e.g. the integration with SnakeYML).
Since the releases are available via Maven, it is possible to easily get Jerkson into a Play! Scala project by adding the following dependency to dependencies.yml:
- com.codahale -> jerkson_2.8.1 0.4.2-SNAPSHOT:
exclude:
- org.scala-lang -> scala-library
repositories:
- codahale:
type: iBiblio
root: "http://repo.codahale.com/"
contains:
- com.codahale.*
And… that’s it. You can now use Jerkson like this (don’t forget to run a play dependencies):
import com.codahale.jerkson.Json._
case class Foo(bar: String, baz: Int)
val serialized: String = generate(Foo("bar", 42))
val deserialized: Foo = parse[Foo](serialized)
Additionally, you can use a trait to mixin with your controllers in order to make JSON serialization look more integrated with other actions:
trait JsonAction { self: Controller =>
def Json(data: AnyRef): Result = new Result() {
def apply(request: Request, response: Response) {
val encoding = getEncoding
setContentTypeIfNotSet(response, "application/json; charset=" + encoding)
response.out.write(Json.generate(data).getBytes(encoding))
}
}
}
Two months ago we started using the Play! framework for our work at Oxiras (as I am writing these lines, the Oxiras website is still totally empty, this should change soon however).
As we started making UI tests using the excellent integration with the Selenium test runner, we quickly noticed that also having an integration with the Selenium IDE plugin would make our lifes much easier. So I looked a bit around, found that creating a Selenium IDE formatter was not all too hard and here we go with what I can call my first Firefox plugin.
So far, there’s no update site defined as the GitHub pages are not available via HTTPS, so this will have to wait until I set up SSL on some machine this is now set-up on the oxiras domain, which means updates will automatically be detected.
If you’re using the plugin, feel free to contact me for feedback / comments / bug reports!
Edit
This morning I spent some time looking for information on how to deploy a legacy webservice on Apache Karaf.
This post isn’t about Karaf and OSGi and webservices though, but a reflection on how to efficiently find information that can lead towards a specific technical problem on the Internet. For quite some time now I feel that this task has become much more complicated in the past 2-3 years than it used to.
We are now in 2011 and at least in my perception, Google isn’t anymore a simple, efficient nor even effective way of finding information regarding specific problems. Which is an euphemism for “Google really sucks these days”. Type in a query, click on a link, search again for something rather different and you’ll see one of the results you clicked earlier appear in the results of your new query (which is related to the previous query by maybe only one keyword, two at most. What’s worse is that in many cases, the page that Google lists as result don’t even contain one of the keywords entered in the query, but have something to do with sites I browsed in the past. Seriously Google, why should I care about things I searched for in the past in a new query?
And yet, especially when it comes to software development, finding sound information about a problem is critical. Way too often I find myself rushing to Google and searching for a solution, then finding some random piece of information and trying to get something out of it – i.e. starting to work on a solution on a shaky ground, even though I perfectly know I’m on shaky grounds (intuition is very helpful there).
So there’s this one issue of finding high-quality information. There’s another, much bigger problem though: understanding the problem to look for the right solution. Oftentimes do I rush into looking for a solution when I haven’t yet understood the problem (or think I do, but really am just headed in the plain wrong direction).
At this point, I shall point to The Five Orders of Ignorance by Phillip G. Armour. If you haven’t read it and are in the business of writing software, do so. Now.
For the sake of the completeness of this post, I’ll list the Five Orders anyhow. They go like this:
So how does that relate to the information search issue from before? Well, as I pointed out, oftentimes a problem when looking for information is the lack of clear understanding of the problem I try to solve. So this is – as far as I understood the 5 Orders – a 2OI: I lack the awareness that I do not understand (know) the problem for which I am looking for a solution.
So how to get from 2OI to 1OI? Of course this is situation dependent, but I would say that when you have some domain knowledge things are much easier. In software development, there’s one type of problems which I call “configuration problems” (although they could probably also be called “integration problems”) and that go like this:
and involve one or more technologies of the many, many technologies that are available in the open-source space.
Whenever I get to one of these questions (which appear to be 1OI-type-of-questions), I try to step back and ask myself: does that even make sense? Isn’t there a deeper problem I actually want / need to solve? I say that I “try to” step back because in some cases I either forget and rush into finding a solution to the wrong problem, or I know or feel that this is not the right question, but have a personal itch to get things to work anyway (“I really want Spring DM 2 to work on Karaf! It just has to work!”).
Now let’s go back to the initial issue described in this post (Google sucks): how to find answers to technical questions on the Internet these days?
When I think of not using Google, I am now trying the following:
Allright, that’s it. Let’s hope Google fixes their search engine, or offers a paying high-quality service for search (I would pay for that).
In a project I’m currently working on we use the OSGi platform in order to achieve higher modularity (the project is about building a framework for massive data ingestion and processing for a high-availability service, and the processing is dealt with via workflows that are composed of a number of plugins that we don’t develop, hence the need for modularity).
We use the Apache Karaf framework, which offers neat extension points and means to customize it. One of the nice features is the WebConsole that can also be extended, which would provide the means to add a GUI for the users. However, this happens through providing a Servlet implementation, which isn’t really handy these days when wanting to develop a bit more complicated GUIs.
Since I’ve got some experience with Google Web Toolkit I thought of using this instead for coding the GUI (and eventually integrate it with the WebConsole later on, not sure at which level of depth yet). And the first thing necessary in order to do this is to get the GWT application to run on the Karaf platform.
Here’s how I managed to get it work:
I used the gwt-maven-plugin archetype in order to create a new GWT module. At the time at which I am writing this post, I had some issues with the unit tests that are generated by default, so I just trashed them for the moment.
Karaf can leverage Pax Runner in order to deploy OSGified WARs. For that, you need to have a MANIFEST.MF generated and attached to the WAR maven generates. There’s two things required for this.
First, configure the Apache Felix maven-bundle-plugin to generate the manifest for you (you could do it by hand, but why bother):
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
</execution>
</executions>
<configuration>
<supportedProjectTypes>
<supportedProjectType>jar</supportedProjectType>
<supportedProjectType>bundle</supportedProjectType>
<supportedProjectType>war</supportedProjectType>
</supportedProjectTypes>
<instructions>
<Bundle-ClassPath>.,WEB-INF/classes</Bundle-ClassPath>
<Embed-Directory>WEB-INF/lib</Embed-Directory>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Import-Package>*;resolution:=optional</Import-Package>
</instructions>
</configuration>
</plugin>
This will generate a manifest which takes into account the required dependencies that come with the WAR, so that it can be deployed by Pax Runner. Note that this isn’t just a configuration for GWT itself, but in theory for any kind of WAR.
Update: I figured out how to customize the webapp name, by adding the following in the manifest instructions:
<!-- OSGi spec config option -->
<Web-ContextPath>gui</Web-ContextPath>
<!-- Pax Runner knows this option -->
<Webapp-Context>gui</Webapp-Context>
Pax Runner does not understand the OSGi spec <Web-ContextPath> instruction, so it needs to be given the <Webapp-Context> instruction.
Next, you need to tell the maven-war-plugin to add the generated manifest file to the WAR:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
...
<configuration>
...
<archive>
<!-- add the generated manifest to the war -->
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
...
</plugin>
Fire up the Karaf console and type:
features:install war
This will activate everything needed in order to deploy WARs on the platform.
Now you just need to deploy the WAR on Karaf. You can do this in several ways:
Since we already had a feature descriptor (because we have many bundles that need to be deployed, I just added the artifact to the feature descriptor XML like this:
<?xml version="1.0" encoding="UTF-8"?>
<features name="uim-features">
<feature name="uim-core" version="1.0.0.SNAPSHOT">
...
<bundle>mvn:eu.europeana/europeana-uim-gui-gwt/1.0.0-SNAPSHOT/war</bundle>
...
</feature>
</features>
Note that you need to explicitly reference the “war” classifier, or this won’t work. Now, re-install the feature in order for the changes to take effect.
You should see something like this (amongst others) in the log:
14:42:50,487 | INFO | l Console Thread | WebXmlObserver | nder.war.internal.WebXmlObserver 128 | 581 - org.ops4j.pax.web.pax-web-extender-war - 0.7.3 | Using [gui] as web application context name
That’s it, you can access your GWT module at http://localhost:8181/<web app name>
I recently started using IntelliJ IDEA in addition to Eclipse after seeing it being used by some dutch colleagues of mine. And I must say that I do like it more and more every day — it just has all these many little things that fill so well to Java.
One thing I do miss though is the ability to debug ANT tasks. In Eclipse, you can make an external launcher, select one or more targets, and run them in debug mode. With IDEA, that just doesn’t seem to work. I did install the ANT debugger plugin which makes it possible to debug through the build file, but that’s not still what I wanted.
So here is a workaround I found, until the folks at JetBrains or HandyEdit will figure it out: make a launch configuration that directly invokes ANT:
org.apache.tools.ant.launch.Launcher
-buildfile your-build-file.xml your.target
Et voilà, now you should be able to debug your ANT task execution.
“Day 4. The headache is bad, the coffee tastes bitter, but I still keep on fighting.”
This is what a diary of mine would sound like, if I were keeping track of my journey of becoming an early riser.
I got inspired by this post and decided to try out if I could get up earlier. Doing the switch from getting up at around 8 AM to 6 AM when the summer time started (effectively getting up 3 hours earlier each day) doesn’t make it any easier. Let’s see how it goes!
Have you ever written a CV as part of a lecture at university (yes, in France, there are such lectures), a seminar for young graduates, or have you ever gotten feedback from the professionals in the branch?
I did. It always ended up with some frustration – all the small pieces of creativity would get marked in red as “too unusual”, and in the end my CV (the one through which I got into my current job last year) looked very standard (or as some may say, professional).
Last week, I finally quit my full-time job (this will be effective by the end of September). The main reason is that I can’t keep up with the slow pace and the lack of meaning (see my last post on the topic). Now, I am looking for a part-time job that will help me pay my bills.
That’s when I decided to do this (many thanks to all of you who gave me precious feedback):
It clearly is not exactly what you’d expect a CV to look like, but I have the feeling that it does what a CV ought to do: transmit a powerful message about the author. In fact, while I was writing this post, I got a call from a company which I wrote an application to one hour ago (and I have an interview with them in 3 hours).
So I’m not saying that you have to climb up to Preikestolen and shoot a cool photo to use it as background for your CV (that is, if you have time, it’s worth going there – unless if you have vertigo, as the cliff behind me goes down 600 m straight). In fact, I believe that using your own creativity or “sense of self” is the key to making a CV that will get you a job that fits (or at least, the first step to it).
In this post I’d like to share some toughts about my struggle to find a meaningful job – or first off, to really define what a meaningful job would be for me.
For almost a year now, I am working for one of the big mobile telco’s in Austria, in the IT department. The work there is everything but what I was hoping for: not really challenging, not innovating, and certainly not international (there are sometimes meetings in other countries, but the day-to-day work is not anything like the international teamwork that can be experienced in BEST).
When starting to look for a job last summer, I did not really know what I was looking for. So my job goal at the time was:
Challenging work in an international IT environment, where creativity, analysis, and problem-solving skills are used for innovation.
And somehow, I got convinced that this would be the case during one of the several job interviews I had with my current employer – which didn’t turn out to be the case, or at least, not to my understanding of challenging, international and innovation.
What I have been trying to do since is to figure out how to get a more precise grip on what I really like in a job. One good hint that you might want to try is to do the value game, which can give you a good clue about what you really value in life – if that is not somehow corresponding to what your company values or what your job gives you, then there might be a chance that you do not enjoy it that much.
Yet, values alone are not much of a help when it comes to finding a “fitting” employer, as job offers usually do not contain a list of values the company shares – or if it does, there is a big chance that those values are more oriented towards the marketing than towards the real organisational values of the company.
Recently I came accross an article on generation Y. Whilst the topic of studying generation Y is not really new, I found from disucssions with many of my friends (which are of generation Y – the generation is usually defined as being born between 1982 and 2001) that they have the same struggle to find a meaningful job for them.
So what is it, that our generation shares regarding their understanding of “meaningful job”? The article depicts three main expectations:
Reflecting on my own experience in BEST, I guess that these three reaons were probably some of the elements that I took as granted, and that simply lack in many companies. Especially the work on IT systems that are being used by real users, and being in touch with those users, makes the voluntary work in that organisation very rewarding.
There is tough another element which I personally would like to see on this list, and tough you might argue that it is somehow part of the “more feedback” expectation, I believe that it is too crucial to be left out. This element is teamwork: many job positions will emphasize the importance of good teamwork skills, but really, a big part of knowledege work (and I think that this is even more true in the IT field) is individual work. Probably the jobs that involve most teamwork are the coordination or project management positions, but for most of the part, the work is to be done alone in front of the computer screen. And doing this all the time, 40 hours a week, is just plain boring, if you ask me.
There’s a last interesting thing I noticed from talking with people around me: it seems that many people who don’t really know what they want to do as a job after graduate studies go for doing a PhD, as a means for delaying the decision-making. Yet, and paradoxically, doing a PhD requires a big amount of internal motivation and real passion for the research topic at hand, or it can become very tiresome (and to some extent, meaningless). Being myself considering to pursuing PhD studies, my main fear is that this is a work that requires a lot of individual effort, whilst I am looking more fore the “teamwork” aspect at the moment.
So these are the considerations I reached until now. What really amazes me is that really many of the people of my age that I am talking with face the same kind of struggle – not really being fullfiled by their day job, but not really finding a place where those conditions would be satisfied and that would also bring some kind of salary. My belief is that there is going to be some shift in society regarding the nature of knowledge work, and my feeling is that our generation is going to play a big role in it.