Friday, September 12, 2014

Migrated to another blog engine

Hi All.
I'm not going to publish any new post in this blog. 

I've completely migrated to All new posts will be published there.

Wednesday, August 13, 2014

Groovy Notes: Pretty print JSON from the command line

JSON pretty print it's common task while working with JSON from command line. There are many ways of doing this via Python, Ruby, node.js, but here I'd like to concentrate on Groovy one-liner:

$ echo '{"foo": "lorem", "bar": "ipsum"}' | groovy -e 'import groovy.json.*; println JsonOutput.prettyPrint('

    "foo": "lorem",
    "bar": "ipsum"

We can slightly improve this one-liner via adding shell alias:
$ alias pp="groovy -e 'import groovy.json.*; println JsonOutput.prettyPrint('"
$ echo '{"foo": "lorem", "bar": "ipsum"}' | pp

Also, we might use Groovy script which might be handy for simple JSON validation also:

$ cat prettyJson.groovy 
import groovy.json.*

try {
  println JsonOutput.prettyPrint(
} catch (JsonException e) {
  println "ERROR: Not valid JSON"

$ echo '{"foo: "lorem", ' | groovy prettyJson.groovy
ERROR: Not valid JSON

Tuesday, August 12, 2014

How to disable Tomcat session serialization

Suppose you have the next error while restart/stop Tomcat (in my case Tomcat 7.x):
WARNING: Cannot serialize session attribute SPRING_SECURITY_CONTEXT for session 54DBA076EDC9B7A24C1AF76824DFD1EF com.MyClass
 at java.util.ArrayList.writeObject(
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(
 at java.lang.reflect.Method.invoke(
It means that Tomcat is trying to persist non-serializable objects which were added into the session. Tomcat uses Manager component which is used to create and maintain HTTP sessions for web application.

By default manager implementation configured to perform session persistence across restarts and we want to disable this functionality.
To disable this persistence feature, create a Context configuration file (context.xml) for your web application and add the following element there:

<Manager pathname="" />

You can add this context.xml file to:
  • App.war:/META-INF/context.xml
  • TOMCAT_HOME/conf/Catalina/localhost/App.xml file
  • TOMCAT_HOME/cont/context.xml

Wednesday, July 23, 2014

Review: Programming Groovy 2: Dynamic Productivity for the Java Developer

Programming Groovy 2: Dynamic Productivity for the Java Developer
Programming Groovy 2: Dynamic Productivity for the Java Developer
This book is well balanced around Groovy language. I would say there is minimal required knowledge regarding the language. The author (Venkat Subramaniam) fluently covered every important aspect of Groovy. But this book is for experienced developer. The accents were made only in scope of Groovy language and totally avoided any "intro" programming talks, which is actually cool.

Here is the book's structure:

  • Beginning Groovy - this part is dedicated to Groovy language features, especially from Java perspective. Reading this part I was building the parallel to Ruby trying to compare these languages. The main difference which I found it's only syntax "sugar". I.e. conceptually these two language are very similar. 
  • Using Groovy - this is a pragmatic part where were highlighted the next topics: Groovy GDK, XML Processing, working with DB,  working with Java.
  • Dynamic features (MOPing Groovy) - here were highlighted two main metaprogramming approaches: run-time and compile-time metaprogramming. Very interesting part. For me Groovy metaprogramming facilities are much clear comparison to Ruby metaprogramming.
  • Using Metaprogramming - it's pragmatic part where were highlighted the applications for Groovy metaprogramming features. It's very important to note that Groovy contains many pre-bundled classes/modules which are really nice samples of metaprogramming Groovy's features.
I highly recommend this book to everybody who is interested in Groovy language. This book highlights Groovy dynamic features.