001: /*
002: * Copyright (c) 1998-2004 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.widget;
030:
031: import com.caucho.util.L10N;
032:
033: import java.io.IOException;
034: import java.lang.reflect.InvocationTargetException;
035: import java.lang.reflect.Method;
036: import java.util.Collections;
037: import java.util.HashMap;
038: import java.util.Map;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: class RendererCache {
043: private static L10N L = new L10N(RendererCache.class);
044:
045: static protected final Logger log = Logger
046: .getLogger(RendererCache.class.getName());
047:
048: static final WidgetRenderer<Object> NULL_RENDERER = new WidgetRenderer<Object>() {
049: public void render(WidgetConnection connection,
050: Object widgetState) throws WidgetException, IOException {
051: }
052: };
053:
054: private Map<String, WidgetRenderer> _rendererCache = Collections
055: .synchronizedMap(new HashMap<String, WidgetRenderer>());
056:
057: public <S extends WidgetState> WidgetRenderer<S> getRenderer(
058: WidgetConnection connection, Widget<S> widget, S widgetState) {
059: String contentType = connection.getContentType();
060: WidgetMode widgetMode = widgetState.getWidgetMode();
061:
062: String cacheKey = contentType + Character.MAX_VALUE
063: + widgetMode.toString();
064:
065: WidgetRenderer renderer = _rendererCache.get(cacheKey);
066:
067: if (renderer != null)
068: return (WidgetRenderer<S>) renderer;
069:
070: if (renderer == NULL_RENDERER) {
071: if (log.isLoggable(Level.FINEST))
072: log
073: .finest(L
074: .l(
075: "no WidgetRenderer for contentType {0} for widget type {1}",
076: contentType, widget.getClass()
077: .getName()));
078:
079: return null;
080: }
081:
082: String methodName = createMethodName(contentType);
083:
084: Class widgetClass = widget.getClass();
085: Class widgetStateClass = widgetState.getClass();
086:
087: // XXX: look for a WidgetRenderer added with addRenderer(WidgetRenderer)
088:
089: // XXX: look for a globally registered WidgetRenderer in a WidgetRendererManager
090:
091: // XXX: this won't work right if getMethod( "foo", { Baz.class } )
092: // matches a method foo( Foo ) where Baz extends Bar
093: // the createRenderer() methods may need to change
094:
095: while (renderer == null && widgetStateClass != null) {
096:
097: // look for render{ContentType}(WidgetConnection, widgetStateClass )
098:
099: renderer = createMethodNameRenderer(widget, widgetClass,
100: widgetStateClass, methodName);
101:
102: // look for render(String, WidgetConnection, widgetStateClass )
103:
104: if (renderer == null)
105: renderer = createContentTypeRenderer(widget,
106: widgetClass, widgetStateClass, contentType);
107:
108: // try with the superclass of widgetStateClass
109:
110: widgetStateClass = widgetStateClass.getSuperclass();
111: }
112:
113: if (renderer == null) {
114: if (log.isLoggable(Level.FINE))
115: log
116: .fine(L
117: .l(
118: "no WidgetRenderer for contentType {0} for widget type {1}",
119: contentType, widget.getClass()
120: .getName()));
121:
122: renderer = NULL_RENDERER;
123: }
124:
125: _rendererCache.put(cacheKey, renderer);
126:
127: return (WidgetRenderer<S>) renderer;
128: }
129:
130: /** look for render{ContentType}( WidgetConnection, widgetClass ) */
131: private <S extends WidgetState> WidgetRenderer<S> createMethodNameRenderer(
132: final Widget<S> widget, Class widgetClass,
133: Class widgetStateClass, String methodName) {
134: Class[] methodArgs = new Class[] { WidgetConnection.class,
135: widgetStateClass };
136:
137: Method findMethod = null;
138:
139: try {
140: findMethod = widgetClass.getMethod(methodName, methodArgs);
141: } catch (NoSuchMethodException ex) {
142: }
143:
144: if (findMethod != null) {
145: if (log.isLoggable(Level.FINEST))
146: log
147: .finest(L
148: .l(
149: "WidgetRenderer for widget type {0} is method {1}()",
150: widget.getClass().getName(),
151: methodName));
152:
153: final Method method = findMethod;
154:
155: return new WidgetRenderer<S>() {
156: public void render(WidgetConnection connection,
157: S widgetState) throws WidgetException,
158: IOException {
159: try {
160: method.invoke(widget, new Object[] {
161: connection, widgetState });
162: } catch (IllegalAccessException ex) {
163: throw new WidgetException(ex);
164: } catch (InvocationTargetException ex) {
165: throw new WidgetException(ex);
166: }
167: }
168: };
169: } else
170: return null;
171: }
172:
173: private <S extends WidgetState> WidgetRenderer createContentTypeRenderer(
174: final Widget<S> widget, Class widgetClass,
175: Class widgetStateClass, final String contentType) {
176: Class[] methodArgs = new Class[] { String.class,
177: WidgetConnection.class, widgetStateClass };
178:
179: Method findMethod = null;
180:
181: try {
182: findMethod = widgetClass.getMethod("render", methodArgs);
183: } catch (NoSuchMethodException ex) {
184: }
185:
186: if (findMethod != null) {
187: if (log.isLoggable(Level.FINEST))
188: log
189: .finest(L
190: .l(
191: "WidgetRenderer for contentType {0} for widget type {1} is method {2}()",
192: contentType, widget.getClass()
193: .getName(), "render"));
194:
195: final Method method = findMethod;
196:
197: return new WidgetRenderer<S>() {
198: public void render(WidgetConnection connection,
199: S widgetState) throws WidgetException,
200: IOException {
201: try {
202: method.invoke(widget, new Object[] {
203: contentType, connection, widgetState });
204: } catch (IllegalAccessException ex) {
205: throw new WidgetException(ex);
206: } catch (InvocationTargetException ex) {
207: throw new WidgetException(ex);
208: }
209: }
210: };
211: } else
212: return null;
213: }
214:
215: private String createMethodName(String contentType) {
216: StringBuffer methodName = new StringBuffer();
217: methodName.append("render");
218:
219: boolean toUpper = true;
220:
221: for (int i = 0; i < contentType.length(); i++) {
222: char ch = contentType.charAt(i);
223:
224: boolean isIdentifier = i == 0 ? Character
225: .isJavaIdentifierStart(ch) : Character
226: .isJavaIdentifierPart(ch);
227:
228: if (!isIdentifier) {
229: if (toUpper)
230: ch = '_';
231: else {
232: toUpper = true;
233: continue;
234: }
235: }
236:
237: if (toUpper)
238: ch = Character.toUpperCase(ch);
239:
240: methodName.append(ch);
241:
242: toUpper = false;
243:
244: }
245:
246: return methodName.toString();
247: }
248: }
|