Search This Blog

Wednesday, March 17, 2010

Instantiate a Generic Type in Java

Once you used generics for a while you might stumble upon the following question:

public class MySample {
       private     T    mySample = null;


       public MySample() {
               mySample = new T();  // well this would be fine, but it doesn't work because the compiler doesn't know the type at this time!
      }
}

Ok, fair enough the compiler doesn't know if T is an interface or an object and couldn't instantiate the type. If your T is a class you could solve the question with this:



public class MySample {
       private     T    mySample = null;

       public MySample(Class _class) {
               mySample = _class.newInstance();
      }
}


This should work with the little overhead of specifying the class in the generic and the constructor:



  MySample  sample = new MySample(Whatever.class); 

  

Tuesday, February 16, 2010

Java File Format Version

Once you're moving around for a while in Java based environments you might have seen an error like this

java.lang.UnsupportedClassVersionError: Bad version number in .class file


If you hit this kind of error than its time to realize that Java byte code might differ between JDK versions :-) You'll find a long list of explanations about the Java Class file format all over in the web (http://en.wikipedia.org/wiki/Class_%28file_format%29).
Here is a list of class file versions and the corresponding JDK

J2SE 6.0 = 50 (0x32 hex),
J2SE 5.0 = 49 (0x31 hex),
JDK 1.4 = 48 (0x30 hex),
JDK 1.3 = 47 (0x2F hex),
JDK 1.2 = 46 (0x2E hex),
JDK 1.1 = 45 (0x2D hex).

Ok thanks for the list but how do I get the version out of an existing class file?
Answer:

 javap -v MyClass | more

this will print out something like this:

 class MyClass extends java.lang.Object
  minor version: 0
  major version: 50
....

Thursday, February 4, 2010

Mount network share at startup in MacOSx

I run a Buffalo TeraStation NAS server within my environment. To use it within a unix based machine like my IMac I just need to open a Finder and select the share on the server. That'll do the trick for instant access or manual connect. If you want to have your shares mapped before you logon do the following:

1. Create Script, which mounts your shares (see my sample below)


2. Add the script to the LoginHook
     sudo defaults write com.apple.loginwindow LoginHook /path/to/script
    (I store my script on MacOSX in /Library/Scripts)
. More infos on the LoginHook

Note: If you mount a share keep in mind that mounting the share with root privileges will only allow root to use the share. Because of that I used su $1 -c to run the mount as the current logged in user. 



Sample mount script for a NAS server with afp protocol for Mac OSX (if your NAS has no afp just change the mount parameter accordingly):
#!/bin/bash
 

# Information about the NAS access
    username=MY_NAS_USER
    password=MY_NAS_PASS
    hostname=MY_NAS_HOST

# The $1 is within login scripts the current user, which enters MacOsx   
loginuser=$1

# Create a mount dir if necessary
save_mkdir() {
    if [ ! -d $1 ];
    then
        su $loginuser -c "mkdir $1"
    fi
}

# Create a mount as the current logged in user
do_mount() {
    if /sbin/mount|grep -q $1; then
    # echo "Mounted"
        /sbin/umount $1
    fi
    save_mkdir $1
    su $loginuser -c "/sbin/mount -t afp afp://$username:$password@$hostname/$2 $1"
}

echo "******************************************************"
echo "*** Unmount all network drives from boshuda"
echo "******************************************************"
/sbin/umount /Volumes/pictures
/sbin/umount /Volumes/music

echo "******************************************************"
echo "*** Mount all network drives from boshuda"
echo "******************************************************"
do_mount /Volumes/pictures pictures
do_mount /Volumes/music my_music

Tuesday, February 2, 2010

Include the output of a servlet within your jsp

Sometimes its useful to call a servlet and include the output of the servlet within your jsp page. The easiest way to call a servlet from a jsp is the jsp:include tag. Lets assume you have a jsp fragment like this

...

VERSION: <jsp:include page="/InfoServlet?name=VERSION"></jsp:include>

..


this will cause a call to the InfoServlet with the parameter name and the value VERSION. The output will be written by the InfoServlet.
HINT:
The calling servlet has to use the same access method to the response as the jsp and should not close the stream. Otherwise the output of your jsp will stop immediately.
So if you don't know just use
response.getOutputStream() to write to the response.

Min .bashrc


# *****************************************************************************
# bashrc
# *****************************************************************************
SHELL=/bin/bash
PATH=$PATH:$HOME/bin:/usr/local/bin
PS1="\u \w$ "

export EDITOR=vi
export HISTCONTROL=ignoredups
# Cli Colors
export CLICOLOR=1
# use yellow for dirs
export LSCOLORS=dxfxcxdxbxegedabagacad
export LSCOLORS=exfxcxdxbxegedabagacad

# *****************************************************************************
# alias
# *****************************************************************************
alias ..='cd ..'
alias h=history
alias ip='ipconfig getifaddr en0'
alias l='ls -al'
alias lp='ls -p'
alias lt='ls -lt'
alias sys_prefs='open -a System\ Preferences'

Monday, February 1, 2010

Log4jdbc Configuration for WebLogic


I like to use log4jdbc (http://code.google.com/p/log4jdbc/) for logging DB statements within Java JDBC environment. Here is a short instruction how to set it up within a BEA/Oracle weblogic container.


1. Download log4jdbc and slf4j

2. Edit the startup script of you domain and add the following lines

set SAVE_JAVA_OPTIONS=%JAVA_OPTIONS% -Dlocal.url=t3://localhost:7001 -Dlog.dir=D:\logs %LOG4JDBC_OPTIONS%
set LOG4JDBC_OPTIONS=-Dlog4jdbc.drivers=oracle.jdbc.OracleDriver -Dlog4jdbc.sqltiming.warn.threshold=200 -Dlog4jdbc.sqltiming.error.threshold=500


3. This will register a log4jdbc driver for Oracle and the log level WARN will be used for statements longer than 200ms, as well as the ERROR level will be uses for statements which take longer than 500ms

4. You also have to add the necessary jar files to the startup classpath of your weblogic container

set LOG4JDBC_HOME=%DOMAIN_HOME%\log4jdbc
set SAVE_CLASSPATH=%CLASSPATH%;%LOG4JDBC_HOME%\log4jdbc3-1.1.jar;%LOG4JDBC_HOME%\slf4j-api-1.5.0.jar;%LOG4JDBC_HOME%\slf4j-log4j12-1.5.0.jar;%LOG4JDBC_HOME%\log4j-1.2.14.jar;%LOG4JDBC_HOME%\log4j.xml;%LOG4JDBC_HOME%\kdm_conf

as you see we need a log4jdbc, slf4j-api, slf4j-log4j and a log4j jar. The version should fit together and the log4j version has to be at least 1.2.14. The rest of the classpath links to the log4j config. A sample config for log4jdbc is here http://code.google.com/p/log4jdbc/source/browse/trunk/doc/log4j.xml.

5. Last step is the setup of your jdbc configuration. To activate the log4jdbc driver you could just change your current jdbc config file or the data source within the weblogic container. Here is a sample:
Original JDBC config
jdbc:oracle:thin:@hostname:2251@ORACLE_SID
oracle.jdbc.OracleDriver

log4jdbc config

jdbc:log4jdbc:oracle:thin:@hostname:2251@ORACLE_SID
net.sf.log4jdbc.DriverSpy





Thats it.