📄 handlermap.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: HandlerMap.java,v 1.15 2005/05/17 16:41:00 jmettraux Exp $ *///// HandlerMap.java//// jmettraux@openwfe.org//// generated with // jtmpl 1.0.04 31.10.2002 John Mettraux (jmettraux@openwfe.org)//package openwfe.org.uman.web;import javax.servlet.ServletContext;import openwfe.org.Parameters;import openwfe.org.xml.XmlUtils;/** * no comment * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Date: 2005/05/17 16:41:00 $ * <br>$Id: HandlerMap.java,v 1.15 2005/05/17 16:41:00 jmettraux Exp $ </font> * * @author jmettraux@openwfe.org */public class HandlerMap{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(HandlerMap.class.getName()); // // the singleton protected static HandlerMap theHandlerMap = null; // // CONSTANTS and definitions public final static String HANDLER_MAP_FILE_NAME = "handlerMapFileName"; public final static String HANDLER = "handler"; public final static String NAME = "name"; public final static String CLASS = "class"; public final static String HANDLES = "handles"; public final static String FIELD = "field"; // // FIELDS protected java.util.Map handlerMap = null; protected java.util.List handledList = null; // // CONSTRUCTORS public HandlerMap (ServletContext context) throws WumanException { String handlerMapFileName = context.getInitParameter(HANDLER_MAP_FILE_NAME); handlerMapFileName = context.getRealPath(handlerMapFileName); if (handlerMapFileName == null) { throw new WumanException ("Parameter '"+HANDLER_MAP_FILE_NAME+ "' is mandatory in web.xml"); } determineHandlers(handlerMapFileName); } // // METHODS public Handler getHandler (Class handledClass) throws WumanException { Object o = this.handlerMap.get(handledClass.getName()); if (o == null) { throw new WumanException ("No handler defined for class '"+handledClass.getName()+"'"); } if (o instanceof Handler) { return (Handler)o; } try { Class handlerClass = (Class)o; return (Handler)handlerClass.newInstance(); } catch (Exception e) { throw new WumanException ("Failed to instantiate handler", e); } } /** * You could feed this method with 'java.security.Principal' and * it would return all the classes extending Principal and for * which there is a handler. * Returns a list of class names (String instances) */ public java.util.List getHandledClasses (Class parentClass) { java.util.List result = new java.util.ArrayList(this.handledList.size()); java.util.Iterator it = this.handledList.iterator(); while (it.hasNext()) { Class c = (Class)it.next(); /* log.debug ("is '"+parentClass.getName()+ "' assignable from '"+c.getName()+"'"); */ if (parentClass.isAssignableFrom(c)) { //log.debug("TRUE"); result.add(c.getName()); } } java.util.Collections.sort(result); return result; } protected void determineHandlers (String handlerMapFileName) throws WumanException { this.handlerMap = new java.util.HashMap(); this.handledList = new java.util.ArrayList(); org.jdom.Element root = null; try { root = XmlUtils.extractXml(handlerMapFileName, false); } catch (Exception e) { throw new WumanException ("Failed to parse '"+handlerMapFileName+"'", e); } java.util.Iterator it = root.getChildren(HANDLER).iterator(); while (it.hasNext()) { org.jdom.Element eHandler = (org.jdom.Element)it.next(); String handlerClassName = eHandler.getAttributeValue(CLASS); String handledClassName = eHandler.getAttributeValue(HANDLES); // // handlerMap Class handlerClass = null; try { handlerClass = Class.forName(handlerClassName); } catch (Exception e) { log.warn("Cannot load handlerClass. Skipped it.", e); continue; } log.debug ("Adding mapping '"+handledClassName+ "' -> '"+handlerClassName+"'"); this.handlerMap.put(handledClassName, handlerClass); // // handledList Class handledClass = null; try { handledClass = Class.forName(handledClassName); } catch (Exception e) { log.warn("Cannot load handledClass. Skipped it.", e); continue; } //log.debug("Adding handled class '"+handledClassName+"'"); this.handledList.add(handledClass); // // fields (for DynamicHandler) if (DynamicHandler.class.isAssignableFrom(handlerClass)) { DynamicHandler dynamicHandler = null; try { dynamicHandler = (DynamicHandler)handlerClass.newInstance(); dynamicHandler.setHandledClass(handledClass); } catch (Exception e) { log.warn("Failed to instantiate dynamic handler", e); continue; } java.util.Iterator fit = eHandler.getChildren(FIELD).iterator(); while (fit.hasNext()) { dynamicHandler.addField (buildField((org.jdom.Element)fit.next())); } this.handlerMap.put(handledClassName, dynamicHandler); } } } protected Field buildField (org.jdom.Element eField) throws WumanException { String name = eField.getAttributeValue(NAME); String className = eField.getAttributeValue(CLASS); if (name == null || className == null) { throw new WumanException ("attribute '"+NAME+ "' or '"+CLASS+ "' is missing from element '"+FIELD+"'"); } try { java.util.Map initParams = Parameters.extractParameters(eField); return Field.buildField(name, className, initParams); } catch (Exception e) { throw new WumanException ("Failed to build field", e); } } // // STATIC METHODS public static HandlerMap getHandlerMap (ServletContext context) throws WumanException { if (theHandlerMap != null) return theHandlerMap; synchronized (HandlerMap.class) { theHandlerMap = new HandlerMap(context); return theHandlerMap; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -