Save This Page
Home » freemarker-2.3.13 » freemarker.ext.beans » [javadoc | source]
    1   package freemarker.ext.beans;
    2   
    3   import java.util.HashMap;
    4   import java.util.HashSet;
    5   import java.util.Map;
    6   import java.util.Set;
    7   
    8   import freemarker.ext.util.ModelCache;
    9   import freemarker.ext.util.ModelFactory;
   10   import freemarker.template.TemplateModel;
   11   
   12   public class BeansModelCache extends ModelCache
   13   {
   14       private final Map classToFactory = new HashMap();
   15       private final Set mappedClassNames = new HashSet();
   16   
   17       private final BeansWrapper wrapper;
   18       
   19       BeansModelCache(BeansWrapper wrapper) {
   20           this.wrapper = wrapper;
   21       }
   22       
   23       protected boolean isCacheable(Object object) {
   24           return object.getClass() != Boolean.class; 
   25       }
   26       
   27       protected TemplateModel create(Object object) {
   28           Class clazz = object.getClass();
   29   
   30           ModelFactory factory;
   31           synchronized(classToFactory) {
   32               factory = (ModelFactory)classToFactory.get(clazz);
   33               if(factory == null) {
   34                   String className = clazz.getName();
   35                   // clear mappings when class reloading is detected
   36                   if(!mappedClassNames.add(className)) {
   37                       classToFactory.clear();
   38                       mappedClassNames.clear();
   39                       mappedClassNames.add(className);
   40                   }
   41                   factory = wrapper.getModelFactory(clazz);
   42                   classToFactory.put(clazz, factory);
   43               }
   44           }
   45           return factory.create(object, wrapper);
   46       }
   47   }

Save This Page
Home » freemarker-2.3.13 » freemarker.ext.beans » [javadoc | source]