Mar 30

Funny as hell. Enjoy.

  1. Before the initial project meeting go to jcp.org. List all JSR’s learn some (5 are enough in general) numbers and headers.. If you have to do with more advanced developers - also read the abstract. Mention the JSRs during the meeting and enjoy the impact.
  2. If you project is going to be a realized with Java EE: Emphasize the verbosity of Java. Point to elegant languages like Python, or Ruby. (it is not necessary to know these languages - it is not very likely, that they will be used in a Java EE environment). If they are going to be chosen go to point 6.
  3. Mention JDK 1.7 and closures. (this strategy is very safe -> most companies are just now thinking about upgrade to Java 5). In general it is enough only mention the term “closure” in the context of Java.
  4. Wait a random amount of time. Pick one of the vs. issues and try to initiate a dicussion; SOA vs. ROA, Ruby on Rails vs. Java EE, Thin vs. Rich, AJAX vs. Rich, Swing vs. SWT, Netbeans vs. Eclipse, Java EE vs. .NET, SOAP vs. REST and OSGI and JSR-277. The highlighted ones are more funny and the discussion longer.
  5. Use terms like Web 2.0-ish, SOA-ish etc. Don’t care about the meaning: no one is able to define them.
  6. Suggest more esoteric languages like Haskell, Scala, or Fortress but not Malbolge for the realization. In general there is no reason to doing that (Java is working well), but why not?
  7. Don’t forget code generation. Sometimes it is enough to initiate a discussion about MDA vs. MDSD. If nothing happens ask the developers about PIM, PSM and code transformation etc.
  8. Very important: mention things like team velocity, pair programming, ask about Xtreme-Programming, Agile, Scrum, Crystal, Test First, Mocking (the more the merrier).

source : Adam Bien’s weblog

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , ,

Mar 29

Sometimes, application servers don’t display the line numbers during printing the trace but instead just print the method name with the “(Unknown Source)” on it.

eg. something like this.

Exception in thread “main” java.lang.ArithmeticException
at TraceWithoutLineNumbers.main
(Unknown Source)

I wrote this small class to just help me learn why the line numbers get missed and Unknown Source comes up when the trace gets printed.

public class TraceWithoutLineNumbers {

public static void main(String[] args) {

System.out.println(”hello world”);

if (true){
throw new ArithmeticException();
}
System.out.println(”very true”);

}

}

For the above code, I gave a

javac TraceWithoutLineNumbers.java

and got

java TraceWithoutLineNumbers
hello world
Exception in thread “main” java.lang.ArithmeticException
at TraceWithoutLineNumbers.main(TraceWithoutLineNumbers.java:9)

Cool. Got the line number.

By default, the minimal debugging option is on. (Only line number and source file information is generated). Here is the list of all the other java compiler options.

So, I do a

javac -g:none TraceWithoutLineNumbers.java

java TraceWithoutLineNumbers
hello world
Exception in thread “main” java.lang.ArithmeticException
at TraceWithoutLineNumbers.main(Unknown Sourc
e)

There is nothing you could do on this if the class is built and run by the server ( read if you dont have control over the class’ compilation). However, if it is a standalone class, you can very well do a javac -g .

Or use this option.

-g:{keyword list}
Generate only some kinds of debugging information, specified by a comma separated list of keywords. Valid keywords are:
source
Source file debugging information
lines
Line number debugging information
vars
Local variable debugging information

Probably, the app server or the web server you are running your class on would have disabled the debugging option for performance purposes.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , ,

Mar 29

I used to wonder why all methods in the java.lang.Object is public and only finalize is protected.  Finalize is just a callback method which is supposed to be called by the JVM.  So, ideally, it should be private.  Here is a mind-blowing explanation of why finalize is protected?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , ,

Mar 29

Back when i was preparing for the Web Component Developer exam, i had an idea of the requestURI and pattern mapping against the exact servlet. In simple words, the logic behind how the server maps the “<url-pattern>” tag and the <servlet-class> tag. I know that it is really a monstrous job, but then i wanted to try it using a simple array. Of course the code i wrote is buggy ( I myself found one big bug in there.) But for most cases, it works fine.

Here is my code. Download from here.
public class Navigation {

static String[] config= { “/test2″, “TestServlet2″, “/test”, “TestServlet” };
static String requestUri= “/test2/”;

public static void main(String[] args) {

System.out.println(”output” +new Navigation().getHandler(config, requestUri));
}

public String getHandler(String[] config, String requestUri)
{
System.out.println(”Request uri : “+requestUri);
int maxMatch=0; //store the length of the match that is the longest.
String currentMatch=null; //the most accurate match string

for (int i=0;i<config.length;i=i+2){
//looping through the array.
// Coz, you know that every odd String in the array is the Handler String,
//you can just skip them

System.out.println(”config[i] :”+config[i]);

if ((requestUri.startsWith(config[i]))){
if (config[i].length()>maxMatch){ //compare the length of the
//previously matched String, if any
maxMatch=config[i].length(); //if so, store it
System.out.println(”max match… “+config[i+1]);
currentMatch=config[i+1]; //Dont forget the String too
}

}

}

if (currentMatch!=null){
return currentMatch; //return it.
}

return “/”;
}

}

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: ,

Mar 28

The Apache Commons Lang page says ” The Lang Component provides a host of helper utilities for the java.lang API, notably String manipulation methods, basic numerical methods, object reflection, creation and serialization, and System properties. Additionally it contains an inheritable enum type, an exception structure that supports multiple types of nested-Exceptions, basic enhancements to java.util.Date and a series of utlities dedicated to help with building methods, such as hashCode, toString and equals.”

I have been using the commons library for quite sometime. But everything are in bits and pieces. So, just thought i could learn something more.  But before that download the commons lang libraries.

Counting the number of occurences of a String in a particular file or String requires lots of code. Here is one simple solution.

import org.apache.commons.lang.StringUtils;

public class StringMatch {

public static void main(String[] args) {

String inputString=”hello arun, say hello to all coz” +
” it’s going to save you from a hell a lot of trouble”;

int occurences=StringUtils.countMatches(inputString, “hell”);
System.out.println(occurences); //gives you 3
}

}

If you are reading from a file, then go for


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import org.apache.commons.lang.StringUtils;

public class StringFromFile {

public static void main(String[] args) {
try {
FileReader fileReader=new FileReader(”InputFile”);
LineNumberReader lineReader=new LineNumberReader(fileReader);
int count=0;
while (lineReader.ready()){
count=count+StringUtils.countMatches(lineReader.readLine(),
“hell”);
}
System.out.println(”Number of occurences is : “+count);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

}

}

Download the entire source code

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • De.lirio.us
  • description
  • Furl
  • Reddit
  • Slashdot
  • Spurl
  • StumbleUpon
  • Technorati
  • TwitThis

Tags: , , ,