Monday, December 7, 2009

The unpredictability of conditional operator

If the 2nd and 3rd operands for conditional operator is not in the same type then better to avoid using it. E.g.

char x = 'X';
int i = 0;
System.out.print(true ? x : 0);
System.out.print(false ? i : x);
System.out.print(true ? new Integer(2) : new Float(3));

The specification for the conditional operator [JLS 15.25] has explained this. If both of the operands are not in the same object type then it will convert one of them into the more general one. For an example if operands and Integer and Float objects then it will convert Integer to Float. So as shown is above examples this will cause some unpredictable behaviors.

Thursday, April 30, 2009

Going backward ........

This is a tool that we can used to convert java 1.5 source to 1.4 or before.

http://retrotranslator.sourceforge.net/

Tuesday, April 28, 2009

Trick and Tips with instanceOf operator

The tricky bit about instanceof operator can be that if object on the left side is null, the condition evaluates to false. Therefore there is no need for null check after a class cast as in the following example.

Do not use instanceOf with this.

Do not use chained instanceOf operators use polymorphism instated of that.
From Effective C++, by Scott Meyers :
"Anytime you find yourself writing code of the form "if the object is of type T1, then do something, but if it's of type T2, then do something else," slap yourself.

http://www.javapractices.com/topic/TopicAction.do?Id=31
http://hanuska.blogspot.com/2006/08/tricky-instanceof-operator.html

Saturday, March 28, 2009

Creating Mobile Games in NetBeans

Nice tutorial to follow in 10 minutes;

Video
http://www.netbeans.tv/screencasts/Creating-Mobile-Games-in-NetBeans-441/

pdf
http://developers.sun.com/events/techdays/downloads/pdfs/6400_MobileGame_labdoc.pdf

but one exception,
need to change getMazeCanvas() on GameMIDlet.java as following

mazeCanvas = new MazeCanvas(this);

Otherwise it will give following null pointer exception

java.lang.NullPointerException
at javax.microedition.midlet.MIDletStateMapImpl.getStateImpl(+1)
at com.sun.midp.midlet.MIDletStateMap.getState(MIDletStateMap.java:46)
at javax.microedition.lcdui.Display.getDisplay(+10)
at org.netbeans.j1.game.MazeCanvas.(MazeCanvas.java:183)
at org.netbeans.j1.game.GameMIDlet.getMazeCanvas(GameMIDlet.java:640)
at org.netbeans.j1.game.GameMIDlet.svgMainMenuAction(GameMIDlet.java:255)
at org.netbeans.j1.game.GameMIDlet.commandAction(GameMIDlet.java:185)
at ..........................

Establishing HTTPS connection with tomcat

It is very simple to configure SSL connection with Tomcat. But before doing this it is essential to get idea about the basics of cryptography concepts with HTTPS.

(source http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Security6.html)

Secure Socket Layer (SSL) technology allows web browsers and web servers to communicate over a secure connection. In this secure connection, the data that is being sent is encrypted before being sent and then is decrypted upon receipt and before processing. Both the browser and the server encrypt all traffic before sending any data. SSL addresses the following important security considerations.

  • Authentication: During your initial attempt to communicate with a web server over a secure connection, that server will present your web browser with a set of credentials in the form of a server certificate. The purpose of the certificate is to verify that the site is who and what it claims to be. In some cases, the server may request a certificate that the client is who and what it claims to be (which is known as client authentication).
  • Confidentiality: When data is being passed between the client and the server on a network, third parties can view and intercept this data. SSL responses are encrypted so that the data cannot be deciphered by the third party and the data remains confidential.
  • Integrity: When data is being passed between the client and the server on a network, third parties can view and intercept this data. SSL helps guarantee that the data will not be modified in transit by that third party.
This tutorial will describe to establish a HTTPS connection with tomcat server by using java provided keytool.

First generate keys with keytool

%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA \
-keystore \"C:keystore"
When doing this please remember to initialize both keystore and privatekey passwords to "changeit"
Then edit $CATALINA_HOME/conf/server.xml file as follows.

<-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
Connector
port="8443" minSpareThreads="5" maxSpareThreads="75"
enableLookups="true" disableUploadTimeout="true"
acceptCount="100" maxThreads="200"
scheme="https" secure="true" SSLEnabled="true"
keystoreFile="C:\keystore\key_filename" keystorePass="changeit"
clientAuth="false" sslProtocol="TLS"


Do not forget to uncomment the above connector if it is commented
Then restart the server and go to url,

https://localhost:8443/