Re: Usage of ToolManager
Mike Kienenberger <[email protected]> Thu, 3 Feb 2022 09:20:39 -0500
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <CAM1yOjZLL1Avv981Hf_2VA1YxgN8hUb0qCPwPxksT-i0TY8Rdg@mail.gmail.com> |
--000000000000a198f205d71dd769
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
I haven't used Velocity 2 or ToolManager 3, but the basics of Velocity are
very simple.
You create a VelocityContext (which is effectively a Java.util.Map).
You "put" objects into it with String keys.
You reference these string keys in your template.
In the setup code:
VelocityContext context =3D ...
context.put("anything", new Object());
in the template:
$anything
VelocityContexts differ from Maps in that they can also wrap one or more
other VelocityContexts to which they will delegate lookups.
Generally it works along the lines of
VelocityContext myAppContext is created with a delegate of ToolContext.
So it first tries to find your key in myAppContext. If not found, it will
try to find your key in ToolContext.
So a ToolManager context for Velocity 1.x is just another VelocityContext.
Glancing at your code, I see that you first created a myAppContext.
Then you create a ToolManager context (with the same name?)
But I don't see that you've linked these together.
Normally, you'd do something like this to create your myAppContext:
ToolboxContext toolCtx =3D mgr.getToolboxContext(null);
VelocityContext myAppContext =3D new VelocityContext(toolCtx);
Again, though, the only reason to use a ToolManager is to make it easier to
configure a context.
Either because the ToolManager supports grabbing the objects from file (ie,
XMLToolboxManager.class)
or because it grabs them from a hardcoded list or reflectively-generated
list. And then it "put"s these object into a VelocityContext that it
makes available to you via getToolboxContext();
You can certainly start by just dumping objects directly into your context.
context.put("esc", new EscapeTool());
context.put("request", httpRequest);
context.put("context", context);
Or creating your own ToolManager:
class MyToolManager implements ToolManager {
ToolboxContext getToolboxContext(Object initData) {
VelocityContext context =3D new VelocityContext();
context.put("esc", new EscapeTool());
context.put("request", httpRequest);
context.put("context", context);
return context;
}
void addTool(ToolInfo info) {
// you can get more fancy by implementing this to fetch tool
config rather than hard coding classes above
};
}
Because Velocity is such a simple idea, you can review the Velocity source
code if you want to see what's going on internally with Velocity contexts
and ToolManagers..
On Thu, Feb 3, 2022 at 5:27 AM Stefan Gro=C3=9Fhauser <
[email protected]> wrote:
> Hello,
>
>
> I just want to kindly ping my question below one last time.
> Unfortunately I have not yet found the way the Velocity Tools are intende=
d
> to be used in an Application.
>
>
> Is somebody able to give me a hint?
>
>
>
> Thank you so much!
>
>
>
> Stefan
>
>
>
>
> Hello everone,
>
>
> I am sorry that I have yet another question.
>
>
> I would like to use some of the Standard Tools
> https://velocity.apache.org/tools/3.1/tools-summary.html .
> (By the way, it has not become clear to me, what the relationship between
> Standard Tools and Generic Tools is.)
>
>
> This example
> https://velocity.apache.org/tools/3.1/standalone.html
> and this one
>
> https://github.com/apache/maven-doxia-sitetools/commit/0abee78da740501fc0=
584f65e06c1007229f58d6
> suggest to me that all I have to do to access Tools like $esc, $display o=
r
> $context is to replace
>
>
>
> - VelocityContext context =3D new VelocityContext();
> + VelocityEngine ve =3D new VelocityEngine();
>
> + ToolManager manager =3D new ToolManager(true, true);
> + manager.setVelocityEngine(ve);
> + logger.debug("hasApplicationTools: {}",
> manager.hasApplicationTools());
> + logger.debug("hasRequestTools: {}", manager.hasRequestTools());
>
> + Context context =3D manager.createContext();
>
>
> But using this context, the hasApplicationTools() and hasRequestTools()
> functions return false and e.g. there is no $context available to the
> template:
>
>
>
> 06:26:53.292 [main] DEBUG o.apache.velocity.tools.ToolManager -
> VelocityEngine instance was changed to
> org.apache.velocity.app.VelocityEngine@70a9f84e
> 06:26:53.293 [main] DEBUG d.h.etiketten.VelocityTemplateEngine -
> hasApplicationTools: false
> 06:26:53.294 [main] DEBUG d.h.etiketten.VelocityTemplateEngine -
> hasRequestTools: false
> 06:26:53.297 [main] DEBUG org.apache.velocity - Initializing Velocity,
> Calling init()...
> 06:26:53.298 [main] DEBUG org.apache.velocity - Starting Apache Velocity
> v2.3
> 06:26:53.302 [main] DEBUG org.apache.velocity - Default Properties
> resource: org/apache/velocity/runtime/defaults/velocity.properties
> 06:26:53.333 [main] DEBUG VelocityEngine - ResourceLoader instantiated:
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:26:53.340 [main] DEBUG VelocityEngine - initialized (class
> org.apache.velocity.runtime.resource.ResourceCacheImpl) with class
> java.util.Collections$SynchronizedMap cache map.
> 06:26:53.347 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Stop
> 06:26:53.351 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Define
> 06:26:53.354 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Break
> 06:26:53.357 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Evaluate
> 06:26:53.360 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Macro
> 06:26:53.366 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Parse
> 06:26:53.370 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Include
> 06:26:53.374 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Foreach
> 06:26:53.422 [main] DEBUG VelocityEngine.parser - Created '20' parsers.
> 06:26:53.461 [main] DEBUG VelocityEngine.macro -
> "velocimacro.library.path" is not set. Trying default library:
> velocimacros.vtl
> 06:26:53.463 [main] DEBUG VelocityEngine.loader.class - Could not load
> resource 'velocimacros.vtl' from ResourceLoader
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:26:53.465 [main] DEBUG VelocityEngine.macro - Default library
> velocimacros.vtl not found. Trying old default library: VM_global_library=
.vm
> 06:26:53.467 [main] DEBUG VelocityEngine.loader.class - Could not load
> resource 'VM_global_library.vm' from ResourceLoader
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:26:53.468 [main] DEBUG VelocityEngine.macro - Old default library
> VM_global_library.vm not found.
> 06:26:53.469 [main] DEBUG VelocityEngine.macro - allowInline =3D true: VM=
s
> can be defined inline in templates
> 06:26:53.470 [main] DEBUG VelocityEngine.macro - allowInlineToOverride =
=3D
> false: VMs defined inline may NOT replace previous VM definitions
> 06:26:53.471 [main] DEBUG VelocityEngine.macro - allowInlineLocal =3D fal=
se:
> VMs defined inline will be global in scope if allowed.
> 06:26:53.472 [main] DEBUG VelocityEngine.macro - autoload off: VM system
> will not automatically reload global library macros
> 06:26:53.528 [main] DEBUG VelocityEngine.loader - ResourceManager: found
> velocity/reflection.vm with loader
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:26:53.531 [main] ERROR VelocityEngine.rendering - Variable $context ha=
s
> not been set at velocity/reflection.vm[line 9, column 19]
> 06:26:53.538 [main] ERROR d.h.etiketten.VelocityTemplateEngine - somethin=
g
> invoked in the template threw an exception
> org.apache.velocity.exception.MethodInvocationException: Variable $contex=
t
> has not been set at velocity/reflection.vm[line 9, column 19]
> at
> org.apache.velocity.runtime.parser.node.ASTReference.getRootVariableValue=
(ASTReference.java:1160)
> at
> org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference=
.java:309)
> at
> org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.j=
ava:704)
> at
> org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:237)
> at
> org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.=
java:304)
> at
> org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java=
:439)
> at org.apache.velocity.Template.merge(Template.java:358)
> at org.apache.velocity.Template.merge(Template.java:262)
>
>
>
> Then I tried to include some more explicit configuration, to enable these
> tools or tool scopes (I am not sure which):
>
>
>
> - VelocityContext context =3D new VelocityContext();
> + VelocityEngine ve =3D new VelocityEngine();
>
> + EasyFactoryConfiguration config =3D new EasyFactoryConfiguration=
();
> + config.toolbox(Scope.REQUEST).property("locale", Locale.GERMAN)
> + .tool(DateTool.class);
> + config.toolbox(Scope.APPLICATION)
> + .tool(NumberTool.class).property("locale", Locale.GERMAN);
> +
> + ToolManager manager =3D new ToolManager(true, true);
> + manager.configure(config);
> + manager.setVelocityEngine(ve);
> + logger.debug("hasApplicationTools: {}",
> manager.hasApplicationTools());
> + logger.debug("hasRequestTools: {}", manager.hasRequestTools());
> + Context context =3D manager.createContext();
>
>
> This makes hasApplicationTools() and hasRequestTools() return true, but
> still in the template, there is no $context or $esc or $display available=
.
>
>
>
> 06:30:28.071 [main] DEBUG o.apache.velocity.tools.ToolManager -
> VelocityEngine instance was changed to
> org.apache.velocity.app.VelocityEngine@6b67034
> 06:30:28.072 [main] DEBUG d.h.etiketten.VelocityTemplateEngine -
> hasApplicationTools: true
> 06:30:28.074 [main] DEBUG d.h.etiketten.VelocityTemplateEngine -
> hasRequestTools: true
> 06:30:28.082 [main] DEBUG org.apache.velocity - Initializing Velocity,
> Calling init()...
> 06:30:28.083 [main] DEBUG org.apache.velocity - Starting Apache Velocity
> v2.3
> 06:30:28.089 [main] DEBUG org.apache.velocity - Default Properties
> resource: org/apache/velocity/runtime/defaults/velocity.properties
> 06:30:28.111 [main] DEBUG VelocityEngine - ResourceLoader instantiated:
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:30:28.117 [main] DEBUG VelocityEngine - initialized (class
> org.apache.velocity.runtime.resource.ResourceCacheImpl) with class
> java.util.Collections$SynchronizedMap cache map.
> 06:30:28.123 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Stop
> 06:30:28.128 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Define
> 06:30:28.131 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Break
> 06:30:28.135 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Evaluate
> 06:30:28.138 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Macro
> 06:30:28.145 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Parse
> 06:30:28.150 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Include
> 06:30:28.154 [main] DEBUG VelocityEngine - Loaded System Directive:
> org.apache.velocity.runtime.directive.Foreach
> 06:30:28.208 [main] DEBUG VelocityEngine.parser - Created '20' parsers.
> 06:30:28.264 [main] DEBUG VelocityEngine.macro -
> "velocimacro.library.path" is not set. Trying default library:
> velocimacros.vtl
> 06:30:28.267 [main] DEBUG VelocityEngine.loader.class - Could not load
> resource 'velocimacros.vtl' from ResourceLoader
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:30:28.268 [main] DEBUG VelocityEngine.macro - Default library
> velocimacros.vtl not found. Trying old default library: VM_global_library=
.vm
> 06:30:28.272 [main] DEBUG VelocityEngine.loader.class - Could not load
> resource 'VM_global_library.vm' from ResourceLoader
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:30:28.273 [main] DEBUG VelocityEngine.macro - Old default library
> VM_global_library.vm not found.
> 06:30:28.274 [main] DEBUG VelocityEngine.macro - allowInline =3D true: VM=
s
> can be defined inline in templates
> 06:30:28.275 [main] DEBUG VelocityEngine.macro - allowInlineToOverride =
=3D
> false: VMs defined inline may NOT replace previous VM definitions
> 06:30:28.276 [main] DEBUG VelocityEngine.macro - allowInlineLocal =3D fal=
se:
> VMs defined inline will be global in scope if allowed.
> 06:30:28.277 [main] DEBUG VelocityEngine.macro - autoload off: VM system
> will not automatically reload global library macros
> 06:30:28.336 [main] DEBUG VelocityEngine.loader - ResourceManager: found
> velocity/reflection.vm with loader
> org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
> 06:30:28.339 [main] ERROR VelocityEngine.rendering - Variable $context ha=
s
> not been set at velocity/reflection.vm[line 9, column 19]
> 06:30:28.346 [main] ERROR d.h.etiketten.VelocityTemplateEngine - somethin=
g
> invoked in the template threw an exception
> org.apache.velocity.exception.MethodInvocationException: Variable $contex=
t
> has not been set at velocity/reflection.vm[line 9, column 19]
> at
> org.apache.velocity.runtime.parser.node.ASTReference.getRootVariableValue=
(ASTReference.java:1160)
> at
> org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference=
.java:309)
> at
> org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.j=
ava:704)
> at
> org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:237)
> at
> org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.=
java:304)
> at
> org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java=
:439)
> at org.apache.velocity.Template.merge(Template.java:358)
> at org.apache.velocity.Template.merge(Template.java:262)
>
>
>
> The lib versions are
>
> implementation ('org.apache.velocity:velocity-engine-core:2.3')
> implementation ('org.apache.velocity.tools:velocity-tools-generic:3.1')
>
>
>
> I have no more ideas now. Maybe somebody can point me out what is going o=
n
> here and how ToolManager is meant to be used?
>
>
> - If I may mention that:
> The code listings on page
>
> https://velocity.apache.org/tools/3.1/generic.html
> have not helped me much: A Map is initialised, but never used. Arguments
> are passed, but not defined.
>
>
> Thank you very much for any advice.
>
>
>
> Stefan
>
>
>
>
>
> JETZT NEWSLETTER ABONNIEREN:
> https://hammerbacher.com/newsletter-anmeldung/
> Hammerbacher GmbH Gesch=C3=A4ftsf=C3=BChrer Bernhard Hammerbacher, Ursula
> Hammerbacher Registergericht N=C3=BCrnberg HRB 10908
> Hausanschrift
> Daimlerstra=C3=9Fe 4-6
> D 92318 Neumarkt Telefon
> +49(0)9181
> 2592-0 Telefax
> +49(0)9181
> 2592-28 E-Mail
> [email protected]
> www.hammerbacher.com
>
>
>
> Haftungsausschluss / Disclaimer
> Die Informationen, die in dieser Kommunikation enthalten sind, sind
> ausschlie=C3=9Flich und allein f=C3=BCr den Empf=C3=A4nger bestimmt. Die =
Verwendung durch
> Dritte ist untersagt. Die Firma Hammerbacher GmbH ist nur f=C3=BCr die vo=
n ihr
> eingegeben Informationen verantwortlich, jedoch nicht f=C3=BCr die einwan=
dfreie
> =C3=9Cbertragung oder im Zusammenhang mit der =C3=9Cbertragung oder dem E=
mpfang
> eingetretene Ver=C3=A4nderungen oder Verz=C3=B6gerungen.
> Diese E-Mail enth=C3=A4lt vetrtrauliche und/oder rechtlich gesch=C3=BCtzt=
e
> Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mai=
l
> irrt=C3=BCmlich erhalten haben, informieren Sie bitte sofort den Absende=
r und
> vernichten Sie diese E-Mail. Das unerlaubte Kopieren sowie die unbefugte
> Weitergabe dieser Mail ist nicht gestattet.
>
--000000000000a198f205d71dd769--