You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by ro...@yahoo.com on 2002/05/23 22:17:48 UTC
MethodExceptionEventHandler
hi,
i am having some problem with using the MethodExceptionEventHandler.
i do create a event-cartridge, add a handler-instance and
attach that to the context, bus somehow the handler never gets
invoked.
i extracted a simple example from my code just to see
if i can get the event-handling stuff running.
this barebone example works quite well.
so it seems i do create some side-effect in my real program
i dont know about.
the following code is some extract from mine. with all the
interesting stuff in it. hopefully somebody can see what i
have been doing wrong.
>>>MethodErrorHandler.java
public class MethodErrorHandler implements MethodExceptionEventHandler
{
public Object methodException(Class _cls, String _message , Exception
_ex) throws Exception
{
return "ERROR!";
//return _ex.getMessage();
}
}
>>>
>>>Util.java
public class Util
{
//this method crashes cause there is a NPE.
//so it should invoke the err-handler
public String getParameters(Element _paramdescs)
{
try
{
String result = "";
int list_size = _paramdescs.getChildren().size();
int counter = 0;
Element paramdesc = null;
for (Iterator params = _paramdescs.getChildren().iterator();
params.hasNext();
(paramdesc = (Element) params.next()))
{
result = result + getParameter(paramdesc);
if (counter < list_size - 1)
{
result = result + ", ";
}
counter++;
}
return result;
}
catch (Exception ex)
{
return ex.getMessage();
}
}
public String getParameter(Element _paramdesc)
{
String result = "";
String name = _paramdesc.getAttributeValue("Name");
result = getType(_paramdesc.getChild("TypeDescription"));
result = result + " " + name;
return result;
}
}
>>>the method of the main-programm where i create the context, handler,
cartridge
>>>and start the merging
private String emitClass(Element _type_info, Element _lib_attrs, String
_name_space)
{
try
{
VelocityContext ctx = new VelocityContext();
ctx.put(NAME_SPACE, _name_space);
ctx.put(TYPE_INFO, _type_info);
ctx.put(LIB_ATTRS, _lib_attrs);
ctx.put("util", new Util());
MethodErrorHandler handler = new MethodErrorHandler();
EventCartridge ec = new EventCartridge();
ec.addEventHandler(handler);
ec.attachToContext(ctx);
StringWriter sw = new StringWriter();
mTemplate.merge(ctx, sw);
return sw.toString();
}
catch (Exception ex)
{
ex.printStackTrace();
}
return "FAILURE";
}
>>>
>>>the template
#macro( getMethod $FuncDesc $Result)
#set ($TypeDescription = $FuncDesc.getChild("TypeDescription"))
#set ($type = $util.getType($TypeDescription))
#set ($paramlist = $FuncDesc.getChild("ParamDescList"))
#if ($paramlist)
$util.getParameters($paramlist)
#end
#set ($Result = "public $type
${FuncDesc.getAttributeValue('Name')}($params)")
#end
>>>
>>> the output is
$util.getParameters($paramlist)
>>> the log is
2002-05-23 21:58:33,143 -
org.apache.velocity.runtime.exception.ReferenceException:
reference : template = run_interface.vm [line 5,column 1] :
$util.getParameters($paramlist) is not a valid reference.
maybe somebody can see what i am obviously missing
ciao robertj
------------------------------------------------------------
Robert Kuzelj
Gaissacherstrasse 7 email: robert_kuzelj@yahoo.com
81371 Muenchen tel: 0177/5302230
the trinity of desirables of (software) architecture:
Firmitas, Utilitas, Venustas (marcus vitruvius 20 BC)
strength, utility, beauty
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>