1 14 package org.wings.externalizer; 15 16 import java.util.Collection ; 17 import java.util.Collections ; 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 25 public class ExternalizeManager extends AbstractExternalizeManager { 26 27 private static final Externalizer[] DEFAULT_EXTERNALIZERS = { 28 ImageExternalizer.SHARED_PNG_INSTANCE, 29 ImageExternalizer.SHARED_GIF_INSTANCE, 30 ImageIconExternalizer.SHARED_PNG_INSTANCE, 31 ImageIconExternalizer.SHARED_GIF_INSTANCE, 32 StaticResourceExternalizer.SHARED_INSTANCE, 33 StringResourceExternalizer.SHARED_INSTANCE, 34 DynamicResourceExternalizer.SHARED_INSTANCE, 35 ResourceExternalizer.SHARED_INSTANCE, 36 }; 37 38 protected final HashMap externalizerByClass = new HashMap (); 39 40 protected final HashMap externalizerByMimeType = new HashMap (); 41 42 protected final Map externalized = Collections.synchronizedMap(new HashMap ()); 43 44 45 public ExternalizeManager() { 46 this(true); 47 } 48 49 public ExternalizeManager(boolean initWithDefaultExternalizers) { 50 if (initWithDefaultExternalizers) { 51 addDefaultExternalizers(); 52 } 53 } 54 55 56 public final void addDefaultExternalizers() { 57 for (int i = 0; i < DEFAULT_EXTERNALIZERS.length; i++) { 58 addExternalizer(DEFAULT_EXTERNALIZERS[i]); 59 } 60 } 61 62 protected final void storeExternalizedResource(String identifier, 63 ExternalizedResource extInfo) { 64 if (log.isDebugEnabled()) { 65 log.debug("store identifier " + identifier + " " + extInfo.getObject().getClass()); 66 log.debug("flags " + extInfo.getFlags()); 67 } 68 externalized.put(identifier, extInfo); 69 } 70 71 public final Object getExternalizedObject(String identifier) { 72 ExternalizedResource info = getExternalizedResource(identifier); 73 74 if (info != null) 75 return info.getObject(); 76 77 return null; 78 } 79 80 83 private String stripIdentifier(String identifier) { 84 if (identifier == null || identifier.length() < 1) 85 return null; 86 87 int pos = identifier.indexOf(org.wings.SConstants.UID_DIVIDER); 88 if (pos > -1) { 89 identifier = identifier.substring(pos + 1); 90 } 91 pos = identifier.indexOf("."); 92 if (pos > -1) { 93 identifier = identifier.substring(0, pos); 94 } 95 96 if (identifier.length() < 1) { 97 return null; 98 } 99 return identifier; 100 } 101 102 public final ExternalizedResource getExternalizedResource(String identifier) { 103 identifier = stripIdentifier(identifier); 104 if (identifier == null) return null; 105 106 if (identifier.charAt(0) == '-') { 108 return SystemExternalizeManager.getSharedInstance(). 109 getExternalizedResource(identifier); 110 } 111 112 return (ExternalizedResource) externalized.get(identifier); 113 } 114 115 public final void removeExternalizedResource(String identifier) { 116 identifier = stripIdentifier(identifier); 117 if (identifier == null) return; 118 Object externalizedResource = externalized.remove(identifier); 119 reverseExternalized.remove(externalizedResource); 120 } 121 122 123 public String externalize(Object obj) { 124 return externalize(obj, SESSION); 125 } 126 127 128 public String externalize(Object obj, Collection headers) { 129 return externalize(obj, headers, SESSION); 130 } 131 132 133 public String externalize(Object obj, int flags) { 134 return externalize(obj, (Collection ) null, flags); 135 } 136 137 138 public String externalize(Object obj, Collection headers, int flags) { 139 if (obj == null) 140 throw new IllegalArgumentException ("object must not be null"); 141 142 Externalizer externalizer = getExternalizer(obj.getClass()); 143 if (externalizer == null) { 144 log.warn("could not find externalizer for " + 145 obj.getClass().getName()); 146 return NOT_FOUND_IDENTIFIER; 147 } 148 149 return externalize(obj, externalizer, headers, flags); 150 } 151 152 153 public String externalize(Object obj, String mimeType) { 154 return externalize(obj, mimeType, SESSION); 155 } 156 157 158 public String externalize(Object obj, String mimeType, Collection headers) { 159 return externalize(obj, mimeType, headers, SESSION); 160 } 161 162 163 public String externalize(Object obj, String mimeType, int flags) { 164 return externalize(obj, mimeType, null, flags); 165 } 166 167 168 public String externalize(Object obj, String mimeType, 169 Collection headers, int flags) { 170 if (obj == null) 171 throw new IllegalStateException ("no externalizer"); 172 173 Externalizer externalizer = mimeType != null ? getExternalizer(mimeType) : null; 174 if (externalizer == null) { 175 return externalize(obj, headers, flags); 176 } else { 177 return externalize(obj, externalizer, headers, flags); 178 } 179 } 180 181 185 public void addExternalizer(Externalizer externalizer) { 186 if (externalizer != null) { 187 Class c[] = externalizer.getSupportedClasses(); 188 if (c != null) 189 for (int i = 0; i < c.length; i++) 190 if (c[i] != null) 191 externalizerByClass.put(c[i], externalizer); 192 193 String mimeTypes[] = externalizer.getSupportedMimeTypes(); 194 if (mimeTypes != null) 195 for (int i = 0; i < mimeTypes.length; i++) 196 if (mimeTypes[i] != null && 197 mimeTypes[i].trim().length() > 0) 198 externalizerByMimeType.put(mimeTypes[i].trim().toLowerCase(), 199 externalizer); 200 } 201 } 202 203 206 public void addExternalizer(Externalizer externalizer, String mimeType) { 207 if (externalizer != null && mimeType != null) 208 externalizerByMimeType.put(mimeType, externalizer); 209 } 210 211 219 public Externalizer getExternalizer(Class c) { 220 Externalizer externalizer = null; 221 if (c != null) { 222 externalizer = getSuperclassExternalizer(c); 223 if (externalizer == null) 224 externalizer = getInterfaceExternalizer(c); 225 } 226 return externalizer; 227 } 228 229 230 private Externalizer getSuperclassExternalizer(Class c) { 231 Externalizer externalizer = null; 232 if (c != null) { 233 externalizer = (Externalizer) externalizerByClass.get(c); 234 if (externalizer == null) 235 externalizer = getExternalizer(c.getSuperclass()); 236 } 237 return externalizer; 238 } 239 240 241 private Externalizer getInterfaceExternalizer(Class c) { 242 Externalizer externalizer = null; 243 Class [] ifList = c.getInterfaces(); 244 for (int i = 0; i < ifList.length; i++) { 245 externalizer = (Externalizer) externalizerByClass.get(ifList[i]); 246 if (externalizer != null) 247 break; 248 } 249 return externalizer; 250 } 251 252 255 public Externalizer getExternalizer(String mimeType) { 256 Externalizer externalizer = null; 257 if (mimeType != null && mimeType.length() > 0) { 258 externalizer = (Externalizer) externalizerByMimeType.get(mimeType); 259 if (externalizer == null) { 260 if (mimeType.indexOf('/') >= 0) 261 externalizer = 262 getExternalizer(mimeType.substring(0, mimeType.indexOf('/'))); 263 } 264 } 265 return externalizer; 266 } 267 268 public void clear() { 269 super.clear(); 270 externalizerByClass.clear(); 271 externalizerByMimeType.clear(); 272 externalized.clear(); 273 } 274 } 275 276 277 | Popular Tags |