| Line | Hits | Source |
|---|---|---|
| 1 | /* | |
| 2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
| 3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
| 4 | ||
| 5 | All rights reserved. | |
| 6 | ||
| 7 | Redistribution and use in source and binary forms, with or without | |
| 8 | modification, are permitted provided that the following conditions are met: | |
| 9 | ||
| 10 | 1. Redistributions of source code must retain the above copyright notice, this | |
| 11 | list of conditions and the following disclaimer. | |
| 12 | ||
| 13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 14 | this list of conditions and the following disclaimer in the documentation | |
| 15 | and/or other materials provided with the distribution. | |
| 16 | ||
| 17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
| 18 | may be used to endorse or promote products derived from this software without | |
| 19 | specific prior written permission. | |
| 20 | ||
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
| 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
| 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
| 28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 31 | */ | |
| 32 | ||
| 33 | package org.chwf.taglib.jutil; | |
| 34 | ||
| 35 | import javax.servlet.ServletException; | |
| 36 | import javax.servlet.http.HttpServletRequest; | |
| 37 | import javax.servlet.jsp.JspException; | |
| 38 | import javax.servlet.jsp.PageContext; | |
| 39 | ||
| 40 | import org.chwf.plugin.Logger; | |
| 41 | import org.chwf.servlet.ControllerException; | |
| 42 | import org.chwf.servlet.InvocationContext; | |
| 43 | import org.chwf.servlet.ServletUtils; | |
| 44 | import org.chwf.servlet.engine.ControllerMapper; | |
| 45 | import org.chwf.servlet.engine.URIMapper; | |
| 46 | import org.chwf.taglib.base.ObjectTagSupport; | |
| 47 | import org.chwf.taglib.base.TagException; | |
| 48 | import org.chwf.util.MiscUtils; | |
| 49 | ||
| 50 | /** | |
| 51 | * Tag handler for the <code><use></code> tag. | |
| 52 | * | |
| 53 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
| 54 | */ | |
| 55 | 8 | public class UseTag extends ObjectTagSupport { |
| 56 | ||
| 57 | /** The controller name. */ | |
| 58 | private String controller; | |
| 59 | ||
| 60 | /** | |
| 61 | * Clean up data for tag handler reuse and pooling. Should be overridden in | |
| 62 | * tag handler. Subclasses should always invoke their superclass's cleanup | |
| 63 | * method: <code>super.cleanup()</code> | |
| 64 | */ | |
| 65 | public void cleanup() { | |
| 66 | 1 | super.cleanup(); |
| 67 | 1 | this.controller = null; |
| 68 | 1 | } |
| 69 | ||
| 70 | /** | |
| 71 | * Calls superclass method. Is present here because of a bug in some servlet | |
| 72 | * engines that requires tag attibute setters to be defined in the tag class | |
| 73 | * itself. | |
| 74 | * | |
| 75 | * @param var The object name. | |
| 76 | */ | |
| 77 | public void setVar(String var) { | |
| 78 | 2 | super.setVar(var); |
| 79 | 2 | } |
| 80 | ||
| 81 | /** | |
| 82 | * The controller name. | |
| 83 | * | |
| 84 | * @param controller The controller name. | |
| 85 | */ | |
| 86 | public void setController(String controller) { | |
| 87 | 3 | this.controller = controller; |
| 88 | 3 | } |
| 89 | ||
| 90 | /** | |
| 91 | * The controller name. | |
| 92 | * | |
| 93 | * @return The controller name. | |
| 94 | */ | |
| 95 | public String getController() { | |
| 96 | 2 | return this.controller; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Calls superclass method. Is present here because of a bug in some servlet | |
| 101 | * engines that requires tag attibute setters to be defined in the tag class | |
| 102 | * itself. | |
| 103 | * | |
| 104 | * @param property The property name. | |
| 105 | * @throws JspException For errors. | |
| 106 | */ | |
| 107 | public void setProperty(String property) throws JspException { | |
| 108 | 2 | super.setProperty(property); |
| 109 | 2 | } |
| 110 | ||
| 111 | /** | |
| 112 | * Retrieve controller. | |
| 113 | * | |
| 114 | * @return The controller. | |
| 115 | * @throws ControllerException For controller failures. | |
| 116 | */ | |
| 117 | private Class getControllerClass() throws ControllerException { | |
| 118 | 1 | return URIMapper.getControllerFromShortName(this.controller); |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * Stores the retrieved bean in the specified variable. | |
| 123 | * | |
| 124 | * @return SKIP_BODY | |
| 125 | * @throws JspException For controller exceptions. | |
| 126 | */ | |
| 127 | public int doStart() throws JspException { | |
| 128 | try { | |
| 129 | 1 | Object result = |
| 130 | initBean(pageContext, getVar(), getControllerClass(), getProperty()); | |
| 131 | 1 | storeDefaultObject(result); |
| 132 | 1 | } catch (ControllerException ex) { |
| 133 | 0 | Logger.getInstance().log(UseTag.class, "Page Controller failed", ex); |
| 134 | 0 | throw new TagException(ex); |
| 135 | } catch (ServletException ex) { | |
| 136 | 0 | Throwable root = ex.getRootCause(); |
| 137 | 0 | Logger.getInstance().log(UseTag.class, "Page Controller failed", ex); |
| 138 | 0 | throw new TagException(root); |
| 139 | } | |
| 140 | 1 | return SKIP_BODY; |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * Call bean initializer. Public to facilitate testing. | |
| 145 | * | |
| 146 | * @param pageContext The page context. | |
| 147 | * @param var The variable name to store the bean. | |
| 148 | * @param controller The controller. | |
| 149 | * @param bean The bean name. | |
| 150 | * @return The bean object. | |
| 151 | * @throws ControllerException For controller exceptions. | |
| 152 | * @throws ServletException For servlet exceptions. | |
| 153 | */ | |
| 154 | public static Object initBean( | |
| 155 | PageContext pageContext, | |
| 156 | String var, | |
| 157 | Class controller, | |
| 158 | String bean) | |
| 159 | throws ControllerException, ServletException { | |
| 160 | ||
| 161 | 4 | HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); |
| 162 | 4 | ControllerMapper mapper = ControllerMapper.getMapper(controller); |
| 163 | 4 | String beanGetter = "get" + MiscUtils.capitalize(bean); |
| 164 | 4 | InvocationContext context = mapper.invoke(request, beanGetter); |
| 165 | 3 | if (context.hasError()) { |
| 166 | 0 | throw new ServletException(context.getError()); |
| 167 | } | |
| 168 | 3 | Object result = context.getResult(); |
| 169 | 3 | ServletUtils.setPageAttribute(pageContext, var, result); |
| 170 | 3 | return result; |
| 171 | } | |
| 172 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |