Announcement Announcement Module
Collapse
No announcement yet.
Can't read a value in a LinkedHashMap Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't read a value in a LinkedHashMap

    my environment;spring 2.5,hibernate3,iceface 1.7

    when I try to retrieve the data in Collections.synchronizedMap(new LinkedHashMap());
    , I always get the null value.
    If i can not use linkedhashmap, is there any alternative method?

    thanks in advances.
    Code:
    package catalog.model.service.impl;
    
    import java.util.List;
    import java.util.LinkedList;
    import java.util.Map;
    import java.util.LinkedHashMap;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Iterator;
    //
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    //
    import org.springframework.dao.DataIntegrityViolationException;
    //
    import catalog.model.service.CatalogService;
    import catalog.model.exception.CatalogException;
    import catalog.model.exception.DuplicateProductIdException;
    import catalog.model.dao.CatalogDao;
    import catalog.model.businessobject.Category;
    import catalog.model.businessobject.Product;
    
    public class CachedCatalogServiceImpl implements CatalogService {
    	//the logger for this class
    	private Log logger = LogFactory.getLog(this.getClass());
    	
    	//the CatalogDao used
    	private CatalogDao catalogDao;
    	
    	//the read/write cache for products
    	private Map productCache;
    	
    	//the read/write cache for categories
    	private Map categoryCache;
    	
    	public CachedCatalogServiceImpl() throws CatalogException {
    		this.productCache = Collections.synchronizedMap(new LinkedHashMap());
    		this.categoryCache = Collections.synchronizedMap(new LinkedHashMap());
    	}
    	
    	
    	public void setCatalogDao(CatalogDao newCatalogDao) {
    		this.catalogDao = newCatalogDao;
    	}
    	
    	
    	
    	
    
    		
    	public Product getProduct(String id) {
    		return (Product)this.productCache.get(id);
    	}
    
    	public Category getCategory(String id) {
    		return (Category)this.categoryCache.get(id);
    	}
    
    	public List getAllProducts() {
    		return this.getValueList(this.productCache);
    	}
    	
    	public List getAllCategories() {
    		return this.getValueList(this.categoryCache);
    	}
    	
    	public void init() throws CatalogException {
    		try {
    			this.populateCache();
    		} catch (Exception e) {
    			String msg = "Could not populate cache";
    			this.logger.error(msg, e);
    			
    			throw new CatalogException(msg, e);
    		}
    	}
    
    	/**
    	 * @see CatalogService#getAllCategories()
    	 */	
    	private void populateCache() {
    		List products = this.catalogDao.getAllProducts();
    		
    		for (int i=0; i<products.size(); i++) {
    			Product p = (Product)products.get(i);
    			this.productCache.put(p.getId(), p);
    		}
    		
    		List categories = this.catalogDao.getAllCategories();
    		
    		for (int i=0; i<categories.size(); i++) {
    			Category c = (Category)categories.get(i);
    			this.categoryCache.put(c.getId(), c);
    		}
    	}
    	
    	private List getValueList(Map data) {
    		List list = new LinkedList();
    		Collection values = data.values();
    		
    		Iterator ite = values.iterator();
    		while(ite.hasNext()) {
    			list.add(ite.next());
    		}
    		
    		return list;
    	}
    }

  • #2
    How are you obtaining an instance of your CatalogService? Make sure you use the one configured in the applicationcontext AND that your init method gets called. If this isn't the case everything will be empty.

    Comment


    • #3
      thanks

      Originally posted by Marten Deinum View Post
      How are you obtaining an instance of your CatalogService? Make sure you use the one configured in the applicationcontext AND that your init method gets called. If this isn't the case everything will be empty.
      it works. thank you

      Comment

      Working...
      X