Posts

Showing posts from 2014

Java Generics Covariant and Contravariant

Image
Introduction Here the getter method ( ) => A Covariant A <: B ( ) => A <: ( ) => B If a A is subtype of B, then getter or A is subtype of getter of B. Here the setter method A => ( ) Contravariant B => ( ) <: A => ( ) Above Covariant and Contravariant can be explained from the following code: Say A is Animal public class Animal { @Override public String toString(){ return "I am a Animal"; } } Say B is Bear as follows public class Bear extends Animal { @Override public String toString(){ return "I am a Bear"; } }   As shown in the above code Bear is subtype of Animal. Here the Zookeeper class where getter and setter methods are public class Zookeeper { private Animal animal; public void setFeed(Animal animal){ this.animal =animal; } public Animal getFed(){ return this.animal; } } As shown in the above Zookeeper class, there is no indication of Bear,

Configure Google Analytics access from Tomcat SSL

Image
My recent web application had a requirement to publish the user requests to Google Analytics (GA) who has confirmed the registration. This is because there can be users who have visited the registration page but not confirm the registration or end up with errors even they click the registration button. Fortunately, this web application is using Spring Web Flow therefore I managed to implement the helper method that will send the confirmation information to the GA as a very last even. I've used the Apache HTTP Client to send a http request to GA using Google Measurement Protocol . In my case, I don't use Java general security store, instead use the custom keystore for Tomcat. This situation is very common when you need to configure the developer environment for the secure web applications development. First you have to enter the following URL to the Firfox browser. Then click the upper left corner pad lock icon: you will get the More information button. In the next window

Java Trick: Generic Array Converter

I had a problem of increasing size of the array. Of course I have to copy the array. But it is not easy with java Generics, because of the following problem: public class ArrayUtils<T>{ public T[] resizeArray(T[] source, int offset){ int length = source.length+offset; Object[] aNew = new Object[length]; System.arraycopy(source, 0, aNew, 0, source.length); return (T[])aNew; } } Of course you can compile the ArrayUtils class, but you cannot use this method as shown in the following code segment: public static void main(String[] args){ Integer[] ints = new Integer[2]; ints[0]=0; ints[1]=1; Integer[] newInts = new ArrayUtils<integer>().resizeArray(ints, 3); System.out.println(newInts.length); } Here the error: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; at net.au.abc.abcid.test.ArrayCopyTest.main(ArrayCopyTest.java:9) the problem, aNew never is possible to cast t

Java Trick: Double brace initialization

This is a trick I found from the Core Java™: Volume I—Fundamentals, Ninth Edition, 6.4.6. Anonymous Inner Classes. import java.util.ArrayList; import java.util.List; public class TestArrayList { private String s1; private static String s2; public static void main(String[] args) { TestArrayList tl = new TestArrayList(); tl.printList(new ArrayList (){ {add("a"); add("b"); add(s2);} }); } public void printList(/*only final variables are accessible to local inner classes*/final List sList){ printDynamicList(new ArrayList (){{add(s1); addAll(sList);}}); } public void printDynamicList(List dList){ } } As shown in the above code: As shown in the line# 10, you can add elements to the anonymous array: this is called Double brace initialization. Again in the line# 16, in the double brace initialization, s1 is added to the array which is a property fo the outer class. To add the parameter in line# 16, it should be final, this is the rule of l

Remote Debugging Tomcat 6 using SSH tunnel

Image
My current IDE is Intellij Idea 12 and it is running on Windows 7 machine. However, server exist on one of the RedHat Enterprise machines. The main challenge is to debug the remote tomcat server via SSH port 22 only because that is the only allowed path. Even I cannot ping the server. Instead of catalina.sh, I have to always run the "sudo service tomcatx start|stop" because tomcat is installed as a service using redhat yum. From this blog I found the details to setup the tomcat. I changed the TOMCAT_HOME/conf/tomcat6.conf file to have the following JAVA_OPTS JAVA_OPTS="${JAVA_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,address=15005,server=y,suspend=n" This is just a one line. As shown in the above, the port is address that is 15005. Now I have to create the SSH connection using Putty as shown in the below source port: 15005 Destination:  localhost:15005 After add as shown in the above screenshot, you have to start the session.  Now yo