ProductCatalog.java
01 /*
02  *
03  * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
04  *
05  */
06 package demo.townsend.service;
07 
08 import java.util.ArrayList;
09 import java.util.List;
10 
11 import net.sf.ehcache.CacheManager;
12 import net.sf.ehcache.Ehcache;
13 import net.sf.ehcache.Element;
14 
15 public class ProductCatalog {
16    private final Ehcache cache;
17 
18    public ProductCatalog() {
19       cache = CacheManager.getInstance().getCache("catalog");
20    }
21 
22    private List<Product> initCatalog() {
23       List<Product> catalog = new ArrayList<Product>();
24       catalog.add(new Product("0001"10"Canon PowerShot A620",
25             "7.1 Megapixel Digital"));
26       catalog.add(new Product("0002"24"Olympus EVOLT E-500",
27             "8.0 Megapixel Digital SLR Camera w/2.5\" LCD & Two Lenses"));
28       catalog.add(new Product("0003"150"Canon PowerShot SD450",
29             "5.0 Megapixel Digital Camera w/3x Zoom & 2.5\" LCD"));
30       catalog.add(new Product("0004"165"Fuji FinePix A345",
31             "4.1 Megapixel Digital Camera"));
32       catalog.add(new Product("0005"205"Olympus SP-310",
33             "7.1 Megapixel Digital Camera"));
34       catalog.add(new Product("0006"90"Canon PowerShot A520",
35             "4.0 Megapixel Digital Camera w/4X Zoom"));
36       catalog.add(new Product("0007"4"Canon PowerShot SD500",
37             "7.1 Megapixel Digital Camera w/3x Optical Zoom"));
38       catalog
39             .add(new Product("0008"14"Casio EX-Z850",
40                   "8.0 MegaPixel Camera with 3x Optical Zoom and Super Bright 2.5\" LCD"));
41       cache.put(new Element("cameras", catalog));
42       return catalog;
43    }
44 
45    @SuppressWarnings("unchecked")
46    public List<Product> getCatalog() {
47       Element cachedCameras = cache.get("cameras");
48       if (cachedCameras == null) {
49          return initCatalog();
50       else {
51          return (List<Product>cachedCameras.getValue();
52       }
53    }
54 }