Tuesday, March 25, 2008

Validation Framework for java

Wouldn't it be nice to be able to do something like this:
public class Address {

@NotEmpty @Max(50)
private String street1;

@Max(50)
private String street2;

@Max(9) @NotNull
private String zipcode;

...

}

and then be able to say something like:
public class Order {
@Valid @NotNull private Address delivery;
}

The implications of something like this go beyond mere validation. IMHO it si an elegant solution that if built properly will improve the DRYness of the code across all layers of any java application.

This is the intention behind jsr-303. .

Monday, March 17, 2008

Another Mac OS annoyance

For some reason in Mac OS, if an application does not have the focus, you would have to click twice on it to get something done. The first click will give the application the focus and the second will do what you where intending to do in the first click. In windows this is not true so for someone coming from Windows, like me, this is particularly annoying. Nonetheless it is not the end of the world.
Mac OS is definitely faster than windows for the kind of work I do on it. I am just finding it a little bit tricky to get use to its windowing system.
It could be the case that, for someone with little or no computer experience; getting use to this kind of things would be easier than it is for someone coming from a different OS.

Thursday, March 06, 2008

Amigo camino a la fama.

Tengo un amigo cineasta de hace mas de 20 anos que, con su opera prima como director, parece haberle acertado al clavo en la cabeza.
La verdad yo siempre crei en el. Muchos no, y no los culpo por que despues de empezar y dejar inconclusas carreras de musica, psicologia y filologia, hay que tener mucha fe en alguien para seguir creyendo que podria llegar a algo. Finalmente encontro se verdadera profesion como cineasta lo cual me hace extremedamante feliz.
Ale, te deseo toda la suerte del mundo, aunque se que, como hasta ahora, la suerte te la haras tu mismo.

Software Market works upside-down

What an interesting take on the software market this is.

Monday, March 03, 2008

No mnemonics in MAC OS X?

I just got a new mac book pro!! I am extremely excited about it.
I have always been a windows guy; and not, I am not exactly ashamed of it. As a developer, the majority of my career has been working on the java platform even though windows was my platform of choice for development.
Anyway, now I find my self lost around mac os x. I have to say that I am having a blast learning to work with it but today I think I found something that in my opinion is pretty serious.
As I started coding away on eclipse in my new mac I went to create a new test class and, to my surprise, eclipse has no mnemonics at all on the mac os platform. On windows I'll go shift+alt+n+j+j+enter to open the new test class dialog, alt+o to select the source folder and alt + f to finalize the wizard. On the mac I'll have to go back to using the mouse for this and many many other things I do just with the keyboard in windows.
At the beginning I thought I was missing some hack in the eclipse configuration to activate this but then realized that iTunes, or any other native mac application for that matter, have no mnemonic support at all. Then I called a fellow developer who is a big mac advocator and he pointed me out to this
(search for mnemonics) and here clearly states that mnemonics are not inline with Aqua guidelines.
Am I the only one who makes heavy use of the keyboard? How come no mac fan ever complained about not been able to drive his/her IDE from the keyboard??

Thursday, November 22, 2007

Agile without Tests???

Can a software organization where the majority of developers have no idea about TDD be considered agile?
I think no, but I may be wrong. What do people out there have to say about this?

Saturday, May 12, 2007

darn hibernate pom.xml

I just finished figuring out a problem for which I could not find any explanation online. It all started happening when I decided to take spring2 aop for a whirl. The app I am working on is a web app using spring and hibernate; both of them to the latest versions which 3.2.x for hibernate and 2.0.4 for spring.
My build system is maven based. The things I love about maven (for regular developer tasks)is its dependency resolution capablities as well as the eclipse:eclipse plugin which helps me setup eclipse in no time.
When I added a dependency on spring-aop and spring-aspects my application stopped deplying complainig about a class in asm that was not found. The exception was comming from hibernate trying to proxy an object using cglib. This was the stacktrace I was getting:
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
at net.sf.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:165)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
at net.sf.cglib.proxy.Enhancer.(Enhancer.java:69)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:107)
at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:135)
at org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(EntityEntityModeToTuplizerMapping.java:56)
at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:434)
at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:109)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:226)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1175)

and more stuff which adds no value to this blog entry.

The problem, after hours of digging, banging, pulling hair and snapping was the hibernate's pom.xml has cglig-2.1_3.jar as dependency and somehow this guy depends on asm-1.5.x.jar. Spring aop depends indirectly of asm-2.2.3 and maven, smart enough, was using the latter is its a newer version. To make a long story short, I added an exclussion to hibernate dependency for cglib and added cglib-nodep as one of my dependencies and this solved the problem.
I hope, if anyone hits this problem ever, he/she will google the stacktrace and will find this post.

Wednesday, November 01, 2006

Validation Framework For Commands.

In my company they came up with a rather strange way to"customize/extend" the product. There is a sort of framework upon whichcustomizations are built. The way to customize a certain piece of logicis by using a variation of the chain of responsibility pattern. Pieces of logic are put into command objects and these objects are chained using Apache Commons-Chain. The reasons why this done like this is a topic for another post.
Becausethe application is spring based, it was awkward to have these commandobject instantiated outside spring. By doing this we were unable toleverage the dependency injection facility in spring and therefore, hadto come up with a very wacky way of doing this which basically wasputting collaborators into the command context.
Recently this wasfixed. We are now able to declare command objects as spring beans andinject anything we want into them. The same goes with chains. This isindeed, a great improvement over what we had before.
Now, to thepoint of this post. In my team, we thought it would be nice to have aframework to validate the command contexts before the actual executemethod is called. By having this, we'll be saving our selves theclutter of having validation code inside the commands; instead, thecommands would assume everything expected to be in the context wasthere, and focus only on getting the job done.
This is the kind ofuse cases aspects are very well suited for. It is a cross cuttingconcern because validation has to be performed across a large set ofobjects.
So, we came up with a before advice for the execute methodon the command objects and also wrote a factory that is able to returna validator object for a particular type of command. This factory onlyhave to be declared as a bean in the spring context and wheninitialized it will search the application context for Validator beansand put them in a map keyed by the type of comand they know how tovalidate.
If the factory returns nullfor a particular type of command then the advice will let theinvocation continue without further actions. At the most, it logs awarning about the missing validator.

This is how the advice looks in real life:




public class CommandContextValidationAdvice implements MethodBeforeAdvice {
private CommandValidatorFactory validatorFactory = null;

/**
* Lookup a CommandContextValidator via the factory. And if not null then execute the validate method.
*/
public void before(Method method, Object[] params, Object command) throws Throwable {
CommandContextValidator validator=validatorFactory.getCommandValidator(command.getClass());
      if(validator != null) {
validator.validate((Context) params[0]);
}
}

/**
* @param validatorFactory The validatorFactory to set.
*/
public void setValidatorFactory(CommandValidatorFactory validatorFactory) {
this.validatorFactory = validatorFactory;
}
}



The second problem we faced was applying the aspect to the commands. We didnot wanted to change every single command bean definition as this woulddefeat the purpose of making the validation framework orthogonal to theactual command hierarchy.
In order to solve this, we used the autoproxy facility in spring. The bean definitions in the applicationcontext then looks like this:


<BEAN class=org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator id=jdkBeanNameProxyCreator>
     <property name="beanNames">
          <VALUE>*Command</VALUE>
     </property>
     <property name="interceptorNames">
          <LIST>
               <VALUE>commandContextValidationAdvisor</VALUE>
          </LIST>
     </property>
</BEAN>

<BEAN class=com.mycompany.myclientcompany.command.CommandContextValidationAdvice id=commandContextValidationAdvice>
     <property name="validatorFactory">
          <REF bean="commandValidatorFactory"/>
     </property>
</BEAN>

<BEAN class=org.springframework.aop.support.RegexpMethodPointcutAdvisor id=commandContextValidationAdvisor>
     <property name="advice">
          <REF local="commandContextValidationAdvice"/>
     </property>
     <property name="pattern">
          <VALUE>org.apache.commons.chain.Command.execute</VALUE>
         </property>
</BEAN>


The first bean definition is the auto proxy creator. It will proxy everybean in the application context whose name end with the word Command. In our case this was not much of a problem because all the commands were following this convention even before we thought about the validation framework. The rest of the bean definitions are the Advice and the Advisor respectively.