Friday

Check if a web service is accessible? Ping a host for availability.

This summary is not available. Please click here to view the post.

WebLogic RMI - Set timeout


Hashtable prop = new Hashtable();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URL, jmsProviderUrl);
prop.put(Context.SECURITY_PRINCIPAL, "");
prop.put(Context.SECURITY_CREDENTIALS, "");
prop.put("weblogic.jndi.connectTimeout", 15000);
prop.put("weblogic.jndi.responseReadTimeout", 15000);
                                   
InitialContext ctx = new InitialContext(prop);

Tuesday

Where is the generated JSP class files stored (JBOSS, Weblogic) ?

In JBOSS 7.1:

C:\jboss\jboss-as-7.1.0.Final\standalone\tmp\work\jboss.web\default-host\<project name>\org\apache\jsp\pages

In Oracle Weblogic Server 11gR1(10.3.6):

<Your weblogic path>\<user_projects>\domains\<YOUR_DOMAIN_NAME>\servers\<APP_SERVER_NAME>\tmp\_WL_user\<APPLICATION_WAR(for me \_auto_generated_ear_\8fkeak\jsp_servlet)>

Monday

Java SE 7 New Features

1. Catching multiple exception together:

} catch(IOException | SQLException e) {
       //rethrow or logging
}

2. Numeric literals with underscores:
To prevent eye misdirection in numbers with multiple zeros,  you can declare these types of numbers as below:

int thousand =  1_000;
System.out.println(thousand);
In Java6:






3. Diamond Operator:
You don't have to declare types in both sides:

List<String> list1 = new ArrayList<>();
In Java 6:





4. Automatic resource management:

Connections, Files, Input/OutStreams etc. are auto closed. (java.lang.AutoCloseable).

public void newTry() {

       try(FileOutputStream fos = new FileOutputStream("test.txt");
           DataOutputStream dos = new DataOutputStream(fos)) {
              dos.writeUTF("Java 7 Automatic resource management");

       } catch (IOException e) {
              // log the exception
       }
             
}

In Java 6:
public void oldTry() {
       FileOutputStream fos = null;
       DataOutputStream dos = null;

       try {
              fos = new FileOutputStream("test.txt");
              dos = new DataOutputStream(fos);
              dos.writeUTF("Java 7 Automatic resource management");

       } catch (IOException e) {
              // log the exception

       } finally {
              try {
                     fos.close();
                     dos.close();
              } catch (IOException e) {
                     // log the exception
              }
       }
}

Wednesday

Java Platforms

Java Platform, Standard Edition (Java SE) :
* Core functionality of Java.
* For desktop applications, deployment technologies.
* The packages it has: java.lang, java.io, java.math, java.text, java.util, java.net, java.applet, java.beans, java.awt, java.security, java.sql, javax.rmi, javax.swing, javax.crypto, javax.naming, javax.print, org.w3c.dom, etc.

Java Platform, Enterprise Edition (Java EE) :
* J2EE was the old name, Java EE is the current one. You can find the history below:
J2EE 1.2 (December 12, 1999)
  J2EE 1.3 (September 24, 2001)
  J2EE 1.4 (November 11, 2003)
  Java EE 5 (May 11, 2006)
  Java EE 6 (December 10, 2009)
  Java EE 7
  Java EE 8
* It is built on the Java SE.
* For developing web applications, large scale enterprise applications.
* It has several specifications: JMS, Web services, Servlet, JTA, EJB, JSP, JSF, JPA, Batch applications, WebSocket, etc.

Java Platform, Micro Edition (Java ME) :
* For mobile devices.

Tuesday

Java AntiPatterns

Anti-patterns could be considered as bad programming practice. I have created a list below  that I have investigated and known. As I come to mind, I will try to add new anti-patterns.

1. The first example ensures that you can safely replace ArrayList to for example LinkedList. And you don't need to change the rest of the code. Interfaces always give more flexibility.
//USE THIS:
List<Object> object1 = new ArrayList<Object>();

//INSTEAD OF:
ArrayList<Object> object1 = new ArrayList<Object>();

2. Measure Time Intervals
javadoc says:
  - nanoTime() is a precision timer, will always produce positive.(Don't use it for Date objects.)
  - currentTimeMillis() is not a timer, it is the "wall clock". The measured time interval could be wrong or even negative.

long startTime = System.currentTimeMillis();
long elapsedTime = System.currentTimeMillis() - startTime;

//a better solution is:
long startTime = System.nanoTime();
long elapsedTime = (System.nanoTime() - startTime) / 1000000; // for ms

3. Return Collections.emptyList() instead of null or new ArrayList<T>().
  - Collections.EMPTY_LIST is not type safe.
  - (Collections.<T>emptyList()) Collections.emptyList() uses type-inference and therefore returns List<T> and it does the cast for you.

Advantages:
  - API clients won't get NullPointerException. (instead of returning null)
  - Better performance for CPU, memory wise. (instead of returning new ArrayList<T>())
  - It saves an if statement, makes code look cleaner. Your callers won't need to control null statement as below:
 if (list != null) {
  for (Item i : list) {

  }
 }

Notes:
  - Collections.emptyList() is an immutable list, a list to which you cannot add element. If you try to do some changes, you would get UnsupportedOperationException().
  - If there is semantic difference between "it is not there(null)" and "it is there but there is nothing in it(empty)" then be carefull.

4. 
Integer i = new Integer(0);
Boolean b = new Boolean(true);

//a better, faster solution also saves memory:    
Integer i = Integer.valueOf(0);
Boolean b = Boolean.valueOf(true); // or Boolean.TRUE

5.  Overrride toString method:
public class TestObject {
       private String name;
       private String old;

       public TestObject() {
       }

       public String getName() {
              return name;
       }

       public void setName(String name) {
              this.name = name;
       }

       public String getOld() {
              return old;
       }

       public void setOld(String old) {
              this.old = old;
       }

       public String toString() {
              return this.getName() + " / " + this.getOld();
       }
}
public static void main(String[] args) {
       List<TestObject> list = new ArrayList<TestObject>();

       TestObject t = new TestObject();
       t.setName("eda");
       t.setOld("1");
       list.add(t); 
           
       for(TestObject o : list) {
              System.out.println(o);
       }
}
Output:
  eda / 1

6. StringBuilder or StringBuffer?
  • StringBuffer is thread safe,synchronized but StringBuilder is not.
  • StringBuilder is faster than StringBuffer because it is not synchronized.
  • Use StringBuilder when you need a modifiable string and only one thread will access and modified this string.
  • Use StringBuffer when you need  a modifiable string and multiple threads will access and modify it.
  • Don't use StrinBuffer unnecessarily, because it takes up CPU time for locking, unlocking code.
7.   Concatenate Multiple Strings
//this solution is memory waster
String s = "";
for(Person p : personList) {
   s += ", " + p.getName();
}
s = s.substring(2);                  

//A better solution
StringBuilder sb = new StringBuilder();
for(Person p : personList) {
     if(sb.length() > 0) sb.append(", ");
     sb.append(p.getName());
}

8. Compare Strings
if (name.compareTo("John") == 0)
if (name == "John")
if (name.equals("John"))
             
//A better solution
if ("John".equals(name))//Constant comes first,so if name is null it won't get exception.
if (name.length() == 0)
if (name.isEmpty())             

//A better solution, if name is null this solution is safe.
if ("".equals(name))

9. Re-throw RuntimeException
try {
       // Code that might throw an exception

} catch(Exception e) {
       throw new RuntimeException(e);
}      
             
//A better solution
//Catch all the checked exceptions separately even if they are a lot.
//Only do this when your clients cannot recover from the problem that you are throwing checked exception. (re-throw any checked exception as RuntimeException())
//RuntimeException(e.getMessage() + ": " + yourinputs, e) --> this is the best way of passing the original exception. Put your inputs to end of messages. If generic messages would be at the beginning, tracing the logs will be easier.

try {
       // Code that might throw an exception

} catch(IOException e) {
       throw new RuntimeException(e.getMessage(), e);

} catch(ClassNotFoundException e) {
       throw new RuntimeException(e.getMessage(), e);

}

10. Catch and Log Exception
//The purpose of this catch statement are logging, it is re-throw the same exception.      
//Do this if only you know the caller doesn't log the exceptions. (especially the method is called somewhere not in your control)
   
try {
       // Code that might throw an exception

} catch(IOException e) {
    log.error(e.getMessage(), e);
    throw e;

} catch(ClassNotFoundException e) {
    log.error(e.getMessage(), e);
    throw e;
}

11. Money in floating points
Money should never be stored in a floating point data type like float, double!!!
BigDecimal d1 = new BigDecimal(2.34); 
BigDecimal d2 = new BigDecimal("2.34"); // use this!!!
System.out.println("d1: " + d1 + " d2: " + d2);

/* 
    Output:
    d1: 2.339999999999999857891452847979962825775146484375  
    d2: 2.34
*/
double d1 = 1.14 * 75; // = 85.5  but d1=85.4999...!!!!!!!!!
System.out.println("d1:: " + d1);
System.out.println("round(d1):: " + Math.round(d1)); //output=85!!!           

//The better solution:
BigDecimal d2 = (new BigDecimal("1.14")).multiply(new BigDecimal(75)); //=85.5 
System.out.println("d2:: " + d2);
d2 = d2.setScale(0, RoundingMode.HALF_UP); // 86
System.out.println("round(d2):: " + d2); // correct output: 86

/* 
   Outputs:
   d1:: 85.49999999999999
   round(d1):: 85

   d2:: 85.50
   round(d2):: 86
*/

Capture screen shots for one or more monitor in Java (Each capture to a single file or to a separate file)

package screen.capture;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import java.io.File;
import java.io.IOException;

public class ScreenCapture {

       public static void main(String[] args) throws Exception {
          ScreenCapture sc = new ScreenCapture();
     
          sc.forOneMonitor("oneMonitor"); // outputs::  D:\oneMonitor.png
          sc.forMultipleMonitorInOneFile("multipleMonitor"); // outputs:: D:\multipleMonitor.png
          sc.forMultipleMonitorInMultipleFile("multipleMonitor_"); // outputs::  D:\multipleMonitor_Display0.png, D:\multipleMonitor_Display1.png
       }

       public void forOneMonitor(String fileName) throws Exception {
          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
          Rectangle screenRectangle = new Rectangle(screenSize);
    
          Robot robot = new Robot();
          BufferedImage image = robot.createScreenCapture(screenRectangle);

          ImageIO.write(image, "png", new File("d:\\" + fileName + ".png"));
       }

       public void forMultipleMonitorInOneFile(String fileName) throws AWTException, IOException {
              Rectangle screenRect = new Rectangle(0, 0, 0, 0);
     
              GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
              GraphicsDevice[] gs = ge.getScreenDevices();
     
              for (GraphicsDevice gd : gs) {
                screenRect = screenRect.union(gd.getDefaultConfiguration().getBounds());
              }
     
              Robot robot = new Robot();
              BufferedImage capture = robot.createScreenCapture(screenRect);
              ImageIO.write(capture, "png", new File("d:\\" + fileName + ".png"));
       }

       public void forMultipleMonitorInMultipleFile(String fileName) throws AWTException, IOException {
              GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
              GraphicsDevice[] gdList = ge.getScreenDevices();

              for (GraphicsDevice gd : gdList) {
                DisplayMode mode = gd.getDisplayMode();
                Rectangle bounds = gd.getDefaultConfiguration().getBounds();
                Rectangle screenRect = new Rectangle((int) bounds.getMinX(), (int) bounds.getMinY(), (int) bounds.getWidth(), (int) bounds.getHeight());

                System.out.println("gd.getIDstring(): " + gd.getIDstring());
                System.out.println("Min (" + bounds.getMinX() + "," + bounds.getMinY() + ") Max(" + bounds.getMaxX()  + "," + bounds.getMaxY() + ")");
                System.out.println("Width : " + mode.getWidth() + "; Height :" + mode.getHeight());

                Robot robot = new Robot();
                BufferedImage capture = robot.createScreenCapture(screenRect);
                ImageIO.write(capture, "png", new File("d:\\" + fileName + gd.getIDstring().replace("\\", "") + ".png"));
              }
       }
}

Outputs: