1 25 26 package org.snipsnap.util; 27 28 import org.snipsnap.app.Application; 29 import org.radeox.util.logging.Logger; 30 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 41 42 public class ApplicationAwareMap { 43 protected Map applicationMaps; 44 private Class mapType; 45 46 public ApplicationAwareMap() { 47 } 48 49 public ApplicationAwareMap(Class outerMapType) { 50 try { 51 applicationMaps = (Map ) outerMapType.newInstance(); 52 } catch (Exception e) { 53 applicationMaps = new HashMap (); 54 } 55 } 56 57 public ApplicationAwareMap(Class outerMapType, Class mapType) { 59 this(outerMapType); 60 this.mapType = mapType; 61 } 62 63 protected Object newInstance() { 64 Object map = null; 65 try { 66 map = mapType.newInstance(); 67 } catch (Exception e) { 68 System.err.println("Cannot instantiate " + mapType.getName() + " " + e); 70 } 71 return map; 72 } 73 74 public Object getObject(String applicationOid) { 75 Object map = applicationMaps.get(applicationOid); 76 if(null == map) { 77 map = newInstance(); 78 applicationMaps.put(applicationOid, map); 79 } 80 return map; 81 } 82 83 public Queue getQueue() { 84 return (Queue) getObject(); 85 } 86 87 public Map getMap() { 88 return (Map ) getObject(); 89 } 90 91 public Map getMap(String applicationOid) { 92 return (Map ) getObject(applicationOid); 93 } 94 95 public Object getObject() { 96 String application = (String ) Application.get().getObject(Application.OID); 97 return getObject(application); 98 } 99 100 105 public Map findMap(Object object) { 106 Iterator mapIt = applicationMaps.values().iterator(); 108 while (mapIt.hasNext()) { 109 Map map = (Map ) mapIt.next(); 110 if (map.containsKey(object)) { 111 return map; 112 } 113 } 114 return null; 115 } 116 } 117 | Popular Tags |