Thursday, July 9, 2009

JVM Internals Series -Part 1

A lot of java developers tend to be unaware about the basics of the internals of the JVM. This series aims to look at the internals of the JVM and explain in a simple way
    What is the Java Virtual Machine
    How does the JVM function


What is the Java Virtual Machine ?

When you talk of the JVM there are three aspects which we speak of

  1. Specification
  2. Concrete Implementation
  3. Runtime

The specification is a concept, Concrete implementations which exist on different platforms and are available from different vendors like IBM, Sun etc are either all software or a mix of hardware and software. A Runtime instance hosts a single running java application.

A runtime instance has one function and that is to run one java application. Whenever a java application runs, a runtime instance is born. Thus the number of runtime instances on a machine are equal to the number of applications which are running. The JVM starts with the invocation of a main() method which needs to be public, static, void and takes a String array as an argument. The main method thus serves as the initial thread for the running application. This can in turn spawn other threads. Inside the VM, threads come in two flavors, daemon and non-daemon. A daemon thread is a thread which is normally used by the VM itself, like the thread which runs the garbage collection. However an application can mark any thread it creates as a daemon. A java application continues to execute until there are any non-daemon threads alive(parallely the VM runtime instance continues to exist). When all non-daemon threads of an application exit, the virtual machine instance exits.

JVM Architecture


The VM needs memory to store a lot of information, like the bytecode, program arguments, objects which have been instantiated etc. Some of the information stored in the memory is shared across all application threads, while other information may be unique to individual threads.

Each instance of a JVM has one method area and a heap. These areas are shared by all threads running within the instance.
The information regarding the type is loaded from the class files when the JVM loads a class file. This information is stored in the method area. All the objects which are instantiated placed on the heap.
As each new thread is created, it is assigned its own pc register(program counter) and java stack. If the thread is executing a java method (not a native method), the counter in the pc register points to the next instruction to execute. The java stack comprises of frames. A frame consists of the state of one method invocation. When a thread invokes a method, a new frame is pushed on the thread’s stack. On completion of the method execution, the VM pops and discards the frame. No other thread can access another threads pc register or java stack.

This gives a brief overview of the java virtual machine’s architecture. In the next post,I will cover data types and the class loading subsystem.


Wednesday, July 8, 2009

Groovy Class for Analyzing java code

A lot of times when i sit down to review code, there are some basic things like the size of the class, the total lines, the maximum width of a line etc, which can help in figuring out bad smells in code. In order to automate the task i wrote a small groovy class which can be run on a directory to figure out these metrices. Although there are lots of tools available, the simplicity and the customization which can be done to a small utility class far outweigh the advantages.

class CodeAnalyzer {
int lineCount
int maxLineWidth
int widestLineNumber
int totalChars
int lineSize
def List findJavaFiles(File parentDir){
def files=new ArrayList()
parentDir.eachFileRecurse{file ->
if(file.isFile() && file.name.endsWith(".java"))
files.add(file)
}
return files
}
def analyzeFile(File javaFile){
javaFile.eachLine{line ->
measureLine(line)
}
println "fileName =" + javaFile.name
println "lineCount =" + lineCount
println "maxLineWidth ="+maxLineWidth
println "widestLineNumber ="+widestLineNumber
println "totalChars ="+totalChars
}
def measureLine(String line){
lineCount++
lineSize=line.length()
totalChars +=lineSize
recordWidestLine(lineSize)
}
def recordWidestLine(int lineSize){
if(lineSize >maxLineWidth){
maxLineWidth =lineSize
widestLineNumber=lineCount
}
}
def getMeanLineWidth(){
return totalChars/lineCount
}
}
The semantics of usage are pretty simple. I will show the groovy way of invoking it
CodeAnalyzer codeAnalyzer=new CodeAnalyzer()
codeAnalyzer.findJavaFiles(new File("D:/MCubeRTV2.0/Source/java/com/velozglobal/mcube/agents")).each{file ->
codeAnalyzer.analyzeFile(file)
}

Monday, July 6, 2009

Eclipse templates for auto code generation

Working with an IDE tends to make life quiet simple. However we still do not leverage the full potential of using an IDE like Eclipse. During development, i have found creating a few templates really helps to speed up things. Here are the few most common ones i use which are not part of the standard templates. 1) Log4J Logging
a) Create Logger
${:import(org.apache.log4j.Logger)}
private static final Logger logger = Logger.getLogger(${enclosing_type}.class);
b) LogInfo
logger.info(${word_selection}${});${cursor}
c) LogError
logger.error(${errorMessage}, ${e});
d) LogDebug
if(logger.isDebugEnabled())
logger.debug(${word_selection}${});${cursor}
2) Constants
a) Const
private static final ${type} ${name} = new ${type} ${cursor};
3) Map Iteration
a) MapIter
for(Map.Entry entry :
${map:var(java.util.Map)}.entrySet()) {
${key} key = entry.getKey();
${value} value = entry.getValue();
${cursor}
}
4) JUnit
a) Test Creation
public void test${name}() throws Exception {
${cursor}
}

Tuesday, June 30, 2009

Automate Unit Test Execution

A lot of times, when doing TDD, we make small refactorings and then have to manually go ahead and run the tests to figure out whether the changes we made are consistent and have not broken the tests. Wouldn't it be good if the tests were run on each Save to ascertain that we have not broken any of the tests. This also lets us know which changes are the culprit. Using eclipse we can automate running the unit tests on each change and save we make. Following are the steps needed in order to accomplish this.
  1. Create a main class
import junit.framework.TestSuite;
import junit.textui.TestRunner;
public class TestBootstrap
{
public static void main(String[] args)
{
TestSuite testSuite=new TestSuite();
testSuite.addTestSuite(TestClass.class);
TestRunner.run(testSuite);
}
}

2. Create an ant build file

<project>
<target name="eclipse-test-runner">
<java classname="TestBootstrap" classpath="bin:libs/junit.jar"> <java> </target>
</project>

3. Create a Builder in eclipse
Open project properties: Right click project -> Propertie. Create new ant builder step after the Java Builder in eclipse: Builders -> new -> Ant Builder. Give your builder a nice name.
Main: Select the build file and the base directory
Targets: Select your ANT target for: “After Clean”, “Manual Build”, and “Auto Build” (most important) Build Options: Check “Specify working set” and select all of your source folders.
And thats all, now every time you save a file, eclipse will run the ant target specified in your build file, which incidentally calls your TestBootstrap's main and invoked all the tests.


Monday, June 15, 2009

Reading an RSS feed in groovy


Here's a small script to read RSS feeds from any site using groovy. You can customize it to read from multiple feeds by feeding the url's from a file etc.

def url='http://feeds.dzone.com/dzone/frontpage'
def channel=new XmlParser().parse(url).channel[0]
println channel.title.text()
println channel.link.text()
println channel.description.text()
println '\nStories:\n---------'
def items=channel.item
for(item in items){
println item.title.text()
println item.link.text()
println item.description.text()
println'-------'
}

Thursday, June 11, 2009

Google Wave - A First look

I took a look at the presentation of Google Wave, conducted by Lars Rasmussen and his technical team. Although its still in sandbox, it appears to be a very interesting product. The capabilities of what can be achieved in a browser are mind blowing. A very interesting point about Wave is that it would be open sourced. Primarily i think Google would need a lot of contributions from the community to really leverage the power which Wave promises. Wave brings the power of IM and email together and adds a different dimension to it. An early release of the wave API's are available .So go ahead and get your hands dirty. The API's available are the extension API's for building wave gadgets, robots and the Embed API's.

Fan -- Another New Java

Fan has recently been doing the rounds and i spent some time looking at it. It appears to be pretty much in the same space as Scala in most aspects. A deeper look at Fan reveals some interesting points. Similarity with Scala is it runs on the vm, has both dynamic and static typing, closures, mixins are built in etc etc. However a few interesting features in Fan over and above these are
1) Core support for the JVM, CLR and Javascript in the browser.
2) No support for global variables
3) Almost Erlang like concurrency semantics
4) Types are non nullable by default
5) Simpler namespaces and less verbose API's
6) JSON like serialization
On the while it appears has a groovy like simplicity with all the power of scala.

Wednesday, June 3, 2009

Proejct Euler Problem 1

Got to know about Project Eular.
So thought about posting a few solutions using groovy. For the 1st problem, the solution is pretty simple

def series3=0;
def series5=0;
def series15=0;
0.step(1000,3){series3+=it}
0.step(1000,5){series5+=it}
0.step(1000,15){series15+=it}
def result=series3 +series5 - series15

Export Data from Flat files to database:The Groovy way

Recently i was supposed to port a lot of data from flat files into a database. Looking at alternatives i thought it would be neat to accomplish the same using a small groovy script. Here's the crux of the script

def sql = groovy.sql.Sql.newInstance('jdbc:mysql://localhost:3306/test' ,
'root', ' ', 'com.mysql.jdbc.Driver' )
new File("C:/data/").eachFile{file->
file.eachLine{line->
def col1
def col2
def col3
def args=line.split(",")
if(args.length==3){
col1=args[0]
col2=args[1]
col3=args[2]
sql.execute"""
INSERT INTO table (col1, col2, col3)
VALUES (${col1}, ${col2}, ${col3});
"""
}
}
}

Wednesday, April 22, 2009

Unit Testing Recipes

A lot of times i tend to come across unit tests where the developer has to create a List and compare the equality of a returned List with some expected List. As an example consider the following test Case

public void testResultsHasSameElements(){
List()expectedResults=new ArrayList();
expectedResults.add("James");
expectedResults.add("Jack");
List customerNames=CustomerDTO.getCustomerNames();
for(String name:customerNames){
assertEquals(name,expectedResult.get(0));
assertEquals(name,expectedResult.get(1));
}

}

If we look at the above testcase, it involves a lot of typing. We can simplify the above test using the fact that 2 Lists are equal if they contain the same objects at the same index. Another optimization which can be used is the Arrays.toList method to create the expectedResults list
The improved version of the test would look like

public void testResultsHasSameElements(){
List()expectedResults=Arrays.asList(
new String[]{"James","Jack"}
);

customerNames=CustomerDTO.getCustomerNames();
assertEquals(customerNames,expectedResult);
}

}

Tuesday, September 30, 2008

Getting teams started quickly on projects

The ability to get teams started on a project is something that takes a lot of time and resources. A few ways to mitigate that and get teams to speed fast are
1) Use Buildix(http://buildix.thoughtworks.com/) for setting up the infrastructure for the project like version control, continuous integration, agile project management and wiki and bug tracker. Buildix is open sourced under the apache license and automates setting up the basic infrastructure required fro the project.
2) Use Panopticode(http://www.panopticode.org/) to setup tools for gathering code metrics. Panopticode provides customized build scripts to integrate tools like Emma, CheckStyle, JDepend,JavaNCSS, Simian etc.
3) Use a VMWare mirror image to setup a developer box so as to remove any discrepancies across developer environments.

Sunday, September 28, 2008

Windows Shortcuts and useful tools for developers

Here are a list of windows shortcuts and tools which every developer should know to work effectively.

Firefox Shortcuts
CTRL + number -- can be used to navigate between tabs in firefox. For example CTRL+1 gives the first tab,CTRL+2 gives the second tab etc.
Explorer Shortcuts
Alt+D -- leads to the address bar. The address bar as auto-completion like the tab in shells.
Command Prompt
F7 key--shows the command history
F8 key can be used to navigate across the history. Type the first few characters of the command and use the F8 key for auto-completion
Use pushd and popd. When inside a directory, pushd can be used to navigate to another directory and popd can be used to get back to the other directory. These commands work like a stack(LIFO), so prefer these over the simple cd.

Tools
CLCL -- a multi-clipboard utility
Command Prompt Explorer bar -- A sticky exlplorer-command prompt utility
PowerToys
Tweak UI
TaskSwitch
Virtual Desktop Manager

Thursday, August 28, 2008

Software Architecture Structures

This post talks about the Software Architecture Structures, and how these structures relate in the creation of an architecture. Software Architecture Structures can be divided into 3 broad categories

1) Modules

2) Component and Connectors

3) Allocations

1) Modules -- These are the units of implementation. These represent code based views of the system. The intent here is not related to any runtime considerations. Module based structures can be further sub-divided into

1. Decomposition --The units addressed here are that of submodules of a particular module. The intent is to break modules into smaller units such that each unit can be understood. This normally is the starting point of high level design and subsequent low level design. It also incorporates things like implementations, test plans etc. In addition a decomposition can also address questions related to changes made to the system. The intent should be to keep the changes local to a few sub-modules.
2. Uses -- One unit uses another if the correctness of one depends on the existence of the correct version of another. The uses structure can be used to extend the capabilities of a system as well as extract subsets of functionality which can be used for incremental development
3. Layered -- When the Uses structure is carefully modeled, normally layers emerge. These layers are areas of common functionality. Layered structures can be used to assess the dependence of one layer on the other as well as ensuring that the interactions of one layers are only with the layers above or below this layer. In addition, a layer at n should only depend for services on the n-1 layer and none other.
4. Class or Generalization -- The units in this structure are classes. Classes can be used to reason about collection of similar behavior or capabilities. Class structures is used for assessing reuse and incremental addition of functionality.

2) Components and Connectors --Component-and-connector structures help answer questions such as What are the major executing components and how do they interact? What are the major shared data stores? Which parts of the system are replicated? How does data flow through the system? What parts of the system can run in parallel? How can the system's structure change as it executes? These can be further sub-divided into

1. Processes --The process structure shows processes connected to each other through connectors, snychronizers,exclusions etc. This structure is important when looking at a systems performance and availibility.
2. Concurrency -- This structure allows for determining the areas where there can be a resource contention or where parallelism can be employed.
3. Shared data -- This comprises connectors and components that create,store or access shared persistent data. It shows how data is produced and consumed by the runtime components and can be used to ensure performance and data integrity.
4. Client -Server -- This structure shows the components as client server units and the connectors are the underlying protocols which are used for communication.

3) Allocation Structures -- This structure shows the relation between the software elements and the environment in which the software executes. These can be classified as follows

1. Deployment -- The deployment structure shows how software is assigned to hardware-processing and communication elements. The elements are software, hardware entities and communication pathways. Relations are allocated-to, showing on which physical units the software elements reside. This view allows one to reason about performance, data integrity, availability, and security.
2. Implementation --This structure shows how usually modules are mapped to the file structure in the system's development, integration, or configuration environments. This is critical for the management of development activities and build processes

Wednesday, July 23, 2008

Kickstarting your day and honing your development skills

As per various studies conducted, it has been shown that developers tend to take at least an hour or more to get into the zone, where they are most productive. There are various ways which have been suggested like writing unit tests at the start if the day,etc. to get into the zone. Being a pragmatic programmer, i have found something which has helped me kick-start my day and get into the zone in the minimum time possible. The following recipe has done wonders for me.
At the start of the day, i take up a puzzle and spend 5-10 minutes in analyzing and solving it. Be aware the intent is not to come to a solution, but just to get the gray cells kicking. After this i normally try some programming problem, albeit a simple one using a new language which i am trying to acquire. I spend another 20-30 minutes in solving the problem. The problem could be as simple as trying to find a repeated integer in a list of otherwise unique sequential numbers. Getting these two activities at the start of the day has two-fold advantages. One it focuses the brain and secondly it helps me acquire a new skill. I have even tried using this after prolonged meetings just to get back into the zone. However a word of caution, the intent is not to get to perfection, but to time-box the effort. You can get back to the problems again the next day if you are stuck on something.

Sunday, June 22, 2008

Regular Expressions Part 2

This entry continues the discussion regarding regular expressions from the previous post(Regular Expression part 1). We continue with a discussion of the rich meta-characters available. In this post i will take up some of the other meta-characters and discussion their usage. The pattern followed is the same as the previous post with some exercises thrown in for good measure.
  • Optional Items -- Suppose we wanted to search for the word June, which could be represented as either June or Jun. The ? meta-character means optional. So the search could be accomplished using June?. This can be interpreted as match J, then u then n followed by e if its there. So the ? is placed after the character that is allowed to appear at that point in the regular expression, but the existence isn't required to consider it a successful match. Expanding on the same lets say we have to match June 5th, which can occur as Jun 5, June 5th, or fifth. To match the following we can use (June|Jun).(5|5th|fifth). The first expression can be simplified as (June?) and the second one as fifth|5(th)?. Hence the complete expression can be re-written as June?.(fifth|5(th)?). One point here is that although there are different alternatives, choosing the right one needs some thought and introspection.
  • Repetition -- The + and * meta-characters are used for checking for repetition. + implies one or more of the preceding character and * implies any number including none of the item. Thus + fails if there are none of the characters but succeeds for one or many, * on the other hand always succeeds. As an example, to match spaces, one can use . ? which would match a single optional space, . + would match any number of spaces with at least one space and . * would match any number of spaces, if present. Lets take another example, where we have to search for the html tag &lt;hr size="10"&gt;. To build the regular expression, we need to have<, then HR followed by one or more spaces(allowed as per the semantics of html), followed by zero or more spaces, followed by = followed by zero or more spaces the number 10 , again zero or more spaces followed by >. Based on our discussion so far, this should be simple, Now lets try and generalise this to any size, not just 10, so the regular expression would look like For case insensitive search using egrep, we can use the -i option.
Exercise -- Write a regular expression for the same examplem as above, where the size attribute is optional e.i a simple
is a legal match.

  • Back References -- Many tools provide the ability for parentheses to remember text matched by the sub-expression they invoke. So as an example, ([a-z])([0-9])\1\2, \1 refers to the text matched by [a-z] and \2 refers text matched by [0-9]. We will see examples of using back-referencing in following posts.
  • Escapting Meta-characters -- In order to escape meta-characters, we can use \ to escape the meta-character. For example if we tried to match att.com, it could end up matching something like watt company. So we need to escape the meta-character . , this can be done using att\.com. This escapes the meta-character and treats the meta-character . like a normal character
This covers some of the most important meta-characters. In future posts i would expand on these fundamentals and show how applying regular expressions makes life simpler.

Friday, June 20, 2008

Regular Expressions Part 1

I have been a great fan of using regular expressions for solving day to day tasks. I thought it was time to post a series for discussing the use and syntax of regular expressions. In this post i will talk about some simple meta-characters and their uses with tools like egrep/grep. In addition i will try and set some small exercises to practice the understanding of the readers.

A regular expression is composed of a set of meta-characters like *, ^ etc and a set of literal characters like A-Z, a-z, 0-9 etc.

Some Simple Meta-characters
Let us start with a some simple meta-characters and how we can apply these to various scenario's. The
  • ^ - The Caret/Anchor is one of the simplest meta-characters and represents the start of the line which is being checked. For example ^cat would match all lines which start with the literal cat like catalog
  • $ - The dollar meta-character represents the end of the line which is being checked. So cat$ would match all lines which end with the literal characters cat, for example scat.
These 2 meta-characters are unique in the sense that they match a position rather than the actual literal characters themselves.

Exercise -- What would ^cat$, ^$ and ^ match ?


  • Character Class -- The meta-character [...] called the character class lets you list the characters you want to allow at that point of match. For example e matches just e , a matches just a, however [ea] would match both e or a. Thus the implied meaning is that of OR. Suppose you wanted to check whether you have spelt grey or gray. The following regular expression would match the occurance of both gr[ea]y. This matches g follows by r followed by either e or a followed by y. The - operator within a character class matches a range, so [0-9A-Z] matches occurances of numbers and capital letters. For example ] would help you while searching headers in html documents. The - is special inside a character class, otherwise it is used as a normal character and not as a range.
  • Negated Character classes -- A ^ inside a character class negates the matches. For example [^a-z] would match any character which is not a through z. Remember a negated character class means match a character thats not listed and not don't match whats listed
Exercise -- Figure out all words in a dictionary which contain a q not followed by a u
  • Matching Any character with dot -- this is simpler to explain with an example. Suppose you want to find a date 26/02/1977, which can also be represented as 26-02-1977 or even 26.02.1977. Not the . character can be used to match any character. So seemingly, 26.02.1977 should return us the date however it be represented, maybe with a - or a /. However, the above would also match a random number like 12264023197734. This is because the dot character matches any character. The correct way to do so would be to use something like 26[-./]02[-./]1977 (Note, the - is this case would not be a meta-character, since it immediately follows the [, if the same was written as [.-/], then it would become the range meta-character, also the . is not treated as a meta-character since it is inside the character class)
  • Matching any one of several sub-expressions -- The | (OR) operator lets you combine expressions into a single expression, that matches any of the individual ones. For example, based on the previous examples, we can rewrite gr[ea]y as gr(e|a)y. (Note-- we cannot use something like gr[e|a]y as inside the character class, | is treated as a normal character). We need the parenthesis because gre|ay would mean gre or ay.
Exercises 1) How does gr[ea]y differ from gr(e|a)y ?
2) What is the difference between ^:List|Of|Books: from ^(List|Of|Books): ?
Note -- Tools like egrep provide switches for case-insensitive searches. For egrep -i performs a case insensitive search.
Example -- egrep -i '^(Subject|Predicate):' myfile.txt
This serves as a good groundwork for someone wanting to start using regular expressions. I will be following up the post with detailed topics. Till then, cheers and keep the faith.

Thursday, June 19, 2008

The Art of analysis and problem solving

I have been wondering about the art of analysis and problem solving and decided to do a writeup using an example. A lot of developers tend to jump to writing code and never seem to bother about thinking out their solutions. For them if it works its fine. Moving away from this is a pre-requisite for anyone who wants to hone his development skills. In order to convey my point, i take up a seemingly simple example which displays how to analyze a solution before implementing it.

Problem Statement

Write a function which removes a set of characters from a given String. The signature of the function should be as follows

public String removeCharsFromString(String source, String remove)


Problem Analysis
The problem involves the following 2 steps
1) For each character in source, determine whether it needs to be deleted
2) Delete the character

Lets start with the actual deletion process first. You have to remove an element from an array. An array is a contiguous block of memory, so you cant remove an element from the middle as can be done with a linked list. So you have to rearrange the elements to maintain it as a contiguous block. For example if you need to remove c from an array containing abcd , you have to rearrange the elements. So you can either shift ab up or shift d down. In addition you need to decrease the size of the string by one.

Now how would the proposed algorithm work if you had to delete all elements from the source string. If the String is n characters long, this would mean shifting the last element by n-1, the second last by n-2 and so on, giving the worst time ass O(n^2).

How can we avoid this. What if you allocated a temporary string buffer and built your modified string there instead of in place? Then you could simply copy the characters you need to keep into the temporary string, skipping the characters you want to delete. When you finish building the modified string, you can copy it from the temporary buffer back into str. This way, you move each character at most twice, giving O(n) deletion. However, you’ve incurred the memory overhead of a temporary buffer the same size as the original string, and the time overhead of copying the modified string back over the original string. Is there any way you could avoid these penalties while retaining your O(n) algorithm?

To implement the O(n) algorithm just described, you need to track a source position for the read location in the original string and a destination position for the write position in the temporary buffer. These positions both start at zero. The source position is incremented every time you read, and the destination position is incremented every time you write. In other words, when you copy a character you increment both positions, but when you delete a character you increment only the source position. This means the source position will always be the same as or ahead of the destination position. Once you read a character from the original string (that is, the source position has advanced past it), you no longer need that character - in fact, you’re just going to copy the modified string over it. Because the destination position in the original string is always a character you don’t need anymore, you can write directly into the original string, eliminating the temporary buffer entirely. This is still an O(n) algorithm, but without the memory and time overhead of the earlier version.

Now to the actual part of deciding whether a character needs to be deleted. The easiest way is to compare all characters in the remove string with all the characters in the source string. If the size of the source string is n and the size of the remove string is m, this entails a running time of O(nm). How can we reduce this to a running time of O(m).

If we construct an array or a hashtable which has a constant time lookup, we can reduce the running time to O(m).
Lets construct an array containing all Booleans which is indexed by all the possible values of the characters in the source string. This enables you to determine whether a character is in remove by checking a single element.

Now the function will have 3 parts
  • Set all elements in the lookup array to false
  • Iterate through each element in remove, setting the corresponding value in the lookup array to true
  • Iterate through str with a source and destination index, copying each character only if its corresponding value in the lookup array is false
Now if we combine all the operations, the running time would be O(n+m) which is still linear.

Now lets take a look at the code


public String removeCharOccurances(String source,String remove){
char[] sourceArr = source.toCharArray();
char[] removeArr = remove.toCharArray();
boolean[] flags = new boolean[128]; // assumes ASCII!
int len = sourceArr.length;
int len1=removeArr.length;
int src, dst;

// Set flags for characters to be removed
for( src = 0; src < len1; ++src ){
flags[removeArr[src]] = true;
}
src = 0;
dst = 0;

// Now loop through all the characters,
// copying only if they aren't flagged
while( src < len ){
if( !flags[ (int)sourceArr[src] ] ){
sourceArr[dst++] = sourceArr[src];
}
++src;
}

return new String( sourceArr, 0, dst );
}

Wednesday, June 18, 2008

REST-ful services using JAX-WS 2.0

REST style services conform to a set of constraints and architectural principles
  • RESTful services are stateless. Each request from a client to a server must contain all the information necessary to understand the request and must not depend on any stored contextual information on the server.
  • REST-ful services have a uniform interface. For example for a RESTful HTTP service, the operation supported are GET, POST, PUT and DELETE.
  • REST based architectures are built from resources which are identified by unique URI's.
  • REST components manipulate resources by exchanging representations of the resources.


The fundamental differences between REST and RPC based services can be outlined as
  • RPC services work by invoking a procedure on a server rather than by exchanging representations of a resource.
  • In the RPC approach typically many operations are invoked at the same URI, which is fundamentally different from the REST way where every resource has a unique URI.




RESTful vs SOAP based services


Message Format


Interface Definition


Transport


XML XML inside a SOAP envelope
none WSDL
HTTP HTTP,FTP,MIME,JMS,SMTP etc

In order to consume a RESFful service using JAX-WS 2.0, the majority of the work needs to be done using the javax.xml.ws.Dispatch interface. The javax.ws.xml.Service interface is the abstraction which provides the client interface to a web service. Since RESTful services do not have WSDL representations, the way to deal is a bit awkward, however that is the way JAX-WS has been designed. We use the Service interface to create an instance of the javax.xml.ws.Dispatch interface. Dispatch is a low level API which allows users to create messages by working directly with XML rather than using high level API's like JAXB etc.

The following code displays the basics and is discussed below

QName svcQName = new QName("http://xyz", "svc");
QName portQName = new QName("http://xyz", "port");
Service svc = Service.create(svcQName);
svc.addPort(portQName, HTTPBinding.HTTP_BINDING, args[0]);
Dispatch dis =
svc.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
Map requestContext = dis.getRequestContext();
requestContext.put(MessageContext.HTTP_REQUEST_METHOD, "GET");
Source result = dis.invoke(null);
try {
TransformerFactory.newInstance().newTransformer()
.transform(result, new StreamResult(System.out));
} catch (Exception e) {
throw new IOException(e.getMessage());
}
The web service url is as a command line argument(arg[0])
  • The client uses the Service.addPort() method to create a port within the Service instance that can be used to access the RESTful Web service.
  • Next, the Service.createDispatch() method is invoked to create an instance of Dispatch—a Dispatch instance that enables you to work with XML request/response messages as instances of javax.xml.transform.Source.
  • The Dispatch.invoke() method then packages the XML request—per the JAX-WS 2.0 HTTP Binding—and sends it to the RESTful service. The invoke() method waits for the response before returning.
  • The service processes the HTTP GET and sends an HTTP response that includes the XML.
  • The invoke() method returns the response XML message as an instance of Source.
The addPort() method takes a URI parameter that defines the transport binding. The default is SOAP over HTTP but for RESTful services we use the HTTPBinding.HTTP_BINDING. The type of payload is specified in the createDispatch() method. Generally when passing SOAP messages, Service.Mode.MESSAGE is used to indicate that you want to work with the complete SOAP envelope as opposed to just the SOAP body(PAYLOAD). In case of RESTful services since there is no enveloper, PAYLOAD mode is used.

Thursday, June 12, 2008

Pair Programming


Kick-start the day by writing a unit test, just to get the grey cells working



Getting down to serious stuff.


This isnt making a lot of sense, is it.




Getting some tips from the senior partner




See that wasn't so difficult was it.

Tuesday, June 3, 2008

Light weight java game library

Being a server side guy, i decided it was time to do a bit of game programming as a challenge. I have started looking at LWJGL (Light Weight Java Game Library) which can interface with libraries like OpenGL and OpenAL. Still need to come up with a concept to start using it for developing a prototype, but time i did something different.

The emperor and me beaching

The Devil next door

Kaiser The Emperor