Posts

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...

JavaScript 1: history object

The top most object in the Browser Object Model (BOM) is the “window”. I have three files history.html:  Start file1.html: Middle file2.html: Last In the Start, you can click the “go to middle” to visit the page file1.html. In this page you have two buttons, when you click “Back” it should take you back to the history.html, otherwise you can go to the last page click by “go to end”. In the “End” page you can go to the page entering -2 for “Start” or –1 for “Middle”. <!DOCTYPE html> <html> <head> </head> <body> <h1>Start</h1> <a href="file1.html">go to middle</a> </body> </html> As shown in the above code, there is just one link to go to the middle page. <!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1> In the Middle, one to go</h1> <a href="file2.html">go to end</a> <input type=...

Java EE 7 enterprise application development tutorial using Eclipse Kepler and GlassFish 4

Image
In this tutorial, I am going to use the latest technologies (to date) Eclipser Kepler GlassFish 4 as an Enterprise application server MySQL 5.6 JSF 2.2 Maven NOTE: The second blog of this is available in the JSF 2 Ajax . I am going to use archetypes to create EAR, EJB and WAR components. First, add the maven repository archetype catalogue http://repo1.maven.org/maven2/archetype-catalog.xml to the eclipse archetype selecting the Windows->Preferences and then Maven. Fig: 1 add maven archetype catalogue. The next step is crating a web project. As shown in the following fig 2, you can find webapp-javaee7. If you have little knowledge about maven, you can create new web application. Fig 2: select archetype to create WAR, EAR and EJB For example as shown in the following fig 3, you need to specify the Group Id, Artifact Id and Package. For both WAR and EJB project you will get the following error but it can be ignored. “Plugin execution not covered by lifecycle configur...

JSF 2.0 Ajax

Image
This is the second compilation of the tutorial Java EE 7 enterprise application development tutorial using Eclipse Kepler and GlassFish 4 . In this tutorial, I’ve enhanced the application to purely use the JSF 2 Ajax. As shown in the following diagram, I used Firebug to trace the Ajax request and response. As you see, jsf.js is the magic of JSF 2 Ajax. As shown in the above figure, when you add a new department, without refreshing the entire page, it updates the table under the “submit” button. Here the source for the facelet <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns = " http://www.w3.org/1999/xhtml " xmlns: ui = " http://java.sun.com/jsf/facelets " xmlns: h = " http://java.sun.com/jsf/html " xmlns: f = " http://java.sun.com/jsf/core " > < h: head > < title...

JAX-RS access via JSON using jQuery

Image
This is the continuation of the blog JSF 2.0 Ajax . In the blog JSF 2.0 Ajax , I’ve considered the Ajax access using JSF 2.0. In JSF, you always have to use the Managed bean. As I see managed bean is over-managed therefor more code needs to be added to avoid the undesirable side effects such as save twice. The REST approach is simple but not secure. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns = " http://www.w3.org/1999/xhtml " xmlns: ui = " http://java.sun.com/jsf/facelets " xmlns: h = " http://java.sun.com/jsf/html " xmlns: f = " http://java.sun.com/jsf/core " > < h: head > < script src = " http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js " > </ script > </ h: head > < body > < form action = "...