Google
 
Web unafbapune.blogspot.com

Monday, January 30, 2006

 

How to write a jaxb2 plugin ?

  1. Subscribe to users@jaxb.dev.java.net;
  2. Check out the JAXB2 RI workspace. See https://jaxb2-sources.dev.java.net/ for the instruction;
  3. 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;
  4. The jaxb2-commons project is intended to host plugins for JAXB 2;
  5. 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/
  6. Projects in java.net are encouraged to use "org.jvnet.", so I used "org.jvnet.jaxb2_commons" as my plugin's package prefix.
  7. Feel free to update www/index.html with a link to www/fluent-api/
  8. 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.

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):
cvs -d:pesrver:yourid@cvs.dev.java.net:/cvs co -d jaxb-ri jaxb2-sources/jaxb-ri
I filed a bug report to Eclipse https://bugs.eclipse.org/bugs/show_bug.cgi?id=125956.

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.

Tuesday, January 24, 2006

 

Fibonacci Numbers in Scheme

A Fibonacci number is basically the sum of the previous two.
pos: 0 1 2 3 4 5 6 7  8  9  10 ...
fib: 0 1 1 2 3 5 8 13 21 34 55 ...
Simple but slow version in Scheme:
; 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))))))
How can we make it faster ?

Warning: reading futher will reveal a faster version.

Fast version in Scheme:
; 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))))
To feel the performance difference, try:
(fibslow 30)
vs.
(fibfast 30)

Monday, January 23, 2006

 

Tower of Hanoi in Scheme

;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))))
Sample run:
> (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
How can we solve it iteratively (ie via tail recursion) ?

Warning: reading futher will reveal an iterative version.

It requires a lot more code and not in any way faster.
; 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 ())))
To run, simply type:
> (moveIter 3 'start 'end 'spare)

 

MIT Open Courseware

http://ocw.mit.edu/index.html

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:
  1. method values(), which returns the full list of enum instances of the specific Enum class; and
  2. method valueOf(String), which returns the respective enum instance with the same name as the input string.
For (2), interestingly there is also a public static method valueOf with 2 parameters defined in the source file of Enum:
// existing method in Enum
public static <T extends Enum<T>> T valueOf(Class<T> enumType, String name);
For (1), it puzzles me why there isn't also a public static method like:
// why not
public static <T extends Enum<T>> T[] values(Class<T> enumType);
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.

 

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!

Tuesday, January 17, 2006

 

Nice CSS Tutorials

http://css.maxdesign.com.au/index.htm

Thursday, January 12, 2006

 

Dynamically create HTML hidden input fields

Here is one way to dynamically create HTML hidden input fields via Javascript:
/*
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
}
What if you want to to dynamically create a form ? Here you go:

/*
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

explorer .
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:
source $VIMRUNTIME/hchar.vim
to file c:\vim\_vimrc
source $VIMRUNTIME/colors/murphy.vim
set guifont=Bitstream_Vera_Sans_Mono:h9:cANSI
set nows
assuming the Bitstream font has been installed.

 

JWSDP Wish List

It would be nice if JAXB supports:

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:

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.

This page is powered by Blogger. Isn't yours?