Friday, July 25, 2008

.bashrc script - feel free to use my bash script on unix flavors.

export PATH=/usr/bin:\
/usr/sbin:\
/usr/sfw/bin:\
/usr/sfw/sbin:\
$ANT_HOME/bin:\
/net/flashy.red.iplanet.com/export/d1/S11/SUNWspro/bin:\
/home/la204265/installations/SunStudio12/SUNWspro/bin:\
$JAVA_HOME/bin:\
/bin:/sbin:\
/usr/ccs/bin:\
/usr/proc/bin:\
/usr/platform/sun4u/sbin:\
/.secure/bin:\
/.secure/arch/sparc_sun_solaris2.3:\
/usr/local/bin:\
/usr/local/sbin:\
/usr/local/netscape:\
/usr/dist/exe:\
~/bin:\
/tools/ns-arch/sparc_sun_solaris2.10/bin:\
$PATH

export MANPATH=/usr/man:\
/usr/dt/man:\
/usr/openwin/share/man:\
/usr/local/man:\
/export/man:$MANPATH

export LD_LIBRARY_PATH=/usr/lib/mps:\
/usr/lib:\
/tools/ns-arch/soft/gcc-3.2.1/run/default/sparc_sun_solaris2.10/lib:\
/usr/local/lib:\
/lib:\
/usr/X/lib:\
/usr/ucblib:\
/usr/suntools/internal/SUNWspro/lib:\
/usr/local/ssl/lib:\
/usr/local/lib/gtk/themes/engines:\
/usr/local/ssl/lib:\
/net/flashy.sfbay/usr/re/tools/sparc/lib:\
$LD_LIBRARY_PATH

export CVSROOT=:pserver:la204265@jescvs.sfbay.sun.com:/m/src

export ANT_HOME=/home/la204265/work/tools/ant/apache-ant-1.7.0

export JAVA_HOME=/share/builds/components/jdk/1.5.0/SunOS
export JAVAHOME=/share/builds/components/jdk/1.5.0/SunOS
export JDK14_COMPILER_PATH=/share/builds/components/jdk/1.4.1/SunOS

export CVS_AM71_TAG="cvs co -r AM_MAIN_BRANCH DSAME"
export CVS_AM70_TAG="cvs co -r DSAME70_PATCH_BRANCH DSAME"
export CVS_AM63_TAG="cvs co -r DSAME63_RTM_BRANCH DSAME"
export CVS_AGENT22_TAG="cvs co -r WebAgents_22_RTM_Branch Agents"
export CVS_AGENT21_TAG="cvs co -r DSAMEagents21_sustaining_branch Agents"
export CVS_CHECKIN_FORMAT="cvs commit -m 'Bug: Esc: CRT: Review:' filename"
export ENV_ECLIPSE="~/tools/eclipse/eclipse -vmargs -Xmx700M"

export SUNSTUDIO_12=/home/la204265/installations/SunStudio12/SUNWspro/bin/sunstudio

export VISUAL=vi
export EDITOR=vi
export TETEXDIR=/usr/local/teTeX
export INFOPATH=$TETEXDIR/info

function get_xserver ()
{
echo $(hostname)

if [[ $(hostname) == "muir" ||
$(hostname) == "mojave" ||
$(hostname) == "lassen" ||
$(hostname) == "avatar" ||
$(hostname) == "koval" ]]; then
DISPLAY="avatar.red.iplanet.com:0.0"
fi
}

if [ -z ${DISPLAY:=""} ]; then
get_xserver
fi

export DISPLAY

#---------------
# Some settings
#---------------

ulimit -S -c 0 # Don't want any coredumps
set -o notify
set -o noclobber
set -o ignoreeof
#set -o nounset
#set -o xtrace # Useful for debuging

# Enable options:
shopt -s cdspell
shopt -s cdable_vars
shopt -s checkhash
shopt -s checkwinsize
shopt -s mailwarn
shopt -s sourcepath
shopt -s no_empty_cmd_completion # bash>=2.04 only
shopt -s cmdhist
shopt -s histappend histreedit histverify
shopt -s extglob # Necessary for programmable completion

# Disable options:
shopt -u mailwarn
unset MAILCHECK # I don't want my shell to warn me of incoming mail

export TIMEFORMAT=$'\nreal %3R\tuser %3U\tsys %3S\tpcpu %P\n'
export HISTIGNORE="&:bg:fg:ll:h"
export HOSTFILE=$HOME/.hosts # Put a list of remote hosts in ~/.hosts

#-----------------------
# Greeting, motd etc...
#-----------------------

# Define some colors first:
red='\e[0;31m'
RED='\e[1;31m'
blue='\e[0;34m'
BLUE='\e[1;34m'
cyan='\e[0;36m'
CYAN='\e[1;36m'
NC='\e[0m' # No Color
# --> Nice. Has the same effect as using "ansi.sys" in DOS.

# Looks best on a black background.....
echo -e "${CYAN}This is BASH ${RED}${BASH_VERSION%.*}\
${CYAN} - DISPLAY on ${RED}$DISPLAY${NC}\n"
date
if [ -x /usr/games/fortune ]; then
/usr/games/fortune -s # makes our day a bit more fun.... :-)
fi

function _exit() # function to run upon exit of shell
{
echo -e "${RED}Hasta la vista, baby${NC}"
}

trap _exit EXIT

#---------------
#---------------
# Shell Prompt
#---------------

if [[ "${DISPLAY#$HOST}" != ":0.0" && "${DISPLAY}" != ":0" ]]; then
HILIT=${red} # remote machine: prompt will be partly red
else
HILIT=${cyan} # local machine: prompt will be partly cyan
fi

alias cls=clear
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# -> Prevents accidentally clobbering files.
alias mkdir='mkdir -p'

alias h='history'
alias j='jobs -l'
alias r='rlogin'
alias rt='rlogin -l root'
alias rl='rlogin -l la204265'
alias which='type -all'
alias ..='cd ..'
alias path='echo -e ${PATH//:/\\n}'
alias print='/usr/bin/lp -o nobanner -d $LPDEST'
# Assumes LPDEST is defined
alias pjet='enscript -h -G -fCourier9 -d $LPDEST'
# Pretty-print using enscript
alias background='xv -root -quit -max -rmode 5'
# Put a picture in the background
alias du='du -kh'
alias df='df -kTh'

# The 'ls' family (this assumes you use the GNU ls)
alias la='ls -Al' # show hidden files
alias ls='ls -hF' # add colors for filetype recognition
alias lx='ls -lXB' # sort by extension
alias lk='ls -lSr' # sort by size
alias lc='ls -lcr' # sort by change time
alias lu='ls -lur' # sort by access time
alias lr='ls -lR' # recursive ls
alias lt='ls -ltr' # sort by date
alias lm='ls -al |more' # pipe through 'more'

alias tree='tree -Csu' # nice alternative to 'ls'

# tailoring 'less'
alias more='less'
export PAGER=less
export LESSOPEN='|/usr/bin/lesspipe.sh %s 2>&-'
# Use this if lesspipe.sh exists.
export LESS='-i -N -w -z-4 -g -e -M -X -F -R -P%t?f%f \
:stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...'

# spelling typos - highly personnal :-)
alias xs='cd'
alias vf='cd'
alias moer='more'
alias moew='more'
alias kk='ll'

#----------------
# a few fun ones
#----------------

function xtitle ()
{
case "$TERM" in
*term | rxvt)
echo -n -e "\033]0;$*\007" ;;
*)
;;
esac
}

# aliases...
alias ecl='/home/la204265/tools/eclipse/eclipse -vmargs -Xmx700M'
alias top='xtitle Processes on $HOST && top'
alias make='xtitle Making $(basename $PWD) ; make'
alias ncftp="xtitle ncFTP ; ncftp"

remoteon

Thursday, May 15, 2008

Interesting socket bug related to data corruption.


A DESCRIPTION OF THE PROBLEM :
Asynchronous closes can result in data that should be written to one channel being
written to a completely unrelated stream if another stream is opened from a
concurrent thread.

Link:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6285901

Wednesday, April 16, 2008

HTTP POST API usage.

Code snippet to do HTTP POST to a web server. Reference javaworld portal.
-------------------------------------------------------------------------
URL url;
URLConnection urlConn;
DataOutputStream printout;
DataInputStream input;
// URL of CGI-Bin script.
url = new URL (getCodeBase().toString() + "env.tcgi");
// URL connection channel.
urlConn = url.openConnection();
// Let the run-time system (RTS) know that we want input.
urlConn.setDoInput (true);
// Let the RTS know that we want to do output.
urlConn.setDoOutput (true);
// No caching, we want the real thing.
urlConn.setUseCaches (false);
// Specify the content type.
urlConn.setRequestProperty
("Content-Type", "application/x-www-form-urlencoded");
// Send POST output.
printout = new DataOutputStream (urlConn.getOutputStream ());
String content =
"name=" + URLEncoder.encode ("Buford Early") +
"&email=" + URLEncoder.encode ("buford@known-space.com");
printout.writeBytes (content);
printout.flush ();
printout.close ();
// Get response data.
input = new DataInputStream (urlConn.getInputStream ());
String str;
while (null != ((str = input.readLine())))
{
System.out.println (str);
textArea.appendText (str + "\n");
}
input.close ();

Saturday, March 29, 2008

Tomcat goodies on troubleshooting standalone server.

There are only really 3 things likely to go wrong during the stand-alone
Tomcat install:

(1) The most common hiccup is when another web server (or any process for that
matter) has laid claim to port 8080. This is the default HTTP port that
Tomcat attempts to bind to at startup. To change this, open the file:

$CATALINA_HOME/conf/server.xml

and search for '8080'. Change it to a port that isn't in use, and is
greater than 1024, as ports less than or equal to 1024 require superuser
access to bind under UNIX.

Restart Tomcat and you're in business. Be sure that you replace the "8080"
in the URL you're using to access Tomcat. For example, if you change the
port to 1977, you would request the URL http://localhost:1977/ in your browser.

(2) An "out of environment space" error when running the batch files in
Windows 95, 98, or ME operating systems.

Right-click on the STARTUP.BAT and SHUTDOWN.BAT files. Click on
"Properties", then on the "Memory" tab. For the "Initial environment" field,
enter in something like 4096.

After you click apply, Windows will create shortcuts which you can use
to start and stop the container.

(3) The 'localhost' machine isn't found. This could happen if you're behind a
proxy. If that's the case, make sure the proxy configuration for your
browser knows that you shouldn't be going through the proxy to access the
"localhost".

In Netscape, this is under Edit/Preferences -> Advanced/Proxies, and in
Internet Explorer, Tools -> Internet Options -> Connections -> LAN Settings.

Friday, February 22, 2008

Notes on Runnable Jars

You can easily package an application's entire set of classes and resources into a Java Archive (JAR). In fact, that is one goal of having jar files. Another is to let users easily execute the application stored in the archive. Why then are jar files second-class citizens in the Java universe—functioning only as archives—when they can be first class, right alongside native executables?

To execute a jar file, you can use the java command's -jar option. For example, say you have a runnable jar file called myjar.jar. Because the file is runnable, you can execute it like this: java -jar myjar.jar.

Alternatively, the Java Runtime Environment (JRE), when installed on an OS like Microsoft Windows, associates jar files with the JVM so you can double-click on them to run the application. These JARs must be runnable.

The question is: How do you make a JAR runnable?

The manifest file and the Main-Class entry

Inside most JARs, a file called MANIFEST.MF is stored in a directory called META-INF. Inside that file, a special entry called Main-Class tells the java -jar command which class to execute.

The problem is that you must properly add this special entry to the manifest file yourself—it must go in a certain place and must have a certain format. However, some of us don't like editing configuration files.

Let the API do it for you

Since Java 1.2, a package called java.util.jar has let you work with jar files. (Note: It builds on the java.util.zip package.) Specifically, the jar package lets you easily manipulate that special manifest file via the Manifest class.

For more details, refer to the link:

http://www.javaworld.com/javaworld/javatips/jw-javatip127.html

Saturday, December 8, 2007

Remote debugging an app deployed in tomcat

Just start tomcat using catalina.sh jpda start from /bin. The default options will start remote debugging in 8000. For details, read below.


How do I configure Tomcat to support remote debugging?

The short answer is to add the following options when the JVM is started: -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n There are a number of ways you can do this depending on how you normally start Tomcat: * Set environment variables JPDA_ADDRESS=8000 and JPDA_TRANSPORT=dt_socket and then start tomcat using catalina jpda start. * If you run Tomcat using service wrapper, check the documentation for the service to determine how to set the required JVM options. * If you start Tomcat from within an IDE, check the documentation for the IDE to determine how to set the required JVM options.

The port does not need to be set to 8000, it may be any value appropriate for your system.

Whilst this is very useful in development it should not be used in production because of both security and performance implications.

How do I remotely debug Tomcat using Eclipse?

This answer assumes that you have a project set up with all of the fixings and have some idea of what you're doing in this respect. If not then thats really outside the scope of this topic and more in the scope of you needing to go to [[WWW] eclipse.org] and read up on how to use your ide, and maybe practice a little bit before you come back to this. We're also going to assume you have some idea of what a debugger is and how to use one.

Make sure tomcat is started and that your app is deployed and the sources, etc are all defined as resources in your app. If you have a servlet or something, set a breakpoint where its sure to hit on the next request. Go to "Run->Debug...". Click on "Remote Java Applications", then click "New". Type in the title and all. Notice that port 8000 from the Tomcat instructions. Save and run. Eclipse will connect to the VM that Tomcat is running under. Wow, that was easy! Now go type the url to submit to your servlet or whatever in your browser. Boom you hit the breakpoint right? Have fun!

How do I remotely debug Tomcat using NetBeans IDE?

This answer assumes that you have correctly set up a NetBeans IDE project and that you know how to use the NetBeans IDE debugger. If not, please go to [WWW] http://www.netbeans.org/kb/using-netbeans/40/debug.html and read up on how to use NetBeans IDE and its debugger.

Make sure that Tomcat is started in debug mode as described above, that your application is deployed, and that the sources are all defined as resources in your application. If you have a servlet or JSP file, set a breakpoint where you think a problem might be occurring. Go to "Run->Attach Debugger". A dialog pops up to let you specify the following options:

  • Debugger: JPDA Debugger

  • Connector: SocketAttach

  • Host: The IP address of the host your Tomcat installation is running on (127.0.0.1 if it is your local machine).

  • Port: The port of your Tomcat debugging interface, which should be 8000 if you've followed the instructions above.

When you press OK, you have a debugging connection very similar to local debugging.

Note that NetBeans IDE has a second option -- you can debug JSP files and servlets locally using a Tomcat server that is bundled with the IDE. When you debug a JSP file or servlet in the IDE, the bundled Tomcat server automatically starts in debug mode, and the debugger connects to it.

Copied from source:

http://wiki.apache.org/tomcat/FAQ/Developing

Friday, November 16, 2007

Differences between HashMap and HashTable

Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.

The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.

Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.

And, a third difference is that HashMap permits null values in it, while Hashtable doesn't.

For new code, try to use HashMap.

(http://www.jguru.com/faq/view.jsp?EID=430247)

Monday, November 12, 2007

Monday, October 8, 2007

Do You Know How to Optimize Your JVM?

Here is a video on "Optimize your JVM"

http://www.theserverside.com/news/thread.tss?thread_id=47121

source: theServerside.com

Wednesday, May 16, 2007

Useful JVM options for debugging.

To attach remote debugger to a stand alone java application, pass the following options to JVM.

${JAVA_HOME}/bin/java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=3345 JAVA_CLASS_NAME

* To make java process wait till debugger is attached, use suspend=y in the above options.

* To remote debug a war or ear application deployed in web or application container, set above options in web server or app server server.xml file.

* To provide more heap space to JVM, use options -Xms and -Xmx.