Tuesday, January 31, 2012

Xerces and Xalan: Recipes for the win

OK. I have explained why Xerces and Xalan are a source of problems, and I have explained why you shouldn't use System.setProperty to resolve them. In this post I wan't to give a few recipes to resolve Xerces and Xalan problems.

  1. Under no circumstances use System.setProperty anywhere in your application. I know I have already dedicated a post, but I wanted to stress it once more. Sometimes you'll need some specific parser (OC4J had a strange bug that needed a specific parser, and Weblogic seems to have special needs too). If you need a specific parser, do it at container level (2nd  point), at application level (3rd point) or at the specific code (4th point).
  2. If you have to define any property do it at the start of the container or the server. For example, in Weblogic 10.3.3 in Windows, we had to edit the file startWeblogic.cmd at the domain path, inserting the following line

    set JAVA_OPTIONS=-Djavax.xml.soap.MessageFactory=weblogic.xml.saaj.MessageFactoryImpl

    Sometimes you don't need to do it at the container/server level. In Weblogic you can use something called XML registry that enable a different parser for a particular Document Type.
  3. Sometimes you can define the parser at the application level. For example, in Weblogic you can define a specific weblogic-application.xml with the following text:
    <?xml version = '1.0' encoding = 'UTF-8'?>
    <weblogic-application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/javaee_5.xsd
    http://xmlns.oracle.com/weblogic/weblogic-application
    http://xmlns.oracle.com/weblogic/weblogic-application/1.1/weblogic-application.xsd"
    xmlns="http://xmlns.oracle.com/weblogic/weblogic-application">
    <xml>
    <parser-factory>
    <saxparser-factory>org.apache.xerces.jaxp.SAXParserFactoryImpl</saxparser-factory>
    <document-builder-factory>org.apache.xerces.jaxp.DocumentBuilderFactoryImpl</document-builder-factory>
    <transformer-factory>net.sf.saxon.TransformerFactoryImpl</transformer-factory>
    </parser-factory>
    </xml>
    </weblogic-application>
  4. When somebody needs a specific property for a factory and is not possible to modify the container or the application, the solution is not to do a System.setProperty (see 1st bullet), but to instantiantiate the specific factory. For exmaple, instead of doing

    import net.sf.saxon.TransformerFactoryImpl;
    System.setProperty("javax.xml.transform.TransformerFactory",
          "net.sf.saxon.TransformerFactoryImpl")
    TransformerFactory tf= TransformerFactory.newInstance();


    you should do

    import net.sf.saxon.TransformerFactoryImpl;
    TransformerFactory tf=new TransformerFactoryImp();


  5. In the long run, it should be great to use mechanisms to prevent using System.setProperty like this link http://stackoverflow.com/a/4208150/54256
Update (2012-10-01): I see lots of visits to this page. Maybe you should visit 


too

Sunday, January 29, 2012

Things about Xerces, Xalan and Saxon I didn't know (III)

In my previous entry, I tried to explain why Xerces and Xalan are so complicated. In this entry I want to focus in System.setProperty. Because, as I said that is the usual solution you are going to find when you need a specific parser for whatever reason.

And that solution is wrong. Very wrong.

Because, if you choose to change the provider using System.setProperty, you are changing the provider for all the applications in the container/server. It doesn't matter that you change it, then instantiate, and then change to the original value. Because, when you're running in an application server, there are some chances of additional threads needing to instantiate and getting a different provider of the one they are trying to get. In fact, that same problem was reported to my group a couple of weeks ago. We tend to give support to applications that use our framework. And, as I say, we received a strange error. Sometimes one report was generated OK, and sometimes it failed with some exception (don't remember off hand). Same query, same data. Fortunately, the stacktrace came with some reference to xerces. Quick search and discovered the report failed when a different report changed the Property for DocumentBuilderFactory to an old sun implementation. What was even better: the old implementation seemed to be not particularly needed. Looked like a copy&paste.

I guess you'll be asking yourself what to do if you can't use System.setProperty. Too late for this blog post. Wait for the next.

EDIT: Stack trace

java.lang.ClassCastException: com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl
            at org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Unknown Source)
            at org.apache.xerces.impl.xs.XMLSchemaValidator.reset(Unknown Source)
            at org.apache.xerces.parsers.XML11Configuration.configurePipeline(Unknown Source)
            at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
            at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
            at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
            at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
            at org.apache.commons.digester.Digester.parse(Digester.java:1647)

Wednesday, January 18, 2012

Things about Xerces, Xalan and Saxon I didn't know (II)

I said in the previous post that Xalan and Xerces sometimes look like magic. So what are they and why are so complicated?.

  • Xerces is a Java library that helps parsing, validating and managing XML documents. You can parse XML documents in different ways. You can transform into a memory structure, building a big object, or you can even parse only the elements you need. There's a whole array of ways to manage XML documents and schemas, and Xerces helps with them. 
  • Xalan is a Java library that helps transforming (XSLT 1.0) and querying (XPATH 1.0) XML documents. 

If you've reached this point you'll be asking why the buzz about Xerces and Xalan. There are four things that make Xerces and Xalan special:

  • Java application servers usually manage their configuration with XML and they need some version of a parser.
  • Because of that, Xerces is probably one of more forked projects in the history. Every little server that worth its bucks have its own version of Xerces. Yes, you have one official version of Xerces. But your own server maybe has it own. And probably instead of org.apache.xerces.jaxp.DocumentBuilderFactoryImpl, they will have called something like com.sun.apache.xerces.jaxp.DocumentBuilderFactoryImpl.
  • Even if your server doesn't use its own version of Xerces, chances are high that it will be using classes of type javax.xml.parsers.DocumentBuilderFactory or  javax.xml.parsers.SAXParserFactory to parse xml documents. To get an instance of these classes you'll use a design pattern called factory that will give you a specific implementation of the class. And the specific implementation you'll get depends on... a system property.
  • If you need to parse an XML document, you'll have to use those classes too. And if for whatever reason you don't like the specific implementation your server (or even somebody in your own project) is using, then you're basically screwed. Because you'll use something that appears in 80% of the articles about the topic: System.setProperty.

Things about Xerces, Xalan and Saxon I didn't know (I)

These days I'm getting very proud of myself. I'm acting less as a project manager and more as a true Scrum Manager. I go to the scrum, listen, and a great deal of my work that day is trying to solve problems for the team. That's great because in order to remove the glitches of my team I have to learn a lot on very different topics.
One of those topics has been Xerces and Xalan.
Xerces and Xalan is one of the those things every developer has listened about authentic horror stories. When it goes OK, you don't worry. And when things go wrong, the usual tactic is to put different versions of those libraries in different places of the server, make some System.setProperty and hope the best.
That tactic usually work (somehow). But not always. And it looks like (dark) magic.

Friday, January 13, 2012

Notes to a newbie spring-mvc developer


  1. Activate log4j. Without it you're lost
  2. Look through the log to see if you're getting the RequestMapping through Spring.
  3. If you are not despite correct annotations, see this post. In particular if you've got Spring Security installed, for Goodness sake, don't forget these lines:
  4. <beans:bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <beans:bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    </beans:bean>
    </beans:bean>

  5. Just in case you decided to change the mappings, and you continue to see the old ones in the logs, clean and rebuild.

Tuesday, January 10, 2012

Budget deficit

Disclaimer: I'm talking about public data. I don't have any unpublished data.

In Spain there is some buzz recently about tax increases approved on December 30th. The government had changed and the new one said it was necessary to raise taxes because data said budget deficit where 8% GDP. The commitment was to finalize 2011 with a 6% GDP, from 9.2 previous year. 

OK. Here is my take. We will end 2011 with more than 9% GDP. And here is my reasoning.

From what I know, public data about budget deficit appears on this page. As of January 10th, the more recent data are for federal government data (Estado) the November 2011 data, and for all public administrations, 2Q2011. 

With November data for the federal government, you get a deficit of 52385 million euros for the 2011. But, in my opinion, to get the whole picture you have to take into account a twelve month period. Adding up December 2010, we would be talking of 64306 million euros. Around 6% GDP vs 4.8%GDP expected.

Similarly, data for all public administrations. Last data available is 2Q2011, with a deficit of 41348 for the 2011. But again you need a twelve month period. Adding 3Q2010 and 4Q2010, you get a total amount of 98283 million euros. Yes. At 2Q2011 the twelve month deficit was greater than at 2Q2010 (although on a percentage of the GDP it was a bit smaller).

Until we get updated data, deficit will end 2011 over 9% GDP.

EDITED (31/01/2011): It seems that public data 3Q2011 for all public administrations is out. 93786 million euros for a twelve month period. Using a GDP for 2011 of  1058701 million euros, deficit was 8,86% GDP.

Sunday, January 8, 2012

When companies just don't get it

I have a two years old Symbian cell phone and a second generation iTouch. I guess that's enough to explain that until very recently I didn't pay any attention at all to mobiles apps ads. Companies regularly announced new  apps for the Apple Store (or the Android one), and I didn't even read the announce.
More recently I've started with mobile development. I have paid the Android and Apple fee, I have developed my own apps with PhoneGap and Appcelerator Titanium (games for my daughters, but nothing too serious). And at work we're starting to thing about possible Apps.
And I'm starting to pay attention to the Apps announced.
Today I received an ad of a new App from Club Vips. Club Vips is a popular chain of restaurants here in Spain. I read the description:

Encuentra tu establecimiento más cercano del Grupo Vips, calcula tu ruta más rápida hasta nuestros locales, infórmate de nuestros horarios, reserva mesa por teléfono o envíanos un e-mail.

My own translation:

Find the nearest restaurant from Club Vips, find the shortest route, look up the timetable, reserve a table on the phone o send us an email.


Not too exciting. Anyway, let's give it a look. Oh. I cannot install it because I don't have iOS 5. "Are you kidding me?" No. I'm not kidding you.

I still remember when the web was THE THING. Every company had to have a web page or else risk to be not cool enough. And companies made horrible pages with no information, or a wonderful Flash animation (again with no information). They didn't get it.

And now social networks and mobile development are THE NEW THING.  They've read in the headline of a report from Gartner, Forrester and the like. But they didn't bother to read the whole report. Or they didn't think too much about it. Or maybe they didn't have too much money and they wanted to get something out as quickly as possible.

Mobile is very powerful, and I feel disappointed when people just don't get it.

Hey. Just because it's the end of Christmas season, and because I'm a client of them, here goes my free piece of advice for them. First the easy part:

  • Not everyone has iOS 5 installed. Unless you really need that version, stay lower.
  • Include a menu with dishes and quotes at each restaurant. Include the weekly menu.
Now another step:
  • Give a free wifi key of each restaurant to those with the app.
  • Offer special discounts, promotions and draws for those with the app.
  • Order from the app. Call the waiter from the app. Tricky part: lock the actual table to the app and user. Possible solutions 1.-Bluetooth proximity 2.-Table selection from the app, and locking confirmation through the waiter.
And then try to combine the app with your registered customer program (Tarjeta Club Vips):
  • Reserve a table through the app.
  • Personalized promotions. 
  • Some of your restaurants offer the ability to order the food, wait for it to be prepared and take it away. Be able to order the food through the app and get a estimated time for taking it. 
  • Rating of specific meal experience.