1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles.preparer;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.tiles.context.TilesRequestContext;
27 import org.apache.tiles.reflect.ClassUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /***
32 * Default implementation of the {@link PreparerFactory}.
33 * This factory provides no contextual configuration. It
34 * simply instantiates the named preparerInstance and returns it.
35 *
36 * @since Tiles 2.0
37 * @version $Rev: 791161 $ $Date: 2009-07-04 20:53:36 +0200 (sab, 04 lug 2009) $
38 */
39 public class BasicPreparerFactory implements PreparerFactory {
40
41 /***
42 * The logging object.
43 */
44 private final Logger log = LoggerFactory
45 .getLogger(BasicPreparerFactory.class);
46
47 /***
48 * Maps a preparer name to the instantiated preparer.
49 */
50 protected Map<String, ViewPreparer> preparers;
51
52 /***
53 * Constructor.
54 */
55 public BasicPreparerFactory() {
56 this.preparers = new HashMap<String, ViewPreparer>();
57 }
58
59
60 /***
61 * Create a new instance of the named preparerInstance. This factory
62 * expects all names to be qualified class names.
63 *
64 * @param name the named preparerInstance
65 * @param context current context
66 * @return ViewPreparer instance
67 */
68 public ViewPreparer getPreparer(String name, TilesRequestContext context) {
69
70 if (!preparers.containsKey(name)) {
71 preparers.put(name, createPreparer(name));
72 }
73
74 return preparers.get(name);
75 }
76
77 /***
78 * Creates a view preparer for the given name.
79 *
80 * @param name The name of the preparer.
81 * @return The created preparer.
82 */
83 protected ViewPreparer createPreparer(String name) {
84
85 if (log.isDebugEnabled()) {
86 log.debug("Creating ViewPreparer '" + name + "' . . .");
87 }
88
89 Object instance = ClassUtil.instantiate(name, true);
90 log.debug("ViewPreparer created successfully");
91 return (ViewPreparer) instance;
92
93 }
94 }