Tuesday, 26 February 2008

Canoo WebTest

I have been looking for unit testing my web pages and came across Canoo WebTest recently. I have to say it is a very simple tool to use, flexible, produce excellent reports, and it supports groovy scripting language as well for more complicated tests. By following the screencast, I was able to start testing my web pages in less than half an hour.

Monday, 25 February 2008

Personal Information Online

I have recently read this article from The Sydney Morning Herald Blogs: MashUp. It is really amazing to note that people put a significant amount of personal detail online. The article also mentions that your mother's maiden name is often used in Internet Banking as a security question, and people do put it somewhere online. It is like keeping your PIN number next to your ATM card! I guess the lesson here is, whatever you put online, most of the time can be traced by criminals, so be careful! Think twice before hitting that "publish"/"submit" button.

Tuesday, 19 February 2008

File Download In Spring

In order to implement a file download in Spring, the key is returning null instead of a ModelAndView object. This controller that I created below is specifically for text/ascii data. You'd need to change the content type appropriately in order to download other file types.

public class DownloadController implements Controller {
/*
* Spring dependency injection
*/
private XService xService;

public void setxService(
XService xService) {
this.xService = xService;
}

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String idStr = request.getParameter("id");
if (idStr != null) {
long id = Long.parseLong(idStr);
String data = xService.getData(id);
doDownload(request, response, data, "file" + idStr + ".xml");
}
return null;
}

private void doDownload(HttpServletRequest request,
HttpServletResponse response, String data, String filename)
throws IOException {
int length = data.length();
ServletOutputStream op = response.getOutputStream();

response.setContentType("text/plain");
response.setContentLength(data.length());
response.setHeader("Content-Disposition",
"attachment; filename=\"" + filename + "\"");

byte[] bbuf = data.getBytes();
op.write(bbuf, 0, length);
op.flush();
op.close();
}
}

Accessing EJB 3 from Spring 2.5

Spring 2.5 now supports EJB 3 access, and it could not be any easier! Here is the spring xml snippet to achieve it:

<beans>
...
<!-- *** EJBs *** -->
<!-- REMOTE JNDI CONNECTION -->
<bean id="remoteJndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
org.jnp.interfaces.NamingContextFactory
</prop>
<prop key="java.naming.provider.url">
localhost
</prop>
<prop key="java.naming.security.principal">
username
</prop>
<prop key="java.naming.security.credentials">
mycredentials
</prop>
</props>
</property>
</bean>

<bean id="abstractRemoteSlsb" abstract="true"
class="org.springframework.ejb.access.
SimpleRemoteStatelessSessionProxyFactoryBean">
<property name="lookupHomeOnStartup" value="false">
<property name="jndiTemplate">
<ref bean="remoteJndiTemplate" />
</property>
</bean>

<bean id="myService" parent="abstractRemoteSlsb">
<property name="jndiName"
value="myapp/MySessionBeanImpl/remote">
<property name="businessInterface"
value="au.com.package.name.MySessionBean">
</bean>
...
</beans>

Friday, 15 February 2008

Logging A StackTrace Exception

Logging an error seems to be a very easy job to do, but one of the common mistakes that Java programmers make is using the wrong method. Consider this piece of code:
try {
..
}
catch (Exception e) {
LOG.error(e);
}

Can you see what is really going on? If you have a look at commons logging's API, there are 2 error methods in the Log interface. All the above code does is it logs e.toString() to the error log, which means the error stack trace will be lost! Here is what you need to do instead:
try {
..
}
catch (Exception e) {
LOG.error("problem...", e);
}

Wednesday, 13 February 2008

EJB 3.0 LazyInitializationException In Unit Tests

I have to say that unit testing in EJB is very challenging. All I want is doing unit tests on a local session bean that someone else created, so here is what I did:
  • Create a remote interface
  • Create a base interface from the current local session bean interface
  • Delete all abstract methods on the local interface and extend the base interface
  • Update the remote interface to extend the base interface
  • Add the methods that I want to unit test in the remote interface
  • Change the methods that I want to test in the implementation to public
  • Edit the implementation class so that it extends both the local and remote interface

Gosh, if that is not time consuming, tell me what is! After all the effort, finally I wrote the actual unit test class, wrote the code to get the remote home that I created earlier using JNDI in the setUp() method, wrote my test, compile the whole project, and then start my JBoss.To my surprise, I got LazyInitializationException, so I thought, all the effort that I did earlier was useless! I tried googling and found some ways to avoid this exception. There are actually 4 ways to do this:

  • Change the session bean to be stateful, and change the persistence context type to EXTENDED. Do I really need to do this just to get the test working?
  • Run the session from within another session bean. Do I really need to create another one just to get the test working?
  • Make fetch type EAGER (obviously). Impossible! There are lots of data, and therefore the laziness
  • Create another method and use Hibernate's JOIN FETCH strategy. This one seems to be the cleanest and the least destructive solution

Honestly, why is it so hard to unit test a Session Bean? If the project had been done in Spring, there are Spring tools that can be utilised and therefore unit testing would have been much easier! See my earlier post to work around this Exception in Spring.

Tuesday, 12 February 2008

Grails

With the recent announcement of Grails version 1.0, it is time to give it another try! This time I went to the screencasts, then downloaded the binary release and started fiddling. Creating a CRUD web application could not be easier with scaffolding feature. Domain objects with relationships can also be handled by following this tutorial.

I also came across another tutorial that I thought would allow Grails to connect to existing EJBs in an existing container. You would actually need to copy the POJO implementations into the project in order to make it work.

Overall, I really like its features ease of use and I reckon it has a great potential to penetrate the enterprise market in the future. It is based on well known and enterprise-ready frameworks: Spring, Hibernate, and Groovy. Well done guys!

Tuesday, 5 February 2008

java.lang.OutOfMemoryError: PermGen space

Recently we've been getting this OutOfMemory error on our development system at work. We've been jumping up and down trying to figure out what is wrong, but we could not, until we came across this article explaining how JVM allocates memory space.
It turns out that the default Permanent Generation memory is 64K, which is definitely not enough! We then increased the size by adding -XX:PermSize="newSize"m and everything went back to normal.

Multiple JBoss Instances

There are 2 ways to achieve this:

  • Use different IP addresses on the machine
  • Use different ports on the machine

The first method is the one recommended by JBoss because it reduces complications in tracking down problems in the future.

Links:

Share |