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 halyph.com. 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(System.in.text)'

{
    "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(System.in.text)'"
$ 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(System.in.text)
} catch (JsonException e) {
  println "ERROR: Not valid JSON"
  System.exit(1)
}

$ 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
java.io.NotSerializableException: com.MyClass
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1180)
 at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1528)
 at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1493)
 at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1416)
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1174)
 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)
 at java.util.ArrayList.writeObject(ArrayList.java:710)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 at java.lang.reflect.Method.invoke(Method.java:601)
 at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:975)
...
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
References

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.


References: