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.
Tags: executable, jetty, war, winstone
Posted in Java EE | No Comments »
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.
Tags: derby, embedded, jpa, nbp, NetBeans, platform
Posted in NetBeans | No Comments »
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):
- Define an interface or abstract class (this will be implemented/overrided by the provider)
- 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):
- Implement/override the code of SPI
- Create a META-INF/services folder
- 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.
Tags: java, spi
Posted in Java SE | No Comments »
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…
Tags: confirmation, jsf, multiple, password, validation
Posted in Java EE | No Comments »
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);
}
}
Tags: autowire, injection, spring
Posted in Java EE | 1 Comment »
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…
Tags: enum, hibernate, jpa, ordinal, varchar
Posted in Java EE | No Comments »
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”).
Tags: filter, jaas, order
Posted in Java EE | No Comments »
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.
Tags: manager, realm, tomcat
Posted in Java EE | No Comments »
March 16th, 2009 by Eduardo Costa
Spring team likes big names. One of the biggest is “SimpleRemoteStatelessSessionProxyFactoryBean“. This is not a problem, since I’m not a Fortran programmer. Unfortunatelly, I found two classes with similar names that aren’t type-safe (i.e.: I don’t receive a ClassCast or similar if I swap them): MethodInvokingFactoryBean e MethodInvokingJobDetailFactoryBean. I use the latter a lot, but, after using NB’s autocomplete feature, I selected the former instead:
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="cache"/>
<property name="targetMethod" value="reload"/>
</bean>
Results? NullPointerException:
org.quartz.SchedulerException: Registration of jobs and triggers failed: null [See nested exception: java.lang.NullPointerException]
at org.springframework.scheduling.quartz.SchedulerFactoryBean.registerJobsAndTriggers(SchedulerFactoryBean.java:861)
at org.springframework.scheduling.quartz.SchedulerFactoryBean.afterPropertiesSet(SchedulerFactoryBean.java:649)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1334)
... 13 more
Caused by: java.lang.NullPointerException
at org.quartz.core.QuartzScheduler.addJob(QuartzScheduler.java:808)
at org.quartz.impl.StdScheduler.addJob(StdScheduler.java:288)
at org.springframework.scheduling.quartz.SchedulerFactoryBean.addJobToScheduler(SchedulerFactoryBean.java:883)
at org.springframework.scheduling.quartz.SchedulerFactoryBean.addTriggerToScheduler(SchedulerFactoryBean.java:906)
at org.springframework.scheduling.quartz.SchedulerFactoryBean.registerJobsAndTriggers(SchedulerFactoryBean.java:842)
... 16 more
This wasn’t easy to solve, since the NPE wasn’t obvious. Both MIFB and MIJDFB are “factory” instances (they create real instances as needed), and, in this case, no type check was done.
Tags: jobdetail, nullpointerexception, spring
Posted in Java SE | No Comments »
March 16th, 2009 by Eduardo Costa
I never planned a “part 2″ about Jasper Reports, but the previous “tutorial” lacks a small concept: how do I make tables? It’s obvious, but only after you figure it out. If you downloaded Jasper Reports examples, you can look at “datasource” sample. Nice one, but not as simple as you may want.
I only want to add a basic table which row data comes from a Java Collection. It is very easy to adapt the JasperFiller: just change JREmptyDatasource with JRBeanCollectionDataSource.
Adapting JRXML is easy, too, but it not obvious for a newbie. I admit I expected something like a “table” element (blame years of HTML), but in JR, you only need to define the “detail” element with the contents you want to repeat for each element on DataSource. Create it as you want, tune the band height to the height of a row, and you have your HTML-ish table.
BTW, you need to define all fields at the top of your JRXML, since JR’s DataSources does not contains metadata.
As a last tip, keep these in mind:
- <title> only appears on first page;
- <pageHeader> and <pageFooter> appears on every page;
- <lastPageFooter> apperars only on last page, REPLACING <pageFooter>
Tags: jasper, tables, tutorial
Posted in Java SE | No Comments »