August 26th, 2011

which jar is your class comming from?

If you are unsure about the location where the classloader loads your class from:

Try this:

<yourclass>.class.getProtectionDomain().getCodeSource().getLocation()

December 14th, 2010

Prevent chrome from filling in saved passwords

At my current project I ran into a problem where Google Chrome fills in the password field and an “username” field in a HTML page.
This is a problem because the page is an add new user page. The password of the user currently logged on is filled in to the new user.

You can add the following line to the form element to solve this issue. There are some counterparts on this solution: There are no suggestions on all the other fields either.

autocomplete="off"

May 11th, 2010

Apache memory leak: Windows 2008 + mod_ssl

A customer of my is using the following configuration to realize a SSO solution.
Windows 2008 server with Apache 2.2.14, mod_ssl, mod_auth_cas and mod_proxy.

This configuration is consuming a lot of memory. After a fresh start the child process is using 10 MB memory. After 1 day 400MB and after 3 days the process will hang. All requests from the clients will be killed directly.

We figured out that the directive MaxRequestsPerChild will help you control the memory usage.
The Apache documentation will give some more information about the setting:

The MaxRequestsPerChild directive sets the limit on the number of requests that an individual child server process will handle. After MaxRequestsPerChild requests, the child process will die. If MaxRequestsPerChild is 0, then the process will never expire.

Add the following code to your httpd.conf. We want Apache to serve 1000 requests

MaxRequestsPerChild 1000

May 4th, 2010

Start local OC4J container with more Permspace

Start an local OC4J container from a Unix shell

export OC4J_JVM_ARGS="-XX:PermSize=128m -XX:MaxPermSize=256m -Xmx1024M -Xms1024M"
export ORACLE_HOME=/opt/oc4j/
/opt/oc4j/bin/oc4j -start

April 19th, 2010

css opacity in IE

In Firefox and webkit browsers you can use opacity for transparent elements.

Use the following to make an element transparent in IE:

div.classname {
  #Firefox + webkit
  opacity: 0.5;
  # IE
  filter: alpha(opacity = 50);
}

more information can be found:
http://www.quirksmode.org/css/opacity.html

April 1st, 2010

jQuery: Keyup event is running twice

Today I saw some strange behavior with my key events defined in jQuery. At first everything was working as expected, but after a while key events where called twice.

I have used the following code:

$(document).keyup(function(event) {
	documentKeyupEvent(event);
});

In my case the code above stood in a function which was called multiple times when the user was navigating through the website. So actually the keybinding was declared multiple times either.

You can solve this problem by defining the key binding in the onLoad of the document.

March 31st, 2010

iPhone: First step to customize your web-site for the iPhone

The iPhone is cool; I think there aren’t many people arguing that. The only hard thing about the iPhone is the app store.
Last year I visited Devoxx. Some people wrote a native-iPhone application to browse through the schedule. Unfortunately the approval process of Apple was blocking the applications from going live. Despite all the effort, there was no application during Devoxx.

I think there are many situations where you don’t need a native application at all. With HTML, CSS and Javascript you can create really good looking web-applications.
The coolest thing is, you can actually “install” web-applications “install” on the iPhone and let the user think he is using a native application, when actually a browser is started.

The first thing you need to have is an icon on the home-page of the iPhone:
A simple png image of 60 x 60 pixels will look great: iPhone logo
Even my ugly logo will look okay, because the iPhone will create rounded edges and a glossy look.

Then add a link to the head of your html page.

<link rel="apple-touch-icon" href="images/logo.png"/>

When you now visit the website on the iPhone and create a bookmark on the home-page this logo will be on the home-page:

Ok, now we can simply start the application, but you still need to deal with the address-bar and other Safari buttons. To eliminate these buttons add the following meta tag to the header:

<meta name="apple-mobile-web-app-capable" content="yes" />

The first thing I noticed when browsing with the iPhone was the default scaling the iPhone does with websites.
Sometimes this works great, sometimes it isn’t that great. To help the iPhone to decide how to display the website you can add some meta data to the website.

My favorite setting is the following:

<meta name = "viewport" content = "width = device-width">

The viewport refers to the area of the website that is visible to the user. What this means is that the website itself is not always entirely visible.
The user can now scroll through the website.

Your web-site is now customized for the iPhone.
More information can be found on the Safari Dev Center

March 30th, 2010

Start using git

Git is a great (distributed) version control system. I have put distributed in parentheses because you don’t need to use it in a distributed way.

In my experience there are many people a bit frightened to start using git. But it is pretty simple to start using it. Let me explain my first experience with git to help you start using git.

Most projects I have worked on we used SVN as our versioning system. So of course my current project is one of them. Because my college Andrej Koelewijn was so enthusiastic about git I wanted to experience it myself.

To start using git (after download and install of course) you need to initiate a local git repository witch is bind to the SVN repository :

git svn init http://<host_name>/svn/repo_1

Now fetch the whole SVN repo. (This can be a hard one when you have a large SVN repository, grab a Coffee and relax ;) )

git svn fetch

After this you can use

git svn rebase

To sync your local git repository with the svn repository on the server.

Now you are ready to use git. Grab a new feature from the backlog and create a new branch to work in:

git branch test
and
git checkout test

or in one command
git checkout -b test

Check which branch you are working on now:

git branch
  master
* test

Now make a change to a file and try to commit: for example the pom.xml.
Show the status after the change.

git status
# On branch test
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   klant/pom.xml
#
no changes added to commit (use "git add" and/or "git commit -a")

Commit the file to your git repository with the option -a to add the files and -m for the commit message

git commit -am "commit for blog"

After you have finished your work you can return to the master branch

git checkout master

Merge your test branch to the master.

git merge test

Get the new code from SVN.

git svn rebase

Commit your work to the (shared) SVN repository.

git svn dcommit

Since you had finished your work you can delete your test branch

git branch -D test

Show the branches on you git repository to check if you branch was actually removed.

git branch

March 30th, 2010

use ls in windows dos box

It’s always irritating me when I type ls on a windows system.

The following example will be the result:

C:\>ls
'ls' is not recognized as an internal or external command,
operable program or batch file.

There is a very simple solution for without installing tools like cygwin.

Create a new file named: ls.bat (touch ls.bat ;) )
Place the file in a directory on your path for example: C:\WINDOWS\system32

Now the magic: put the following 3 characters on the first line of the file:

dir

After this you can use ls in windows!!!

C:\Java>ls

C:\Java>dir
 Volume in drive C is SYSTEM
 Volume Serial Number is 00A9-F695

 Directory of C:\Java

12/20/2007  03:47 PM    <DIR>          .
12/20/2007  03:47 PM    <DIR>          ..
08/15/2007  11:41 AM    <DIR>          apache-ant-1.6.5
01/13/2010  06:02 PM    <DIR>          jadnt158
               0 File(s)              0 bytes
               4 Dir(s)   1,996,722,176 bytes free

March 24th, 2010

Post json data to rest service with jQuery

Yesterday I had some issues with sending data to a rest service in json format.
I followed the examples on the jQuery website, but the result was a HTTP Status 415 code: Unsupported Media Type.

This is where Firebug helps a lot. With FireBug it is very easy to show the headers sent to the service.
In my case the following headers where sent:

Host	localhost:8080
User-Agent	Mozilla/5.0 (Windows; U; Windows NT 5.1; nl; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)
Accept	application/json, text/javascript, */*
Accept-Language	nl,en;q=0.5
Accept-Encoding	gzip,deflate
Accept-Charset	ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive	115
Connection	keep-alive
Content-Type	application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With	XMLHttpRequest
Referer	http://localhost:8080/restPhotoService/
Content-Length	41
Pragma	no-cache
Cache-Control	no-cache

I think it is very strange that setting the “dataType” to json only effects the Accept header, the Content-Type (which affects the type of data I sent to the service) is still “application/x-www-form-urlencoded”.

Why is this happening? We are talking about a POST request right? So, my primary concern is about getting my data towards the service!

After setting the “contentType” option to “application/json; charset=utf-8″ the request finally hits the service!!!

My complete setup was:

Jersey rest service:

@POST
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void savePhoto(Photo photo) {
	if (photo != null) {
	System.out.println("savePhoto: " + photo);
	} else {
		System.out.println("savePhoto: photo is null");
	}

}

and the working jQuery call:

$.ajax({
	url:'http://localhost:8080/restPhotoService/rest/photo/',
	type:'POST',
	data: '{"id":"' + photoes[currentPhoto].id + '","name":"' + $("#imageTitle").text() + '"}',
	dataType: 'json',
	contentType: "application/json; charset=utf-8",
	success:function(res){
	alert("it works!");
	},
	error:function(res){
		alert("Bad thing happend! " + res.statusText);
	}
});