This blog is mainly about Java...

Thursday, August 20, 2009

Using Enum in your entity beans

It is quite handy to use an Enum, especially with type String. This means that the enum will be translated as a text field in your database.

Imagine the following Entity Simple.java:
@Entity
public class Simple {
Process process;

@Enumerated(EnumType.STRING)
public void Process getProcess() {
return process;
}
}

And Process.java:
public enum Process {
WHAT,
THE,
BUCK
}


Now, I initally thought, that since you are defining the enum type to be string, then you can simply query the database using string as well like this:
entityManager.createQuery("from Simple s where s.process=:process").setParameter("process","WHAT").getResultList();
However this doesn't work. You will need to use an Enum type. So the solution is to use Enum.valueOf()
entityManager.createQuery("from Simple s where s.process=:process").setParameter("process",Process.valueOf("WHAT")).getResultList();

Now the query will run and return your list :-)

Tuesday, August 4, 2009

Wicket error in Seam test

The Problem

Recently we upgrade to Seam 2.1.x and suddenly our testng test using ant failed with the following message:
No wicket components directory specified to give Seam super powers to
Wicket was added to Seam 2.1.0.BETA1.
This error message doesn't really say anything. Also it said further:
Must set application-class using  in components.xml 

But we where not using wicket and didnt want to add it.

The stacktrace was as following:
WARN  [org.jboss.seam.wicket.web.WicketFilterInstantiator] No wicket components directory specified to give Seam super powers to
[testng] FAILED: getDefaultContacts
[testng] java.lang.IllegalStateException: Must set application-class using in components.xml
[testng] at org.jboss.seam.wicket.web.WicketFilterInstantiator$1.init(WicketFilterInstantiator.java:107)
[testng] at org.jboss.seam.web.WicketFilter.doFilter(WicketFilter.java:124)
[testng] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
[testng] at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
[testng] at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
[testng] at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
[testng] at org.jboss.seam.mock.AbstractSeamTest$Request.run(AbstractSeamTest.java:491)
[testng] at no.whatever.test.AddressBookTest.getDefaultContacts(AddressBookTest.java:18)
[testng] ... Removed 22 stack frames
[testng] FAILED: setup

The Solution


It seems like ant will just put all the libraries in the lib folder in the classpath, and eclipse obviously would not. Thus, when the wicket jars where removed, the tests where back online. I believe it is sufficient to remove jboss-seam-wicket.jar and jboss-seam-wicket-ant.jar, however I just removed all jars that included the name wicket.

Tuesday, July 14, 2009

Injecting checking of roles in Seam

It is well known that you can perform
#{s:hasRole('admin')}
on the view.
You can easily check for the role of the logged in user, by injecting the same thing.

@In(value = "#{s:hasRole('admin')}")
private boolean isAdmin;
If the user has a role of admin the boolean is set to true. You can also use the static method,
Identity.instance().checkRole("admin");

But the former is nicer right?

Sunday, June 28, 2009

Iteration on password is added in JBoss Seam 2.1.2

I blogged about how you could hash your password using Seam 2.1.1 hash-user-password-in-seam-211-manually.

In the latest production release of JBoss Seam 2.1.2 and the @UserPassword annotation, an attribute of iteration is now added with a default of 1000.

We should then modify our GeneratePasswordHash method to the following:

/**
* This method will generate a hash password
*
* @param password
* - The password in cleartext
* @param salt
* - The username is used as salt
* @return - hash password based on password and username
*/
public static String generatePasswordHash(String password, String salt) throws GeneralSecurityException {
char[] passToChar;
byte[] saltToByte;
String thePassword;
try {
passToChar = password.toCharArray();
saltToByte = salt.getBytes();
AnnotatedBeanProperty<UserPassword> userPasswordProperty = new AnnotatedBeanProperty<UserPassword>(ProcessUser.class, UserPassword.class);
// Will get the hash value from annotation UserPassword in ProcessUser.class
PasswordHash.instance().setHashAlgorithm(userPasswordProperty.getAnnotation().hash().toUpperCase());
thePassword = PasswordHash.instance().createPasswordKey(passToChar, saltToByte, userPasswordProperty.getAnnotation().iterations());
return thePassword;
} finally {
// Ensure that the password is not in memory
password = null;
passToChar = null;
salt = null;
saltToByte = null;
thePassword = null;
}
}

Wednesday, June 17, 2009

Review of Seam 2.x Web Development

So I have read the Seam 2.x Web Development
Build Robust Web Applications with Seam, Facelets, and RichFaces, using the JBoss Application Server

This book tries to cover a very very large aspect of Seam and friends.
The book has 11 chapters and almost 300 pages. If you are new Seam, this is a great kick start. Because the book covers a huge portion of the most important aspects of Seam, plus some advanced topics such as Remoting, however I feel that the chapters are too small and that they don't give a thourough and in depth explanation. Many of the examples can be found by googling a bit and reading the documentation of Seam, but this is why it is so good to have a book covering a very large portion of it.

The RichFaces chapter could have been improved alot. Around 30 pages is hardly enough to cover this very big framework. I believe you will get more out of downloading the live examples and looking at the source code and playing around with it instead.

So to sum up, this book is good for someone new to Seam, that want an overview of most of what Seam has to offer, however I would supplement with Seam in Action to cover more ground.

Labels