Sunday, October 2, 2011

How to enable Maven for Dynamic Web Projects in Eclipse

I'm currently toying around a bit with JSF 2.0 and came to the situation where I wanted to enable Maven dependency management. It's doable but unfortunately not as straightforward as it could be. That's why I'm outlining the steps that have to be done to do just that here. My environment is Eclipse 3.7 (Indigo) with Web Tools Platform (WTP) and m2e (Maven integration for Eclipse).

When creating a new Dynamic Web Project there is already the opportunity to create the Maven typical source folders. For a Web project these are at least
  • src/main/java
    Unsurprisingly this directory contains the Java source code for the Web application (Servlets, JSF components, Managed Beans, validators, renderers, etc.).
  • src/main/webapp
    This directory contains everything that normally resides in a directory called WebContent, i.e. JSPs, Facelets and the like as well as the special directories META-INF and WEB-INF.
Other standard Maven source folders such as src/main/resources may be created as well. Just remove the default source folder src and create the above source folders.

If the project already exists, this is just as easy. Create the correct source folders, move all content that already exists to the correct locations and delete the old source folders.

Now enable Maven by right-clicking the project in the Project Explorer and selecting Configure - "Convert to Maven Project" (This is for the new m2e plugin. For the old m2eclipse plugin this is Maven - "Enable Dependency Management"). Enter all details as required and remember to make sure the packaging type to war.

A few problems have to fixed first: Since the default compiler level for Maven projects is Java 5, Eclipse might complain that no such runtime could be found (if you use Java 5 then I guess you're clear of course). So first thing to do is add the plugin maven-compiler-plugin and configure Java 6 (or maybe 7). Right click the project, select Maven - "Add Plugin", search for maven-compiler-plugin and add it. In the pom you have to add the configuration manually to receive the following (code with Blogger is a pita so I didn't bother):


Then add other dependencies as you need.

Finally the so called Deployment Assembly has to be fixed. Basically this is a mapping from resources as they are in the project to resources as they appear once the application is deployed.

Right-click the project and go to the Properties. Open Deployment Assembly. There is still a mapping from /WebContent. This is not needed, so remove it. Create a new mapping from src/main/java to WEB-INF/classes by clicking "Add…" - Folder, select the desired folder and Finish. It should automatically get mapped to WEB-INF/classes. If not, correct that. Add another folder mapping from src/main/webapp to / (the root). Last but not least add a Java Build Path Entries mapping for Maven Dependencies. It should automatically get mapped to WEB-INF/classes. Again, if that's not the case, correct it. Now add all other mappings as you need them, this is just the bare minimum. In the end you should get something like that:


Now publish and verify. Everything should work.

Saturday, October 1, 2011

How to install Eclipse Marketplace if it is missing

Eclipse 3.6 (Helios) introduced the Eclipse Marketplace that allows easy installation of Eclipse plugins. It still is slow as shit but it's much easier than having to handle all those update sites manually.

For some reason the Marketplace is not bundled in the Eclipse Classic distribution. However it is easy to install. I'm showing this for Eclipse 3.7 (Indigo).
  1. Open Help - Install new Software…
  2. Select the update site for the current Eclipse version (usually it's simply named after the version and has the URL http://download.eclipse.org/releases/indigo
  3. Search for marketplace
  4. The entry Marketplace Client should appear
  5. Install as usual

Thursday, March 31, 2011

How to end all flame wars

Recently it came to my mind how to end all flame wars. It's kinda easy. Just throw all participants together and let them fight every single flame war at once. The sheer number of combinations of possible sides they can choose in every single of them should suffice to single out each individual no matter how large the group. Once they realize this, they stop fighting.

One can dream…

Saturday, July 31, 2010

Need for Speed crashes on Windows 7

Recently I started replaying the Need for Speed parts from the Underground era, which by my definition is anything from the first Underground till Carbon. Despite having some drawbacks (sometimes too much traffic, ridiculous physics, extreme rubber band effect which doesn't add to challenge but lots of frustration and merely shows the incompetence of the AI programmers) these games are those that I replay most often.

Now was the first time I played them on Windows 7 64bit. Underground worked fine but Underground 2 and Most Wanted (Carbon not tested yet) experienced random crashes. Googling didn't yield much information except that the games are not Windows 7 compatible. Of course I couldn't settle for that. One forum entry suggested to deactivate all cores except one for the game. In my desperation I tried that and low and behold this really works. Here is how you do that:


  1. start the game
  2. wait until you are in the main menu
  3. Alt-Tab back to the desktop
  4. open Task Manager
  5. look for speed.exe, right click and choose Set Affinity...
  6. disable all cores but one and click OK
  7. go back into the game


It should be needless to say that this only applies to multi core CPUs. I never experienced another random crash.

Update: Carbon worked out of the box for me.

Thursday, June 11, 2009

How to use SSL with a client certificate in Java

I recently had to access a server (a web service to be specific but this doesn't matter) with a custom client certificate in Java. I never worked with SSL in Java so this was some challenge but I finally managed it. I also found most of the resources on the web rather unhelpful or at the least my specific use case was nowhere covered on the whole. So here is how I did it. Please note my instructions refer to Java 6 SE only.

Let me start at the end. What you need for SSL in Java is a keystore containing your private key information and a trust store containing the certificates you trust. When you access a secure site with a browser and the certificate authority (CA) that signed the site's certificate is unknown to the browser you get a prompt whether you trust this certificate or not. The trust store is the equivalent of you accepting a new certificate in a web browser. This is only needed in cases where you signed the certificate yourself.

The rest of this article assumes the file that represents the keystore is named keystore.jks and the file that represents trust store is named cacerts. The default format of the keystore is the proprietary Java KeyStore format, hence the .jks suffix. Java KeyStores are created with the keytool which is shipped with every Java 6 SDK.
Note: If you don't specify a file for the keystore, a file is created for you in your home directory (in Linux it's ~/.keystore). In this case you don't need to set up anything special in your Java program because this keystore is automatically searched for in case you try to establish a secure connection. I deliberately use my own file because it's a little bit more flexible—especially in development environments. There also exists a global trust store. You can find it under <jdk-install>/jre/lib/security/cacerts. I don't recommend to tamper with this file for development purposes. At the very least backup this file before you modify it.

Anyway, how do we create these two stores?

At first I assume you have a signed client certificate in e.g. PEM format. This means the content of the certificate will look something like this:

-------- BEGIN CERTIFICATE ----------
// lots of base64
-------- END CERTIFICATE ------------
-------- BEGIN RSA PRIVATE KEY ------
// lots of base64
-------- END RSA PRIVATE KEY --------

I'll refer to this file with the name client-cert.pem.
I think it doesn't really matter if the file has another format because you can convert many of these formats to PEM with one of the OpenSSL utilities. We're going to need these tools anyway.

The first approach I used does NOT work:

keytool -importcert -keystore keystore.jks -file client-cert.pem

The resulting file contains no information about the private key. Somehow only the certificate gets imported or something. You can easily verify this by issuing the following command:

keytool -list -v -keystore keystore.jks

This lists the entry type trustedCertEntry. What we need is PrivateKeyEntry.
To be honest I don't really understand this behaviour and it took me hours to realize that this step was the reason SSL didn't work for me.

At first we have to create a certificate in PKCS#12 format. With OpenSSL we can achieve this like that:

openssl pkcs12 -export -in client-cert.pem -out client-cert.p12

You get a prompt to enter a password. It is possible to leave this empty and theoretically you could do that because this file is only an intermediate file for your Java KeyStore. Don't leave the password empty! keytool doesn't like empty passwords so you definitely have to provide one. Just use password or something.

Now you get a new file client-cert.p12. Next we can create the keystore:

keytool -importkeystore -srckeystore client-cert.p12 -srcstoretype PKCS12 -destkeystore keystore.jks

You get prompted for the password for the PKCS#12 certificate (this is the point where an empty password is fatal) and another one for your new keystore. It is not possible to enter a password with less than six characters. If you check with the above used -list command you can see that this time the entry type is correct.

If you use a self signed certificate you need to create an appropriate trust store. I refer to this blog post. I used the program InstallCert with a few modifications (basically I changed some file paths because I wanted my very own trust store as I mentioned before). I hope this provides no difficulties.

Now we want to use these two files in our Java program. This is quite easy to accomplish. There are four properties we need: javax.net.ssl.keyStore and javax.net.ssl.trustStore for the file locations of our key and trust store respectively. The passwords to the files are provided with javax.net.ssl.keyStorePassword and javax.net.ssl.trustStorePassword.

You set these properties either before you start the JVM like this:

java -Djavax.net.ssl.keyStore=/path/to/keystore.jks -Djavax.net.ssl.keyStorePassword=password etc.

or in your code like this:

System.setProperty("javax.net.ssl.keyStore", "/path/to/keystore.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
// etc.


Well, I hope this helps someone. This took me quite some time to figure out.

Sunday, June 7, 2009

Slashdot in Opera

I'm a longtime Opera user that still refuses (and hopefully always will) to switch to Firefox. I just don't see any need for it. I'm sort of proud of it and regularly advertise it without trying to be too nagging. I believe in freedom of choice so use what you want, be it Safari or IE or lynx.

But one thing that has always been bugging me is the browsing experience of Slashdot in Opera. There is just one word for it: abysmal. I don't understand how a site can honestly proclaim to have "News for nerds" without paying respect to a few standards regarding the layout and design of the site. Right now the main page doesn't validate with 84 errors. Now I dont't think that validation is everything (although I'm a fan of it). But I think that every site owner should check that his site looks and feels good in every major browser. And even that is only partly necessary because in today's software world with all its reusing of components many browsers share the same layout engine (Gecko, KHTML/WebKit, Trident, Presto, …).
Anyway… the design of Slashdot is quite broken. The "new" discussion system D2 doesn't really work. I really like it because it enables you to fold in and out comments on the fly without having to reload an entire page which is much faster. But after opening and closing a comment certain elements of the page disappear. At least it used to be the case. Right now it doesn't work at all. Clicking the headline of a comment opens a new page with the comment in question.
A few days ago I had the error that headlines of all comments were white on white background. Every time I wanted to see the titles or scores I had to highlight it which was very annoying. It also seemed to be limited to certain categories of the site but I didn't look for it and thank god this issue seems to be fixed anyway.
But now something new appeared: Weird graphics appear in the corners of many comments (see screenshot below).

These strange elements seem to be part of this bigger graphics file. Although the file name suggests something about controls, in Opera these annoying things serve no purpose. I noticed that Chrome has these as well but not IE6 (I have no newer version at hand).
The tag system also doesn't work. Upon hovering the keywords there used to appear some "things" (category highlighters or something, I never knew what they were supposed to be/do) but right now nothing happens anymore. Then again I never used the tags so I don't really care about this issue.

I really don't see why the site gets online like that. Especially a site for nerds. Of course the right course of action would be to file a few bug reports. The reason I didn't do it in the past is that these bugs are just obvious. The don't require long steps to reproduce. You just open the site and they slam you in the face.

From the comments of Opera related news one can easily see that there are quite a few Opera users on Slashdot. Also Opera is quite nice (see video) to Slashdot. Why can't Slashdot be nice to Opera?

-------
Update: It got worse.

-------
Update 2: It seems to be over. Could've been for a couple of days already but I didn't have much time to read Slashdot lately.

Friday, November 21, 2008

tcsh madness

Today I had a rather interesting experience with the tcsh.

At the university I attend are different computer pools. There's Windows (recently the machines have been upgraded to Vista), there's Solaris (recently upgraded to Solaris 9 I think) and there is Linux (recently upgraded to a newer version of Suse Linux, I don't know if it's the newest but newer than what was running there before). So obviously over the last summer holidays upgrade mania raged and the machines are all slow as hell. Well, maybe except for the Solaris terminals but I don't get to use them a lot anymore.

In one particular course we always visit the pool with the Suse machines. Fine by me. The default shell is tcsh. Every time I opened a new shell it would use all processor resources for about 10 seconds before showing me the prompt, hogging about 500 megs of RAM as well. I was used to the fact that the machines are slow but that is just ridiculous. It's a shell! But I didn't bother the last few times because in the time I have in the course I need to get some stuff done. Like listening to the professor, trying out stuff he talks about and playing Frozen Bubble. By the way: closing a shell would also take about 15 to 20 seconds.

Today I thought I could speed up the process a bit by spawning a couple of shells right at the beginning. It was only then that I learned about the insanely huge memory hunger as my RAM got used up and finally the machine started to swap. I barely could move my mouse anymore. So I started to look around a bit. At first I started a bash. In the blink of an eye it was there. Then I strace'd a new tcsh and got quite an impressive trace with 3.5 mb in size (compared to a few hundred kb of a new bash). I looked through it and found a lot of lines that resembled Linux commands. Could it be the history? Well yeah. I looked at my .history file and indeed it was about 26 mb in size. While I have to admit that a 26 mb history file is way too large I also fail to understand how anyone could make 500 mb out of it.

So there it was: tcsh read the history, did something with it and then continued. After closing the shell it would write the history back. This also caught me the first time when I deleted the history file. After closing the shell it was back of course. So I just closed all shells (which took quite some time), opended a new one, deleted that pesky file and killed the shell.

I'm a bit more used to bash but changing the shell with chsh didn't work either...