KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > externalizer > ExternalizeManager


1 /*
2  * $Id: ExternalizeManager.java,v 1.4 2004/12/01 07:54:08 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.externalizer;
15
16 import java.util.Collection JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 /**
22  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
23  * @version $Revision: 1.4 $
24  */

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 JavaDoc externalizerByClass = new HashMap JavaDoc();
39
40     protected final HashMap JavaDoc externalizerByMimeType = new HashMap JavaDoc();
41
42     protected final Map JavaDoc externalized = Collections.synchronizedMap(new HashMap JavaDoc());
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 JavaDoc 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 JavaDoc getExternalizedObject(String JavaDoc identifier) {
72         ExternalizedResource info = getExternalizedResource(identifier);
73
74         if (info != null)
75             return info.getObject();
76
77         return null;
78     }
79
80     /**
81      * stripts the identifier from attachments to the external names.
82      */

83     private String JavaDoc stripIdentifier(String JavaDoc 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 JavaDoc identifier) {
103         identifier = stripIdentifier(identifier);
104         if (identifier == null) return null;
105
106         // SystemExternalizeManager hat Minus as prefix.
107
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 JavaDoc identifier) {
116         identifier = stripIdentifier(identifier);
117         if (identifier == null) return;
118         Object JavaDoc externalizedResource = externalized.remove(identifier);
119         reverseExternalized.remove(externalizedResource);
120     }
121
122
123     public String JavaDoc externalize(Object JavaDoc obj) {
124         return externalize(obj, SESSION);
125     }
126
127
128     public String JavaDoc externalize(Object JavaDoc obj, Collection JavaDoc headers) {
129         return externalize(obj, headers, SESSION);
130     }
131
132
133     public String JavaDoc externalize(Object JavaDoc obj, int flags) {
134         return externalize(obj, (Collection JavaDoc) null, flags);
135     }
136
137
138     public String JavaDoc externalize(Object JavaDoc obj, Collection JavaDoc headers, int flags) {
139         if (obj == null)
140             throw new IllegalArgumentException JavaDoc("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 JavaDoc externalize(Object JavaDoc obj, String JavaDoc mimeType) {
154         return externalize(obj, mimeType, SESSION);
155     }
156
157
158     public String JavaDoc externalize(Object JavaDoc obj, String JavaDoc mimeType, Collection JavaDoc headers) {
159         return externalize(obj, mimeType, headers, SESSION);
160     }
161
162
163     public String JavaDoc externalize(Object JavaDoc obj, String JavaDoc mimeType, int flags) {
164         return externalize(obj, mimeType, null, flags);
165     }
166
167
168     public String JavaDoc externalize(Object JavaDoc obj, String JavaDoc mimeType,
169                               Collection JavaDoc headers, int flags) {
170         if (obj == null)
171             throw new IllegalStateException JavaDoc("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     /**
182      * Adds an externalizer. If an externalizer is already
183      * registered for a class or a mime type, it will be replaced.
184      */

185     public void addExternalizer(Externalizer externalizer) {
186         if (externalizer != null) {
187             Class JavaDoc 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 JavaDoc 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     /**
204      * Adds an Externalizer
205      */

206     public void addExternalizer(Externalizer externalizer, String JavaDoc mimeType) {
207         if (externalizer != null && mimeType != null)
208             externalizerByMimeType.put(mimeType, externalizer);
209     }
210
211     /**
212      * Returns an object externalizer for a class. If one could not be found,
213      * it goes down the inheritance tree and looks for an object externalizer
214      * for the super classes. If one still could not be found, it goes
215      * through the list of interfaces of the class and checks for object
216      * externalizers for every interface. If this also doesn't return an
217      * object externalizer, null is returned.
218      */

219     public Externalizer getExternalizer(Class JavaDoc 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 JavaDoc 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 JavaDoc c) {
242         Externalizer externalizer = null;
243         Class JavaDoc[] 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     /**
253      * returns an object externalizer for a mime type
254      */

255     public Externalizer getExternalizer(String JavaDoc 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