1 5 package com.opensymphony.webwork.dispatcher; 6 7 import javax.servlet.ServletContext ; 8 import java.io.Serializable ; 9 import java.util.*; 10 11 12 21 public class ApplicationMap extends AbstractMap implements Serializable { 22 24 ServletContext context; 25 Set entries; 26 27 29 34 public ApplicationMap(ServletContext ctx) { 35 this.context = ctx; 36 } 37 38 40 43 public void clear() { 44 entries = null; 45 46 Enumeration e = context.getAttributeNames(); 47 48 while (e.hasMoreElements()) { 49 context.removeAttribute(e.nextElement().toString()); 50 } 51 } 52 53 58 public Set entrySet() { 59 if (entries == null) { 60 entries = new HashSet(); 61 62 Enumeration enumeration = context.getAttributeNames(); 64 65 while (enumeration.hasMoreElements()) { 66 final String key = enumeration.nextElement().toString(); 67 final Object value = context.getAttribute(key); 68 entries.add(new Map.Entry() { 69 public boolean equals(Object obj) { 70 Map.Entry entry = (Map.Entry) obj; 71 72 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue())); 73 } 74 75 public int hashCode() { 76 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); 77 } 78 79 public Object getKey() { 80 return key; 81 } 82 83 public Object getValue() { 84 return value; 85 } 86 87 public Object setValue(Object obj) { 88 context.setAttribute(key.toString(), obj); 89 90 return value; 91 } 92 }); 93 } 94 95 enumeration = context.getInitParameterNames(); 97 98 while (enumeration.hasMoreElements()) { 99 final String key = enumeration.nextElement().toString(); 100 final Object value = context.getInitParameter(key); 101 entries.add(new Map.Entry() { 102 public boolean equals(Object obj) { 103 Map.Entry entry = (Map.Entry) obj; 104 105 return ((key == null) ? (entry.getKey() == null) : key.equals(entry.getKey())) && ((value == null) ? (entry.getValue() == null) : value.equals(entry.getValue())); 106 } 107 108 public int hashCode() { 109 return ((key == null) ? 0 : key.hashCode()) ^ ((value == null) ? 0 : value.hashCode()); 110 } 111 112 public Object getKey() { 113 return key; 114 } 115 116 public Object getValue() { 117 return value; 118 } 119 120 public Object setValue(Object obj) { 121 context.setAttribute(key.toString(), obj); 122 123 return value; 124 } 125 }); 126 } 127 } 128 129 return entries; 130 } 131 132 139 public Object get(Object key) { 140 String keyString = key.toString(); 143 Object value = context.getAttribute(keyString); 144 145 return (value == null) ? context.getInitParameter(keyString) : value; 146 } 147 148 155 public Object put(Object key, Object value) { 156 entries = null; 157 context.setAttribute(key.toString(), value); 158 159 return get(key); 160 } 161 162 168 public Object remove(Object key) { 169 entries = null; 170 171 Object value = get(key); 172 context.removeAttribute(key.toString()); 173 174 return value; 175 } 176 } 177 | Popular Tags |