Sunday, October 31, 2010

Useful abuse of API

Can abuse of API be useful? Sometimes it can be!


Word "abuse" is defined as the improper usage or treatment for a bad purpose
    (from Wikipedia)

    Have you ever used a knife as a screw driver? A screw driver is better but if it is not available a knife can help too.

    How can this principle be applied for programming?

    Implementation of Command pattern

    Who has never implemented command patternIt is simple. Define interface Command with one no-arguments method execute, create as many implementations as you need and use it. But JDK already contains 2 command like interfaces: java.util.concurrent.Callable and java.lang.Runnable, so why not to use them? Such usage has a positive side effect. You can execute this command in separate thread or in thread pool without any modification or wrapping.


    Implementation of Filter pattern

    Sometimes we  have to implement filter pattern. Interfaces FileFilter and FilenameFilter already exist in JDK but they deal with files. This fact does not allow their reuse for other purposes. But there is other interface java.lang.Comparable<T>. This interface is already parametrized and declares method

    int compareTo(T obj)

    It is not expected to be used for filter implementation. Here is the quotation from JDK javadoc:

    "This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method."

    But what is the difference between methods boolean accept(T) and int compareTo(T)? Only semantics and result type. I suggest to use this interface to implement external filter. 0 means true, non zero value means false. Let's assume that we have collection of strings and would like to filter only 5 character long elements.

    This is the implementation of Comparable interface:

    
    public class StringLengthFilter implements Comparable<Integer> {
        private String str; 
     
        public StringLengthFilter(String str) {
            this.str = str;
        }
    
        @Override
        public int compareTo(Integer len) {
            return str.length() - len;
        }
        
        public String getString() {
            return str;
        }
    }
    

    The following code snippet counts number of 5 characters long strings in collection:
    
    int count = 0;
    for (String s : src) {
        if (new StringLengthFilter(s).compareTo(5) == 0) {
            count++;
        }
    }
    



    Passing primitive wrappers by reference

    All objects are passed by reference in java. Only primitives and their wrappers are passed by value.
    Argument that is passed to method by reference may be changed by this method. For example:

    Collections.sort(list);

    sorts the given list "in place", i.e. the method sort() changes object passed by reference. We cannot do the same with primitives.

    Value of param is not changed after the following method call:
    
    int param = 1;
    foo(param); 
    // param here is still 1 
    //.............
    void foo(int p) {
        p *= 2; 
    }
    

    Passing immutable argument to method by reference may be implemented using AtomicInteger:
    
    AtomicInteger arg = new AtomicInteger(5);
    foo(arg);
    System.out.println(arg.get()); 
     
    This code snippet will print 10. The good side effect of this method is thread safety.


    Discover available implementations

    Assume that we have interface and several implementations packaged in different JARs that can be available in application classpath. Sometimes application should know all available implementations. For example to choose the best one automatically or to allow the user to choose one from drop down list.
    We can use java.class.path and path.separator to find the application classes, then read classes as resources to perform self discovery and locate available implementations dynamically. The following code snippet looks for all available implementations of BSFEngine interface.
    
        private static Map<String, Boolean> getEngines() throws Exception {
            Map<String, Boolean> result = new HashMap<String, Boolean>();
            String[] pathElements = System.getProperty("java.class.path").split(System.getProperty("path.separator"));
            for (String pathElement : pathElements) {
                File resource = new File(pathElement);
                if (!resource.isFile()) {
                    continue;
                }
                JarFile jar = new JarFile(resource);
                for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
                    JarEntry entry = e.nextElement();
                    if(entry.isDirectory()) {
                        continue;
                    }
                    if(!entry.getName().endsWith("Engine.class")) {
                        continue;
                    }
                    String className = entry.getName().replaceFirst("\\.class$", "").replace('/', '.');
                    try {
                        if(BSFEngine.class.getName().equals(className)) {
                            continue;
                        }
                        Class<?> clazz = Class.forName(className);
                        if(BSFEngine.class.isAssignableFrom(clazz) && !clazz.equals(BSFEngine.class)) {
                            result.put(className, true);
                        }
                    } catch (NoClassDefFoundError ex) {
                        // ignore...
                        result.put(className, false);
                    }
                }
            }
            return result;
        }
            
    
    The following method call
    
    System.out.println(getEngines().toString().replaceFirst("\\{", "").replaceFirst("\\}", "").replace(", ", "\n"));
    

    produces the following output:
    
    org.apache.bsf.engines.xslt.XSLTEngine=true
    org.apache.bsf.engines.jacl.JaclEngine=false
    org.apache.bsf.engines.javascript.JavaScriptEngine=false
    org.apache.bsf.engines.jython.JythonEngine=false
    org.apache.bsf.engines.netrexx.NetRexxEngine=true 

    So, we have discovered all implementations of BSFEngine interface available in the application classpath.


    Use AbstractCollection.toString() for debugging

    Method toString() typically creates human readable text representation of the object. It is not recommended to base any logic on toString() format. But there is no rule without an exception.

    Debugger is a great tool that allows viewing of variable values. Unfortunately sometimes it is not powerful enough. Let's assume that you have the linked list or set that contains 10 thousand objects. You toggle breakpoint and want to check whether specific value is on the list. Good luck to find this object :(.

    To save time I typically use the following technique. I create the following watch expression:
    
    list.toString().substring(1).replaceFirst("]$", "").repalce(" , ", "\n") 

    This code produces multi-line string. Each line contains string representation of the list element. This method works fine for relatively small lists. If the quantity of elements is high I put this expression into System.out.println(), so the list is printed on STDOUT where I can perform textual search.



    Copy Map to Properties

    Since Java 5 is released I prefer to use parametrized collections. Although class java.util.Properties is a part of java collection framework and is typically used to store strings it is not Map<String, String> but Map<Object, Object> for backwards compatibility. So, we cannot copy elements from Map<String, String> to Properties using putAll() that expects in this case Map<Object, Object>. Sometimes I have to copy content of my parametrized collection to properties (for example to use its store() method). The regular solution is to create loop that iterates over all entries of source Map and puts them to Properties object. But sometimes we can save a couple of code lines using AbstractMap.toString() and Properties.load().

    Here is an example. I defined map where both keys and values are Integers:

    Map<Integer, Integer> imap = new HashMap<Integer, Integer>();
    imap.put(1, 10);
    imap.put(2, 20); 

    Now I am copying this map to properties using the following code:
    
    Properties props = new Properties();
    props.load(new StringReader(imap.toString().substring(1).replaceFirst("}$", "").replace(", ", "\n")));
    



    Socket based singleton application

    Typically we use sockets for process-to-process communication. In most cases the processes run on different computers. Usually the processes are different too: one of them is server, other is client. This example shows how application uses socket connection to communicate with other instance of itself that runs on the same machine.

    Typical example of singleton application is MS Windows Task manager. You can run only one instance of this application. If you try to run the second instance the already existing one is activated even if its window was minimized before. I have implemented class SingletonApplication. It tries to connect to predefined port. Connection succeeds if the other instance of application is already running, so my class calls System.exit(1):
    
    new Socket().connect(new InetSocketAddress(port)); 
    System.exit(1);
    

    If connection fails we assume that this is the first instance of the application and open server socket that is listening to connections from other instances.
    
    ServerSocket serverSocket = new ServerSocket(port);
    while(true) {
        serverSocket.accept(); // blocked until client connects
        activateWindow();
    }
    

    Implementation of method activateWindow() brings us to the next abuse:


    Portable window activation

    The goal is to bring existing window on top and select (activate) it. Java focus system is sophisticated and not simple. There are a lot of methods that provide focus control.

    Component.requestFocus works for component that is displayable, visible etc.
    Window.toFront brings the window to front and may make it the focused window if this window is visible. What really happens (at least on Windows) is that the button which represents the window on toolbar starts blinking but the window itself is not focused.
    Window.setAlwaysOnTop(boolean) sets whether this window should always be above other windows. It does not make this window active.

    I spent a lot of time but failed to find a good portable way to put window on top of others and make it active. Finally I found the following trick. First I call window.setAlwaysOnTop(true). This brings window on top of others but does not make it active. Then I move mouse to the window header and simulate left mouse click using java.awt.Robot. Then I move mouse back. It is ugly trick but it works:

    
    mainWindow.setAlwaysOnTop(true);
    Robot robot = new Robot();
    robot.mouseMove(mainWindow.getX() + 40, mainWindow.getY() + 10);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    

    I have implemented generic class SingletonApplication that makes each application to behave like Task manager by adding only one line to the beginning of main(String[]) method:

    new SingletonApplication(12345).start();

    Where 12345 is a TCP port that will be used by our application.

    Full source code of SingletonApplication is available here.


    Conclusions

    Typically abuse is bad. But sometimes improper usage of existing mechanism, API or technique may be very useful. "Useful" abuse may save time and produce well designed working code.


    Acknowledgments

    I would like to thank my former manager Avshi Avital that commented one of my design solutions as "elegant abuse." Although it was several years ago and I do not remember details of that abuse, the sentence gave me an idea for this article.

      Tuesday, October 5, 2010

      Hierarchical structures with Java Enums

      Java enums are typically used to hold array like data. This tip shows how to use enum for hierarchical structures.

      Motivation

      Once upon a time I wanted to create enum that contains various operating system, i.e.

      public enum OsType {
                  WindowsNTWorkstation,
                  WindowsNTServer,
                  Windows2000Server,
                  Windows2000Workstation,
                  WindowsXp,
                  WindowsVista,
                  Windows7,
                  Windows95,
                  Windows98,
                  Fedora,
                  Ubuntu,
                  Knopix,
                  SunOs,
                  HpUx,
      }

      I was not satisfied of this structure because I'd like to see a group of WindowsNT that contains WinNTWorkstation and WindNT server. All windows versions should be in super group of "windows". Fedora, Knopix and Ubuntu are distributions of Linux. All Linux distributions together with SunOs and HpUx are Unix systems. Obviously that all Windows systems have common properties. The same is about Unix systems. And I hate copy/paste programming.


      Class per OS Solution

      The obvious solution here is to create separate classes per operating system and several abstract classes. For example class Fedora extends class Linux that extends class Unix that extends class OperatingSystem. We can enjoy all advantages of inheritance, so all common properties of Windows OS are stored in class Windows and can be overridden by its subclasses.

      But now we cannot see all operating systems together, iterate over them etc., i.e. very useful features of Java enum are missing.
      No problem! Now we can create enum like previous that holds custom field of type Class:


      public enum OsType {
                  WindowsNTWorkstation(WindowsNTWorkstation.class),
                  WindowsNTServer(WindowsNTServer.class),
                  Windows2000Server(Windows2000Server.class),
                  Windows2000Workstation(Windows2000Workstation.class),
                  WindowsXp(WindowsXp.class),
                  WindowsVista(WindowsVista.class),
                  Windows7(Windows7.class),
                  Windows95(Windows7.class),
                  Windows98(Windows98.class),
                  Fedora(Fedora.class),
                  Ubuntu(Ubuntu.class),
                  Knopix(Knopix.class),
                  SunOs(SunOs.class),
                  HpUx(HpUx.class),
                  ;
                  private Class clazz;
                  OsType(Class clazz) {
                      this.clazz = clazz;
                  }
      }

      This solution is better but it still has disadvantages:
      1. Implementation of method that retrieves all "children" of specific OS (for example all Linux distributions) is hard and ineffective.
      2. Grouping is separate from enum.
      3. The solution is very verbose: each OS is represented by its own class even if the class has nothing to override.

      Hierarchical Enum

      To create hierarchy using enum we need custom field "parent" that is initialized by constructor:
      public enum OsType {
          OS(null),
              Windows(OS),
                  WindowsNT(Windows),
                      WindowsNTWorkstation(WindowsNT),
                      WindowsNTServer(WindowsNT),
                  Windows2000(Windows),
                      Windows2000Server(Windows2000),
                      Windows2000Workstation(Windows2000),
                  WindowsXp(Windows),
                  WindowsVista(Windows),
                  Windows7(Windows),
                  Windows95(Windows),
                  Windows98(Windows),
              Unix(OS) {
                      @Override
                      public boolean supportsXWindows() {
                          return true;
                      }
                  },
                  Linux(Unix),
                  AIX(Unix),
                  HpUx(Unix),
                  SunOs(Unix),
          ;
          private OsType parent = null;

          private OsType(OsType parent) {
              this.parent = parent;
          }
      }

      This structure allows implementation of method "is" that works like operator instanceof for classes and interfaces. For example Windows2000 is Windows, Fedora is Linux, Windows is not Unix etc.

      public boolean is(OsType other) {
          if (other == null) {
              return false;
          }
         
          for (OsType t = this;  t != null;  t = t.parent) {
              if (other == t) {
                  return true;
              }
          }
          return false;
      }


      Sometimes we need method that returns all "children" of current nodes, e.g. all Linux systems or all variants of Windows2000. The easiest way to implement this is to hold collection of children per element and fill it from constructor:


      private List<OsType> children = new ArrayList<OsType>();
      private OsType(OsType parent) {
          this.parent = parent;
          if (this.parent != null) {
              this.parent.addChild(this);
          }
      }

      Now method "children()" that returns direct node's children is trivial:
      public OsType[] children() {
          return children.toArray(new OsType[children.size()]);
      }

      It is not hard to implement recursive method "allChildren()" that returns all children of current node (see full source code). 

      But hierarchy term is always accompanied by inheritance that allows overriding methods of parent. This is the basic feature of classes in all object oriented languages. Is it possible to implement a kind of inheritance relationship for elements of one enum?

       
      Overriding parent's method

      Unix systems support X Window graphical environment. MS Windows does not. We would like to be able to ask OS whether is supports X Window.

      We can define boolean flag "supportsX" and boolean method
      public boolean supportsX() {return suppotsX;}

      Now we have to add yet another argument to OsType constructor and pass true/false for each element of the enum. But it is too verbose. Is it possible to say that Unix supports X, Windows does not support X and be sure that Fedora's supportX() returns true while Winddows95's supportX() returns false?

      The implementation is pretty simple. First for simplicity let's say that X Window is supported by all Unix systems and is not supported by others.
      So, we can implement method supportsXWindowSystem() at enum level as following:

      public boolean supportsXWindowSystem() {
          return false;
      }


      Now we have to override it for all Unix systems. To implement this we change the default implementation to following:

      public boolean supportsXWindowSystem() {
          if (this == OsType.OS) {
              return false;
          }
          
          for (OsType t = this;  t != null;  t = t.parent()) {
              if(!t.getClass().equals(OsType.class)) {
                  try {
                      return (Boolean)t.getClass().
                              getDeclaredMethod(
                                  "supportsXWindowSystem").
                              invoke(t);
                  } catch (SecurityException e) {
                      continue;
                  } catch (NoSuchMethodException e) {
                      continue;
                  } catch (IllegalArgumentException e) {
                      throw new IllegalStateException(e);
                  } catch (IllegalAccessException e) {
                      throw new IllegalStateException(e);
                  } catch (InvocationTargetException e) {
                      throw new IllegalStateException(e);
                  }
              }
          }
          return OsType.OS.supportsXWindowSystem();
      }

      This method invokes itself using reflection starting from the current enum element and iterating over its parents until it succeeds. The trick here is in condition !t.getClass().equals(OsType.class): the reflection call happens only if enum elements really overrides the method. So, the method of first parent in hierarchy that implements the method will be used. If no one of parents and parents of parents does not implement this method itself we call method of root element. 


      Now we can say the following:

      ...
              Unix(OS) {
                      @Override
                      public boolean supportsXWindowSystem() {
                          return true;
                      }
                  },
                  Linux(Unix),
                  AIX(Unix),
                  HpUx(Unix),
                  SunOs(Unix),
      ...


      The method is overridden for Unix element only and all its children will use this method.

      The problem is solved. We got enum based hierarchical polymorphic structure! We can implement method in base element (using it like a super class) and then override it in any element we want.

      The only disadvantage of this solution is that now we have to create similar implementation for each method we add to this enum and for all other enums that hold hierarchical structure. 


      Utility that helps to call hierarchical method

      The utility is implemented as static method that makes more general implementation than supportsXWindowSystem(). The main difference between common utility and method implemented for specific enum is that
      1. we cannot use the enum's fields directly
      2. we cannot use hard-coded method name and return type.
      3. we need a way to get parent of current enum element.
      To solve the first problem we pass to the utility current element, the root element and root default value element value, so the method signature looks like:

      public static <R> R callHierarchicalMethod(Enum<?> enumElem, Enum<?> rootElem, R rootValue, String parentAccessor)

      Utility is parametrized that allows to support any return type.
      The utility uses reflection. But how to discover the "real" method name that should be called? The Throwable.getStackTrace() helps us:

      String methodName = new Throwable().getStackTrace()[1].getMethodName();

      Element #1 is the a "real" enum method, i.e. the method that directly called the utility.

      The next difference is where to use getClass() and where getDecalaringClass(). While we are iterating over the chain of elements from current to its parents we use getClass() that returns class of enum if the element does not  redefine any method and anonymous inner class if element defines at least one method. The fallback code in the end of the utility uses getDecalaringClass() to invoke the business method that is implemented on enum level itself.

      Moreover we cannot just invoke
      elem.getClass().getDeclaredMethod(methodName).invoke(elem)

      Although the method must be accessible (better public) the class itself may be not public. For example it happens if element is presented by anonymous inner class. In this case the method can be invoked only after calling setAccessible(true).

      Method m = clazz.getDeclaredMethod(methodName);
      m.setAccessible(true);  

      m.invoke(elem)


      Finally we need a way to access parent element. I decided to use reflection for this purpose. It is easier than creating interface with one method Enum<?> getParent(Enum<?> elem). This is classical use-case for closures, so I am waiting for Java 7 to improve this code.


      Now default implementation of our business method looks like:

      public boolean supportsXWindowSystem() {
          return EnumUtil.<Boolean>callHierarchicalMethod(

                 this, OsType.OS, false, "parent"); 
      }
       



      Full source code of utility can be found here. Please find OsType enum as an usage example. JUnit test case that can be used as code example as well is available also.


      Conclusions
       
      Although we are regular to use enums as some kind of static arrays they also can be used to present hierarchical tree-like data structures where each node can find its parent, its children and even inherit and override parent's method almost exactly as we do with class inheritance.

      Send delayed JMS messages

      Very often I had to implement feature that has to do something asynchronously in a minute, day, or at 5PM next Monday. Every time I implemented some serialization mechanism (typically based on DB) and some scheduled task that runs periodically, checks the table and runs tasks that should be executed now. Sometimes more generic tools were used. For example Quartz. What bothered me to implement such tasks using JMS that is built to perform asynchronous tasks? The reason is that JMS API does not allow sending delayed messages, i.e. messages that will not be received by subscriber or receiver immediately. Occasionally I found out that some JMS implementations have proprietary implementation for delayed messages. I decided to perform some search in Internet and aggregate this information in one place. Here is a list of the most popular JMS implementations (see wikipedia):
      • Apache ActiveMQ
      • Apache Qpid
      • FUSE Message Broker (enterprise ActiveMQ)
      • Mantaray a P2P JMS implementation
      • OpenJMS, from The OpenJMS Group
      • JBoss Messaging from JBoss
      • HornetQ from JBoss
      • JORAM, from the OW2 Consortium
      • Open Message Queue, from Sun Microsystems
      • Sun Java System Message Queue, from Sun Microsystems, supported version of Open Message Queue
      • Rabbit MQ
      Due to JMS API does not define interface for delayed messages most SMS providers that support this feature implemented it using message property. You just have to say something like msg.setLongProperty(“DELAY”, delay). Some implementations require casting to specific class and invocation of proprietary method. The following table summarizes differences between implementations of different SMS providers I found.

      JMS provider Implementation
      Oracle AQ msg.setIntProperty(“JMS_OracleDelay”, delay);
      JBoss msg.setLongProperty(“JMS_JBOSS_SCHEDULED_DELIVERY”, now + delay);
      ActiveMQ msg.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
      OpenJMS ((org.exolab.jms.message.MessageImpl)msg).setJMSXRcvTimestamp(now + delay);
      BEA Weblogic queueConnection = queueConnectionFactory.createQueueConnection(); QueueSession queueSession = queueConnection.createQueueSession(true, 0); QueueSender queueSender = queueSession.createSender(queue); ObjectMessage jmsMsg = queueSession.createObjectMessage(message); //Casts queueSender to weblogic.jms.extensions.WLMessageProducer interface and set delivery time ((WLMessageProducer) queueSender).setTimeToDeliver(timeToDeliver); queueSender.send(jmsMsg);

      Do we have solution for JMS providers that do not have native support of delayed message delivery? Yes, we do. I would like to suggest the following solution.
      Send message to special queue. Let’s call it DELAYED_QUEUE. Add to delayed message the special properties:
      • JMS_DESTINATION that contains name of queue or topic where this message should be finally delivered.
      • DELIVERY_TIME that contains time stamp in milliseconds (now + delay).
      For each enqued delayed message create scheduled task that that will run once when message should be delivered. This scheduled task will create JMS receiver with selector that looks like DELIVERY_TIME < now (where now is the timestamp), receives all expired messages and send them to real JMS destination using property JMS_DESTINATION.
      Scheduled task may be implemented as resource adapter (JCA):
      
      BootstrapContext ctx;
      ctx.getWorkManager().createTimer(). schedule(new DelayedMessageTimerTask(msg), new Date(now + delay))
      

      This is not ideal solution. It is OK for relatively small number of messages and non persisted JMS destinations. Improvements of this solution are beyond the scope of this article.

      Conclusions

      Most popular JMS implementations support delayed delivery of messages. Even if this feature is not supported we can always implement it using additional queue and scheduled task.
      This article is published at DZone.

      Customized ValueOf

      When I am writing enum I very often found myself implementing static method similar to standard enum’s valueOf() but based on field other than name:
      
      public static TestOne valueOfDescription(String description) {
          for (TestOne v : values()) {
              if (v.description.equals(description)) {
                  return v;
              }
          }
          throw new IllegalArgumentException(
          "No enum const " + TestOne.class + "@description." + description);
      }
      
      where “description” is yet another String field in my enum. And I am not alone. See for example this beautiful article.
      Obviously this method is very ineffective. Every time it is invoked it iterates over all members of enum. Here is the improved version that uses cache:
      
       private static Map map = null;
       public static TestTwo valueOfDescription(String description) {
        synchronized(TestTwo.class) {
         if (map == null) {
          map = new HashMap();
          for (TestTwo v : values()) {
           map.put(v.description, v);
          }
         }
        }
      
        TestTwo result = map.get(description);
        if (result == null) {
               throw new IllegalArgumentException(
                       "No enum const " + TestTwo.class + "@description." + description);
        }
      
        return result;
       }
      
      It is fine if we have only one enum and only one custom field that we use to find the enum value. But if number each one of 20 enums has 3 such fields the code will be very verbose. As far as I dislike copy/paste programming I have implemented utility that helps to create such methods. I called this utility class ValueOf. It has 2 public methods:
      
      public static <T extends Enum<T>, V> T valueOf(Class<T> enumType, String fieldName, V value);
      
      that finds required field in specified enum. It is implemented utilizing reflection and uses hash table initialized during the first call for better performance. Other overridden valueOf() looks like:
      
      public static <T extends Enum<T>> T valueOf(Class<T> enumType, Comparable<T> comparable);
      
      This method does not cache results, so it iterates over enum members on each invocation. But it is more universal: you can implement comparable as you want, so this method may find enum member using more complicated criteria.
      Full code with examples and JUnit test case is available here.

      Conclusions

      Java Enums provide ability to locate enum member by name. This article describes utility that makes it easy to locate enum members by any other field.

      Dynamic Enums

      Introduction

      Enums introduced to Java 1.5 is a very useful and well known feature. There are a lot of tutorials that explain enums usage in details (e.g. the official Sun’s tutorial). Java enums by definition are immutable and must be defined in code. In this article I would like to explain use case when dynamic enums are needed and how to implement them.

      Motivation

      There are 3 ways to use enums:
      1. direct access using the enum value, e.g. Color.RED
      2. access using enum name, e.g. Color.valueOf("RED")
      3. get enumeration of all enum values using values() method, e.g. Color.values()
      Sometimes it is very convenient to store some information about enum in DB, file etc. In this case the enum defined in code must contain appropriate values.
      For example let’s discover the enum Color:
      enum Color {RED, GREEN, BLUE;}
      Let’s assume that we would like to allow user to create his custom colors. So, we have to handle table “Color” in database. But in this case we cannot continue using enum Color: if user adds new color (e.g. YELLOW) to DB we have to modify code and add this color to enum too.
      OK, we can refuse to use enum in this case. Just rename enum to class and initialize list of colors from DB. But what if we already have 100 thousand lines of code where methods values() and valueOf() of enum Color are used? No problem: we can implement valueOf() and values() manually for the new class “Color.”
      Now is the problem. What if we have to refactor 12 enums like Color? Copy/Paste the new methods to all these classes? I would like to suggest other, more generic approach.

      Making enums dynamic

      Enum is compile time feature. When we create enum Foo, class Foo that extends java.lang.Enum is generated for us automatically. This is the reason why enum cannot extend other class (multiple inheritance is not supported in Java). Moreover some compiler’s magic prevents attempt to manually write class that extends java.lang.Enum.
      The only solution is to write yet another class similar to Enum. I wrote class DynaEnum. This class mimics functionality of Enum but it is regular class, so it can be inherited. Static hash table holds elements of all created dynamic enums.
      My DynaEnum contains generic implementation of valueOf() and values() done using reflection, so users of this class do not have to implement them.
      This class allows relatively easy refactoring for use case described above. Just change enum to class, inherit it from DynaEnum and write code to initialize members.

      Example

      Source code of examples can be found here.
      I implemented 2 examples: DigitsDynaEnum that does not have any added value relatively to enum. It is implemented mostly to check that base class functionality works. DigitsDynaEnumTest contains several unit tests.
      PropertiesDynaEnum is a dynamic enum that reads its members from properties file. Its subclass WritersDynaEnum reads list of famous writers from properties file WritersDynaEnum.properties.
      The goal is implemented! We have class that mimics functionality of enum but is absolutely dynamic. We can change list of writers without re-compiling the code. Therefore we can actually store the enum values everywhere and change “enums” at runtime.
      It is not a problem to implement something like “JdbcDynaEnum” that reads values from database but this implementation is out of scope of this article.

      Limitations

      The solution is not ideal. It uses static initialization that could cause problems in multi-class loaders environment. Members of dynamic enums obviously cannot be accessed directly (Color.RED) but only using valueOf() or values(). But still in some cases this technique may be very useful.

      Conclusions

      Java enum is a very powerful feature but it has serious limitation. Enum values are static and have to be defined in code. This article suggests trick that allows to mimic enum functionality and store “enum” values separately from code.

      Discover type of parametrized class

      Introduction

      Generics is a beautiful feature of Java 1.5. But it has some limitations. One of them is that it is impossible to discover type of parametrized class. For example if you have generic code like:
      List<?> mylist = getList();
      where getList() is can return either List<String> or List<Integer> there is not convenient way to discover it. Class Class and reflection API do not provide ability to discover the type of parameter. The reason is simple: generics is the compiler’s feature. The class’ parameters are not stored in .class file and therefore cannot be accessed at runtime. Sometimes we can use workarounds. For example in case of list we can retrieve the first element and then call getClass(). Obviously this method will not work for empty lists and for null elements.

      Use abstract class

      Other workaround exists if we develop both sides: the discovered class and the code that discovers it. Assume that we have interface Foo:
      
      interface Foo<T> {
          void foo(T t);
      }
      
      Assume also that we want to implement framework that deals with instances of classes implementing Foo and this framework should know the concrete type of T. One solution is to add special method that returns T.
      
      interface Foo<T> {
          void foo(T t);
          T getType();
      }
      
      Each class that implements Foo must now implement 2 methods. I think that this design may be improved. We can move this functionality to abstract class:
      
      public abstract class AbstractFoo<T> implements Foo<T> {
          private Class<T> t;
      
          protected AbstractFoo(Class<T> t) {
              this.t = t;
          }
      
          public Class<T> getType() {
              return t;
          }
      }
      
      The abstract class defines protected constructor that receives parameter of type Class, so all its sub classes must call it.
      
      public class FooImpl extends AbstractFoo<String> {
          public FooImpl() {
              super(String.class);
          }
      
          public void foo(String s) {
              // the implementation...
          }
      }
      
      I think that this approach is better than implementing getType() in each sub-class separately because sub-class is dedicated on its own task while abstract class cares cares to provide information about the type to framework. Constructor is typically written in the beginning of the class, i.e. near the definition of class itself and therefore it is easier to see mistakes.

      Real example

      Here is more complicated example. Several year ago I worked for small start up company named NLayers that implemented network discovery solution. We connected to remote devices using various protocols that were represented in application by hierarchy of interfaces extended from top level interface Session. Logic of discovery itself was implemented in relatively small classes we called “probes.” All these classes implemented interface Probe. The framework decided which probe should be executed and when. The framework should know the Session type supported by probe. There were many types of session and parallel hierarchy of abstract probes: ShellSession and ShellProbe, SnmpSession and SnmpProbe etc. The years passed since that time and now I’d like to suggest improved design with only one AbstractProbe. The design uses ideas described above. The code snippets here are not stolen from my former employer but written yesterday from scratch. Real code was more complicated and contained more details irrelevant for this example.

      Acknowledgments

      I wold like to thank my former colleagues at NLayers: Avshi Avital, Oran Epelbaum, Chaim Linhart, Yariv Bandiel, David Resnik. I enjoyed working with them and develop product that gave me idea for this article.

      Speciality of mobile web applications

      Introduction

      WML is died. Mobile devices support XHTML, so theoretically regular web applications work on mobile devices. Amazing. Unfortunately real life is more complicated.

      Problems with WAP browsers

      • Mobile screens are small. Sites built for big screens are very inconvenient when browsing on mobile device.
      • There are 5 popular desktop web browsers and dozens of mobile browsers, so cross-browsing compatibility becomes serious problem.
      • Existing mobile browsers do not support all HTML/CSS features and often behave differently. So cross browser writing is hard.
      • JavaScript/ECMA Script is not widely supported. Even if it is supported the DOM of each browser has its own specialty.
      • Some standard events are not supported.
        • onmouseover cannot be supported on simple phones that do not have mouse pointer and on new phones with touch screen.
        • onkeypres, onkedown, onkeyup are often not supported because when user types text into “input” element the phone actually opens native editor, so browser even does not “know” that a key is pressed.
      • Java applets are not supported; Flash is supported by very few models.
      • Supported fonts list is very limited.
      • Different phones support different media format. Most phones support 3GP but as far as I know modern phones do not support this format but do support MP4. And we have to be careful when we choose codec we use for video clip.
      • Even phone identification is may be problematic. User can use any browser compatible with his phone instead of built-in browser. This custom browser may send User-Agent that does not contain any information about the device.
      This means that to make web application useful on mobile phone we can
      • make it primitive, static, as portable as it is possible. Avoid using scripts, complicated markup and absolute positioning. Application will not be “sexy” but will be available on maximal types of devices. Chance to succeed →0.
      • Write 2 separate applications: one for desktop, other for mobile phones. The mobile version could have even subversions for different platforms. Number of working hours →∞.
      Amazing trade-off :( .

      Solution

      So, what’s the solution?
      • Write 2 different applications. One for WEB, other for WAP.
      • Use WURFL and if you are lucky Java programmer :) user tag library WALL (both by Luca Passani). This will help to render elements, support legacy WML devices, show the best quality images and video etc.
      • Use script-less tricks that help to make illusion of dynamic application.
      • Sometimes use scripts for devices that support them but only if scripts add functionality to the application so that poor users of old phones can still use it.
      Let’s see some details of suggested tricks.

      Scriptless tricks

      Experienced WEB designers are regular to use client side scripting for a lot of tasks. This is good and convenient technique very that allows to create dynamic, responsive, good looking application. Obviously this technique does not work when scripts are disabled or even unsupported by client’s browser. A lot of relatively simple phones do not support client side scripting. Here is a humble list of tips that can help to create powerful applications even with these restrictions.

      Use animated GIFs

      Animated GIFs are supported by most cellular phones. WURFL property “gif_animated” can help to identify whether phone supports animated GIFs.

      *:hover and *:focus

      Hover is irrelevant for old phones that do not have mouse cursor and for touch screens. So, for better compatibility we should use them together. Selected element should be always marked using background, border or font color. Otherwise user with device without mouse cursor or touch screen cannot understand which element is active now.

      Save space

      Screens of mobile devices are very small. We do not have enough space to show label, icon and text field. We should show text field only and put label on it. Label should disappear when user selects the element. How to implement this in scriptless world? Create transparent image with text and put it as background of the text field when it is not selected. Background of selected field should be empty.
      This solution has disadvantage: someone has to create the image that contains text only and handle it if text is changed. But it can be improved. It is not a problem to write servlet that receives required text as a parameter and writes it to image. URL of this servlet may be used in CSS background-url definition. Implementation of such servlet may be a subject of separate article.
      Find here examples of scriptless tricks.

      Limited Scripting

      Browsers typically ignore unknown tags. We can write scripts that will work if browser supports them and ignored otherwise.

      Control typed characters

      Keypad of mobile phone is typically small and inconvenient. You have to press button 5 times to type digit 7 into a text field. Only very motivated user is able to type his phone number into text field. How to make field numeric? There is an CSS extension -wap-input-format that helps to do this:

      <input type="text" style='-wap-input-format: "N"'/>

      For more details click here.
      Unfortunately this CSS extension is not supported by many smart phones like Symbian, Blackberry, IPhone. But these phones support java script.
      Solution implemented here uses both -wap-input-format for Nokia S40, SonyEricson and others and java script that substitutes letters by digits.
      Function that implements the substitution is called from “onkeyup” event of text field. But many phones do not fire this event. To support such devices I implemented timer task that starts when field is selected, runs periodically and replaces characters entered by user to digits.

      Conclusions

      There are huge number of different mobile devices and browser. Each one has its own limitations and characteristics. But it does not mean that web applications for mobile devices must be primitive and ugly. Some simple techniques and tricks allow creating powerful and good looking applications targeted for mobile devices.

      Acknowledgments

      I would like to thank Yaron Amir begin_of_the_skype_highlighting     end_of_the_skype_highlighting,  my colleague at Vringo for valuable ideas in WEB design he gave me during our discussions.

      Portable custmized <hr>

      When graphic designer created new design of web application I was working on I started to play with CSS trying to create view as close as it possible to what she drew using PhotoShop. Imagine that it was not always easy. Making long story short one of problems was to create custom horizontal line. Typically people use <hr> tag. But standard <hr> does not look exactly as what was drawn.
      People have written very good articles about the <hr> customization (e.g. this). Unfortunately the ways are not portable enough and making the story more complicated the web application was targeted for mobile phones where you can always have surprise from specific phone model.
      So, I found the following trick that worked well. I just wrote <div> tag with &nbsp; as a body and required background image stretched wide:
      
      <div class="customhr">&nbsp;</div> 
       
      where customhr definition looked like:
      
      .hr {
          margin-top:1em;
          margin-bottom:1em;
          background-image:url('/images/hr.png');
          background-repeat:repeat-x;
          height: 1px;
      }
      

      This worked well for all browsers we tried.
      It means that sometimes we do not need <hr> at all. Can play its role very well.

      Text direction

      Most of scripts in the world use left-to-right direction (LTR). But some scripts are right-to-left (RTL), e.g. Hebrew and Arabic. OK, the days when poor programmers had to revert Hebrew strings to show them right-to-left. But still the document layout is our responsibility. Speaking about HTML we have to define document direction (ltr or rtl) and choose between margin-left and margin-right, padding-left and padding-right.
      But first we have to determine whether the current script is LTR or RTL. I typically used code like

      private static boolean isRTL(Locale locale) {
          Pattern rtl = Pattern.compile("he|iw|ar");
          return rtl.matcher(locale.getLanguage()).matches();
      }
      

      But once I had several free minutes and tried to investigate what is the code that supports all languages. Fortunately I found such code in Google GWT:
       
      private static final Pattern RtlLocalesRe = Pattern.compile(
       "^(ar|dv|he|iw|fa|nqo|ps|sd|ug|ur|yi|.*[-_](Arab|Hebr|Thaa|Nkoo|Tfng))" +
       "(?!.*[-_](Latn|Cyrl)($|-|_))($|-|_)");
       public static boolean isRtlLanguage(String language) {
           return language != null && RtlLocalesRe.matcher(language).find();
       }
      

      Obviously I did not want to put GWT to my classpath only for this small function, so I have created my humble utility class and copied this code there. The full code can be found in the end of this post.
      But this was not enough. I found that I need method that returns “ltr” or “rtl” for current language (see getDocumentDirection). Method getAlign that returns “right” or “left” is useful for value of CSS attribute value of align and for generating of margin and padding attributes:
       
      <div style="margin-RTLUtil.getLangAlign(lang): 2em; direction: RTLUtil.getLangDirection(lang)">
      </div>
      

      OK, everything is fine when whole screen is written in one language. But some sites contain user created content. For example blogs, advertisement sites etc. Some users may post text in language that uses other direction than the site itself. This looks bad. I suggest the following solution.
      As we see in previous code snippet there are very few RTL languages. All others are LTR. I did not spend too much time to investigate all languages mentioned in Google’s regular expression but definitely Hebrew and Arabic use their own script. It means that it is very easy to write code that identifies the script type using the unicode range.  But how may letters to check? Even one paragraph may use letters from several different character sets that use different directions (it is for example very common practice in job description for programmers.) I decided that 50% criterion is good enough, i.e. the text should be aligned to the left if most characters in the text  should be shown from left to right and wise versa. The following method implements this idea.
       
      public static boolean isRtlScript(String text) {
          int[][] rtlRanges = new int[][] {
              new int[] {'\u0590', '\u05FF'}, // Hebrew and Yiddish
              new int[] {'\u0600', '\u06FF'}, // Arabic
              new int[] {'\u0780', '\u07BF'}, // Thaa
          };
          int rtlCount = 0;
          for (int i = 0;  i < text.length();  i++) {
              char c = text.charAt(i);
              RANGES:
              for (int j = 0;  j < rtlRanges.length;  j++) {
                  if (c >= rtlRanges[j][0] && c <= rtlRanges[j][1]) {
                  rtlCount++;
                  break RANGES;
              }
          }
          return rtlCount > text.length() / 2;
      }
      

      This method does not support all languages mentioned in Google’s regular expression because I did not spend enough time googling these languages.
      Here is the source code of utility itself and its JUnit test saved in MS Word format to satisfy this blog’s restrictions.

      RtlUtil.java
      RtlUtilTest.java

      Sending HTML formatted email

      Last week I had to fix email sent from java code. The email was formatted as HTML. It worked fine in left-to-right languages but did not look good in Arabic (that uses right-to-left script).
      The email that we sent was formatted as full HTML document like

      <!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">
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>مرحبا</title>
      </head>
      <body>
      مرحبا ، العالم
      <body> 
       
      (I do not know Arabic, so the text is just a google’s translation of “Hello, world.” The original text was longer and contained some irrelevant and probably confidential details of company I worked for.)
      I added attributes dir="rtl" lang="ar" to tag html, saw that now the text looks right-to-left in browser and event did not try to send it by email. I thought that issue is fixed.
      But the bug was re-opened shortly. And really the text was adjusted to the left in both outlook and different web based email readers.
      I spent a lot of time trying to understand the problem relatively to the complexity of the task.
      To make the long story short all attempts to put anything to tags html, head, body failed.
      because email clients ignore (or even remove in case of web based emails) all HTML heading tags of received message.
      But all tags inside body work well. So the easiest (and I believe “right”) solution was to send HTML fragment wrapped with div tag instead of full document.
      In our case the text looks like:

      <div style="text-align:right;direction:rtl;">
      مرحبا ، العالم
      <br/>
      שלום העולם
      <div>
      

      The second line is in Hebrew to show that it works well too.
      Our email contained link. For better portability the text contains the same URL as HREF attribute, i.e.

      <a href="http://www.mycompany.com">http://www.mycompany.com</a>

      Embedding this link into HTML does not cause any problem. The link is left-to-right into right-to-left document:

      <div style="text-align:right;direction:rtl;">
      مرحبا ، العالم <a href="http://www.mycompany.com">http://www.mycompany.com</a>
      <br/>
      שלום העולם <a href="http://www.mycompany.com">http://www.mycompany.com</a>
      <div> 
       
      Unfortunately Hebrew and Arabic co-exist so easily only in HTML documents…

      Avoid " "?

      Last week I received bug report that says that there is some “garbage” in one of the screens of web application I am working on. This web application is written using JSP and targeted for mobile devices. I examined the screenshot attached to bug report and saw that kind white rectangle appears on screen. Such rectangles typically appear instead of some illegal character.
      More interesting details:
      • This page has not being changed for a long time.
      • The bug happens on recently introduced Arabic version only
      I was sure that the problem is in Arabic localized resource and examined it. Due to I do not read Arabic it was not as easy as it could be, but finally I understood that the problem is not there. Moreover everything works fine in FF. Then I tried 3 different phones: Nokia 6280, Nokia N73 and SonyEricson. The screen looked well everywhere except one device that was used in QA (Nokia N95).
      Fortunately I found yet another N95 at the office and saw that the problem happens.
      I checked what is written in code and saw the following:

      <img align="middle" src="http://www.my.com/images/share.png"/>&nbsp;
      <a class="actionHeader" style="margin-top:0px;" href="<%=shareLink%>"><%=Lang.getString("share") %></a><br />

      The code contained newline right after  . I added some text between   and new line and problem disappeared. So, the solution was to add a single space after

      Conclusions
      My “fix” works but it is bad and very fragile solution. The best way is using margin style for img. Something like the following:

      <img align="middle" style="margin-right:1em" src="http://
      www.my.com/images/share.png"/>

      Migrating here

      Due to the stability problems of my previous blog alexr.blog.com still exist that bothers me very much I decided to move here and I am going to copy all old articles to http://alexradzin.blogspot.com/.

      Find unused localized messages

      I am working for company where refactoring is foul term. Died code is never removed etc. Fortunately modern IDEs (e.g. Eclipse) have enough search capabilities. Nevertheless problem of unused localized messages is very serious. There is no compiler that shows you “unused” warning. Many unused sentences are stored in *.prooperties files and must be handled, translated to new languages etc.
      I wrote very simple UNIX shell script that finds such messages. I
      f you have a bad luck (like me) to work on Windows, do not worry. You can always run it using Cygwin (as I do).
      I work in Java, so script searches for *.java and *.jsp files. But it can be easily modified.

      #!/bin/sh

      ###########################################################
      # This script searches source code for unesed strings and prints them.
      # It assumes that strings are stored properties files and code is in *.java or *.jsp files.
      ###########################################################
      if [ "$#" -ne 2 ]; then
      echo Usage: $0 RESOURCE_FILES_PATTERN SRC_CODE_ROOT_DIRECTORY
      echo e.g. $0 “MyProject/resources/Messages*.properties” “MyProject/src”
      exit 0;
      fi
      texts=$1
      src=$2
      for line in `(ls $texts | while read file; do cat $file; done;) | sed ‘s/#.*//’ | sed ‘s/=.*//’ | sed ‘s/ //’ | egrep -v “^$” | sort -u`; do
      found=false
      for file in `find $src -name *.java -or -name *.jsp`; do
      grep “\”$line\”" $file >/dev/null
      if [ "$?" == "0" ]; then
      found=true
      continue
      fi
      done;
      if [ $found == false ]; then
      echo $line is not found
      fi
      done;

      This post was previously published at alexr.blog.com

      Monday, September 20, 2010

      My previous posts to other blog

      This is my first post here. I started my professional blog at alexr.blog.com but unfortunately I am not satisfied with stability and feature set that this blog provides. So, I decided to try this one.
      I do not want to copy and paste all my posts from there to here but here is the list of my articles:
      To be continued...