2011-11-26

Minimal JavaBean introspection example

Just playing with JavaBeans, so here's a small example to create a simple JavaBean, set a property and read it.

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SimpleBean {

  private final String name = "SimpleBean";
  private int size;
  public int getSize() {
    return size;
  }
  public void setSize(int size) {
    this.size = size;
  }
  public String getName() {
    return name;
  }
  
  public static void main(String[] args) throws IntrospectionException {
    SimpleBean sb = new SimpleBean();
    sb.setSize(59);
    BeanInfo bi = Introspector.getBeanInfo(sb.getClass());
    for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
      testGetValue(sb, pd);
    }
  }
  
  public static void testGetValue(Object sb, PropertyDescriptor pd) {
    System.out.print(pd.getName() + "=");
    Method getter = pd.getReadMethod();
    Object arg1[] = {};
    try {
      Object value = getter.invoke(sb, arg1);
      System.out.println(value);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
  }
}

The output of the program is:

class=class SimpleBean
name=SimpleBean
size=59

2011-11-11

Grep -f: No blank lines

If you search for a list of patterns in a data file using grep like below, don't include a blank line in the pattern file otherwise grep prints the entire data file.

grep -f patterns.txt data.txt

It's as if you had typed:

grep "" data.txt

See GNU Grep Manual

2011-09-18

Create an MSIE Favorite using VBA & WSH

A little sub-routine to create MSIE Favorites for MS-Office users. It uses Windows Scripting Host (WSH) which has a shell object that has functions to find a computer's Favorites folder (SpecialFolders()) and create a URL shortcut (CreateShortcut()).


Option Explicit

'Add references:
'1. Microsoft Scripting Runtime (Scripting namespace)
'2. Windows Script Host Object Model (IWshRuntimeLibrary namespace)

Public Sub CreateFavorite(ByVal strName As String, ByVal strUrl As String)
  Dim fso As Scripting.FileSystemObject
  Set fso = CreateObject("Scripting.FileSystemObject")
  
  Dim wshShell As IWshRuntimeLibrary.wshShell
  Set wshShell = CreateObject("Wscript.Shell")
  
  Dim strFavorites As String
  strFavorites = wshShell.SpecialFolders("Favorites")
  
  Dim shortcut As IWshRuntimeLibrary.WshURLShortcut
  Set shortcut = wshShell.CreateShortcut(fso.BuildPath(strFavorites, strName & ".url"))
  shortcut.TargetPath = strUrl
  shortcut.Save
  
  Set shortcut = Nothing
  Set wshShell = Nothing
  Set fso = Nothing
End Sub

Note to myself: don't confuse:


  • IWshRuntimeLibrary.WshShell with Shell32.Shell in Microsoft Shell Controls and Automation
  • WshShell.SpecialFolders() with FileSystemObject.GetSpecialFolders() function. The latter function only returns a small number of special folders (windows, system and temporary).

2011-05-29

Avoid multiple instances of a task in Windows Task Scheduler

Using Task Scheduler, one way to run a task more often than once a day was to repeat the task within the day. For instance, if you wanted to run a task every hour, you would repeat the task once an hour for that day. Now, if the task's running time is longer (e.g. the task is delayed by a slow network connection) than the repeat interval then another instance would be started even if you specify Do not start a new instance!

For a backup task, my workaround is to specify an hourly repeat interval and call a wrapper (e.g. a CMD or VBS script) to test for a lock file before executing the main program. In the wrapper, if the lock file exists then the wrapper aborts else it creates a lock file, runs the main program then deletes the lock file.

It looks like a task's Do not start a new instance parameter only applies to triggers. With Task Scheduler 2.0 (Vista onwards), you can create triggers for specific times of the day (say 01:00, 02:00, etc. for hourly triggers) and only one instance of a task is started. It's a pity there aren't shorter trigger intervals because to schedule a task every hour of the day, you have to create 24 triggers.