case
25Jan/100

Static files locked by Jetty in Eclipse

jetty_logo If you use Jetty you might notice that once it's running you can't edit any static files because it says they are already in use and locked.
Why ? Because Jetty buffers static content for webapps such as html files, css files, images etc and uses memory mapped files to do this if the NIO connectors are being used. The problem is that on Windows, memory mapping a file causes the file to be locked, so that the file cannot be updated or replaced. This means that effectively you have to stop Jetty in order to update a file.

In case this happens follow these steps:

1. Extract the runjettyrun_1.0.1.jar in the eclipse plugin directory
2. Extract the jetty-6.1.6.jar in the lib directory of the previous jar.
3. Edit the file org\mortbay\jetty\webapp\webdefault.xml and change the "useFileMappedBuffer" to false. It should look like the following:

1
2
3
4
<init -param>
      <param -name>useFileMappedBuffer</param>
      <param -value>false</param>
</init>

4. Pack everything back up and overwrite the runjettyrun jar in the plugin directory.
5. If you still get the error after step 4 start eclipse.exe with -clean and then recreate the Jetty configuration. (It just need to replace whatever jar's it copies in your workspace .plugins dir)

24Sep/094

The simplest way to parse XML in Java

You can do a lot with XML, but often all you want to do is to read a simple file with some basic data in it. The options for doing this, SAX, DOM and JAXB are all relatively verbose and often off-putting. So here is a class that will make all this much much more simple.

Parsing a file such as:

1
2
3
4
5
6
7
8
9
10
11
12
<config>
	<title>test</title>
	<version major="1" minor="2"/>
	<roles>
		<role name="admin"/>
		<role name="user"/>
	</roles>
	<users>
		<user name="joe" password="pass" role="admin"/>
		<user name="harry" password="secret" role="user"/>
	</users>
</config>

Can be achieved with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Xml config = new Xml("config.xml","config");
 
System.out.println("title: "+config.child("title").content());
 
Xml version = config.child("version");
System.out.println("version: "+version.integer("major")+"."+version.integer("minor"));
 
for(Xml role:config.child("roles").children("role"))
	System.out.println("role: name: "+role.string("name"));
 
for(Xml user:config.child("users").children("user"))
{
	System.out.println(
		"user: name: "+user.string("name")+
		", password: "+user.string("password")+
		", role: "+user.string("role"));
}

As you can see, it's nice and simple and allows you to get to the information quickly and without any hassle. The API uses the DOM parser underneath, but attempts to make the data more easily available. All you need is the following [Click to download] class, which you can of course customise however you like.

Update:
I've just added another nice method to this class. Is called e4xEval, that is actualy a xpath eval but i made it use the flex e4x syntax. So, if you want to get all the role name you can do:

1
2
3
 
Xml config = new Xml("config.xml","config");
String[] list = config.e4xEval("config.roles.role.@name");  // @ is attribute

Tagged as: , , , 4 Comments