1 10 package org.mmbase.bridge.jsp.taglib.functions; 11 12 13 import java.lang.reflect.Method ; 14 import java.util.Iterator ; 15 16 import javax.servlet.jsp.JspTagException ; 17 18 import org.mmbase.bridge.NotFoundException; 19 import org.mmbase.bridge.jsp.taglib.*; 20 import org.mmbase.bridge.jsp.taglib.containers.*; 21 import org.mmbase.bridge.jsp.taglib.util.*; 22 import java.util.Map ; 23 import org.mmbase.util.functions.*; 24 import org.mmbase.util.functions.Functions; 25 import org.mmbase.util.logging.*; 26 27 41 abstract public class AbstractFunctionTag extends NodeReferrerTag { 42 43 private static final Logger log = Logging.getLoggerInstance(AbstractFunctionTag.class); 44 45 public static final String THISPAGE = "THISPAGE"; 46 47 protected Attribute container = Attribute.NULL; 48 protected Attribute name = Attribute.NULL; 49 protected Attribute parametersAttr = Attribute.NULL; 50 51 protected Attribute module = Attribute.NULL; 52 protected Attribute nodeManager = Attribute.NULL; 53 54 protected Attribute functionSet = Attribute.NULL; 55 56 protected Attribute functionClass = Attribute.NULL; 57 58 protected Attribute referids = Attribute.NULL; 59 60 public void setName(String n) throws JspTagException { 61 name = getAttribute(n); 62 } 63 64 public void setContainer(String c) throws JspTagException { 65 container = getAttribute(c); 66 } 67 68 public void setParameters(String p) throws JspTagException { 69 parametersAttr = getAttribute(p); 70 } 71 72 public void setModule(String m) throws JspTagException { 73 module = getAttribute(m); 74 } 75 76 public void setNodemanager(String n) throws JspTagException { 77 nodeManager = getAttribute(n); 78 } 79 80 public void setSet(String s) throws JspTagException { 81 functionSet = getAttribute(s); 82 } 83 84 public void setClassname(String c) throws JspTagException { 85 functionClass = getAttribute(c); 86 } 87 88 public void setReferids(String r) throws JspTagException { 89 referids = getAttribute(r); 90 } 91 92 95 protected final Function getFunction(String functionName) throws JspTagException { 96 Function function; 97 if (nodeManager != Attribute.NULL) { 99 if (module != Attribute.NULL || functionSet != Attribute.NULL || functionClass != Attribute.NULL || parentNodeId != Attribute.NULL) { 100 throw new TaglibException("You can only use one of 'nodemanager', 'module', 'set', 'class' or 'node' on a function tag"); 101 } 102 return getCloudVar().getNodeManager(nodeManager.getString(this)).getFunction(functionName); 103 } else if (module != Attribute.NULL) { 104 if (nodeManager != Attribute.NULL || functionSet != Attribute.NULL || functionClass != Attribute.NULL || parentNodeId != Attribute.NULL) { 105 throw new TaglibException("You can only use one of 'nodemanager', 'module', 'set', 'class' or 'node' on a function tag"); 106 } 107 return FunctionFactory.getFunction(getCloudContext().getModule(module.getString(this)), functionName); } else if (functionSet != Attribute.NULL) { 109 if (nodeManager != Attribute.NULL || module != Attribute.NULL || parentNodeId != Attribute.NULL || functionClass != Attribute.NULL) { 110 throw new TaglibException("You can only use one of 'nodemanager', 'module', 'set', 'class' or 'node' on a function tag"); 111 } 112 String set = functionSet.getString(this); 113 if (set.equals(THISPAGE)) { 114 Class jspClass = pageContext.getPage().getClass(); 115 Method method = Functions.getMethodFromClass(jspClass, functionName); 116 return FunctionFactory.getFunction(method, functionName); } else { 118 CloudProvider cp = findCloudProvider(false); 119 if (cp != null) { 120 return FunctionFactory.getFunction(cp.getCloudVar(), set, functionName); 121 } else { 122 return FunctionFactory.getFunction(set, functionName); 123 } 124 } 125 } else if (functionClass != Attribute.NULL) { 126 if (nodeManager != Attribute.NULL || module != Attribute.NULL || parentNodeId != Attribute.NULL || functionSet != Attribute.NULL) { 127 throw new TaglibException("You can only use one of 'nodemanager', 'module', 'set', 'class' or 'node' on a function tag"); 128 } 129 String className = functionClass.getString(this); 130 try { 131 Class clazz; 132 if (className.indexOf(".") == -1) { 133 Class jspClass = pageContext.getPage().getClass(); 134 clazz = BeanFunction.getClass(jspClass, className); 135 } else { 136 clazz = Class.forName(className); 137 } 138 return FunctionFactory.getFunction(clazz, functionName); } catch (Exception e) { 140 throw new TaglibException(e); 143 } 144 145 } else { 147 log.debug("Node-referrer?"); 148 if (container != Attribute.NULL || "".equals(parentNodeId.getValue(this)) || functionName == null) { log.debug("explicitely not"); 150 FunctionContainerTag functionContainer = (FunctionContainerTag) findParentTag(FunctionContainer.class, (String ) container.getValue(this), false); 151 if (functionContainer != null) { 152 function = functionContainer.getFunction(functionName); 153 return function; 154 } else { 155 } 157 } 158 FunctionContainerOrNodeProvider functionOrNode; 160 log.debug("Checking for 'closer' node provider"); 161 if (parentNodeId != Attribute.NULL) { 162 log.debug("explicitely specified node"); 163 functionOrNode = findNodeProvider(); 164 } else { 165 functionOrNode = (FunctionContainerOrNodeProvider) findParentTag(FunctionContainerOrNodeProvider.class, null, false); 166 } 167 if (log.isDebugEnabled()) { 168 log.debug("Found functionOrNode " + functionOrNode); 169 } 170 if (functionOrNode != null) { 171 if (functionOrNode instanceof NodeProvider) { log.debug("using node-function!"); 173 return ((NodeProvider) functionOrNode).getNodeVar().getFunction(functionName); 174 } else { return ((FunctionContainerTag) functionOrNode).getFunction(functionName); 176 } 177 } else { 178 return null; 179 } 180 } 181 182 } 183 184 protected final Function getFunction() throws JspTagException { 185 String functionName = name.getString(this); 186 FunctionContainerTag functionContainer = (FunctionContainerTag) findParentTag(FunctionContainer.class, (String ) container.getValue(this), false); 187 if(log.isDebugEnabled()) { 188 log.debug("Getting function value. Container " + functionContainer); 189 } 190 191 Function function; 192 if ("".equals(functionName)) { if (functionContainer == null) throw new JspTagException ("No function name given, and no function container tag found either"); 194 function = functionContainer.getFunction(functionContainer.getName()); 195 if (function == null) { 196 throw new JspTagException ("Could not determine the name of the function to be executed"); 197 } 198 } else { 199 log.debug("Trying self for function " + functionName); 200 function = getFunction(functionName); 202 if (function == null) { 203 throw new NotFoundException("Could not find function with the name '" + functionName + "'"); 204 } 205 } 206 return function; 207 208 } 209 210 protected final Object getFunctionValue() throws JspTagException { 211 return getFunctionValue(true); 212 } 213 214 215 protected final Object getFunctionValue(boolean register) throws JspTagException { 216 String functionName = name.getString(this); 217 Object value; 218 if (getReferid() != null) { 219 if (! "".equals(functionName)) { 220 throw new TaglibException("Cannot specify both 'referid' and 'name' attributes on a function tag"); 221 } 222 value = getObject(getReferid()); 223 } else { 224 Function function = null; 225 try { 226 function = getFunction(); 227 } catch (RuntimeException e) { 228 if ("log".equals(pageContext.getServletContext().getInitParameter("mmbase.taglib.function.nonExistance"))) { 229 log.error("Could not find function with the name '" + functionName + "'"); 230 return null; 231 } 232 throw e; 233 } 234 235 if (log.isDebugEnabled()) { 236 log.debug("Function to use " + function); 237 } 238 Parameters params; 239 try { 240 params = function.createParameters(); 241 } catch (IllegalStateException ise) { 242 log.warn("Undefined parameters for function '" + functionName + "', trying without definition."); 243 params = new AutodefiningParameters(); 244 } 245 params.setAutoCasting(true); 246 247 FunctionContainerTag functionContainer = (FunctionContainerTag) findParentTag(FunctionContainer.class, (String ) container.getValue(this), false); 248 try { 249 if (functionContainer != null) { 250 Iterator i = functionContainer.getParameters().iterator(); 251 while (i.hasNext()) { 252 Map.Entry entry = (Map.Entry ) i.next(); 253 params.set((String ) entry.getKey(), entry.getValue()); 254 } 255 } 256 if (referids != Attribute.NULL) { 257 params.setAll(Referids.getReferids(referids, this)); 258 } 259 260 fillStandardParameters(params); 261 262 if (log.isDebugEnabled()) { 263 log.debug("using parameters " + params + " on " + function.getClass() + " " + function); 264 } 265 266 params.checkRequiredParameters(); 267 } catch (Throwable e) { 268 throw new IllegalArgumentException ("function " + functionName + " " + e.getMessage()); 269 } 270 271 value = function.getFunctionValue(params); 272 273 } 274 if (register && getId() != null) { 275 getContextProvider().getContextContainer().register(getId(), value); 276 } 277 return value; 278 } 279 280 } 281 | Popular Tags |