Friday, July 11, 2008

Minor update for StaxMate in making: 1.2 with Sjsxp compatibility

It will be a while until StaxMate 2.0 is done, mostly since I have decided that it should offer full support for the new (and still work-in-progress...) Typed Access API. Since this beast will be included in Woodstox 4.0 (as part of Stax2 Extension API, curiously versioned at 3.0), it will be a while until all pieces are in place.

But in the meantime there are smaller (but tasty) fish to fry. For example, while it has been implied that StaxMate ought to work with any old Stax implementation, it really has only been tested on Woodstox. Of other implementations, there is just one that actually matters, Sun Streaming Java Xml Parser (Sjsxp), which is bundled with JDK 6 (and 7, I assume). After some testing, turns out that StaxMate 1.1 almost works with Sjsxp. But not quite. 2 particular fixes were needed to get all unit tests to pass. Given that this was relatively easy to do, and that results are useful (one can test StaxMate using, say, Sjsxp, and upgrade to Woodstox as necessary), I think this might just be the main new thing for StaxMate 1.2 maintenance release; and if other low-hanging fruit are found, there's always possibility of cutting 1.3.

Thursday, July 10, 2008

Jackson with some Objectivity: TIMTOWTDI

(that is: "There Is More Than One Way To Do It", aka "Tim Toady")

Now that Jackson is creeping closer to its 1.0 release, package also contains simple robust functionality to map json data to and from Java objects. To be precise, Jackson actually has not just one way to do it but two ways.

Methods are called "Java type mapper" and "JSON type mapper", although other nicknames can be readily coined. For example: "Poor Man's Objects" (aka "Everything's a Map with numbers, Strings and booleans") and "DOM wanna-be" (or "You Know Tree By Nodes It Has").

Java Type Mapper

This mapper is most similar to other Json mappers that everyone (and their monkey [and monkey's fleas' buddies cousins]) have written. Given Json content you can get Maps or Lists that contain other Maps, List, Strings, wrappers (Integer, Long, Boolean) and nulls. And given Maps, List et al., you can generate Json content. Here's how to use this functionality:

  String jsonContent = "{ \"name\" : \"Jackson\", \"data\" : [ 0, 15 ] }";

JsonFactory jf = new JsonFactory(); // need factory for creating parser to use Map hash = (Map) new JavaTypeMapper().read(jf.createJsonParser(new StringReader(jsonContent))); String name = (String) hash.get("name"); List l = (List) hash.get("data"); int maxValue = (Integer) l.get(1);

And to write content back as Json:

  
  StringWriter sw = new StringWriter();
  JsonGenerator gen = new jf.createJsonGenerator(sw);
  new JavaTypeMapper().writeAny(gen, hash);

Simple enough: values are basic Java data structs and values, and you modify them using normal List.add(), Map.put() methods; iterate over elements and so on.

So why do I call it "Poor Man's Objects"? Because most of the time, Maps, Lists (etc) are used to emulate "real" objects (like beans), with more dynamic access. In some cases this works well (when accessing data in a dynamic context, say, from jsp page); in others it just means losing type-safety without gaining anything. And that is probably the biggest missing piece: ability to map data to and from beans. That is something I hope to address in near future (but probably not within core Jackson project itself).

Json Type Mapper

Whereas the first mapper should be familiar for anyone used to other Java json processing packages, the other alternative should look familiar to those who are working with XML using tree models such as XOM, JDOM, DOM4j, or (unlucky bastards), DOM. This mapper constructs an in-memory tree, represented as set of Nodes traversable using convenient path accessors. One obvious benefit here is that one can eliminate casts: as long as you know type of List entries and Map entry values, you just use appropriate accessor and get results correctly typed (or, type cast exception if type didn't match). So, you will do something like:

  JsonNode rootNode = new JsonTypeMapper().read(jf.createJsonParser(new StringReader(jsonContent)));

// for node traversal, can either use getElementValue(int)/getFieldValue(String), or getPath().
// Difference is dealing with missing elements: getPath() allows for safe de-referencing of missing
// values (essentially creating dummy "missing" node that resolves to N/A value)
String name = rootNode.getPath("name").getTextValue(); // or '.getValueAsText()' for extra safety int maxValue = rootNode.getPath("data").getPath(1).getIntValue();

Looks better? I tend to think so -- while this could be called Poor Man's XPath, I think it reasonably convenient as is. In addition to data extraction as shown, tree can be modified, children iterated and so forth. Beyond explicit traversal using node methods, there are also future plans for implementing JsonPath to be able to use more concise notation. Such implementation could be based on the Json Type Mapper and should be easy to implement.

Which one should I use?

Whichever fits your use case, of course. But what would that be? One way to think about is whether you prefer path-access (json type mapper), or Java container traversal (java type mapper). Or more generally: if you have plenty of Java structs waiting to be thrown around, Java type mapper may be more convenient way to join things together. Especially so when you are actually just generating Json content, and not parsing it. So perhaps it makes most sense to generate Json from java stuff using Java type mapper; but to extract data from Json content using Json type mapper.

Anyway: I am using Java Type mapper at work for freezing/thawing state of a batch processing system (over restarts), but I hope others are using one or both in more creative ways. Please let me know if you do, add a comment here or email me (at yahoo.com, cowtowncoder).

Friday, June 20, 2008

On Rock Star Coders: thanks Dan!

Getting compliments is always nice, and never more so when they come from someone who you respect professionally. As such, I was delighted to see this interview, which talks about various things, including performance of xml processing with web services. But more importantly, I was mentioned. :-D

It also reminded me of why I should write a short essay on "6 Java XML processing tools You Should Know About". Stay tuned.

ps. Dan, your 30 yr old Macallan bottle will be delivered shortly.

Thursday, June 05, 2008

Woodstox 3.2.6 released: DOM writing, XMLReporter fixes

Yes, summer may be slow season in software business, but Woodstox patches keep on coming. This time there are 2 main issues that needed prompt patching:

  1. DOM-backed writer (one you get when constructing XMLStreamWriter using DOMResult) had a serious problem when trying to add namespace declarations ( WSTX-144)
  2. XMLReporter was not getting properly called for DTD-validation problems, but rather exception was directly generated ( WSTX-153).

These were the only changes, beyond adding regression unit tests to verify the fixes. Upgrade hence is only necessary if you use DOM-backed writers, or rely on XMLReporter. But then again, if you do either of those, you definitely want the update.

And now back to the regular programming, trying to get the "Typed Access API" (org.codehaus.stax2.typed.*) implemented in Woodstox. Stay tuned!

Tuesday, May 06, 2008

Performance of XML data binding on Java Platform

One of possible future projects on my sizable mental list has been that of comparing performance of open source xml/POJO data binding toolkits. For some reason there are not many actual up-to-date good benchmarks out there. In fact, I can not name even one (for example, BindMark project which might do the trick seems to be dead, and test code I looked at looks.... well, rotten...). My own limited testing has led me to suspect that the fastest current choice might well be JAXB 2 (it seems to have very low overhead over basic Stax parsing), but it would be nice to prove that. Besides, it would be good to check out how JiBX would fare: it is supposed to be highly performant as well.

But today I found this article. Very cool, there are actually people doing serious benchmarking too, and in the very area I would be interested in testing. It also does further support my thinking about limited overhead of not only JAXB 2, but the cool StaxMate helper library.

Thursday, April 24, 2008

Progress of Servlet 3.0 specs, pluggability

I have not really been following progress towards Servlet 3.0 specs (beyond observing some discussion regarding "asynchronous"/non-blocking servlets). But this blog entry by Greg the Jetty Author (a very smart and talented individual, and thankfully, a leader within Open Source Java community) caught my eye. It is good to know that there will be more pluggability -- monolithic nature of web.xml has actually been one of my pet peeves lately. But perhaps the most interesting "new" thing was the reference to things that already exist within Servlet 2.5 specs. I guess this should teach me to pay closer attention to new developments... while 2.5 annotations seem limited, they are existing extension points nonetheless.

Anyway, who'd have thunk: after years of trusty service by mostly basic vanilla Servlet API specification there seems to be actual useful improvements to be had from the latest revision.

Saturday, April 12, 2008

For those about to grok... we salute you!

Ok ok. I know it's bit too geeky to blog about what others are blogging (as well as have this meta-discussion of the fact). But I really must point out some Grade A Alpha Geek investigation by Kohsuke (little wonder he is considered a "rock star programmer"). I mean, color me strange, but I actually do find details of low-level assembly code produced by JVM's JIT compiler fascinating. I hope that doesn't quite classify me an Asperger.

Anyway, it is darn cool what all optimizations are being made. It also confirms my suspicion: current JVMs are not your yester-years simple stack machines any more: they are not only more complicated than you think, they may well be more complicated and clever than you can imagine.

Friday, April 11, 2008

Jetty Keeps On Rocking!

For a while now, I have been a happy Jetty user. Life has been good for me: Jetty is a "Container with Good Attitude": it is transparent, easily and well integratable component. The last part -- Jetty being, and viewing itself as, a component -- is the most important part for me. While I have used Tomcat with success, I just can't help but prefer Jetty due to its pragmatic egalitarian attitude. It's not "the platform", if you don't want it to be; if you want it to, it can do that too. But there is no "Hollywood Rule" here ("don't call us, we call you"). Little wonder that platform builders (Eclipse, most J2EE Application servers), too, are major Jetty users. When you need to get a servlet container for your stack, or for testing purposes, it is a very good choice.

Anyway, as a content user I was happy to see Jetty author's blog entry, which confirms my suspicion: more and more developers have found Jetty. And apparently like what they see. While the metrics in question is not the most relevant out there (after all, I suspect most Jetty instances really are behind-the-firewall web service containers), it is telling that Jetty is doing well within even this market segment.

Congratulations to Greg and the Jetty team. And please keep up the good work.

Let's hear it for the "51 Unsung Heroes of Woodstox"

One thing that has amazed me throughout the development cycle of Woodstox has been amount of invaluable, free and extensive (and sporadic, unexpected and chaotic as well) support from the user community. Most communication has been very helpful, especially when a user has both reported a problem and pointed out a reasonable solution for it. And quite often even contributed a unit test to verify the fix. That's kind of support you just can't buy.

Extensiveness of support is evident from the simple fact that there has been no fewer than 51 individual contributors to Woodstox. And while some have provided more than their share of useful feedback, each and every one of them has been integral part of success of Woodstox. Implementation quality would be nowhere as good as it is now without these individuals, and scope of functionality would likewise be less, and not directed as well to reflect actual user needs. And their help should be appreciated by the huge group of developers who use Woodstox even without realizing they do this: given that Woodstox is the de facto standard Stax implementation within J2EE world (being the default that ships with JBoss, Geronimo, possibly with Glassfish; being recommended by CXF, Axis 2 etc. etc.). I guess it could be called a virtuous cycle, "take a penny, leave a penny". Whatever it is, it works and does wonders.

Anyway, the full CREDIT file is the simplest way to browse Woodstox List of Fame. Let's just hope that next 4 years will bring as much as good help as first 4 years of the project!

Wednesday, February 20, 2008

Cowtown Classic Project I: Codebounce Applet

The first "Forgotten Project" (see Forgotten CTC Projects) to be refurbished is a simple applet called CodeBounce. It's one from class of things ("those goddamn useless applets") that many web surfers in late 90s considered a major annoyance. Had it been deployed by hordes of home page designers, it might have been infamous. It was not, for better or worse. :-)

Applet (which can also be run as a command-line application, via "java Bounce") is simple eye candy, displaying hard-coded number of elastic bouncing balls, bumping into each other and so forth.

Now, there is nothing spectacular here, just 600 lines of trivially simple position and velocity calculation code sprinkled with AWT code. But it is kind of amazing that it still works as well (or poorly) as the day it was written, come to think of that. At the time JDK 1.1 was already mature, and that was the platform on which it was tested, although browsers still only or mostly supported 1.0.2. And runs just as expected on JDK 1.6, including adapting to resized main window (well, as I said, as well as it ever did). It was also cool that even when Java still did run slowly back then, as interpreted language, applet was plenty fast on an antiquated Pentium 66 mhz PC and Netscape browser.

At the time I had done at least 3 other apps in Java (NetReaper, Fractalizer/Fractlet and JiveTerm a.k.a SafeTerm, and this is by far the simplest. But I kind of like that -- writing Java apps and applets was refreshing: after having to write thousands of lines of C code to get anything to show on a window, 600 lines of code for this puppy seemed very compact indeed (although not nearly as compact as those 100 line basic programs in C-64, but close enough, and much prettier).

So that's the part explaining "what". But more importantly, why? Why was it written? CodeBounce project page talks a bit about this. Basically, I wrote it as a proof-of-concept, trying to figure out some simple animation, to be used as an Easter Egg for a well-known and widely used commercial application. In the end, things worked bit differently: easter egg(s) were added (go Phoenix team!), but this particular idea wasn't used. It was simpler to use static graphics and straight-forward linear shuffling. But writing the thing did help in many other ways -- I hadn't used Java for almost a year at that point (having had to work on C, C++); but going back felt like home. So I was even more motivated to find a company that did use Java. I did, and got hired there (company's name nowadays even reflects their use of Java; that's the only hint I'll give). But that's another story for another day.

Oh, and as to the Easter Eggs: sad thing is I don't even know how many (or any?!) users know about these particular ones. Many people are familiar with the "martian guy with laser gun". But I doubt many have seen the "shuffling web form components". It was still cool to have added my own "footprint on concrete", so to speak. Besides, there's at least one other cool Easter Egg in there, which adds "eyes that follow mouse pointer" in there, that I didn't write but knew about, and which probably is as little known as ones I worked on.


Sponsored By

Related Blogs

(by Author (topics))

Recommended Tools

Powered By