Wednesday, October 31, 2007

The art of debugging


Software debugging is used to troubleshoot a problem in code.

So what is the best way of debugging?
1.  Run through the log statements
2.  Start  a debug session in your favourite IDE. The session can be remote(for a web app) or local.
3.  Narrow down to the problem by reducing the dependencies. Is it because of the framework or your own code, to know that  reduce the  code and test piece by piece.

Option 2, Debugger is the best arsenal for troubleshooting issues in code. Mixing 1,2 and 3 would be the ultimate way to fix the worst problem.
For details of remote debugging in eclipse please refer to my earlier posts.

In eclipse I find the below things very useful in debugging session:
  1. Breakpoints:
1.Put a breakpoint in eclipse. You can also edit the breakpoint properties by right clicking on the breakpoints properties. Here you can put a condition and put S.o.p for quickly identifying the problem 2. Exception breakpoint is a cool feature which will catch the exception which you specify. This will help you find the control flow(especially with nasty Null pointer exceptions). You can specify this by clicking on the "Add Java Exceptions" breakpoint of breakpoints view.
2.If you're running in debug code, you can actually just edit the method and re-invoke it. None of this resetting variables on the fly; just save the new method and do 'drop to stack' to get it to re-run the code. You can't do this if you're not running your application in eclipse, but are rather joined to it as a remore debugger. 3. Variables can be reset in debugging session through expressions or variables view. 4. Short cuts: CTRL+SHIFT +I -- to check a variable after selection. F5, F6, F8 Useful links Jacoozi - Remote Debugging with Eclipse

Monday, October 29, 2007

JNDI test JSP page

<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8" import="javax.naming.*" %>
<% InitialContext initialContext = new InitialContext();
   Object obj=initialContext.lookup("java:comp/env/jdbc/xmpPubPool");
   out.print(obj);
 %>

Thursday, October 25, 2007

Useful Eclipse Plugins + shortcut keys

Here is a list of plugins and their usefulness:
  • MyEclipse -- Is a heavy wieght champion among plugins.Has lot of good features that makes it good as well as bulky. It sucks lot of memory.
Swing Tools
  • jigloo -- Very good, clean and free Swing code generator. Makes swing development like VB.
Code Analysis:
  • pmd
Javascript:
  • jsEclipse
  • Teniga_Javascript_Editor
Miscellaneous
  • Mylyn --- Connects to task repository and eases task managemnt.Also standalone support
  • JadClipse --Uses Jad to decompile class
  • jautodoc -- Very useful for quick API documentation.
  • junitBuilder -- Generate Junit automatically for classes..Saves time
  • moreunit - a must have for junit. http://moreunit.sourceforge.net/org.moreunit.updatesite/
  • phpeclipse -- Good for PHP developers.
  • VelocityWebEdit -- Cool velocity template validation and edit tool
  • patternbox -- very useful for building regular expressions quickly.
  • xmlbuddy -- neat small and simple xml editing tool.
  • subclipse --SVN plugin
  • NTail - tail for eclipse http://www.certiv.net/
  • Eclipse HttpClient -- is a Rich HttpClient Plugin with stunning user interface and build on top of the famous Apache HttpClient
  • Jar class finder - give a class , it will find the jar where it belongs to.
http://www.alphaworks.ibm.com/tech/jarclassfinder/download MDA tools/plugins ---These have the ability to generate j2ee code automatically based on model driven architecture
  • andromda
AJAX Plugins
  • aptana
DB Modelling
  • Clay modelling tool DB_Modelling_jp.azzurri.clay
Short Cut keys : CTRL+E for switching editors.Ctrl + F6 to navigate between the open editors, CTRL+ F11 for executing last command. Refactoring ALT +SHIFT+R --rename variable ALT + SHIFT + M -- extract method Indentation CTRL + I -- correct indentation individually. CTRL + SHIFT + F -- whole page format. Creation: CTRL+ N. I found it much easier to press ALT + E (extend) to add extended class and ALT + A (add interface) to add interfaces in the new class wizard. My class will then come out with empty overridden methods and correct imports. Search CTRL+Shift+G, which searches the workspace for references to the selected method or variable. 5 stars. Agility F3 Go to a type declaration CTRL + SHIFT + UP(DOWN) -- move up or down to the next member CTRL+T on an interface name...This list the implementation of the interface. Very useful. 5 stars CTRL+. and CTRL+, Move to one problem (i.e.: error, warning) to the next (or previous). Ctrl-F6 is a great way to jump around between open editors. CTRL+F7 to switch views...Verrrry useful CTRL+F8 to switch perspectives. Ctrl-O in a class definition brings up Quick Outline. Start typing a member name and hit return once it’s unambiguous. Combined with Open Type this is a lightning fast way to go to any method in any class.ctrl-o ctrl-o to show inherited members too! Less typing

I think of Quick Fix as a tool for writing code, not something that just corrects accidental errors. Instead of trying to type perfect code and using Quick Fix only when you make a mistake, try intentionally leaving out code, then using Quick Fix to add it in. For example, call a function that isn’t defined, passing in all the arguments you want to to take. Use Ctrl-1, Enter to create an empty version of the function with all the right parameter types. I also like Quick Fix for creating casts. I assign an expression to a variable without a cast, then use Quick Fix to add it in.

Instead of declaring a variable and then assiging the value of an expression to it, try just writing the expression, then use Quick Assist – Assign to Field (Ctrl-2 L) to generate the declaration. The variable name will be highlighted and the drop down gives you several reasonable alternatives to use for the variable name. Tab again to get to the type to choose an alternative. For example if you had new ArrayList<String&gt;() and used Assign to Field you might choose List<String&gt; from the list of type alternatives.

More here : http://eclipse.dzone.com/news/effective-eclipse-shortcut-key

More here : http://rayfd.wordpress.com/2007/05/20/10-eclipse-navigation-shortcuts-every-java-programmer-should-know/

Powered by ScribeFire.

Monday, October 22, 2007

linux tips


Tomcat Remote debugging in linux
Put the below in catalina.sh

JPDA_TRANSPORT=dt_socket
JPDA_ADDRESS=5005
$./catalina.sh jpda start
Startup script with logging: Create a file with the following and change the mode to executable under $catalina_home/bin ./startup.sh;tail -f ../logs/catalina.out For shutdown create as below: ./shutdown.sh;tail -f ../logs/catalina.out Restart script for linux: ./shutdown.sh echo "shutdown.sh executed" echo "Sleeping for 5 seconds after shutdown" sleep 5 ./startup.sh echo "starting up" echo "Sleeping for 1 second" tail -f ../logs/catalina.out Jar usage $cd portal-impl $jar xvf portal-impl.jar :extracts the jar file into portal-impl dir. $jar cvf portal-impl-patched.jar portal-impl/ :make jar file from portal-impl dir contents the jar will be having portal-impl as the root dir. To change ownership: $chown tomcat:tomcat FILE_NAME To change group: $chgrp tomcat FILE_NAME To Switch user: $su USER_NAME
Clear all unwanted .SVN files
find . -name ".svn"-exec rm -rf {} \;

Friday, October 12, 2007

JProfiler - A J2ee profiling tool

This is one of the best profiling tools i have seen. They give a 10 day free evaluation with all feature's enabled. Setting up the Jprofiler was a little tedious until I came to know how it works. I was expecting that the Jprofiler would put some custom code in tomcat(like profiler4j) but actually it runs the tomcat in it's own session. On windows, setup the application with downloaded exe. Go to New -- Start center -- New Server integraion and you are done. The app server gets started and log console is available through Jprofiler itself. Another nice feature of jProfiler is remote connectivity. We can connect to server running in linux from windows. To do that, install jProfiler on linux as below rpm -i http://download.ej-technologies.com/jprofiler/jprofiler_linux_5_0_1.rpm Get the catalina.sh(for tomcat) and give to the start center wizard. Copy back this(jprofiler_startup.sh) to tomcat on linux. Now start the server using jprofiler_startup.sh. Connect from windows GUI and you are ready for profiling.. The real thing : Profiling can be done for memory or CPU load. The hot spots are the best ones to quickly find out the problem areas. Also J2ee components can be easily profiled for JDBC calls, JMS calls,etc.

Powered by ScribeFire.

Thursday, October 4, 2007

Apache 2.x setup Quick guide for Linux

I am herewith trying to log all the issues faced during apache web server setup.
Download apache from http://httpd.apache.org/download.cgi

There you can find either a source or binary.

Normally it is good to compile the source to a OS platform. But it is little extra effort. Setting up using a apache binary is faster but may not be reliable.

Here are basic and advanced steps for apache setup:

To setup , follow these very basic steps to setup apache in it's default location
Extract the apache bundle

1. tar xvf apache.x.tar
2. cd apache.x
3. ./configure
4. make
5. make install
6. cd ./apache/bin
7. ./httpd -k start[stop] # run's on port 80 which is default

Open browser..Go to http://localhost/ #....This should open a page ...It Works!!

To make an advanced setup like configuring additional modules, setup destination,etc , do the following:

1. tar xvf apache.x.tar
2. cd apache.x
3. ./configure --prefix=/path/to/apache --enable-[auth]=shared
4. make
5. make install
6. cd ./apache/bin
7. ./httpd -k start[stop]


Go to ./apache/conf/httpd.conf to change the port or any other setting. Will blog later on httpd.conf .

To have apache use a custom httpd.conf on startup do this :
./httpd -k start -f /custom/conf/httpd-custom.conf

To chek if apache supports DSO, run
./httpd -l # this lists all the modules setup in apache

I have listed a set of modules which were enabled for apache 2.0.59 to run a website on production:

--enable-authn_file=shared --enable-authn_anon=shared --enable-authz_host=shared --enable-log_config=shared --enable-logio=shared --enable-expires=shared --enable-headers=shared --enable-setenvif=shared --enable-mime=shared --enable-status=shared --enable-vhost_alias=shared --enable-dir=shared --enable-alias=shared --enable-rewrite=shared --enable-access=shared --enable-auth=shared --enable-userdir=shared --enable-autoindex=shared --enable-negotiation=shared

There are thousands of directives to be configured in httpd.conf .Refer http://httpd.apache.org/docs/2.2/mod/directives.html for apache 2.2

To change the server port by changing in ./apache/conf/httpd.conf the Listen 80 directive

Virtual host directive : A single apache server can have multiple virtual hosts and can server different sites.

To set this, open ./apache/conf/httpd.conf and enter the directive as below:


<VirtualHost 172.20.41.39:80>
 ServerName xyz.com
 ServerAlias www.v2.xyz.com v2.xyz.com www.eetonline.com www.xyz.com eetonline.com

 DocumentRoot /web/docs/electronics/v3.xyz.com
 DirectoryIndex index.html index.htm
 ErrorDocument 403 /nopagefound.jhtml
 Redirect /uk/motorola http://www.xyznet.com/
 
 <Location "/globalSpec"> # to specifiy the mounted directory to be used
  ExpiresActive on
  ExpiresDefault "access"
  Header append Cache-Control "no-cache"
  Header append Expires "Thu, 4 Jan 1990 10:00:01 GMT"
  Header append Last-Modified "Tue, Jan 27 2099 23:59:59 GMT"
  Header append Pragma "no-cache"
  Options FollowSymLinks MultiViews
  satisfy any
  Order deny,allow
  Deny from all
  AuthType Basic
  AuthName "xyz login"
  AuthUserFile /web/admin/apache/etc/auth/xyz.com-globalSpec
  require valid-user
 </Location>
<VirtualHost>