Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Wednesday, December 12, 2007

Building thread-safe code with SimpleDateFormat


The scenario is simple.

Each resource in JeromeDL has an upload and update dates; each is stored in a very simple format yyyyMMdd, e.g., 20071212 (for today).

From time to time - we need to get a Java Date object of out this information.

What we do?

Use SimpleDateFormat object parse and format date, getting it back and forth between Java and RDF.

Simple, right?

Yes, as long as your read the whole documentation of the SimpleDateFormat class - to discover that the objects are not synchronized. In other words - what will run on a standalone application with one processor - might very easily break when you try it on a server with multiple processors (or Intel HT).

Luckily, the solution is equally simple - use synchronized block, and you’re done.

Wednesday, November 21, 2007

Who is using my system?



If you have a web application, based on Servlets/JSP (JEE), which sits behind the Apache/mod_jk, and you would like to track who is using your system, here is what to do:

1. In the Apache configuration (probably Vhost definition, as in my case), you will need:
        • JkMount /* NAME_OF_YOUR_WORKER
        • JkEnvVar REMOTE_USER "<NOTSET>"
The second entry will pass the information about user logged in on the Apache level down to your web application
2. In your application you can check who is using it, with credentials on the Apache level, with:
        • ${ REMOTE_USER } (JSP/EL)
        • request.getAttribute("REMOTE_USER") (Servlet or JSP/Scriptlets)
3. In your application you can also check who is using it, with credentials on the Tomcat level, with:
        • ${ pageContext.request.remoteUser } (JSP/EL)
        • request.getRemoteUser() (Servlet or JSP/Scriptlets)

That’s it.

Unless, your system is DSpace - where more problems will arise. You will need to, e.g.,
1. The register user is hidden in request.getAttribute("dspace.current.user"), and is of type org.dspace.eperson.EPerson
2. In order to get some human-readable description of this user you can call getEmail() method to get the email of the user.