Labels

Sunday, September 23, 2012

Chiếc Lá!

Lá kia buồn lẻ trên cành
Đêm đông nảy lộc, duyên đành phận trao
Ngày qua tháng lại ngóng đào
Bao giờ hoa nở lá chào đón hoa

Đồi hoa nẩy mộng anh đào
Ong kia bướm nọ xúm vào xúm ra
Lặng ơi tiếng gió chiều tà
Hoa ơi có biết rằng ta ngóng nảng

...

Tiếng ai long lảnh chiều tàn
Nhạc khúc vui nhộn muôn ngàn hoa rơi
Hoa đi lá lại ...  lả lơi
Tìm trong tiếng nhạc mảnh đời ngây ngây

Rồi đây Hạ đến ấm đầy
Ru ơi khúc Hạ nắng gầy nhẹ trao
Cỏi ta còn lắm lao đao
Hạ ơi đừng nhé kiếm đào mà đi !

Tặng em,
T.A. Nguyen
September 23, 2012

Sunday, May 6, 2012

TinyWebServer.java an embedded sun httpserver implementation since JRE 1.6 less than 300 lines!

TinyWebServer.java is a Java implementation based on the embedded Sun httpserver which available since JRE 1.6. (in rt.jar) and is less than 300 lines of codes.

During my development work, I started out using the JETTY a small and lightweight web engine, but I need to attach a few jars to my projects. My automated build/deployment also need to pack these libraries as well since my test cases depend on it.

Since version 1.6 of the sun JRE, the rt.jar come with the embedded version (limited functionality) that work well, and just what I needed to set up my stub web services to ensure reliable test run. I also use this "TinyWebServer.java" for a small demo to my student as well. So, here are the codes for it. Next post I will share with you on how I use it in my JUnit test to set up and run my stub web services.

Remember, if you get errors in your eclipse project, simply remove the "System Library" and add it back, this will remove the security policy from the eclipse build.

package org.cnci.util;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URL;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.xml.ws.Endpoint;
import org.apache.log4j.Logger;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
/**
 * TinyWebServer
 * 
 * @author T.A. Nguyen
 * 
 *  Tiny Embedded (Stub) Web Server, intended for testing only.  This implementation
 *  will take advantage of the "sun" httpserver package that shipped with the JRE 
 *  since release 1.6.
 *  
 *  Provide an easy creation of a TinyWebServer that can be start up and host as
 *  an embedded (secure) web server.  Just pass in the URL as you intended to access
 *  and it will attempt to host in that url, whether it is secure or not.  This 
 *  will make it an ideal to configure and setup as a unit test stub server.
 * 
 *  By default, Eclipse will add restriction to com.sun.net.httpserver.*
 *  package access, just need to remove the JRE library and add it back
 *  then it will allow you to use it.
 * 
 *  Usage: 
 *  
 *  TinyWebServer server = new TinyWebServer("https://*:8080"); 
 *  AddressBookSoap myBook = new AddressBookSoapImpl(); // your service implementation here 
 *  server.addSoapService("/ws/myBook", myBook);
 *  PersonnelSoap myHR = new StubPersonnelSoapImpl(); // my other web service, local stub 
 *  server.addSoapService("/ws/myHR", myHR); 
 *  server.start();
 *  
 */
public class TinyWebServer {
  private static final String SSL_VERSION = "SSLv3";
  private static Logger log = Logger.getLogger(TinyWebServer.class);
  private static final String KEYSTORE_FILENAME = "localhost.server.keystore.jks";
  private static final String KEYSTORE_PASSWORD = "cnci.org";
  private static final int RETRY_TIME = 5;
  private static final int SLEEP_TIME = 500;
  private static final String DEFAULT_PROTOCOL = "https";
  private static final String DEFAULT_HOST = "localhost";
  private static final int DEFAULT_PORT = 8080;
  private HttpServer server = null;
  private Map services = new HashMap();
  private List endpoints = new ArrayList();
  private String url;      // same as the client access url
  private String protocol; // http/https
  private String host;     // ip or host name
  private int port;
  /**
   * Simple, just pass the url as the client expected, 
   * make sure that the host is of a local host name or ip 
   * otherwise it will default to "localhost"
   * 
   * https://*:8080 will listen to all local addresses on port 8080 via https (secure) protocol
   * https://localhost:8080 will listen to 127.0.0.1:8080 via https (secure) protocol
   * http://*:8080 will listen to all local addresses on port 8080 via http protocol 
   * 
   * @param host
   * @param port
   */
  public TinyWebServer(String url) {
    this.url = url;
  }
   
  public TinyWebServer(String host, int port) {
    this.host = host;
    this.port = port;
  }
  public void addSoapService(String context, Object service) throws Exception {
    if (context == null || context.trim().length() <= 0) {
      throw new Exception("Context uri is a required parameter.");
    }
    if (service == null) {
      throw new Exception("Soap service handler is a required parameter.");
    }
    if (services.containsKey(context)) {
      throw new Exception("Context uri is already exist.");
    }
    services.put(context, service);
    createEndpoint(getServer().createContext(context), service);
  }
  public void addHttpService(String context, HttpHandler handler, Executor executor) throws Exception {
    if (context == null || context.trim().length() <= 0) {
      throw new Exception("Context uri is a required parameter.");
    }
    if (handler == null) {
      throw new Exception("HttpHandler service handler is a required parameter.");
    }
    if (services.containsKey(context)) {
      throw new Exception("Context uri is already exist.");
    }
    services.put(context, handler);
    server.createContext(context, handler);
    if (executor == null) executor = Executors.newCachedThreadPool();
    // Executors.newFixedThreadPool(5)
    server.setExecutor(executor); // null for Default executor
  }
  public void start() throws Exception {
    HttpServer s = getServer();
    Exception e = null;
    boolean started = false;
    int count = 0;
    while (!started && count  0) {
          port = u.getPort();
        }
        if (u.getProtocol() != null) {
          protocol = u.getProtocol();
        }
      } catch (Exception e) {
        // ignore
      }
    }
    if (protocol == null) protocol = DEFAULT_PROTOCOL;
    if (host == null) host = DEFAULT_HOST;
    if (port <= 0) port = DEFAULT_PORT;
  }
   
  private boolean isSsl() {
    return DEFAULT_PROTOCOL.equalsIgnoreCase(getProtocol());
  }
   
  private String getHost() {
    if (host == null && url != null) validateUrl();
    return host;
  }
   
  private int getPort() {
    if (host == null && url != null) validateUrl();
    return port;
  }
   
  private String getProtocol() {
    if (host == null && url != null) validateUrl();
    return protocol;
  }
}

Saturday, April 28, 2012

DailyFileAppender.java a Solution to a log4j Daily Appender with a Retention!

I use the Log4j DailyRollingFileAppender over many years and many projects but always end up controlling the retention via a Cron jobs. That's work well, but what if we can control the retention in the log4j configuration file? Wouldn't that be great? Well, that came up and I wanted to share my solution with you:

package org.cnci.util;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.FileAppender;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

/**
 * Provide AS IS, no warranty!
 * 
 * Special log appender for log4j. Adds the current date (ie. year-mon) to the
 * end of the file name, so that rolling on to the next log is simply a case of
 * starting a new one - no renaming of old logs.
 * 
 * ONLY support: yyyy (yearly) | yyyy-MM (monthly) | yyyy-MM-dd (daily) |
 * yyyy-ww (weekly) yyyy-MM-dd-a (midnight, midday) | yyyy-MM-dd-HH (hourly) |
 * yyyy-MM-dd-HH-mm (every minute)
 * 
 * An example log4j.properties (one log per day, retains 30 days of logs)
 * 
 * log4j.rootCategory=INFO, DAILY
 * log4j.appender.DAILY=org.cnci.util.DailyFileAppender
 * log4j.appender.DAILY.File=log/MyApplication.log
 * log4j.appender.DAILY.DatePattern=yyyy-MM-dd log4j.appender.DAILY.MaxLogs=30
 * log4j.appender.DAILY.layout=org.apache.log4j.PatternLayout
 * log4j.appender.DAILY.layout.ConversionPattern=%d %-5p %c @ %m%n
 * 
 * @author T.A. Nguyen
 **/
public class DailyFileAppender extends FileAppender {
  private static final String DEFAULT_DATE_PATTERN = "'.'yyyy-MM-dd";
  private static final String[] SUPPORTED_DATE_PATTERNS = {
    "yyyy" /** (yearly) **/, 
    "yyyy-MM" /** (monthly) **/, 
    "yyyy-MM-dd" /** (daily) **/,
    "yyyy-ww" /** (weekly) **/,
    "yyyy-MM-dd-a" /** (midnight, midday) **/,
    "yyyy-MM-dd-HH" /** (hourly) **/,
    "yyyy-MM-dd-HH-mm" /** (every minute) **/
  };

  /**
   * Used internally and contains the name of the date derived from current
   * system date.
   **/
  private Date now = new Date();
  private String datePattern;
  private SimpleDateFormat formater;
  private int maxLogs = 0;
  private String scheduledFileName;

  /**
   * Default constructor. This is required as the appender class is dynamically
   * loaded.
   **/
  public DailyFileAppender() {
    super();
  }

  /**
   * computing for the scheduledFileName and set the now
   **/
  @Override
  public void activateOptions() {
    super.activateOptions();
    if (datePattern != null && fileName != null) {
      now.setTime(System.currentTimeMillis());
      formater = new SimpleDateFormat(datePattern);
      File file = new File(fileName);
      scheduledFileName = fileName + formater.format(new Date(file.lastModified()));
    } else {
      LogLog.error("Either File or DatePattern options are not set for appender [" + name + "].");
    }
  }

  public String getDatePattern() {
    return this.datePattern;
  }

  public void setDatePattern(String datePattern) {
    // make sure the the pattern conform to what we allow.
    this.datePattern = checkPattern(datePattern);
  }

  public int getMaxLogs() {
    return maxLogs;
  }

  public void setMaxLogs(int maxLogs) {
    this.maxLogs = maxLogs;
  }

  @Override
  protected void subAppend(LoggingEvent logEvent) {
    now.setTime(System.currentTimeMillis());

    try {
      rollOver();
    } catch (IOException IOEx) {
      LogLog.error("rollOver() failed!", IOEx);
    }

    super.subAppend(logEvent);
  }

  @Override
  public void setFile(String file) {
    try {
      // Fix the STUPID file path... Windows will use \ and *NIX will use
      // /,
      // so make sure we store the correct file path.
      File f = new File(file);
      file = f.getAbsolutePath();
    } catch (Exception e) {
      // ignore!
    }
    super.setFile(file);
  }

  void rollOver() throws IOException {
    /** Compute filename, but only if datePattern is specified **/
    if (datePattern == null) {
      // LogLog.error("Missing DatePattern option in rollOver().");
      errorHandler.error("Missing DatePattern option in rollOver().");
      return;
    }
    String datedFilename = fileName + formater.format(now);
    LogLog.debug(fileName + " ==> " + scheduledFileName + " ==> " + datedFilename);
    // It is too early to roll over because we are still within the
    // bounds of the current interval. Rollover will occur once the
    // next interval is reached.
    if (scheduledFileName.equals(datedFilename)) {
      return;
    }

    // close current file, and rename it to datedFilename
    this.closeFile();

    File file = new File(fileName);
    File target = new File(fileName + formater.format(new Date(file.lastModified())));
    if (target.exists()) {
      target.delete();
    }

    boolean result = file.renameTo(target);
    if (result) {
      // LogLog.debug(fileName + " -> " + scheduledFileName);
      scheduledFileName = datedFilename;
    } else {
      LogLog.error("Failed to rename [" + fileName + "] to [" + scheduledFileName + "].");
    }

    try {
      // This will also close the file. This is OK since multiple
      // close operations are safe.
      this.setFile(fileName, true, this.bufferedIO, this.bufferSize);
    } catch (IOException e) {
      // LogLog.error("setFile(" + fileName + ", true) call failed.");
      errorHandler.error("setFile(" + fileName + ", true) call failed.");
    }
    cleanupOldFiles();
  }

  /**
   * The helper function to validate the DatePattern.
   * 
   * @param pattern
   *          The DatePattern to be validated.
   * @return The validated date pattern or default DEFAULT_DATE_PATTERN
   **/
  private String checkPattern(String pattern) {
    if (isSupportedPattern(pattern)) {
      try {
        this.formater = new SimpleDateFormat(pattern);
        return pattern;
      } catch (Exception e) {
        LogLog.error("Invalid DatePattern " + pattern, e);
      }
    }
    return DEFAULT_DATE_PATTERN;
  }

  private boolean isSupportedPattern(String pattern) {
    if (pattern != null) {
      // give 'em that last chance to conform to our supported pattern.
      // Convert " " and ":" into "-" so that file operation will work
      // correctly!
      pattern = pattern.replaceAll(" |:", "-");

      for (String p : SUPPORTED_DATE_PATTERNS) {
        if (pattern.contains(p))
          return true;
      }
    }
    return false;
  }

  /**
   * Now delete those old files if needed.
   * 
   * @param pstrName
   *          The name of the new folder based on current system date.
   * @throws IOException
   **/
  static private boolean deletingFiles = false;

  private static synchronized void setDeletingFiles(boolean isDelete) {
    deletingFiles = isDelete;
  }

  private static synchronized boolean isDeletingFiles() {
    return deletingFiles;
  }

  boolean isExtendedLogFile(String name) {
    if (name == null)
      return false;
    File logMaster = new File(fileName);
    String master = logMaster.getName();
    return name.contains(master);
  }

  void cleanupOldFiles() {
    // If we need to delete log files
    if (maxLogs > 0 && !isDeletingFiles()) {
      // tell other thread, that we will do the clean up
      setDeletingFiles(true);

      try {
        // Array to hold the logs we are going to keep
        File[] logsToKeep = new File[maxLogs];

        // Get a 'master' file handle, and the parent directory from it
        File logMaster = new File(fileName);
        File logDir = logMaster.getParentFile();
        if (logDir.isDirectory()) {
          File[] logFiles = getLogFiles(logDir);
          // more files than expected, time to clean up!
          if (logFiles.length > maxLogs)
            for (File curLog : logFiles) {
              removeOldFile(logsToKeep, curLog);
            }
        }
      } catch (Exception e) {
        // ignore
        // LogLog.error("While deleting old files. " + e.getMessage(), e);
      } finally {
        // we are done with clean up.
        setDeletingFiles(false);
      }
    }
  }

  private void removeOldFile(File[] logsToKeep, File curLog) {
    String name = curLog.getAbsolutePath();

    // Check that the file is indeed one we want
    // (contains the master file name)
    if (name.indexOf(fileName) >= 0) {
      // Iterate through the array of logs we are
      // keeping
      for (int i = 0; curLog != null && i < logsToKeep.length; i++) {
        // Have we exhausted the 'to keep' array?
        if (logsToKeep[i] == null) {
          // Empty space, retain this log file
          logsToKeep[i] = curLog;
          curLog = null;
        }
        // If the 'kept' file is older than the
        // current one
        else if (logsToKeep[i].getName().compareTo(curLog.getName()) < 0) {
          // Replace tested entry with current
          // file
          File temp = logsToKeep[i];
          logsToKeep[i] = curLog;
          curLog = temp;
        }
      }

      // If we have a 'current' entry at this point,
      // it's a log we don't want
      if (curLog != null) {
        curLog.delete();
      }
    }
  }

  private File[] getLogFiles(File logDir) {
    // Iterate all the files in that directory
    FilenameFilter filter = new FilenameFilter() {
      @Override
      public boolean accept(File dir, String name) {
        return isExtendedLogFile(name);
      }
    };
    File[] logFiles = logDir.listFiles(filter); // only get the
    // extended
    // files
    return logFiles;
  }
}

And here is the sample log4j.properties

#------------------------------------------------------------------------------
#  Provide AS IS No Warranty!
#  Author: T.A. Nguyen
#
#  Default log4j.properties file.  This should be in the class path
#
#    Possible Log Levels:
#      FATAL, ERROR, WARN, INFO, DEBUG
#
# Production Setting:
#log4j.rootCategory=ERROR, DAILY
# Development and Build Setting:
log4j.rootCategory=DEBUG, DAILY, CONSOLE

#------------------------------------------------------------------------------
#
#  The following properties configure the console (stdout) appender.
#
#------------------------------------------------------------------------------
log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
#log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n

#------------------------------------------------------------------------------
#
#  The following properties configure the Daily File appender.
#
#------------------------------------------------------------------------------
log4j.appender.DAILY = org.cnci.util.DailyFileAppender
log4j.appender.DAILY.File = log/MyApplication.log
log4j.appender.DAILY.MaxLogs = 30
log4j.appender.DAILY.Append = true
log4j.appender.DAILY.DatePattern = '.'yyy-MM-dd
log4j.appender.DAILY.layout = org.apache.log4j.PatternLayout
log4j.appender.DAILY.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Extracting all MyApplication logging into it own file
#log4j.logger.org.cnci=DEBUG, DAILY
#log4j.additivity.org.cnci=false

Monday, April 9, 2012

Double dereference for Ant Property

Just random note on solving the double dereference that ANT property will not allow.


  
 
    
    
    
    
     
    
       myApp     : ${org.cnci.firstapp}
       myCompany : ${firstCompany}
       myPackage : ${firstPackage}
       myClass   : ${firstClass}
    
 
 
 
    
    
    
       
    
 
 
 
    
    
    
    
     
    
       myApp     : ${org.cnci.secondapp}
       myCompany : ${secondCompany}
       myPackage : ${secondPackage}
       myClass   : ${secondClass}
    
 
 
 

And the output looks like this:
NOT-WORKING-double-dereferences:
     [echo] 
     [echo]         myApp     : SimpleIRC
     [echo]         myCompany : cnci
     [echo]         myPackage : org.cnci.firstapp
     [echo]         myClass   : ${org.${firstCompany}.firstapp}
     [echo]         
WORKING-double-dereferences:
     [echo] 
     [echo]         myApp     : AdvanceIRC
     [echo]         myCompany : cnci
     [echo]         myPackage : org.cnci.secondapp
     [echo]         myClass   : AdvanceIRC
     [echo]         

Simple trick to convert Date format with timezone in Java!

I am sure most of you got frustrated from the fact that SimpleDateFormat can not handle ISO8601 format. Here is my little trick to solve this nuisance.

Create a list of Know format you know that you will use for your application and apply SimpleDateFormat to the list. Now, in your formatDate() method, simply try all your known format and trap the Exception, then if still did not have a date, just use javax.xml.bind.DatatypeConverter.parseDateTime(date) to try it and see if that work.

Here is my DateUtils.java

package org.cnci.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public abstract class DateUtils {
    // XML formater: this only for converting date into XML string
    private static final SimpleDateFormat xmlFormater = 
                           new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    // define all known date formats
    private static final SimpleDateFormat[] allFormats = new SimpleDateFormat[] {
        //new SimpleDateFormat("MM/dd/yyyy hh:mm:ss.SSS a"),
        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"), // XSD 
        new SimpleDateFormat("MM/dd/yyyy HH:mm:ssZ"), // Oracle
        new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"), 
        new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"), // rfc822
        new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US), 
        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"), // XSD
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"), 
        new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS"),
        new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"), // Oracle
        new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"),
        new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"),
        new SimpleDateFormat("yyyy.MM.dd GGG hh:mm aaa"),
        new SimpleDateFormat("yy-MM-dd HH:mm:ss.SSS"),
        new SimpleDateFormat("M/d/yyyy hh:mm:ss"), 
        new SimpleDateFormat("M/d/yyyy hh:mm a"), 
        new SimpleDateFormat("M/d/yy hh:mm:ss"), 
        new SimpleDateFormat("M/d/yy hh:mm a"),
        new SimpleDateFormat("M/d/yy HH:mm"),
        new SimpleDateFormat("M/d/yyyy HH:mm"),
        new SimpleDateFormat("M-d-yy HH:mm"), 
        new SimpleDateFormat("M-d-yyyy HH:mm"), 
        new SimpleDateFormat("M/d/yy"), 
        new SimpleDateFormat("M/d/yyyy"), 
        new SimpleDateFormat("M-d-yy"),
        new SimpleDateFormat("M-d-yyyy"), 
        new SimpleDateFormat("MMMM d, yyyyy"), 
        new SimpleDateFormat("MMM d, yyyyy") 
    };
    //-----------------------------------------------------------------------
    public static Date parseDate(final String date) throws ParseException {
        if (isEmptyString(date))
            throw new ParseException("Error: date is null or empty!", 0);
        // iterate over the array and parse
        Date myDate = null;
        if (date.endsWith("Z")) {
            myDate = parseXMLDate(date);
        }
        if (myDate == null) {
            for (DateFormat df : allFormats) {
                try {
                    myDate = df.parse(date);
                    return myDate;
                }
                catch (Exception e) {
                    // do nothing because we want to try the next
                    // format if current one fails
                }
            }
            if (myDate == null) {
                myDate = parseXMLDate(date);
            }
            // nothing returned so couldn't parse
            if (myDate == null) throw new ParseException("Error: unable to parse date: " + date, 0);
        }
        return myDate;
    }
    //Use the JAXB data type conversion, since it already know the xml valid date format
    private static java.util.Date parseXMLDate(final String date) {
        Calendar cal = javax.xml.bind.DatatypeConverter.parseDateTime(date);
        return cal.getTime();
    }
     
    //convert to the string representation of the datetime format
    //NOTES: this should be the same as formatDate() but keep 
    // and preferred to use this one instead.  Just in case the JAXB
    // document DatetypeConverter change in the future.
    public static String formatXMLDate(final java.util.Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return javax.xml.bind.DatatypeConverter.printDateTime(cal);
    }
     
    // should yield the same results as formatXMLDate() method above.
    public static String formatDate(final java.util.Date date) {
        return xmlFormater.format(date);
    }
     
    public static boolean isEmptyString(String str)
    {
        if (str == null) return true;
        return "".equals(str.trim());
    }
}