001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.externalizer;
014:
015: import java.util.Collection;
016: import java.util.Collections;
017: import java.util.HashMap;
018: import java.util.Map;
019:
020: /**
021: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
022: */
023: public class ExternalizeManager extends AbstractExternalizeManager {
024:
025: private static final Externalizer[] DEFAULT_EXTERNALIZERS = {
026: ImageExternalizer.SHARED_PNG_INSTANCE,
027: ImageExternalizer.SHARED_GIF_INSTANCE,
028: ImageExternalizer.SHARED_JPG_INSTANCE,
029: ImageIconExternalizer.SHARED_PNG_INSTANCE,
030: ImageIconExternalizer.SHARED_GIF_INSTANCE,
031: ImageIconExternalizer.SHARED_JPG_INSTANCE,
032: StaticResourceExternalizer.SHARED_INSTANCE,
033: StringResourceExternalizer.SHARED_INSTANCE,
034: DynamicResourceExternalizer.SHARED_INSTANCE,
035: ResourceExternalizer.SHARED_INSTANCE, };
036:
037: protected final HashMap<Class, Externalizer> externalizerByClass = new HashMap<Class, Externalizer>();
038:
039: protected final HashMap<String, Externalizer> externalizerByMimeType = new HashMap<String, Externalizer>();
040:
041: protected final Map<String, ExternalizedResource> externalized = Collections
042: .synchronizedMap(new HashMap<String, ExternalizedResource>());
043:
044: public ExternalizeManager() {
045: this (true);
046: }
047:
048: public ExternalizeManager(boolean initWithDefaultExternalizers) {
049: if (initWithDefaultExternalizers) {
050: addDefaultExternalizers();
051: }
052: }
053:
054: public final void addDefaultExternalizers() {
055: for (Externalizer aDEFAULT_EXTERNALIZERS : DEFAULT_EXTERNALIZERS) {
056: addExternalizer(aDEFAULT_EXTERNALIZERS);
057: }
058: }
059:
060: protected final void storeExternalizedResource(String identifier,
061: ExternalizedResource extInfo) {
062: if (LOG.isDebugEnabled()) {
063: LOG.debug("store identifier " + identifier + " "
064: + extInfo.getObject().getClass());
065: LOG.debug("flags " + extInfo.getFlags());
066: }
067: externalized.put(identifier, extInfo);
068: }
069:
070: public final Object getExternalizedObject(String identifier) {
071: ExternalizedResource info = getExternalizedResource(identifier);
072:
073: if (info != null)
074: return info.getObject();
075:
076: return null;
077: }
078:
079: /**
080: * stripts the identifier from attachments to the external names.
081: */
082: private String stripIdentifier(String identifier) {
083: if (identifier == null || identifier.length() < 1)
084: return null;
085:
086: int pos = identifier.indexOf(org.wings.SConstants.UID_DIVIDER);
087: if (pos > -1) {
088: identifier = identifier.substring(pos + 1);
089: }
090: if (identifier.length() < 1) {
091: return null;
092: }
093: return identifier;
094: }
095:
096: public final ExternalizedResource getExternalizedResource(
097: String identifier) {
098: identifier = stripIdentifier(identifier);
099: if (identifier == null)
100: return null;
101:
102: // SystemExternalizeManager hat Minus as prefix.
103: if (identifier.charAt(0) == '-') {
104: return SystemExternalizeManager.getSharedInstance()
105: .getExternalizedResource(identifier);
106: }
107:
108: return externalized.get(identifier);
109: }
110:
111: public final void removeExternalizedResource(String identifier) {
112: identifier = stripIdentifier(identifier);
113: if (identifier == null)
114: return;
115: Object externalizedResource = externalized.remove(identifier);
116: reverseExternalized.remove(externalizedResource);
117: }
118:
119: public String externalize(Object obj) {
120: return externalize(obj, SESSION);
121: }
122:
123: public String externalize(Object obj, Collection headers) {
124: return externalize(obj, headers, SESSION);
125: }
126:
127: public String externalize(Object obj, int flags) {
128: return externalize(obj, (Collection) null, flags);
129: }
130:
131: public String externalize(Object obj, Collection headers, int flags) {
132: if (obj == null)
133: throw new IllegalArgumentException(
134: "object must not be null");
135:
136: Externalizer externalizer = getExternalizer(obj.getClass());
137: if (externalizer == null) {
138: LOG.warn("could not find externalizer for "
139: + obj.getClass().getName());
140: return NOT_FOUND_IDENTIFIER;
141: }
142:
143: return externalize(obj, externalizer, headers, flags);
144: }
145:
146: public String externalize(Object obj, String mimeType) {
147: return externalize(obj, mimeType, SESSION);
148: }
149:
150: public String externalize(Object obj, String mimeType,
151: Collection headers) {
152: return externalize(obj, mimeType, headers, SESSION);
153: }
154:
155: public String externalize(Object obj, String mimeType, int flags) {
156: return externalize(obj, mimeType, null, flags);
157: }
158:
159: public String externalize(Object obj, String mimeType,
160: Collection headers, int flags) {
161: if (obj == null)
162: throw new IllegalStateException("no externalizer");
163:
164: Externalizer externalizer = mimeType != null ? getExternalizer(mimeType)
165: : null;
166: if (externalizer == null) {
167: return externalize(obj, headers, flags);
168: } else {
169: return externalize(obj, externalizer, headers, flags);
170: }
171: }
172:
173: /**
174: * Adds an externalizer. If an externalizer is already
175: * registered for a class or a mime type, it will be replaced.
176: */
177: public void addExternalizer(Externalizer externalizer) {
178: if (externalizer != null) {
179: Class c[] = externalizer.getSupportedClasses();
180: if (c != null)
181: for (Class aC : c) {
182: if (aC != null) {
183: externalizerByClass.put(aC, externalizer);
184: }
185: }
186:
187: String mimeTypes[] = externalizer.getSupportedMimeTypes();
188: if (mimeTypes != null) {
189: for (int i = 0; i < mimeTypes.length; i++) {
190: if (mimeTypes[i] != null
191: && mimeTypes[i].trim().length() > 0) {
192: externalizerByMimeType.put(mimeTypes[i].trim()
193: .toLowerCase(), externalizer);
194: }
195: }
196: }
197: }
198: }
199:
200: /**
201: * Adds an Externalizer
202: */
203: public void addExternalizer(Externalizer externalizer,
204: String mimeType) {
205: if (externalizer != null && mimeType != null)
206: externalizerByMimeType.put(mimeType, externalizer);
207: }
208:
209: /**
210: * Returns an object externalizer for a class. If one could not be found,
211: * it goes down the inheritance tree and looks for an object externalizer
212: * for the super classes. If one still could not be found, it goes
213: * through the list of interfaces of the class and checks for object
214: * externalizers for every interface. If this also doesn't return an
215: * object externalizer, null is returned.
216: */
217: public Externalizer getExternalizer(Class c) {
218: Externalizer externalizer = null;
219: if (c != null) {
220: externalizer = getSuperclassExternalizer(c);
221: if (externalizer == null)
222: externalizer = getInterfaceExternalizer(c);
223: }
224: return externalizer;
225: }
226:
227: private Externalizer getSuperclassExternalizer(Class c) {
228: Externalizer externalizer = null;
229: if (c != null) {
230: externalizer = externalizerByClass.get(c);
231: if (externalizer == null)
232: externalizer = getExternalizer(c.getSuperclass());
233: }
234: return externalizer;
235: }
236:
237: private Externalizer getInterfaceExternalizer(Class c) {
238: Externalizer externalizer = null;
239: Class[] ifList = c.getInterfaces();
240: for (Class anIfList : ifList) {
241: externalizer = externalizerByClass.get(anIfList);
242: if (externalizer != null) {
243: break;
244: }
245: }
246: return externalizer;
247: }
248:
249: /**
250: * returns an object externalizer for a mime type
251: */
252: public Externalizer getExternalizer(String mimeType) {
253: Externalizer externalizer = null;
254: if (mimeType != null && mimeType.length() > 0) {
255: externalizer = externalizerByMimeType.get(mimeType);
256: if (externalizer == null) {
257: if (mimeType.indexOf('/') >= 0)
258: externalizer = getExternalizer(mimeType.substring(
259: 0, mimeType.indexOf('/')));
260: }
261: }
262: return externalizer;
263: }
264:
265: public void clear() {
266: super.clear();
267: externalizerByClass.clear();
268: externalizerByMimeType.clear();
269: externalized.clear();
270: }
271: }
|