Thursday, December 16, 2010

Accessing spring beans from liferay templates

I had an interesting task, display the version of the portal.
The question which immediately arises is: how do I get from my velocity template to my spring beans?

1. The first stop is the VelocityVariable class from Liferay.
there your fine "serviceLocator" variable which sound just as the right variable to use.
2. In your tpl file add the following


  • $serviceLocator.findService("versionBean").getVersion()



3. You may think now that you only have to configure a Spring bean named "versionBean". Wrong!







The bean name must be augmented with ".velocity" and the bean you are talking to is not yours, but the "ProxyFactoryBean" in which you inject your bean!
4. The Java bean class is up to you then.

Thanks you Martin for your help with this one.

Wednesday, December 1, 2010

Choose default java version in Ubuntu

I got annoyed today, as I needed the sun jdk on my Ubuntu machine.
I had to goodle a while before finding how to change the default from open jdk to sun jdk

The how to is located here.

But I reproduce the text here:

Choosing the default Java to use

  • Open a Terminal window
  • Run sudo update-java-alternatives -l to see the current configuration and possibilities.
  • Run sudo update-java-alternatives -s XXXX to set the XXX java version as default. For Sun Java 6 this would be sudo update-java-alternatives -s java-6-sun
  • Run java -version to ensure that the correct version is being called.
You can also use the following command to interactively make the change;
  • Open a Terminal window
  • Run sudo update-alternatives --config java
  • Follow the onscreen prompt
I still would prefer some gui to perform this. But it is a start.

Monday, November 29, 2010

Caching in Firefox

I was checking if my static content was effectively cached by my browser, but was stuck. Nothing got cached !
Until I found a very informative forum contribution from Aso

Type about:config in FF and search for 'cache'.
Check
browser.cache.disk.enable
browser.cache.memory.enable and
network.http.use-cache
are set to true.

Also try cranking up the size of browser.cache.disk.capacity (50000).
Lastly, make sure browser.cache.check_doc_frequency is integer 3.

Another thing I should mention - when you say you 'reload' the page, are you hitting refresh? Because this effectively reloads the page (and all it's objects) from the server, so you will not experience the effect of cache. Only when browsing a site will cache make a difference.

Monday, August 16, 2010

Property files encoding

Java resource bundle are saved in ascii. This can lead to problems and the special character must therefore be converted. There is a utility which comes with Java which allows to to exactly this.
The file is then awful, but everything is displayed correctly afterwards.


jdk1.6.0_18\bin\native2ascii.exe translations.properties translations_converted.properties

Friday, July 9, 2010

Java Quiz

I found a dirty peace of code and made a small quiz out of it.

What is the result of the following code?

I encountered twice the construction of a inner class
Outter.Inner in = out.new Inner();

but only in books. I never had any use of such a construct.

If you don't find out just try it :)


class Outter {
private int value;
private Inner in = new Inner();

Outter() {
value = 41;
}

void print() {
in.print();
}

class Inner {

Inner() {
print();
}

void print() {
System.out.println(value++);
}
}
}

public class Main {
public static void main(String[] args) {
Outter out = new Outter();
out.print();

Outter.Inner in = out.new Inner();
in.print();
}
}

Thursday, May 13, 2010

Taxme Ubuntu Lucid Lynx

Well this post is very strongly Swiss oriented and of no interest to anyone not living in Bern.

I downloaded the new Taxme software, for paying taxes, nothing very funny!
Unfortunately, it worked fine with Ubuntu 9.10, but with Lucid, it just did not start.
Silently without any notice. The log was not very explicit, but I then tried to start the application with root. And it worked.

i.e. from the console:

sudo ./TaxMeBe2009

There is still a problem with the help which cannot be displayed as apparently the application cannot find firefox.

Strangely I had to download the version with the java virtual machine. But this is most probably due to the fact that I have a 64b system

Monday, May 10, 2010

Ubuntu lucid lynx post installation

There is a few things you need to do after installing Unubu lucid lynx

1. Ubuntu restricted extra (thank you Ranjith Siji, even if I saw it also elsewhere). The problem is if you want real Java as I do, you will have to remove openJDK 6 and install the sun version.

2. As I use gmail on the web, I found a well integrated application (thank D0od ). It is perfectly integrated into the me menu. The problem is you can only watch one mail account at the time.

To install it open a terminal and enter the following lines carefully: -

  • sudo add-apt-repository ppa:gm-notify-maintainers/ppa
  • sudo apt-get update && sudo apt-get install gm-notify

To set up your GMail account with GMail Notifier head to System > Preferences > GMail Notifier Configuration. From here you can also specify alert sounds, labels and on-launch preferences.

3. Skype I'm not very happy that Skype is not in the repositories anymore, thus you have to download it from the skype site

4. vlc media player

5. Desktop Webmail allows an additional integration between gmail and Ubuntu. (Thank you Kaba)
After installing Desktop Webmail (sudo apt-get install desktop-webmail) set-it-up as your default e-mail client in System -> Preferences -> Preferred Application – > Mail Reader, replacing Evolution with desktop-webmail.

Friday, May 7, 2010

Facelets and eclipse

Per default, eclipse does not recognize xhtml files, the default file extension for facelets. I.e., you don't have code completion and the jsf managed beans are not recognized either.

There is a solution:

In window->preferences you do 2 things...

1. In General->Content Types you choose text/jsp in the content types and add *.xhtml as a file association

2. In General->Editors->File Associations select the *.xhtml file type and add th JSP Editor as the Default

I'm not sure if you really need to put the TLD in WEB-INF. (at least with eclipse 3.5) It workds without for me.
now just add the TLD to WEB-INF dir of your project and hey presto tag:completion now works for xhtml files.

I picked this from Andrew Lovell, thanks a lot.

Friday, April 16, 2010

Template Method Pattern

I'm not a big fan of the template method pattern, but it is useful.
Usually I apply it only when I have to made a change, and realize, I have to do it at too many places. I don't like it because it is a bit confusing, especially the stack traces.

Here is an example taken from a db connection.

public abstract class Template{
String sql ;
Template(String sql){
this.sql = sql;
}

public void exec() {
Statement statement = null;
ResultSet rs = null;
try {
statement = getConnection().createStatement();
rs = statement.executeQuery(sql);
execute(rs);
} catch (SQLException e) {
log.error("failed to execute query " + sql);
throw e;
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
log.error("failed to close result set");
}
}
if (statement != null) {
statement.close();
}
}
public abstract void execute(ResultSet resultSet);
}


The problem lies in retrieving something from the execute method


final List results = new ArrayList();
Template call = new Template(sql) {

@Override
public void execute(ResultSet rs) throws SQLException {
rs.next();
Boolean result = rs.getBoolean("BoolColumn");
results.add(result);
}
};
call.exec();
boolean result = results.get(0);


Maybe I missed something, but It is the best way I found for retrieving a value.

Tuesday, March 16, 2010

Hidden feature of ant's Taskdef

I recently had a problem with ant taskdef task.
I could launch findbugs with ant from within eclipse and from the console, but once it was launched by hudson, I got an error:

BUILD FAILED
/var/lib/hudson/jobs/jpf/workspace/build.xml:30: taskdef class edu.umd.cs.findbugs.anttask.FindBugsTask cannot be found

the ant file was correct but I found out (by googling of course) that the by adding a classpathref entry, I solved the problem.









The taskdef task documentation does not evoke the classpathref tag. It should probably be well known, but I don't use ant on an everyday basis.