Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package freemarker.debug.impl;
  
  import java.util.Arrays;
  import java.util.Iterator;
  import java.util.List;
 

Author(s):
Attila Szegedi
Version:
$Id: RmiDebuggedEnvironmentImpl.java,v 1.3.2.2 2006/11/27 07:55:17 szegedia Exp $
 
 extends
 implements
 {
     private static final long serialVersionUID = 1L;
 
     private static final CacheStorage storage = new SoftCacheStorage(new IdentityHashMap());
     private static final Object idLock = new Object();
     private static long nextId = 1;
     
     private boolean stopped = false;
     private final long id;
     
     private RmiDebuggedEnvironmentImpl(Environment envthrows RemoteException
     {
         super(new DebugEnvironmentModel(env), .);
         synchronized()
         {
              = ++;
         }
     }
 
     static synchronized Object getCachedWrapperFor(Object key)
     throws
         RemoteException
     {
         Object value = .get(key);
         if(value == null)
         {
             if(key instanceof TemplateModel)
             {
                 int extraTypes;
                 if(key instanceof DebugConfigurationModel)
                 {
                     extraTypes = .;
                 }
                 else if(key instanceof DebugTemplateModel)
                 {
                     extraTypes = .;
                 }
                 else
                 {
                     extraTypes = 0;
                 }
                 value = new RmiDebugModelImpl((TemplateModel)keyextraTypes);
             }
             else if(key instanceof Environment)
             {
                 value = new RmiDebuggedEnvironmentImpl((Environment)key); 
             }
             else if(key instanceof Template)
             {
                 value = new DebugTemplateModel((Template)key);
             }
             else if(key instanceof Configuration)
             {
                 value = new DebugConfigurationModel((Configuration)key);
             }
         }
         return value;
     }
 
     public void resume()
     {
         synchronized(this)
         {
            notify();
        }
    }
    public void stop()
    {
         = true;
        resume();
    }
    public long getId()
    {
        return ;
    }
    
    boolean isStopped()
    {
        return ;
    }
    
    private abstract static class DebugMapModel implements TemplateHashModelEx
    {
        public int size()
        {
            return keySet().size();
        }
        public TemplateCollectionModel keys()
        {
            return new SimpleCollection(keySet());
        }
        public TemplateCollectionModel values() throws TemplateModelException
        {
            Collection keys = keySet();
            List list = new ArrayList(keys.size());
            
            for(Iterator it = keys.iterator(); it.hasNext();)
            {
                list.add(get((String)it.next()));
            }
            return new SimpleCollection(list);
        }
        public boolean isEmpty()
        {
            return size() == 0;
        }
        
        abstract Collection keySet();
        static List composeList(Collection c1Collection c2)
        {
            List list = new ArrayList(c1);
            list.addAll(c2);
            Collections.sort(list);
            return list;
        }
    }
    
    private static class DebugConfigurableModel extends DebugMapModel
    {
        static final List KEYS = Arrays.asList(new String[]
        {
            .,
            .,
            .,
            .,
            .,
            .,
        });
        final Configurable configurable;
        
        DebugConfigurableModel(Configurable configurable)
        {
            this. = configurable;
        }
        
        Collection keySet()
        {
            return ;
        }
        
        public TemplateModel get(String keythrows TemplateModelException
        {
            String s = .getSetting(key);
            return s == null ? null : new SimpleScalar(s);
        }
    }
    
    private static class DebugConfigurationModel extends DebugConfigurableModel
    {
        private static final List KEYS = composeList(., Collections.singleton("sharedVariables"));
        private TemplateModel sharedVariables = new DebugMapModel()
        {
            Collection keySet()
            {
                return ((Configuration)).getSharedVariableNames();
            }
        
            public TemplateModel get(String key)
            {
                return ((Configuration)).getSharedVariable(key);
            }
        };
        
        DebugConfigurationModel(Configuration config)
        {
            super(config);
        }
        
        Collection keySet()
        {
            return ;
        }
        public TemplateModel get(String keythrows TemplateModelException
        {
            if("sharedVariables".equals(key))
            {
                return 
            }
            else
            {
                return super.get(key);
            }
        }
    }
    
    private static class DebugTemplateModel extends DebugConfigurableModel
    {
        private static final List KEYS = composeList(.
            Arrays.asList(new String[] {
                "configuration"
                "name",
                }));
    
        private final SimpleScalar name;
        DebugTemplateModel(Template template)
        {
            super(template);
            this. = new SimpleScalar(template.getName());
        }
        Collection keySet()
        {
            return ;
        }
        public TemplateModel get(String keythrows TemplateModelException
        {
            if("configuration".equals(key))
            {
                try
                {
                    return (TemplateModel)getCachedWrapperFor(((Template)).getConfiguration());
                }
                catch (RemoteException e)
                {
                    throw new TemplateModelException(e);
                }
            }
            if("name".equals(key))
            {
                return ;
            }
            return super.get(key);
        }
    }
    private static class DebugEnvironmentModel extends DebugConfigurableModel
    {
        private static final List KEYS = composeList(.
            Arrays.asList(new String[] {
                "currentNamespace",
                "dataModel",
                "globalNamespace",
                "knownVariables",
                "mainNamespace",
                "template",
                 }));
    
        private TemplateModel knownVariables = new DebugMapModel()
        {
            Collection keySet()
            {
                try
                {
                    return ((Environment)).getKnownVariableNames();
                }
                catch (TemplateModelException e)
                {
                    throw new UndeclaredThrowableException(e);
                }
            }
        
            public TemplateModel get(String keythrows TemplateModelException
            {
                return ((Environment)).getVariable(key);
            }
        };
         
        {
            super(env);
        }
        Collection keySet()
        {
            return ;
        }
        public TemplateModel get(String keythrows TemplateModelException
        {
            if("currentNamespace".equals(key))
            {
                return ((Environment)).getCurrentNamespace();
            }
            if("dataModel".equals(key))
            {
                return ((Environment)).getDataModel();
            }
            if("globalNamespace".equals(key))
            {
                return ((Environment)).getGlobalNamespace();
            }
            if("knownVariables".equals(key))
            {
                return ;
            }
            if("mainNamespace".equals(key))
            {
                return ((Environment)).getMainNamespace();
            }
            if("template".equals(key))
            {
                try
                {
                    return (TemplateModelgetCachedWrapperFor(((Environment)).getTemplate());
                }
                catch (RemoteException e)
                {
                    throw new TemplateModelException(e);
                }
            }
            return super.get(key);
        }
    }
New to GrepCode? Check out our FAQ X