Tuesday, September 21, 2010

Abusing? javascript functional powers

Javascript being a functional language makes it very easy to pass functions around as param to other function.

However when we do that we might lose readability of the code

Consider this:

mapcar1 is just another function, same as dojo.forEach in functionality.

The code in bold has lots of redirection and very hard to debug as it calls the callback if something is successful etc.

withTransforms : function (transforms, callback, errorCallback, config) {

if (reqNeeded) {

var trs = mapcar1 (
function (t) {

return {
"toSrs" : t.to
};
},
transforms);

var callbacks =
{
success : callback,
failure :this.cbWithTransformsFailed,
b4Success : this.cbB4SuccessWithTransforms,
scope : this
};

this.ajaxTool.post (
getWebappBaseUrl () + "/transformGeometry.do",
{
"transformRequests" : trs
},
callbacks,
config);

} else {

callback (
mapcar1 (
function (t) {

return t.geometry;
},
transforms));
}
},

Sunday, September 19, 2010

Java Hidden Iterator causing ConcurrentModificationException

The " System.out.println("DEBUG: added ten elements to " + set);" effectively compiles into a HiddenIterator that iterates the "set", and this is not thread-safe.

Work around is to wrap "set" into a synchronizedSet

public class HiddenIterator {
@GuardedBy("this")
private final Set set = new HashSet();

public synchronized void add(Integer i) { set.add(i); }
public synchronized void remove(Integer i) { set.remove(i); }

public void addTenThings() {
Random r = new Random();
for (int i = 0; i < 10; i++)
add(r.nextInt());
System.out.println("DEBUG: added ten elements to " + set);
}
}

Wednesday, September 15, 2010

Weblogic + maven2 + war

1. Add the following to your pom.xml, making sure you have the correct path to weblogic
[...]
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<container>
<containerId>weblogic103x</containerId>
<home>C:OracleMiddlewarewlserver_10.3</home>
</container>
</configuration>
</plugin>
[...]

2. mvn cargo:start