Posted by Ajay
I think it is time I start of Did you know facts to keep my blog site updated regularly. Let’s start with a Java one
- Did Ya Know that Java has virtual functions?
A virtual function is a member function that you expect to be redefined in derived classes. So yes Java does have virtual functions. Although functions in Java are virtual by default. But in essence what we cann method overriding is actually virtual functions. In order to prevent a method from being vitual just declare it as final.
Posted by Ajay
Have you ever encountered a “Connection Refused” error and beaten your head around to figure out what in the world was happening? Well I found myself sailing in the same boat, just 6 hours before a client demo, a day after I had an outpatient surgery.
What was the issue?
Our application was a web based GIS decision support system. We have a web mapping server that renders out WMS and WFS responses. We have a caching layer, a servlet that caches responses from the web mapping server and connects to it through a URLConnection. Our application is SSL based i.e. we use https. We had a demo scheduled for a client when lo and behold Mr murphy strikes.
We were able to access our application, login to it and perform most of the task. But when accessing a particular cachable GIS layer, bada bing!! … we got the notorious
java.net.ConnectException: Connection refused
So how did we tame the beast ?
So we followed the myriads of steps described in tons of forums:
1. Check apache’s httpd.conf (or ssl.conf if you have SSL configured) configuration and check if the VirtualHost matches the Listen address and port
2. Check if the JkWorkers is defined correctly
3. Check if JkMount mounts the addresses correctly matching the right URL to the right Runner that is defined in the workers file.
Finally we hit jackpot when we found out the real reason for the issue - our hosts table was screwy with incorrect IP address settings
THis leads us to another mystery — how did everything else work and the failure happened only when URLCOnnection was invoked ?? , Sherlock Holmes wanna take the case??
Posted by Ajay
Some bits of news (good?) that has been trickling through the blogosphere is that Java is finally gonna have Closures (whooohooo). Mark Reinhold announced it was time for closures. This would probably be one giant step for Java. Having observed closures in action in Javascript and Groovy, this can definitely affect the way we see Java.
What are closures?
In simple terms closure is dynamic code that can be passed around as objects and as argument to a method(quite similar to pointers to functions in C, but with a difference). In fact closures can refer to variables defined outside the closure within the scope of the closure definition. Function pointers cannot do this.
Here is a simple example from Groovy
1
2
3
| def clos = { println "hello!" }
println "Executing the Closure:"
clos() |
A major distinction between closures and inner classes is that inside a closure you cannot call any other method including itself, a closure cannot call other methods defined in the closure.
Java and Closures
The closest Java comes to providing closure support right now is through anonymous inner classes - it has worked fine so far, but it’s just another name for clunkiness. JDK 7 will now include closures in some form. There are many different proposals out in the wild:
- Gilad Bracha, Neal Gafter, James Gosling and Peter von der Ahe want to build on Ruby and use Ruby like closure mechanism . They introduce function types, where each type definition defines a return type, list of argument types and set of thrown exceptions. This is also popularly known as the BGGA proposal.
Ex:
1
2
3
4
5
6
| public void init(JButton button)
{
button.addActionListener({ActionEvent ev =>
handleButtonPress(ev);
});
} |
- CICE creates a simplified syntax to define Inner classes.
Ex:
1
2
3
4
5
6
| public void init(JButton button)
{
button.addActionListener(ActionListener(ActionEvent ev) {
handleButtonPress(ev);
});
} |
- First Class Method (http://www.jroller.com/scolebourne/entry/first_class_methods_java_style) - This focusses on methods, taking them and using them as objects. This proposal introduces a type safe way to refer to methods(maybe using #), a way to define parameters and method results, a way to create invocable method references and inner methods.
Ex:
1
2
3
4
5
6
| public void init(JButton button)
{
button.addActionListener(#(ActionEvent ev) {
handleButtonPress(ev);
});
} |
Latest Announcements:
So it looks like the starting point of the closure functionality will be a lot of ideas from the First Class Method semantics.
JDK7 Closures are not gonna have control invocation statements nor non local returns and access to non final variables are also unlikely.
My Views on this
The syntax for (1) reminds me of groovy and I do not like it that much, especially being from a Java background using -> gives me the shivers (it reminds me of C++). FCM and BGGA look like they are completely gettign rid of inner classes. Is this a good idea, I am not sure. I like the this reference in FCM and BCGA. Both of these use this to refer to the nearest surrounding class.
But in general the proposals do not vary much except for the semantics of it’s syntax, but hey as long as I get closures I am happy.
References
- http://www.emxsoftware.com/What+are+closures
- http://www.breakitdownblog.com/what-are-closures-in-java/
- http://www.artima.com/weblogs/viewpost.jsp?thread=202004
- http://jroller.com/scolebourne/entry/java_7_comparing_closure_proposals (Examples borrowed from here)
Posted by Ajay
I came across an interesting problem when working on an interesting feature for our web mapping application. Let me first give you a very brief background about our application. Our web mapping setup uses Mapbuilder/OpenLayers as the client side Javascript API, A custom WMS/WFS server, geowebcache as our caching intermediary and PostGIS/PostGRES as our geospatial database. Every web mapping software will have a set of layers that are rendered. Every web mapping software will also have a basemap which defines the projection and bounding box for the model.
Our application has a very beautiful basemap designed by our GIS Analysts, that even includes beautiful hill terrain details. But one fine day our client gave us some extra money to research the feasibility of allowing users to select multiple types of basemaps , allowing them options to use Google and Yahoo basemaps instead of the basemaps we designed for them.
This led me on the journey to figure how to set our client to get a google basemap. One of the very irritating things about Google is their affinity to the 900913 SRS code.
Mapbuilder/OGC provides a very nice standard context document called OWSCOntext that allows Mapbuilder to use non WMS based layers like google and yahoo maps. Setting this up is simple as creating a context document that serves an OWSContext. But the catch is trying to get our WMS layers (that are 4326 based) to work with google maps. Google maps accepts two CRS 4326 and 90013, but guess what, it’s only the 900913 system that guarantees that the WMS layers lines up with the google map. The catch doesnt stop there, apparently PostGIS does not really have 90013 SRS setup.
SO in order to use google maps as a basemap on Mapbuilder/OPenLayers along with custom WMS layers served by our geospatial server, we need to follow the following steps
1. Setup an OWSCOntext document
2. make sure all the bounding box and SRS in the context document is in SRS 900913
3. make sure the spatial database recognizes 900913
4. if you use a custom geospatial server, make sure that you perform necessary transformations to convert your 4326 data to 900913.
And guess what geotools does not support 900913 off the box, but it does allow you to use the WKT of that CRS to create your CoordinateReferenceSystem. Next on my to do list, get our mapping application working with yahoo maps and Microsoft map.
Posted by Ajay
From today I have decided to start blogging right on my home page, no more seperate technical blogs. Today is a good day to blog!!
Tags: introducing