001: /*
002: * Copyright 2003 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package velosurf.util;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import javax.servlet.http.HttpSession;
023:
024: /**
025: * An utility class used to find a tool in the toolbox. For now, it is only implemented for session tools.
026: *
027: * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
028: */
029:
030: public class ToolFinder {
031:
032: private static String toolsMapKey = null;
033: private static int toolsLibraryVersion = 0;
034:
035: static {
036: try {
037: Class
038: .forName("org.apache.velocity.tools.view.VelocityView");
039: // tools v2.x
040: toolsLibraryVersion = 2;
041: } catch (ClassNotFoundException cnfe) {
042: // tools v1.x
043: toolsLibraryVersion = 1;
044: toolsMapKey = "org.apache.velocity.tools.view.servlet.ServletToolboxManager:session-tools";
045: }
046: }
047:
048: /**
049: * Find a session tool.
050: * @param session the http session the tool is
051: * @return the tool if found in the session, or null
052: */
053: public static <T> T findSessionTool(HttpSession session,
054: Class<T> toolClass) {
055: if (session != null) {
056: // check first in attributes if the tool registered itself
057: Object registered = session.getAttribute(toolClass
058: .getName());
059: if (registered != null
060: && toolClass
061: .isAssignableFrom(registered.getClass())) {
062: return (T) registered;
063: }
064:
065: if (toolsLibraryVersion == 1) {
066: // tools v1.x
067: Map sessionTools = (Map) session
068: .getAttribute(toolsMapKey);
069: if (sessionTools != null) {
070: for (Object t : sessionTools.values()) {
071: if (toolClass.isAssignableFrom(t.getClass())) {
072: return (T) t;
073: }
074: }
075: Logger.warn("findtool: requested tool ("
076: + toolClass.getName() + ") not found!");
077: } else {
078: Logger
079: .warn("findtool: no tools map found in session!");
080: }
081: } else {
082: // tools v2.x - TODO find a way...
083: Logger.warn("findtool: no way to find requested tool: "
084: + toolClass.getName());
085:
086: /* Object toolbox = session.getAttribute(toolsMapKey);
087: if (toolbox != null) {
088: Map<String,Object> sessionTools;
089: try {
090: sessionTools = (Map<String,Object>)_getAll.invoke(toolbox,new Object[] {new HashMap()});
091: } catch(Exception e) {
092: Logger.error("findtool: error getting tool "+toolClass.getName());
093: Logger.log(e);
094: return null;
095: }
096: if (sessionTools != null) {
097: for(Object t:sessionTools.values()) {
098: if (toolClass.isAssignableFrom(t.getClass())) {
099: return (T)t;
100: }
101: }
102: Logger.warn("findtool: requested tool ("+toolClass.getName()+") not found!");
103: } else {
104: Logger.warn("findtool: could not retrieve the map of session tools!");
105: }
106: } else {
107: Logger.warn("fintool: session toolbox not found!");
108: }
109: */
110: }
111: }
112: return null;
113: }
114: }
|