0

February 17

DYK Fact #1

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.

 
0

January 12

Connection refused

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??

 
0

November 20

Bringing closures closer to Java

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:

  1. 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);
       });
    }
  2. 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);
       });
    }
  3. 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

  1. http://www.emxsoftware.com/What+are+closures
  2. http://www.breakitdownblog.com/what-are-closures-in-java/
  3. http://www.artima.com/weblogs/viewpost.jsp?thread=202004
  4. http://jroller.com/scolebourne/entry/java_7_comparing_closure_proposals (Examples borrowed from here)

 
0

July 28

REST from a GIS developer’s perspective

Posted by Ajay

Lately I have been instrumental in converting our GIS web based functionality into more modularized service based functionality. A year back, our application had a very tightly coupled custom built WMS/WFS server. Imagine being constrained to only using the client that our web application provided, imagine a rogue application that did not follow the WMS/WFS specification, thats what we had - a tightly coupled rogue. This was fine as long as we serviced just one client. But we could not hope to market our product unless we could shift to a more service oriented framework, or atleast a more modular one.

That is when we envisioned the idea to make our GIS more service oriented and modular. This laid the beginning of our trailblazing path towards geoserverization. Now after a couple of months of effort we have a WMS/WFS server that can be used as a GIS service from any client the user prefers to use. All was nice and dandy with this world. Until the day the user wanted more.

One fine day our users decided they wanted to edit the layers on the fly. They wanted to change layer styles through an admin interface in our web application. They even went all the way to asking us to provide functionality, in the admin GUI, to allow them to modify a layer’s data and update it on the fly, as the application was running. Guess who came to my rescue, none other than the REST functionality packaged within geoserver. The rest of this post gives you an introduction to REST and a quick info about geoserver’s REST interface and how I implemented our functionality using REST.

What are thou REST ?

REST or Representational State Transfer is an architectural style, it is not a design pattern nor a true architecture, but just a style, a set of rules that any architecture should conform to. It defines a common interface set that any REST based design can interpret and use. REST revolves around the use of URIs and the use of the HTTP protocol.

REST provides a lot of inbuilt security safeguards since the method that is used can be an indicator of the action that is to be performed – a GET is safe, POST, PUT and DELETE are relatively unsafe. REST provides authorization and authentication through the web server’s inbuilt functionality.

Key Concepts in REST

  1. Application state is called as a resource and is identified by a URI. Resources communicate through a standard interface, HTTP. Ex: book is a resource concept. http://myserver.com/book is the URI that returns a file which is a representation of the book resource.
  2. Information about a resource is called state and is encoded as part of the resource; these encodings along with the resource are called representations of the resource. Ex: http://myserver.com/book/book1 with an Accept header of text/xml is a particular representation of the resource, which returns the data as XML.
  3. Representations are linked together through hypermedia or links. Ex: I can access readers of a book as follows: http://myserver.com/book/users
  4. Invoking a REST interface method transfers the state of the resource through representations.
  5. The key set of standard interactions are
    • GET – it says give me the information in my chosen format
    • POST – it says add information that I give you
    • PUT – it says replace my information with what you have currently
    • DELETE - it says delete the information that you have

  6. REST can evolve with the standard interactions staying the same, new document types can be created and yet the interactions stay the same.
  7. It extends the statelessness that HTTP provides, every request is completely encapsulated to perform it’s task independent of previous tasks

REST and Geoserver

Geoserver has been providing a very efficient REST based interface to allow access to geoserver’s admin functionality. Geoserver’s REST defines the following key concepts:

  1. workspace – A group of data stores and feature types. This is equivalent to a namespace.
  2. datastore – A source of vector based spatial data. It may be a PostGis datastore, shapefile or even a WFS server.
  3. featuretypes – A vector spatial resource that originates from a data store
  4. coveragestore – A source of raster based spatial data.
  5. coverages – A raster based dataset with a coveragestore as the data store
  6. styles – describe how a resource should be symbolized or rendered
  7. layers – it is a published resource, equivalent to a feature type or coverage
  8. layergroups – grouping of layers accessible under a single resource name

One of the key aspects of using REST in my project was to implement two major functionalities:

Provide users a way to dynamically set user styles on the fly.

There are many ways to implement this. The WMS specification allows for SLD and SLD_BODY parameters to be provided as part of the GetMap request. Unfortunately these two methodologies would not work if users store their data in a database. One of the main problems with the SLD parameter is that it requires a SLD file to be created on the server, on the fly which was not an option for us. So. Moving on to SLD_BODY. I was so happy when I implemented this, and it worked, ooooh but I got happy a bit too early. Unfortunately for me I hit upon a restriction in IE, the dreaded GET request length, something I did not even think of since I primarily use Firefox, but unfortunately users use IE. Finally I decide to settle on using the REST interface provided with geoserver.

I decided to use Jersey as my RESTful client API. Jersey builds on a builder pattern to simplify the task of creating a Client, Resources and invoking the RESTful methods, all in a couple of lines of code. At it’s base it uses apache’s HttpClient to perform it’s task with multithreaded efficiency.

A simple PUT as follows:

PUT http://myserver/geoserver/rest/styles/layerName with the SLD string as the request body performed the work for me – modify existing layer’s style with the new style information.

Status codes play a big role in defining how the process was performed. The major ones to be really concerned about are

  • 200 – OK - if GET, PUT executed fine
  • 201 – Created – if POST executed fine
  • 403, 404, 405 – if resource could not be updated for some reason

Allow users to be able to modify a layer

Imagine that you have an application and the user wants to be able to modify a layer on the fly, maybe he has his own data that he wants to upload. Now imagine a restriction that you cannot use the geoserver admin interface. How would you do this? REST baby REST!!

My requirement was to change the originating datastore from a postgis to a shapefile. This was as easy as a PUT to create a new datastore, a DELETE to delete the existing the featuretype and layer, a POST to create a new featuretype, tadaaa…, as easy as pie, all courtesy of the REST API.

How is REST different from RPC

  • RPC application is exposed as a network object with one or more exposed public functions, so before communication client must have information about the network object. In REST, this is accomplished through hyperlink relations between objects. RPC uses named operations while REST uses named resources.
  • RPC like SOAP can have support for multiple protocols, REST depends on HTTP.
  • RPC invariably involves some concept of transaction like WS-AtomicTransaction, REST on the other hand follows HTTP phiolosophy, i.e. deal with failures through retries
  • REST provide more diverse formats like JSON,a dn better scalability since it can be cached, SOAP requests cannot be cached

References

  1. http://rest.blueoxen.net/cgi-bin/wiki.pl
  2. http://tomayko.com/writings/rest-to-my-wife
  3. http://en.wikipedia.org/wiki/Representational_State_Transfer
  4. http://www.infoq.com/articles/rest-introduction

Tags: , , ,

 
1

June 5

Virtual Box - Virtualization, the way FOSS (Free and Open source) does it

Posted by Ajay

How it all began

It was one of those fine Wednesday mornings when my machine crashed for no obvious reason, well there was an obvious reason, it was a Windows machine. So grumbling all the way, I started contemplating life and it’s grand ironies, when I decided for better or for worse to switch over to Ubuntu. It worked great, I loved the interface, the speed, the no crashing, aptitude was my best friend. But then all of a sudden my world started crashing down in little pieces.

This was when I realized I could not watch Kimi Raikkonen in his Scuderia Ferrari, trying to take the Formula One championship season by storm , nor could I watch the team of my heart, the glorious boys in blue playing cricket to win the world cup for my motherland. All this because my paid subscription to streaming video sites, needs installation of sopcast or TVUplayer’s streaming plugin and as luck would have it Ubuntu did not support either of those. In addition I could not use my favourite image editor, Paint.NET

So as I was contemplating the grand ironies, I came across VirtualBox, and suddenly my world became a better place to live. I could both have the cake and eat it, I became alive and started loving life.

What is it??

Virtual Box is a virtualization software . It is also commonly known as a hypervisor or a VMM (Virtual Machine Monitor) whichis a platform virtualization software that can allow multiple OSes to run on a host system concurrently. Hypervisors maybe of two types:

  • Type 1: native hypervisors runs directly on the host’s hardware as a hardware control. The guest OS runs on a level about the hypervisor. This is the classic VM architecture. Ex: Oracle VM

  • Type 2: hosted hypervisors are software applications that run within a conventional operating system environment. Ex: VirtualBox.

Another term used with VirtualBox is paravirtualization, a virtualization software that connects the Guest OS to the hardware through a virtual software interface.  Paravirtualization provides many benefits over Full Virtualization Increased security as Host server cannot access virtual machine neither can there be communication between VMs. Failsafe and redundancy by distributing VM over multiple hosts VirtualBox provides many of the features available in all other Virtualization software. The greatest thing about it is that it is Open Source, competing with VMWare Fusion. With it’s modularity, shared folder concept, provides an almost seamless way to running multiple “guest” OSes. It provides built in RDP server that allows us to use an RDP client to view the desktop which is pretty cool. It also has USB support. It works on x86 and AMD architcture based machines. It has got some excellent documentation, although their snapshot feature was little badly documented. Another issue is that It doesn’t have OpenGL / DirectX support yet, but that does not affect me yet.

Allin all, a grat way to run all those other operating systems you wanted, for none of the cost that VMware brings you. I give VirtualBox a 4/5 star for performance, ease of use and user documentation.

References

  1. www.virtualbox.org
  2. vkernel.co.uk/?p=5

Tags: ,

Copyright © 2010 “An Image of My Life” blog series All rights reserved. Theme by Laptop Geek.

Total hits: 38632