| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 |
|
| 4 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
| 5 |
<head> |
| 6 |
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> |
| 7 |
<title>Chapter 11: Joran</title> |
| 8 |
<link rel="stylesheet" type="text/css" href="../css/common.css" /> |
| 9 |
<link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" /> |
| 10 |
<link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" /> |
| 11 |
<link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen" /> |
| 12 |
</head> |
| 13 |
<body onload="prettyPrint(); decorate();"> |
| 14 |
<script type="text/javascript">prefix='../'</script> |
| 15 |
<script type="text/javascript" src="../js/prettify.js"></script> |
| 16 |
<script type="text/javascript"> src="../templates/header.js"</script> |
| 17 |
<script type="text/javascript" src="../js/jquery-min.js"></script> |
| 18 |
<script type="text/javascript" src="../js/decorator.js"></script> |
| 19 |
|
| 20 |
<div id="left"> |
| 21 |
<noscript>Please turn on Javascript to view this menu</noscript> |
| 22 |
<script src="../templates/left.js" type="text/javascript"></script> |
| 23 |
</div> |
| 24 |
<div id="right"> |
| 25 |
<script src="menu.js" type="text/javascript"></script> |
| 26 |
</div> |
| 27 |
<div id="content"> |
| 28 |
|
| 29 |
<h1>Chapter 11: Joran</h1> |
| 30 |
|
| 31 |
<a href="onJoran_ja.html">和訳 (Japanese translation)</a> |
| 32 |
|
| 33 |
<div class="quote"> |
| 34 |
<p><em>The answer, my friend, is blowin' in the wind, The answer |
| 35 |
is blowin' in the wind.</em></p> |
| 36 |
|
| 37 |
<p>—BOB DYLAN, <em>The Freewheelin' Bob Dylan</em></p> |
| 38 |
</div> |
| 39 |
|
| 40 |
<p>Joran stands for a cold north-west wind which, every now and |
| 41 |
then, blows forcefully on Lake Geneva. Located right in the middle |
| 42 |
of Western-Europe, the surface of Lake Geneva is smaller than many |
| 43 |
other European lakes. However, with its average depth of 153 |
| 44 |
meters, it is unusually deep, and happens to be, by volume, the |
| 45 |
largest sweet water reserve in Western-Europe. |
| 46 |
</p> |
| 47 |
|
| 48 |
|
| 49 |
<p>As apparent in previous chapters, logback relies on Joran, a |
| 50 |
mature, flexible and powerful configuration framework. Many of the |
| 51 |
capabilities offered by logback modules are only possible on |
| 52 |
account of Joran. This chapter focuses on Joran, its basic design |
| 53 |
and its salient features. |
| 54 |
</p> |
| 55 |
|
| 56 |
<p>Joran is actually a generic configuration system which can be |
| 57 |
used independently of logging. To emphasize this point, we should |
| 58 |
mention that the logback-core module does not have a notion of |
| 59 |
loggers. In that spirit, most of the examples in this chapter have |
| 60 |
nothing to do with loggers, appenders or layouts. |
| 61 |
</p> |
| 62 |
|
| 63 |
<p>The examples presented in this chapter can be found under |
| 64 |
<em>LOGBACK_HOME/logback-examples/src/main/java/chapters/onJoran/</em> |
| 65 |
folder. |
| 66 |
</p> |
| 67 |
|
| 68 |
<p>To install Joran, simply <a |
| 69 |
href="../download.html">download</a> logback and add |
| 70 |
<em>logback-core-${project.version}.jar</em> to your |
| 71 |
classpath.</p> |
| 72 |
|
| 73 |
<h2 class="doAnchor">Historical perspective</h2> |
| 74 |
|
| 75 |
<p>Reflection is a powerful feature of the Java language, making |
| 76 |
it possible to configure software systems declaratively. For |
| 77 |
example, many important properties of an EJB are configured with |
| 78 |
the <em>ejb.xml</em> file. While EJBs are written in Java, many of |
| 79 |
their properties are specified within the <em>ejb.xml</em> |
| 80 |
file. Similarly, logback settings can be specified in a |
| 81 |
configuration file, expressed in XML format. Annotations available |
| 82 |
in JDK 1.5 and heavily used in EJB 3.0 replace many directives |
| 83 |
previously found in XML files. Joran also makes use of annotations |
| 84 |
but at a much smaller extent. Due to the dynamic nature of logback |
| 85 |
configuration data (compared to EJBs) Joran's use of annotations |
| 86 |
is rather limited. |
| 87 |
</p> |
| 88 |
|
| 89 |
<p>In log4j, logback's predecessor, the |
| 90 |
<code>DOMConfigurator</code> class, which is part of log4j version |
| 91 |
1.2.x and later, could also parse configuration files written in |
| 92 |
XML. <code>DOMConfigurator</code> was written in a way that forced |
| 93 |
us, the developers, to tweak the code each time the structure of |
| 94 |
the configuration file changed. The modified code had to be |
| 95 |
recompiled and redeployed. Just as importantly, the code of the |
| 96 |
<code>DOMConfigurator</code> consisted of loops dealing with |
| 97 |
child elements containing many interspersed if/else |
| 98 |
statements. One could not help but notice that this particular |
| 99 |
code reeked of redundancy and duplication. The <a |
| 100 |
href="http://jakarta.apache.org/commons/digester/">commons-digester |
| 101 |
project</a> had shown us that it was possible to parse XML files |
| 102 |
using pattern matching rules. At parse time, digester would apply |
| 103 |
rules that matched designated patterns. Rule classes were usually |
| 104 |
quite small and specialized. Consequently, they were relatively |
| 105 |
easy to understand and maintain. |
| 106 |
</p> |
| 107 |
|
| 108 |
<p>Armed with the <code>DOMConfigurator</code> experience, we |
| 109 |
began developing <code>Joran</code>, a powerful configuration |
| 110 |
framework to be used in logback. Joran was largely inspired by the |
| 111 |
commons-digester project. Nevertheless, it uses a slightly |
| 112 |
different terminology. In commons-digester, a rule can be seen as |
| 113 |
consisting of a pattern and a rule, as shown by the |
| 114 |
<code>Digester.addRule(String pattern, Rule rule)</code> |
| 115 |
method. We find it unnecessarily confusing to have a rule to |
| 116 |
consist of itself, not recursively but with a different |
| 117 |
meaning. In Joran, a rule consists of a pattern and an action. An |
| 118 |
action is invoked when a match occurs for the corresponding |
| 119 |
pattern. This relation between patterns and actions lies at the |
| 120 |
core of Joran. Quite remarkably, one can deal with quite complex |
| 121 |
requirements by using simple patterns, or more precisely with |
| 122 |
exact matches and wildcard matches. |
| 123 |
</p> |
| 124 |
|
| 125 |
<h3 class="doAnchor" name="saxOrDom">SAX or DOM?</h3> |
| 126 |
|
| 127 |
<p>Due to the event-based architecture of the SAX API, a tool based |
| 128 |
on SAX cannot easily deal with forward references, that is, |
| 129 |
references to elements which are defined later than the current |
| 130 |
element being processed. Elements with cyclical references are |
| 131 |
equally problematic. More generally, the DOM API allows the user to |
| 132 |
perform searches on all the elements and make forward jumps. |
| 133 |
</p> |
| 134 |
|
| 135 |
<p>This extra flexibility initially led us to choose the DOM API |
| 136 |
as the underlying parsing API for Joran. After some |
| 137 |
experimentation, it quickly became clear that dealing with jumps |
| 138 |
to distant elements while parsing the DOM tree did not make sense |
| 139 |
when the interpretation rules were expressed in the form of |
| 140 |
patterns and actions. <em>Joran only needs to be given the |
| 141 |
elements in the XML document in a sequential, depth-first |
| 142 |
order.</em> |
| 143 |
</p> |
| 144 |
|
| 145 |
<p>Moreover, the SAX API offers element location information which |
| 146 |
allows Joran to display the exact line and column number where an |
| 147 |
error occurred. Location information comes in very handy in the |
| 148 |
identification of parsing problems. |
| 149 |
</p> |
| 150 |
|
| 151 |
<h3>Non goals</h3> |
| 152 |
|
| 153 |
<p>Given its highly dynamic nature, the Joran API is not intended |
| 154 |
to be used to parse very large XML documents with many thousands |
| 155 |
of elements. |
| 156 |
</p> |
| 157 |
|
| 158 |
|
| 159 |
<h3 class="doAnchor" name="pattern">Pattern</h3> |
| 160 |
|
| 161 |
<p>A Joran pattern is essentially a string. There are two kind of |
| 162 |
patterns, <em>exact</em> and <em>wildcard</em>. The pattern "a/b" |
| 163 |
can be used to match a <code><b></code> element nested within a |
| 164 |
top-level <code><a></code> element. The "a/b" pattern will not match |
| 165 |
any other element, hence the <em>exact</em> match designation.</p> |
| 166 |
|
| 167 |
<p>Wildcards can be used to match suffixes or prefixes. For |
| 168 |
example, the "*/a" pattern can be used to match any suffix ending |
| 169 |
with "a", that is any <code><a></code> element within an XML |
| 170 |
document but not any elements nested within <code><a></code>. |
| 171 |
The "a/*" pattern will match any element prefixed by |
| 172 |
<code><a></code>, that is any element nested within an |
| 173 |
<code><a></code> element. |
| 174 |
</p> |
| 175 |
|
| 176 |
<h3 class="doAnchor" name="action">Actions</h3> |
| 177 |
|
| 178 |
<p>As mentioned above, Joran parsing rules consists of the |
| 179 |
association of patterns. Actions extend the <a |
| 180 |
href="../xref/ch/qos/logback/core/joran/action/Action.html"><code>Action</code></a> |
| 181 |
class, consisting of the following abstract methods. Other methods |
| 182 |
have been omitted for brevity. |
| 183 |
</p> |
| 184 |
|
| 185 |
|
| 186 |
<pre class="prettyprint source">package ch.qos.logback.core.joran.action; |
| 187 |
|
| 188 |
import org.xml.sax.Attributes; |
| 189 |
import org.xml.sax.Locator; |
| 190 |
import ch.qos.logback.core.joran.spi.InterpretationContext; |
| 191 |
|
| 192 |
public abstract class Action extends ContextAwareBase { |
| 193 |
/** |
| 194 |
* Called when the parser encounters an element matching a |
| 195 |
* {@link ch.qos.logback.core.joran.spi.Pattern Pattern}. |
| 196 |
*/ |
| 197 |
public abstract void begin(InterpretationContext ic, String name, |
| 198 |
Attributes attributes) throws ActionException; |
| 199 |
|
| 200 |
/** |
| 201 |
* Called to pass the body (as text) contained within an element. |
| 202 |
*/ |
| 203 |
public void body(InterpretationContext ic, String body) |
| 204 |
throws ActionException { |
| 205 |
// NOP |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* Called when the parser encounters an endElement event matching a |
| 210 |
* {@link ch.qos.logback.core.joran.spi.Pattern Pattern}. |
| 211 |
*/ |
| 212 |
public abstract void end(InterpretationContext ic, String name) |
| 213 |
throws ActionException; |
| 214 |
}</pre> |
| 215 |
|
| 216 |
<p>Thus, every action must implement the <code>begin()</code> and |
| 217 |
<code>end()</code> methods. The implementation of the |
| 218 |
<code>body()</code> method is optional on account of the |
| 219 |
empty/nop implementation provided by <code>Action</code>.</p> |
| 220 |
|
| 221 |
|
| 222 |
<h3 class="doAnchor" name="ruleStore">RuleStore </h3> |
| 223 |
|
| 224 |
<p>As mentioned previously, the invocation of actions according to |
| 225 |
matching patterns is a central concept in Joran. A rule is an |
| 226 |
association of a pattern and an action. Rules are stored in a <a |
| 227 |
href="../xref/ch/qos/logback/core/joran/spi/RuleStore.html">RuleStore</a>. |
| 228 |
</p> |
| 229 |
|
| 230 |
<p>As mentioned above, Joran is built on top of the SAX API. As an |
| 231 |
XML document is parsed, each element generates events corresponding |
| 232 |
to the start, body and end of each element. When a Joran |
| 233 |
configurator receives these events, it will attempt to find in its |
| 234 |
rule store an action corresponding to the <em>current |
| 235 |
pattern</em>. For example, the current pattern for the start, body |
| 236 |
or end event of element <em>B</em> nested within a top-level |
| 237 |
<em>A</em> element is "A/B". The current pattern is a data |
| 238 |
structure maintained automatically by Joran as it receives and |
| 239 |
processes SAX events. </p> |
| 240 |
|
| 241 |
<p>When several rules match the current pattern, then exact |
| 242 |
matches override suffix matches, and suffix matches override prefix |
| 243 |
matches. For exact details of the implementation, please see the <a |
| 244 |
href="../xref/ch/qos/logback/core/joran/spi/SimpleRuleStore.html">SimpleRuleStore</a> |
| 245 |
class. |
| 246 |
</p> |
| 247 |
|
| 248 |
|
| 249 |
<h3 class="doAnchor" name="interpretationContext">Interpretation |
| 250 |
context</h3> |
| 251 |
|
| 252 |
<p>To allow various actions to collaborate, the invocation of begin |
| 253 |
and end methods include an interpretation context as the first |
| 254 |
parameter. The interpretation context includes an object stack, an |
| 255 |
object map, an error list and a reference to the Joran interpreter |
| 256 |
invoking the action. Please see the <a |
| 257 |
href="../xref/ch/qos/logback/core/joran/spi/InterpretationContext.html"><code>InterpretationContext</code></a> |
| 258 |
class for the exact list of fields contained in the interpretation |
| 259 |
context. |
| 260 |
</p> |
| 261 |
|
| 262 |
<p>Actions can collaborate together by fetching, pushing or popping |
| 263 |
objects from the common object stack, or by putting and fetching |
| 264 |
keyed objects on the common object map. Actions can report any error |
| 265 |
conditions by adding error items on the interpretation context's |
| 266 |
<code>StatusManager</code>. |
| 267 |
</p> |
| 268 |
|
| 269 |
<h3 class="doAnchor" name="helloWorld">Hello world</h3> |
| 270 |
|
| 271 |
<p>The first example in this chapter illustrates the minimal |
| 272 |
plumbing required for using Joran. The example consists of a |
| 273 |
trivial action called <a |
| 274 |
href="../xref/chapters/onJoran/helloWorld/HelloWorldAction.html"> |
| 275 |
<code>HelloWorldAction</code></a> which prints "Hello World" on the |
| 276 |
console when its <code>begin()</code> method is invoked. The |
| 277 |
parsing of XML files is done by a configurator. For the purposes of |
| 278 |
this chapter, we have developed a very simple configurator called |
| 279 |
<a |
| 280 |
href="../xref/chapters/onJoran/SimpleConfigurator.html"><code>SimpleConfigurator</code></a>. |
| 281 |
The <a |
| 282 |
href="../xref/chapters/onJoran/helloWorld/HelloWorld.html"><code>HelloWorld</code></a> |
| 283 |
application brings all these pieces together: |
| 284 |
</p> |
| 285 |
|
| 286 |
<ul> |
| 287 |
<li>It creates a map of rules and a <code>Context</code></li> |
| 288 |
<li>It creates a parsing rule by associating the |
| 289 |
<em>hello-world</em> pattern with a corresponding |
| 290 |
<code>HelloWorldAction</code> instance</li> |
| 291 |
<li>It creates a <code>SimpleConfigutator</code>, passing it the |
| 292 |
aforementioned rules map</li> |
| 293 |
<li>It then invokes the <code>doConfigure</code> method of the |
| 294 |
configurator, passing the designated XML file as parameter |
| 295 |
</li> |
| 296 |
<li>As a last step, the accumulated Status message in the context, |
| 297 |
if any, are printed</li> |
| 298 |
</ul> |
| 299 |
|
| 300 |
<p>The <em>hello.xml</em> file contains one <hello-world> |
| 301 |
element, without any other nested elements. See the |
| 302 |
<em>logback-examples/src/main/java/chapters/onJoran/helloWorld/</em> |
| 303 |
folder for exact contents. |
| 304 |
</p> |
| 305 |
|
| 306 |
<p>Running the HelloWorld application with <em>hello.xml</em> file |
| 307 |
will print "Hello World" on the console.</p> |
| 308 |
|
| 309 |
<p class="command">java chapters.onJoran.helloWorld.HelloWorld src/main/java/chapters/onJoran/helloWorld/hello.xml</p> |
| 310 |
|
| 311 |
<p>You are highly encouraged to poke about in this example, by adding |
| 312 |
new rules on the rule store, modifying the XML document |
| 313 |
(hello.xml) and adding new actions. |
| 314 |
</p> |
| 315 |
|
| 316 |
<!-- ====================================================== --> |
| 317 |
|
| 318 |
<h3 class="doAnchor" name="calculator">Collaborating actions</h3> |
| 319 |
|
| 320 |
<p>The <em>logback-examples/src/main/java/joran/calculator/</em> |
| 321 |
directory includes several actions which collaborate together |
| 322 |
through the common object stack in order to accomplish simple |
| 323 |
computations. |
| 324 |
</p> |
| 325 |
|
| 326 |
<p>The <em>calculator1.xml</em> file contains a |
| 327 |
<code>computation</code> element, with a nested |
| 328 |
<code>literal</code> element. Here are its contents. |
| 329 |
</p> |
| 330 |
|
| 331 |
<em>Example 10.<span class="autoEx"/>: First calculator example |
| 332 |
(logback-examples/src/main/java/chapters/onJoran/calculator/calculator1.xml)</em> |
| 333 |
|
| 334 |
<em> </em> |
| 335 |
<pre class="prettyprint source"><computation name="total"> |
| 336 |
<literal value="3"/> |
| 337 |
</computation></pre> |
| 338 |
|
| 339 |
<p>In the <a href="../xref/chapters/onJoran/calculator/Calculator1.html"> |
| 340 |
<code>Calculator1</code></a> application, we declare various |
| 341 |
parsing rules (patterns and actions) collaborating together to |
| 342 |
compute a result based on the contents of an XML document. |
| 343 |
</p> |
| 344 |
|
| 345 |
<p> Running <code>Calculator1</code> application with |
| 346 |
<em>calculator1.xml</em></p> |
| 347 |
|
| 348 |
<p class="command">java chapters.onJoran.calculator.Calculator1 src/main/java/chapters/onJoran/calculator/calculator1.xml</p> |
| 349 |
|
| 350 |
<p>will print:</p> |
| 351 |
|
| 352 |
<p class="console">The computation named [total] resulted in the value 3</p> |
| 353 |
|
| 354 |
|
| 355 |
<p>Parsing the <em>calculator1.xml</em> document (listed above) |
| 356 |
involves the following steps: |
| 357 |
</p> |
| 358 |
|
| 359 |
<ul> |
| 360 |
<li>The start event corresponding to the <computation> |
| 361 |
element translates into the current pattern |
| 362 |
"/computation". Since in the <a |
| 363 |
href="../xref/chapters/onJoran/calculator/Calculator1.html"> |
| 364 |
<code>Calculator1</code></a> application we associated the |
| 365 |
pattern "/computation" with a |
| 366 |
<a |
| 367 |
href="../xref/chapters/onJoran/calculator/ComputationAction1.html"> |
| 368 |
<code>ComputationAction1</code></a> instance, the |
| 369 |
<code>begin()</code> method of that |
| 370 |
<code>ComputationAction1</code> instance is invoked. |
| 371 |
</li> |
| 372 |
|
| 373 |
<li><p>The start event corresponding to the <literal> |
| 374 |
element translates into the current pattern |
| 375 |
"/computation/literal". Given the association of the |
| 376 |
"/computation/literal" pattern with a |
| 377 |
<a |
| 378 |
href="../xref/chapters/onJoran/calculator/LiteralAction.html"> |
| 379 |
<code>LiteralAction</code></a> instance, the |
| 380 |
<code>begin()</code> method of that <code>LiteralAction</code> |
| 381 |
instance is called.</p> |
| 382 |
</li> |
| 383 |
|
| 384 |
<li><p>By the same token, the end event corresponding to the |
| 385 |
<literal> element triggers the invocation of the |
| 386 |
<code>end</code>() method of the same <code>LiteralAction</code> |
| 387 |
instance.</p> |
| 388 |
</li> |
| 389 |
|
| 390 |
|
| 391 |
<li><p>Similarly, the event corresponding to the end of |
| 392 |
<computation> element triggers the invocation the |
| 393 |
<code>end()</code> method of the <code>ComputationAction1</code> |
| 394 |
same instance. |
| 395 |
</p> |
| 396 |
</li> |
| 397 |
</ul> |
| 398 |
|
| 399 |
<p>What is interesting here is the way actions collaborate. The |
| 400 |
<code>LiteralAction</code> reads a literal value and pushes it in |
| 401 |
the object stack maintained by the |
| 402 |
<code>InterpretationContext</code>. Once done, any other action |
| 403 |
can pop the value to read or modify it. Here, the |
| 404 |
<code>end()</code> method of the <code>ComputationAction1</code> |
| 405 |
class pops the value from the stack and prints it. |
| 406 |
</p> |
| 407 |
|
| 408 |
<!-- TO BE CONTINUED --> |
| 409 |
|
| 410 |
<p>The next example, <em>calculator2.xml</em> file is a bit more |
| 411 |
complex, but also more interesting.</p> |
| 412 |
|
| 413 |
<em>Example 10.<span class="autoEx"/>: Calculator configuration |
| 414 |
file |
| 415 |
(logback-examples/src/main/java/chapters/onJoran/calculator/calculator2.xml)</em> |
| 416 |
<pre class="prettyprint source"><computation name="toto"> |
| 417 |
<literal value="7"/> |
| 418 |
<literal value="3"/> |
| 419 |
<add/> |
| 420 |
<literal value="3"/> |
| 421 |
<multiply/> |
| 422 |
</computation></pre> |
| 423 |
|
| 424 |
|
| 425 |
<p>As in the previous example, in response to the <literal> |
| 426 |
element,the appropriate <a |
| 427 |
href="../xref/chapters/onJoran/calculator/LiteralAction.html"> |
| 428 |
<code>LiteralAction</code></a> instance will push an integer, |
| 429 |
corresponding to the value attribute, at the top of the |
| 430 |
interpretation context's object stack. In this example, that is |
| 431 |
<em>calculator2.xml</em>, the values are 7 and 3. In response to the |
| 432 |
<add> element, the appropriate <a |
| 433 |
href="../xref/chapters/onJoran/calculator/AddAction.html"><code>AddAction</code></a> |
| 434 |
will pop two previously pushed integers, compute their sum and push |
| 435 |
the result, i.e. 10 (=7+3), at the top of the interpretation |
| 436 |
context's stack. The next literal element will cause LiteralAction |
| 437 |
to push an integer with value 3 at the top of the stack. In response |
| 438 |
to the <multiply> element, the appropriate <a |
| 439 |
href="../xref/chapters/onJoran/calculator/MultiplyAction.html"><code>MultiplyAction</code></a> |
| 440 |
will pop two previously pushed integers, i.e. 10 and 3, and compute |
| 441 |
their product. It will push the result, i.e. 30, at the top of the |
| 442 |
stack. At the very end, in reponse to the end event corresponding to |
| 443 |
the </computation> tag, the ComputationAction1 will print the |
| 444 |
object at the top of the stack. Thus, running: |
| 445 |
</p> |
| 446 |
|
| 447 |
<p class="command">java chapters.onJoran.calculator.Calculator1 src/main/java/chapters/onJoran/calculator/calculator2.xml </p> |
| 448 |
|
| 449 |
<p>will yield</p> |
| 450 |
|
| 451 |
<p class="console">The computation named [toto] resulted in the value 30 </p> |
| 452 |
|
| 453 |
|
| 454 |
<!-- |
| 455 |
|
| 456 |
|
| 457 |
<p>Finally, a <em>calculator3.xml</em> is also provided, to |
| 458 |
demonstrate the possibility elements that contain instances of the |
| 459 |
same element. Here's the content of <em>calculator3.xml</em>:</p> |
| 460 |
|
| 461 |
<em>Example 10.<span class="autoEx"/>: Calculator configuration file |
| 462 |
(logback-examples/src/main/java/chapters/onJoran/calculator/calculator3.xml)</em> |
| 463 |
|
| 464 |
<pre class="prettyprint source"><computation name="toto"> |
| 465 |
<computation> |
| 466 |
<literal value="7"/> |
| 467 |
<literal value="3"/> |
| 468 |
<add/> |
| 469 |
</computation> |
| 470 |
|
| 471 |
<literal value="3"/> |
| 472 |
<multiply/> |
| 473 |
</computation></pre> |
| 474 |
|
| 475 |
<p>Much like the use of parentheses in an algebrical equation, the |
| 476 |
presence of a <code>computation</code> element nested in another is |
| 477 |
managed by the <a |
| 478 |
href="../xref/chapters/onJoran/calculator/ComputationAction2.html"> |
| 479 |
<code>ComputationAction2</code></a> class using an internal |
| 480 |
stack. The well-formedness of XML will guarantee that a value saved |
| 481 |
by one <code>begin()</code> will be consumed only by the matching |
| 482 |
<code>end()</code> method.</p> |
| 483 |
--> |
| 484 |
|
| 485 |
<h3 class="doAnchor" name="implicit">Implicit actions</h3> |
| 486 |
|
| 487 |
<p>The rules defined thus far are called explicit actions because an |
| 488 |
pattern/action association could be found in the rule store for the |
| 489 |
current element. However, in highly extensible systems, the number |
| 490 |
and type of components can be so large so as to make it very tedious |
| 491 |
to associate an explicit action for all patterns. |
| 492 |
</p> |
| 493 |
|
| 494 |
<p>At the same time, even in highly extensible systems one can |
| 495 |
observe recurrent rules linking various parts together. Assuming we |
| 496 |
could identify such rules, we could process components composed of |
| 497 |
sub-components unknown at compilation time (of logback). For |
| 498 |
example, Apache Ant is capable of handling tasks which contain tags |
| 499 |
unknown at compile time, simply by inspecting the component for |
| 500 |
methods whose names start with <em>add</em>, as in |
| 501 |
<code>addFile</code>, or <code>addClassPath</code>. When Ant |
| 502 |
encounters an embedded tag within a task, it simply instantiates an |
| 503 |
object that matches the signature of the task class' add method and |
| 504 |
attaches the resulting object to the parent. |
| 505 |
</p> |
| 506 |
|
| 507 |
<p>Joran supports a similar capability in the form of implicit |
| 508 |
actions. Joran keeps a list of implicit actions which are applied if |
| 509 |
no explicit pattern could match the current pattern. However, |
| 510 |
applying an implicit action may not be always appropriate. Before |
| 511 |
executing the implicit action, Joran asks a given implicit action |
| 512 |
whether it is appropriate in the current situation. Only if the |
| 513 |
action replies in the affirmative does the Joran configurator invoke |
| 514 |
the (implicit) action. Note that this extra step makes it possible |
| 515 |
to support multiple implicit actions or possibly none, if no |
| 516 |
implicit action is appropriate for a given situation. |
| 517 |
</p> |
| 518 |
|
| 519 |
<p>You can create and register a custom implicit action as |
| 520 |
illustrated in the next example contained within the |
| 521 |
<em>logback-examples/src/main/java/chapters/onJoran/implicit</em> folder. |
| 522 |
</p> |
| 523 |
|
| 524 |
<p>The <a |
| 525 |
href="../xref/chapters/onJoran/implicit/PrintMe.html"><code>PrintMe</code></a> |
| 526 |
application associates an <a |
| 527 |
href="../xref/chapters/onJoran/implicit/NOPAction.html"> |
| 528 |
<code>NOPAction</code></a> instance with the pattern "*/foo", that |
| 529 |
is any element named as "foo". As its name indicates, the |
| 530 |
<code>begin</code>() and <code>end</code>() methods of |
| 531 |
<code>NOPAction</code> are empty. The <code>PrintMe</code> |
| 532 |
application also registers an instance of <a |
| 533 |
href="../xref/chapters/onJoran/implicit/PrintMeImplicitAction.html">PrintMeImplicitAction</a> |
| 534 |
in its list of implicit actions. The |
| 535 |
<code>PrintMeImplicitAction</code> is applicable for any element |
| 536 |
which has a <span class="attr">printme</span> attribute set to |
| 537 |
true. See the <code>isApplicable()</code> method in |
| 538 |
<code>PrintMeImplicitAction</code>. The <code>begin()</code>() method |
| 539 |
of <code>PrintMeImplicitAction</code> prints the name of the current |
| 540 |
element on the console. |
| 541 |
</p> |
| 542 |
|
| 543 |
<p>The XML document <em>implicit1.xml</em> is designed to illustrate |
| 544 |
how implicit actions come into play.</p> |
| 545 |
|
| 546 |
<em>Example 10.<span class="autoEx"/>: Usage of implicit rules |
| 547 |
(logback-examples/src/main/java/chapters/onJoran/implicit/implicit1.xml)</em> |
| 548 |
<pre class="prettyprint source"><foo> |
| 549 |
<xyz printme="true"> |
| 550 |
<abc printme="true"/> |
| 551 |
</xyz> |
| 552 |
|
| 553 |
<xyz/> |
| 554 |
|
| 555 |
<foo printme="true"/> |
| 556 |
|
| 557 |
</foo></pre> |
| 558 |
|
| 559 |
<p>Running</p> |
| 560 |
|
| 561 |
<p class="command">java chapters.onJoran.implicit.PrintMe src/main/java/chapters/onJoran/implicit/implicit1.xml</p> |
| 562 |
<p>yields:</p> |
| 563 |
|
| 564 |
<p class="console">Element [xyz] asked to be printed. |
| 565 |
Element [abc] asked to be printed. |
| 566 |
20:33:43,750 |-ERROR in c.q.l.c.joran.spi.Interpreter@<b>10:9</b> - no applicable action for [xyz], current pattern is [[foo][xyz]]</p> |
| 567 |
|
| 568 |
<p>Given that <code>NOPAction</code> instance is explicitly |
| 569 |
associated with the "*/foo" pattern, <code>NOPAction</code>'s |
| 570 |
<code>begin()</code> and <code>end()</code> methods are invoked on |
| 571 |
<foo> elements. <code>PrintMeImplicitAction</code> is never |
| 572 |
triggered for any of the <foo> elements. For other elements, |
| 573 |
since there are no matching explicit actions, the |
| 574 |
<code>isApplicable()</code> method of |
| 575 |
<code>PrintMeImplicitAction</code> is invoked. It will return true |
| 576 |
only for elements having a <span class="attr">printme</span> |
| 577 |
attribute set to true, namely the first <xyz> element (but not |
| 578 |
the second) and the <abc> element. The second <xyz> element on |
| 579 |
line 10, there are no applicable actions, an internal error message |
| 580 |
is generated. This message is printed by the |
| 581 |
<code>StatusPrinter.print</code> invocation, the last statement in |
| 582 |
the <code>PrintMe</code> application. This explains the output shown |
| 583 |
above (see previous paragraph). |
| 584 |
</p> |
| 585 |
|
| 586 |
<h3 class="doAnchor" name="iaPractice">Implicit actions in |
| 587 |
practice</h3> |
| 588 |
|
| 589 |
<p>The respective Joran configurators of logback-classic and |
| 590 |
logback-access include just two implicit actions, namely <a |
| 591 |
href="../xref/ch/qos/logback/core/joran/action/NestedBasicPropertyIA.html"> |
| 592 |
<code>NestedBasicPropertyIA</code></a> and <a |
| 593 |
href="../xref/ch/qos/logback/core/joran/action/NestedComplexPropertyIA.html"> |
| 594 |
<code>NestedComplexPropertyIA</code></a>. |
| 595 |
</p> |
| 596 |
|
| 597 |
<p><code>NestedBasicPropertyIA</code> is applicable for any property |
| 598 |
whose type is a primitive type (or equivalent object type in the |
| 599 |
<code>java.lang</code> package), an enumeration type, or any type |
| 600 |
adhering to the "valueOf" convention. Such properties are said to |
| 601 |
be <em>basic</em> or <em>simple</em>. A class is said to adhere to |
| 602 |
the "valueOf" convention if it contains a static method named |
| 603 |
<code>valueOf</code>() taking a <code>java.lang.String</code> as |
| 604 |
parameter and returning an instance of the type in question. At |
| 605 |
present, the <a |
| 606 |
href="../xref/ch/qos/logback/classic/Level.html"><code>Level</code></a>, |
| 607 |
<a |
| 608 |
href="../xref/ch/qos/logback/core/util/Duration.html"><code>Duration</code></a> |
| 609 |
and <a |
| 610 |
href="../xref/ch/qos/logback/core/util/FileSize.html"><code>FileSize</code></a> |
| 611 |
classes follow this convention. |
| 612 |
</p> |
| 613 |
|
| 614 |
<p><code>NestedComplexPropertyIA</code> action is applicable, in the |
| 615 |
remaining cases where <code>NestedBasicPropertyIA</code> is not |
| 616 |
applicable <em>and</em> if the object at the top of the object stack |
| 617 |
has a setter or adder method for a property name equal to the |
| 618 |
current element name. Note that such properties can in turn contain |
| 619 |
other components. Thus, such properties are said to be |
| 620 |
<em>complex</em>. In presence of a complex property, <a |
| 621 |
href="../xref/ch/qos/logback/core/joran/action/NestedComplexPropertyIA.html"> |
| 622 |
<code>NestedComplexPropertyIA</code></a> will instantiate the |
| 623 |
appropriate class for the nested component and attach it to the |
| 624 |
parent component (at the top of the object stack) by using the |
| 625 |
setter/adder method of the parent component and the nested element's |
| 626 |
name. The corresponding class is specified by the <span |
| 627 |
class="attr">class</span> attribute of the (nested) current |
| 628 |
element. However, if the <span class="attr">class</span> attribute |
| 629 |
is missing, the class name can be deduced implicitly, if any of the |
| 630 |
following is true: |
| 631 |
</p> |
| 632 |
|
| 633 |
<ol> |
| 634 |
<li>there is an internal rule associating the parent object's |
| 635 |
property with a designated class |
| 636 |
</li> |
| 637 |
<li>the setter method contains a @DefaultClass attribute |
| 638 |
designating a given class</li> |
| 639 |
|
| 640 |
<li>the parameter type of the setter method is a concrete class |
| 641 |
possessing a public constructor |
| 642 |
</li> |
| 643 |
</ol> |
| 644 |
|
| 645 |
<h4 class="doAnchor" name="defaultClassMapping">Default class |
| 646 |
mapping</h4> |
| 647 |
|
| 648 |
<p>In logback-classic, there are a handful of internal rules mapping |
| 649 |
parent class/property name pairs to a default class. These are |
| 650 |
listed in the table below.</p> |
| 651 |
|
| 652 |
<table class="bodyTable"> |
| 653 |
<tr> |
| 654 |
<th>Parent class </th> |
| 655 |
<th>property name </th> |
| 656 |
<th>default nested class</th> |
| 657 |
</tr> |
| 658 |
|
| 659 |
<tr > |
| 660 |
<td>ch.qos.logback.core.AppenderBase</td> |
| 661 |
<td>encoder</td> |
| 662 |
<td>ch.qos.logback.classic.encoder.PatternLayoutEncoder</td> |
| 663 |
</tr> |
| 664 |
|
| 665 |
<tr class="alt"> |
| 666 |
<td>ch.qos.logback.core.UnsynchronizedAppenderBase</td> |
| 667 |
<td>encoder</td> |
| 668 |
<td>ch.qos.logback.classic.encoder.PatternLayoutEncoder</td> |
| 669 |
</tr> |
| 670 |
|
| 671 |
<tr > |
| 672 |
<td>ch.qos.logback.core.AppenderBase</td> |
| 673 |
<td>layout</td> |
| 674 |
<td>ch.qos.logback.classic.PatternLayout</td> |
| 675 |
</tr> |
| 676 |
|
| 677 |
<tr class="alt"> |
| 678 |
<td>ch.qos.logback.core.UnsynchronizedAppenderBase</td> |
| 679 |
<td>layout</td> |
| 680 |
<td>ch.qos.logback.classic.PatternLayout</td> |
| 681 |
</tr> |
| 682 |
|
| 683 |
<tr> |
| 684 |
<td>ch.qos.logback.core.filter.EvaluatorFilter</td> |
| 685 |
<td>evaluator</td> |
| 686 |
<td>ch.qos.logback.classic.boolex.JaninoEventEvaluator</td> |
| 687 |
</tr> |
| 688 |
</table> |
| 689 |
|
| 690 |
<p>This list may change in future releases. Please see |
| 691 |
logback-classic <a |
| 692 |
href="../xref/ch/qos/logback/classic/joran/JoranConfigurator.html">JoranConfigurator</a>'s |
| 693 |
<code>addDefaultNestedComponentRegistryRules</code> method for the |
| 694 |
latest rules. |
| 695 |
</p> |
| 696 |
|
| 697 |
<p>In logback-access, the rules are very similar. In the default |
| 698 |
class for the nested component, the ch.qos.logback.classic package |
| 699 |
is replaced by ch.qos.logback.access. See logback-access <a |
| 700 |
href="../xref/ch/qos/logback/access/joran/JoranConfigurator.html">JoranConfigurator</a>'s |
| 701 |
<code>addDefaultNestedComponentRegistryRules</code> method for the |
| 702 |
latest rules. |
| 703 |
</p> |
| 704 |
|
| 705 |
<h4 class="doAnchor">Collection of properties</h4> |
| 706 |
|
| 707 |
|
| 708 |
<p>Note that in addition to single simple properties or single |
| 709 |
complex properties, logback's implicit actions support collections of |
| 710 |
properties, be they simple or complex. Instead of a setter method, |
| 711 |
the property is specified by an "adder" method.</p> |
| 712 |
|
| 713 |
<h3 class="doAnchor" name="newRule">New rules on the fly</h3> |
| 714 |
|
| 715 |
<p>Joran includes an action which allows the Joran interpreter to |
| 716 |
learn new rules on the fly, that is while interpreting an XML |
| 717 |
document. See the |
| 718 |
<em>logback-examples/src/main/java/chapters/onJoran/newRule/</em> directory |
| 719 |
for sample code. In this package, the <a |
| 720 |
href="../xref/chapters/onJoran/newRule/NewRuleCalculator.html"> |
| 721 |
<code>NewRuleCalculator</code></a> application sets up just two |
| 722 |
rules, one rule to process the top-most element, and a second rule |
| 723 |
to learn new rules. Here is the relevant code from |
| 724 |
<code>NewRuleCalculator</code>. |
| 725 |
</p> |
| 726 |
|
| 727 |
<pre class="prettyprint source">ruleMap.put(new Pattern("*/computation"), new ComputationAction1()); |
| 728 |
<b>ruleStore.addRule(new Pattern("/computation/newRule"), new NewRuleAction());</b></pre> |
| 729 |
|
| 730 |
<p><a |
| 731 |
href="../xref/ch/qos/logback/core/joran/action/NewRuleAction.html"><code>NewRuleAction</code></a>, |
| 732 |
part of logback-core, works pretty much like the other actions. It |
| 733 |
has a <code>begin()</code> and <code>end()</code> method, and is |
| 734 |
called each time the parser finds a <em>newRule</em> element. When |
| 735 |
invoked, the <code>begin()</code> method looks for <em>pattern</em> |
| 736 |
and <em>actionClass</em> attributes. It then instantiates the |
| 737 |
corresponding action class and adds the pattern/action association |
| 738 |
as a new rule in Joran's rule store.</p> |
| 739 |
|
| 740 |
|
| 741 |
<p>Here is how new rules can be declared in an xml file:</p> |
| 742 |
|
| 743 |
<pre class="prettyprint source"><newRule pattern="*/computation/literal" |
| 744 |
actionClass="chapters.onJoran.calculator.LiteralAction"/></pre> |
| 745 |
|
| 746 |
<p>Using such newRule declarations, we can transform |
| 747 |
<code>NewRuleCalculator</code> to behave like the |
| 748 |
<code>Calculator1</code> application we saw earlier. involving the |
| 749 |
calculation, could be expressed this way:</p> |
| 750 |
|
| 751 |
<em>Example 10..<span class="autoEx"/>: Configuration file using new |
| 752 |
rules on the fly |
| 753 |
(logback-examples/src/main/java/chapters/onJoran/newrule/newRule.xml)</em> |
| 754 |
|
| 755 |
<pre class="prettyprint source"><computation name="toto"> |
| 756 |
<newRule pattern="*/computation/literal" |
| 757 |
actionClass="chapters.onJoran.calculator.LiteralAction"/> |
| 758 |
<newRule pattern="*/computation/add" |
| 759 |
actionClass="chapters.onJoran.calculator.AddAction"/> |
| 760 |
<newRule pattern="*/computation/multiply" |
| 761 |
actionClass="chapters.onJoran.calculator.MultiplyAction"/> |
| 762 |
|
| 763 |
<computation> |
| 764 |
<literal value="7"/> |
| 765 |
<literal value="3"/> |
| 766 |
<add/> |
| 767 |
</computation> |
| 768 |
|
| 769 |
<literal value="3"/> |
| 770 |
<multiply/> |
| 771 |
</computation></pre> |
| 772 |
|
| 773 |
|
| 774 |
<p class="command">java java chapters.onJoran.newRule.NewRuleCalculator src/main/java/chapters/onJoran/newRule/newRule.xml</p> |
| 775 |
|
| 776 |
<p>yields</p> |
| 777 |
|
| 778 |
<p class="console">The computation named [toto] resulted in the value 30</p> |
| 779 |
|
| 780 |
<p>which is identical to the output of the <a |
| 781 |
href="#calculator">original calculator example</a>.</p> |
| 782 |
|
| 783 |
|
| 784 |
<script src="../templates/footer.js" type="text/javascript"></script> |
| 785 |
</div> |
| 786 |
</body> |
| 787 |
</html> |