Search This Blog

Wednesday, June 2, 2010

Command prompt from here

Well sometimes I need a cmd right from the directory in the explorer within Windows. Microsoft offered the so called MS Powertoys, which have had a feature called command prompt from here. What that does was just a simple registry entry, which added an entry to the folder context menu. If you want to have such an extension you could easily add the following to your registry:



[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\command prompt]
@="Devenv from here"


[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\command prompt\command]
@="C:\\bin\\devenv.cmd %L"

(Just paste the lines above into a file with a .reg extension and click on it)

Now you could create a file called C:\bin\devenv.cmd and add you favorite Path extensions or variables. After adding the two entries to your registry you will see a new entry when you right click on a folder "Devenv from here".

Tuesday, April 20, 2010

Eclipse Galileo 3.5.1 Index Problems

After a while working with eclipse Galileo SR1 the startup of the IDE slows down. Beside this I frequently got an error like that:

Class file name must end with .class

To fix this I've done the following:
  1. Close Eclipse
  2. Delete /.metadata/.plugins/org.eclipse.jdt.core/*.index
  3. Delete /.metadata/.plugins/org.eclipse.jdt.core/savedIndexNames.txt
  4. Start Eclipse again
The eclipse bug list mentioned the behaviour in defect 286379 fixed for Milestone 3.6 M3.

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.

Keystore für Java Web Start Code Signing

Hier mal eine kleine Anleitung für die Erstellung eines Keystores, der zum Code Signing benutzt werden kann.


1. Neuen Keystore anlegen

keytool -genkey -keyalg RSA -keystore keystore.ks -alias myalias

Hier wird man nun nach allem möglichen Informationen gefragt, die später Inhalt des Zertifikates sind. Diese Informationen bekommen die Endbenutzer zu sehen, wenn sie z.B. auf eine Java Web Start Anwendung zugreifen, die mit diesem keystore signiert wurde.


2. Certrequest generieren

keytool -certreq -keystore keystore.ks -file csr.txt -alias myalias

Dieser Request muss nun von einer vertrauenswürdigen RootCa unterschrieben werden.


3. Generiertes Zertifikat für den Request importieren

keytool -import -file my.cert -alias myalias -trustcacerts -keystore keystore.ks

Es kann passieren das hier eventuell eine Fehlermeldung auftritt diese Fehlermeldung leigt meist am Inhalt des emfpangenen Zeritifkats. Hier darf nur der Text mit Begin und End drin stehen. Alle anderen Sachen haben hier nichts zu suchen.
Wenn es trotzdem nicht klappt, kann das Certificat auch erst im IE unter Datenschutz ->Zertifiakte importiert werden und danache als p7b (PKCS) exportiert werden.


4. RootCa Cert importieren

keytool -import -file rootca.cer -alias myrootca -trustcacerts -keystore keystore.ks

Das RootCa Zertifikat ist notwendig, falls die RootCa nicht im allgemeinen Java trusted RootCa File enthalten ist (cacerts). Mit dem oben aufgeführten Befehl wird das RootCa Zertifikat mit in den neu erstellten Keystore aufgenommen. Das RootCa Zertifikat kann einfach aus einer vorhandenen SSL Seite exportiert werden.

Friday, January 29, 2010

Keys I missed on an Apple keyboard

Well to all of you movers to Apple here is a list of the most missed keys on my Apple Keyboard:

Tilde : ~ ALT-N
Pipe : | ALT-7
curly bracket : {} ALT-8/9
square bracket: [] ALT-5/6

End of line : CMD-right
Start of line: CMD-left
Begin of doc : CMD-UP
End of doc : CMD-DOWN
Jump word : ALT-left/right

Generate a self signed SSL cert for your webserver

Well this is I guess the quickest way to generate a self signed SSL cert:


Step 1: Generate a Private Key
openssl genrsa -des3 -out server.key 1024


Step 2: Generate a CSR (Certificate Signing Request)
openssl req -new -key server.key -out server.csr


Step 3: Remove Passphrase from Key
openssl rsa -in server.key.org -out server.key


Step 4: Generating a Self-Signed Certificate
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt


Step 5: Installing the Private Key and Certificate
cp server.crt /etc/apache2/ssl
cp server.key /etc/apache2/ssl


Step 6: Configuring SSL Enabled Virtual Hosts

SSLEngine on
SSLCertificateFile /usr/local/apache/conf/ssl.crt/server.crt
SSLCertificateKeyFile /usr/local/apache/conf/ssl.key/server.key
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

Password after standby Windows

Well one of the things which are really bugging me when I run Windows as a HTPC OS is the default setting for wake up after standby. The default will lock your machine, which is totally useless for an HTPC. To get rid of this just:

  • Go to the Power Options (or I guess Energy Options in Vista or Win7)
  • Search within the list for standby
  • Select "Prompt for password when computer resumes from standby"
et voila

Password expired Windows 7

The default setting in Windows 7 for your password policies is an expiry time about 42 days. If you want to get rid of this just do

  • Start->lusrmgr.msc
  • Select your user and check "Password never expires"

Betriebszustände

ACPI-Betriebszustände für die Energieverwaltung eines PCs

ACPI Beschreibung Leistungsaufnahme
S0 Normalzustand, einzelne Komponenten können sich im Standby-Modus befinden, Leistungsaufnahme je nach Auslastung 40 bis 200 Watt
S1 Bildschirm ist aus, kaum sparsamer als S0, CPU: C1 bis C3 40 bis 100 Watt
S2 wie S1, wird in der Praxis fast nie benutzt 40 bis 100 Watt
S3 System schläft, Daten befinden sich im Hauptspeicher, Aufwachzeit innerhalb weniger Sekunden, Betriebszustand wird als Suspend-to-RAM bezeichnet 2 bis 15 Watt
S4 System schläft, Daten werden auf der Festplatte gespeichert, Aufwachzeit innerhalb einer Minute, Betriebszustand wird als Suspend-to-Disk bezeichnet 1 bis 10 Watt
S5 PC lässt sich per Taster starten 1 bis 10 Watt
G3 Netzteil ist vom Stromnetz getrennt 0 Watt
C0 Prozessor arbeitet normal, verschiedene P-States möglich 10 bis 130 Watt
C1 Prozessor im leichten Schlafzustand (Halt-Befehl), Wechsel zwischen C0 und C1 mehrmals pro Sekunde möglich 10 bis 30 Watt
C2/C3 Prozessor in tieferen Schlafzuständen 7 bis 15 Watt
C4... Prozessor in noch tieferen Schlafzuständen, vorwiegend Mobilprozessoren