NetBeans 6.7

June 30th, 2009 by Eduardo Costa

Two days ago, NetBeans 6.7 was released. These are the new features I tested:

  • Maven integration was improved with autocomplete and dependency graph;
  • Profiler’s HeapWalker supports OQL - very useful if you do memory tunning;
  • Hudson server management and a status bar icon with build status.

See more at release page!

Using Spring with Mojarra InjectionProvider

May 22nd, 2009 by Eduardo Costa

Are you using SpringBeanFacesELResolver to handle dependency injection on managed beans? It works nice, but your faces-config grows fast with this approach.

Two months ago, I posted about using Spring injection outside application context. I forgot to mention that the same trick can be used to replace ELResolver solution if you are using Mojarra as your JSF provider.

You only need to add a servlet context parameter:

<context-param>
  <param-name>
    com.sun.faces.injectionProvider
  </param-name>
  <param-value>
    mypkg.SpringJSFInjectionProvider
  </param-value>
</context-param>

And the implementation of SpringJSFInjectionProvider is as simple as:

public class SpringJSFInjectionProvider
       implements InjectionProvider {
  private WebContainerInjectionProvider wcip =
      new WebContainerInjectionProvider();

  @Override
  public void inject(Object bean)
         throws InjectionProviderException {
    FacesContextUtils
      .getWebApplicationContext(
         FacesContext.getCurrentInstance())
      .getAutowireCapableBeanFactory()
      .autowireBeanProperties(bean,
         AutowireCapableBeanFactory.AUTOWIRE_NO,
         false);
    wcip.inject(bean);
  }
  @Override
  public void invokePreDestroy(Object bean)
         throws InjectionProviderException {
    wcip.invokePreDestroy(bean);
  }
  @Override
  public void invokePostConstruct(Object bean)
         throws InjectionProviderException {
    wcip.invokePostConstruct(bean);
  }
}

Adding this, you can shrink your faces-context, removing all “managed-property” tags. Your managed beans can look more JavaEE 5 after you change this:

private BO bo;

public void setBo(BO bo) {
  this.bo = bo;
}

To this:

@Resource(name="bean-on-appctx")
private BO bo;

No setter, no ELResolver. This means less code to maintain! If you plan to migrate from Spring to EJB, this means you will only need to promote the BO to an stateless bean and change @Resource to @EJB!

Executable WAR files using Winstone and Maven

May 7th, 2009 by Eduardo Costa

After a couple of years developing webapps, I’m starting to get problems to design a non-web UI. Not a big deal, since I only use Swing on personal projects. The biggest problem with web projects is the need of at least a servlet container. If you host a lot of projects, Tomcat, JBoss, Glassfish and Geronimo are good choices. But, if you need to host only one “web-container-only” project, Jetty or Winstone are way better.

I’m planning to use maven instead of ant for all of my projects, both personal and professional. It is easier to maintain and more IDE-portable. When I’m developing, I can use “maven jetty:run” to automatically compile and start Jetty (ctrl-c ends the server). The whole process is faster than a “ant dist / deploy / OOM / kill -9″ when using Tomcat. You only need to add jetty plugin to your POM (inside “project > build > plugins”):

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <configuration>
    <contextPath>/</contextPath>
  </configuration>
</plugin>

This is useful to develop, but, when I want to create the “executable WAR”, I prefer using Winstone - it’s even lighter than Jetty. This means I need another plugin:

<plugin>
  <groupId>net.sf.alchim</groupId>
  <artifactId>winstone-maven-plugin</artifactId>
</plugin>

Running “winstone:embed” will create a “xxx-standalone.jar” on “target” folder. This JAR can be run with “java -jar” and your application becames avaliable on “http://localhost:8080/”. This plugin has a lot of configurations, and, using maven, you can configure it to run this goal on package phase. Read its manual for more information.

NetBeans Platform + JPA + Derby embedded

March 27th, 2009 by Eduardo Costa

If you want to use NetBeans Platform and JPA together, there’s a great tutorial on NB’s site. Unfortunatelly, it explains how to do it with an external database and using an external JAR for your entities.

If you want to have a entity module with your classes (instead of an external JAR and a library wrapper), no big deal - it works! You can follow GJ’s tutorial, but you will create a module project instead and you will not have NB’s wizards to help creating entity classes.

I also created a module install on my Derby wrapper, with this line:

FileUtil configRoot = FileUtil.getConfigRoot();
System.setProperty("derby.system.home",
    FileUtil.toFile(configRoot).getCanonicalPath());

This defines where Derby will create database files (user’s dir, in this case).

BTW, if a “no suitable driver found” is thrown, you forgot to add a dependency between JPA wrapper (EclipseLink, TopLink, OpenJPA or Hibernate) and JDBC wrapper.

Java Service Provider Interface (SPI)

March 19th, 2009 by Eduardo Costa

Long time ago, I had a bad time trying to develop a “plugin mechanism” to my “open source Toad“. What I didn’t know is how to use the service provider infrastructure avaliable since Java 1.3 (what a shame!).

If you want to create a service provider interface (SPI):

  1. Define an interface or abstract class (this will be implemented/overrided by the provider)
  2. Use “ServiceLoader.load” when you need the concrete instances (it’s an iterator that can be used in a “foreach”)

If you want to provide a service (the plugin itself):

  1. Implement/override the code of SPI
  2. Create a META-INF/services folder
  3. Create a file with the same name of the SPI class and add the name of the concrete classes from step 1

And we are done! A simple, yet robust, solution to add extensibility.

Password and confirmation with JSF

March 17th, 2009 by Eduardo Costa

If you need to validate a field in JSF and the value is dependent of another field (like password/confirmation), BalusC posted a nice tutorial about it. You need to create a JSF Validator and add a <f:attribute/> to your field. Easier than I thought…

Spring injection outside application context

March 17th, 2009 by Eduardo Costa

Are you using ApplicationContext (and friends) to create a single “spring context” for all your web application? If so, you are on the right way. And it is possible that you feel the need to inject properties where Spring isn’t accessible (like a Servlet Filter). You can use this little trick:

public static void inject(ServletContext context,
    Object bean) {
  WebApplicationContext wCtx =
      WebApplicationContextUtils
          .getWebApplicationContext(context);
  if (wCtx != null) {
    wCtx.getAutowireCapableBeanFactory()
        .autowireBeanProperties(bean,
            AutowireCapableBeanFactory.AUTOWIRE_NO,
            false);
  }
}

Hibernate JPA and Enums

March 17th, 2009 by Eduardo Costa

Assume you have a simple enum:

public enum Status {
  ACCEPTED, REJECTED, NOTREADDEN
}

And your legacy database stores it as ordinal values (ACCEPTED = 0, REJECTED = 1, NOTREADDEN = 2). For reasons lost in time, DBA created the column as “varchar”. If you want to use JPA, you will have a bad time using Hibernate:

java.lang.IllegalArgumentException: No enum const class mypkg.Status.1
  java.lang.Enum.valueOf(Enum.java:196)
  org.hibernate.type.EnumType.nullSafeGet(EnumType.java:110)

After digging into EnumType.java, I noticed it is hardcoded “if-varchar-then-name-else-ordinal”. I assume they never dealed with ancient, legacy databases.

If I switch to TopLink, it works as expected… One thumb down to Hibernate…

JAAS and Filters

March 17th, 2009 by Eduardo Costa

Small tip of the day. If you define a servlet filter and container managed security on the same application, keep these in mind:

  • If your filter is “url-mapped”, it is executed BEFORE the security manager;
  • If it is “servlet-mapped”, it is executed AFTER the security manager.

I tested it when developing a filter to handle “user life cycle” (expired passwords, agreement signing, etc). If I map both filter and security to “/*”, filter was run. If I map security to “/*” and filter to “Faces Servlet”, it is not executed until authenticated (even when accessing “/faces/index.jsp”).

Tomcat manager with SSO and a different Realm

March 16th, 2009 by Eduardo Costa

Tomcat Manager’s default configuration isn’t pratical (nor secure): you must store your users and passwords in a plain XML file. Of course, you can change the “<Realm>” definition on “<Engine name=’Catalina’>”, but it has no effect if you need a host with Single Sign On.

Since Manager needs to share the same host with managed applications, you need to create a context.xml for your Manager:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/manager" privileged="true">
  <Realm className="my.secure.and.custom.Realm"/>
</Context>

Put it on “conf/<engine>/<host>/manager.xml”. The “privileged=’true’” will prevent a “java.lang.SecurityException: Servlet of class org.apache.catalina.manager.HTMLManagerServlet is privileged and cannot be loaded by this web application”.

You must change “webapps/manager/WEB-INF/web.xml” and replace “<role-name>manager</role-name>” with the name of the role you want.