Archive for the ‘Programming’ Category

sleep() function in JavaScript

Wednesday, January 6th, 2010

JavaScript doesn't have a handy sleep() or wait() or delay() function to pause the execution for a few seconds, so we can use setTimeout() function to emulate such behaviour. It's not straightforward and may require you to refactor the workflow, but it's delaying the execution and is really simple:

JAVASCRIPT:

  1. setTimeout(start, 1000);
  2.     var i = 0;
  3.    
  4.     function start() {
  5.         console.log('i am here ' + i);
  6.         i++;
  7.         setTimeout(start, 1000);
  8.     }

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Tuesday, November 24th, 2009

If you are getting this exception from your Java program using HTTPClient library, that means the site you are trying to connect to has a test/self-signed or invalid certificate. The easiest way to workaround this problem is to grab InsecureSSLProtocolSocketFactory and InsecureTrustManager from HTMLUnit project and before making a request use this code:

JAVA:

  1. final ProtocolSocketFactory factory = new InsecureSSLProtocolSocketFactory();
  2. final Protocol https = new Protocol("https", factory, 443);
  3. Protocol.registerProtocol("https", https);

RIA’s war

Thursday, August 2nd, 2007

RIA warIsn't this me-too RIA buzz a little bit naive? A long time ago Java "invented" applets - a great idea for RIA that has gracefully failed. Flash has taken the seat. MS's silverlight is not getting developers anywhere for too many reasons. JavaFX requires the latest Java JVM installed on the user's computer. I believe in the power of the Sun, but the UI is not their strongest side. Flex is the only way to go if you want to have a good-looking RIA application. Almost everybody has it installed and it is a really quick download if you don't have it. Great look, nice cooperation with J2EE technology.

Fake it!

Friday, July 13th, 2007

Sometimes you need test data to... test some class or jsp page. Here is the java class that can be of help to someone.



JAVA:

  1. import org.apache.commons.lang.RandomStringUtils;
  2. import org.apache.commons.lang.StringUtils;
  3. import org.apache.commons.lang.math.RandomUtils;
  4.  
  5. public class Fake {
  6.  
  7.     public static String getAddress() {
  8.         String str = RandomStringUtils.randomNumeric(2) + "/"
  9.                 + RandomStringUtils.randomNumeric(3);
  10.         String streetName = get(7) + 8;
  11.         streetName = StringUtils.capitalise(streetName);
  12.         str += " ";
  13.         str += streetName;
  14.         return str;
  15.     }
  16.  
  17.     public static String getWord() {
  18.         String streetName = RandomStringUtils.randomAlphabetic(7).toLowerCase();
  19.         streetName = StringUtils.capitalise(streetName);
  20.         return streetName;
  21.     }
  22.  
  23.     public static String get(int maxLength) {
  24.         int l = RandomUtils.nextInt(maxLength);
  25.         return RandomStringUtils.randomAlphabetic(maxLength + 3).toLowerCase();
  26.     }
  27.    
  28.     public static String getSentence(int numberOfWords) {
  29.         StringBuffer sb = new StringBuffer();
  30.         for (int i = 0; i <numberOfWords; i++) {
  31.             sb.append(get(16));
  32.             sb.append(" ");
  33.         }
  34.         return StringUtils.capitalise(sb.toString());
  35.     }
  36.  
  37.     public static String getEmail() {
  38.         StringBuilder sb = new StringBuilder(get(4));
  39.         sb.append("@");
  40.         sb.append(get(4));
  41.         sb.append(".");
  42.         sb.append(RandomStringUtils.randomAlphabetic(3).toLowerCase());
  43.         return sb.toString();
  44.  
  45.     }
  46.  
  47.     public static int getId() {
  48.         return RandomUtils.nextInt(99999);
  49.     }
  50.  
  51.     public static String getPhoneNumber() {
  52.         return RandomStringUtils.randomNumeric(12);
  53.     }
  54.  
  55.     public static String getPostalCode() {
  56.         return RandomStringUtils.randomNumeric(7);
  57.     }
  58.  
  59.     public static String getState() {
  60.         return RandomStringUtils.randomAlphabetic(3).toUpperCase();
  61.     }
  62. }