Monday, January 30, 2006
How to write a jaxb2 plugin ?
- Subscribe to users@jaxb.dev.java.net;
- Check out the JAXB2 RI workspace. See https://jaxb2-sources.dev.java.net/ for the instruction;
- Consider looking at the "Developing on top of JAXB RI" section of https://jaxb.dev.java.net/. The sample plugins in XJC should help you get started on how to write one;
- The jaxb2-commons project is intended to host plugins for JAXB 2;
- For example, after writing to users@jaxb.dev.java.net, I was added as a developer and directories "fluent-api" and "www/fluent-api" were created. I can put whatever I want there. Files in "www/fluent-api" can be served from http://jaxb2-commons.dev.java
.net/fluent-api/ - Projects in java.net are encouraged to use "org.jvnet.
", so I used "org.jvnet.jaxb2_commons" as my plugin's package prefix. - Feel free to update www/index.html with a link to www/fluent-api/
- Pick the license of your choice.
Hsql File Locking failed in Ant + Windows
I had two junit tests using hsql to perform db operations. They were run using Ant 1.6.5 with hsql-1.8.0.2. When run on Linux 7.2, they worked fine. When run in Eclipse 3.1.1 (without using Ant), they also worked fine.
However, when they were run in ant 1.6.5 + Cygwin on Windows, the 2nd test always failed saying it couldn't acquire the lock as another process already had it.
Thanks to Campbell Boucher-Burnet, the problem can be resolved via the shutdown=true connection property. Details can be found here.
More on hsql connection properties can be found here.
However, when they were run in ant 1.6.5 + Cygwin on Windows, the 2nd test always failed saying it couldn't acquire the lock as another process already had it.
Thanks to Campbell Boucher-Burnet, the problem can be resolved via the shutdown=true connection property. Details can be found here.
More on hsql connection properties can be found here.
Sunday, January 29, 2006
Comparing IDE's on support of Java 5 Generics
One interesting thing I've observed from building jaxb 2.0 (from source) is that if I set up a project in Netbeans (4.1.x) or IntelliJ (5.0.x) or just build it directly via Ant (1.6.5), the source code compiles just fine. But if I set up a project in Eclipse (3.1.2), a lot of source codes with Java 5 generics constructs are marked as errors by the IDE! (Example: ElementInfoImpl.java under package com.sun.xml.bind.v2.model.impl)
This seems to indicate that as jar as Java generics goes Eclipse is not quite up to the task yet.
I am not sure if they have anonymous cvs access, but if you subscribe to www.dev.java.net (which is free), you can check out the jaxb 2.0 cvs source tree and see the IDE differences (simply by importing the existing project config files set up for various IDE's including Eclipse, Netbeans and IntelliJ):
Update on 23Feb06: I received a email from Kent Johnson saying he has this bug fixed in Eclipse 3.2M6. Thanks Kent!
This seems to indicate that as jar as Java generics goes Eclipse is not quite up to the task yet.
I am not sure if they have anonymous cvs access, but if you subscribe to www.dev.java.net (which is free), you can check out the jaxb 2.0 cvs source tree and see the IDE differences (simply by importing the existing project config files set up for various IDE's including Eclipse, Netbeans and IntelliJ):
I filed a bug report to Eclipse https://bugs.eclipse.org/bugs/show_bug.cgi?id=125956.cvs -d:pesrver:yourid@cvs.dev.java.net:/cvs co -d jaxb-ri jaxb2-sources/jaxb-ri
Update on 23Feb06: I received a email from Kent Johnson saying he has this bug fixed in Eclipse 3.2M6. Thanks Kent!
Thursday, January 26, 2006
JAXB 2.0 XJC Fluent API Plugin launched!
As mentioned in my previous post, one thing I wish to have is the support of method chaining in JAXB. It turns out it's really easy to write a JAXB 2.0 XJC plugin. Indeed, you can now download it from https://jaxb2-commons.dev.java.net/fluent-api/.
It's also announced at the acquarium and mentioned here.
Special thanks to Kohsuke Kawaguchi.
It's also announced at the acquarium and mentioned here.
Special thanks to Kohsuke Kawaguchi.
Tuesday, January 24, 2006
Fibonacci Numbers in Scheme
A Fibonacci number is basically the sum of the previous two.
Warning: reading futher will reveal a faster version.
Fast version in Scheme:
Simple but slow version in Scheme:pos: 0 1 2 3 4 5 6 7 8 9 10 ...
fib: 0 1 1 2 3 5 8 13 21 34 55 ...
How can we make it faster ?; Time is of exponential order of N; whereas
; space is of linear order of N
(define (fibslow N)
(cond ((< N 2) N)
(else (+ (fibslow (- N 1))
(fibslow (- N 2))))))
Warning: reading futher will reveal a faster version.
Fast version in Scheme:
To feel the performance difference, try:; Time is of linear order of N; whereas
; space is of constant order of N
(define (fibfast N)
(cond ((< N 2) N)
(else (fibup N 2 1 0))))
(define (fibup MAX count n-1 n-2)
(cond ((= MAX count) (+ n-1 n-2))
(else (fibup MAX (+ 1 count) (+ n-1 n-2) n-1))))
vs.(fibslow 30)
(fibfast 30)
Monday, January 23, 2006
Tower of Hanoi in Scheme
Sample run:;Solving the Tower of Hanoi in Scheme
(define (move N from to spare)
(if (= N 0)
(display "")
(begin
(move (- N 1) from spare to)
(display "move from ")(display from)(display " to ")(display to)(display "\n")
(move (- N 1) spare to from))))
How can we solve it iteratively (ie via tail recursion) ?> (move 3 'start 'end 'spare)
move from start to end
move from start to spare
move from end to spare
move from start to end
move from spare to start
move from spare to end
move from start to end
Warning: reading futher will reveal an iterative version.
It requires a lot more code and not in any way faster.
To run, simply type:; Solving the Tower of Hanoi iteratively in Scheme
(define (moveIter N from to spare)
(moveIterImpl N from to spare () ()))
(define (moveIterImpl N from to spare displayList moveList)
(if (= N 0)
(if (null? displayList)
(display "")
(begin
(output (car displayList))
(if (null? moveList)
(display "")
(eval (toMoveIterCmd moveList displayList)))))
(moveIterImpl (- N 1) from spare to
(cons (toPair from to) displayList)
(cons (toMoveParam (- N 1) spare to from) moveList))))
(define (toMoveIterCmd moveList displayList)
(cons 'moveIterImpl
(cons (car (car moveList))
(cons (cons 'quote (cons (car (cdr (car moveList))) ()))
(cons (cons 'quote (cons (car (cdr (cdr (car moveList)))) ()))
(cons (cons 'quote (cons (car (cdr (cdr (cdr (car moveList))))) ()))
(cons (cons 'quote (cons (cdr displayList) ()))
(cons (cons 'quote (cons (cdr moveList) ()))
()))))))))
(define (toMoveParam N from to spare)
(cons N (cons from (cons to (cons spare ())))))
(define (output displayItem)
(begin
(display "move from ")(display (car displayItem))
(display " to ")(display (car (cdr displayItem)))
(display "\n")))
(define (toPair from to)
(cons from (cons to ())))
> (moveIter 3 'start 'end 'spare)
MIT Open Courseware
Saturday, January 21, 2006
Trivial Java Quiz
Can you name a public method in JDK1.5 that exists in a class, but cannot be found declared or defined anywhere in the class (source file) ? In other words, you can invoke the method and it will behave as expected, but the method (and even the method name) cannot be found in the source code of the class (or it's super classes or super interfaces), nor in the respective javadoc.
Warning: reading futher will reveal the answer.
Here is one answer:
It appears there are two static methods generated for every enum class:
For (1), it puzzles me why there isn't also a public static method like:
The Enum javadoc doesn't even mention the 2 generated static methods, although one can see them when using auto-completion in an IDE. Seems like an oversight to me.
Warning: reading futher will reveal the answer.
Here is one answer:
It appears there are two static methods generated for every enum class:
- method values(), which returns the full list of enum instances of the specific Enum class; and
- method valueOf(String), which returns the respective enum instance with the same name as the input string.
// existing method in Enum
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name);
// why not
public static <T extends Enum<T>> T[] values(Class<T> enumType);
Structure and Interpretation of Computer Programs
http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/
The video is pretty cool, and the online book is free!
The video is pretty cool, and the online book is free!
Tuesday, January 17, 2006
Nice CSS Tutorials
Thursday, January 12, 2006
Dynamically create HTML hidden input fields
Here is one way to dynamically create HTML hidden input fields via Javascript:
What if you want to to dynamically create a form ? Here you go:/*
Adds an html hidden input field (dynamically created) to the given HTML form element.
@param formElement the given HTML form element
@param fieldName the name of the hidden input field
@param fieldValue the (string) value of the hidden input field
*/
addHiddenInputField(formElement, fieldName, fieldValue) {
var inputElement = document.createElement("input")
inputElement.setAttributeNode(createHtmlAttribute("type", "hidden"))
inputElement.setAttributeNode(createHtmlAttribute("name", fieldName))
inputElement.setAttributeNode(createHtmlAttribute("value", fieldValue))
formElement.appendChild(inputElement)
return
}
/*
Creates an html attribute.
@param name the name of the attribute.
@param value the (string) value of the attribute.
@return the newly created html attribute
*/
createHtmlAttribute(name, value) {
var attribute = document.createAttribute(name)
attribute.nodeValue = value
return attribute
}
/*
Adds an html form (dynamically created) to the HTML body element (assuming it exists.).
@param actionValue action string
@return the newly created HTML form element.
*/
addHtmlForm = function(actionValue) {
var formElement = document.createElement("form")
formElement.method = "POST"
formElement.action = actionValue
var body = document.getElementsByTagName("body")[0]
body.appendChild(formElement)
return formElement
}
Wednesday, January 11, 2006
Cygwin Tricks
- To open up a Windows Explorer on the current directory in Cygwin, type:
explorer .
- To cd to a particular directory in Cygwin using a Windows path, simply quote the path. eg:
cd "c:\a\b\c"
where the Windows path can be copied and pasted from Windows Explorer.
Customizing Vim 6.3
Here is how I set up my Vim 6.3 installed at C:\Vim\vim63 on Windows:
- set VIM_HOME=c:\vim
- put c:\vim\vim63 in PATH
- Add the line
source $VIMRUNTIME/hchar.vim
to file c:\vim\_vimrc
- Create the file c:\vim\vim63\hchar.vim with the following content:
source $VIMRUNTIME/colors/murphy.vimassuming the Bitstream font has been installed.
set guifont=Bitstream_Vera_Sans_Mono:h9:cANSI
set nows
JWSDP Wish List
It would be nice if JAXB supports:
- Command chaining in setting the content model;
- Supports failfast validation so setting an invalid value causes an exception instead of delaying the process to validate-on-demand or unmarshalling;
- Supports Java 5 generics in the generated classes; (Actually this is supported in JAXB 2.0 but not JAXB 1.6.)
- Uses Java 5 enum instead of simulating a type safe enum; (This is also supported in JAXB 2.0)
Tuesday, January 10, 2006
Tapestry 3.0 in Hindsight
About 9 months ago I worked on a 3 month contract that developed a brand new webapp in Tapestry 3.0. I've done a lot of Struts and other home-grown frameworks. My impressions on Tapestry are:
- Highly and truly reusable component is possible in Tapestry;
- Extremely steep learning curve, and the Tapestry in Action book is a must read; Not a very good book though as it focuses too much on how Tapestry does things rather than how it can be used.
- Almost completely incompatible with other web framework such as Struts, JSP, etc.
- Almost no access to the underlying Servlet API.
- I frequently needed to dig into the Tapestry implementation source code to figure out what's going on.
- I preferred POJO and didn't like the fat class hierarchy that must be inherited.
- The lack of a MVC 2 framework seems like a step back.
- Code base is changing rapidly. Tapestry 4.0 has just been released, and the documentation is probably still a problem.
Friday, January 06, 2006
Control browser caching from jsp
The trick is to do it via http:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
long time = ... // get the file last modified or deployment time.
response.setDateHeader("Last-Modified", time);
%>
<c:choose>
<c:when test='${empty header["If-Modified-Since"]}'>
<%
System.out.println("Reponding with full content.");
%>
</c:when>
<c:otherwise>
<%
// check the If-Modified-Since against some value...
System.out.println("Setting not modified status");
response.setStatus(javax.servlet.http.HttpServletResponse.SC_NOT_MODIFIED);
%>
</c:otherwise>
</c:choose>
Thursday, January 05, 2006
Oracle's Project Raptor rocks!
A great cross platform substitute for Toad.
Update March 06: Oracle has renamed the project. It's now called Oracle SQL Developer.
Update March 06: Oracle has renamed the project. It's now called Oracle SQL Developer.