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 org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.tiles.TilesException;
26 import org.apache.tiles.context.TilesRequestContext;
27 import org.apache.tiles.reflect.ClassUtil;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 /***
33 * Default implementation of the {@link PreparerFactory}.
34 * This factory provides no contextual configuration. It
35 * simply instantiates the named preparerInstance and returns it.
36 *
37 * @since Tiles 2.0
38 * @version $Rev: 680124 $ $Date: 2008-07-27 17:20:00 +0200 (Sun, 27 Jul 2008) $
39 */
40 public class BasicPreparerFactory implements PreparerFactory {
41
42 /***
43 * The logging object.
44 */
45 private static final Log LOG =
46 LogFactory.getLog(BasicPreparerFactory.class);
47
48 /***
49 * Maps a preparer name to the instantiated preparer.
50 */
51 protected Map<String, ViewPreparer> preparers;
52
53 /***
54 * Constructor.
55 */
56 public BasicPreparerFactory() {
57 this.preparers = new HashMap<String, ViewPreparer>();
58 }
59
60
61 /***
62 * Create a new instance of the named preparerInstance. This factory
63 * expects all names to be qualified class names.
64 *
65 * @param name the named preparerInstance
66 * @param context current context
67 * @return ViewPreparer instance
68 * @throws TilesException If something goes wrong during preparer creation.
69 */
70 public ViewPreparer getPreparer(String name, TilesRequestContext context)
71 throws TilesException {
72
73 if (!preparers.containsKey(name)) {
74 preparers.put(name, createPreparer(name));
75 }
76
77 return preparers.get(name);
78 }
79
80 /***
81 * Creates a view preparer for the given name.
82 *
83 * @param name The name of the preparer.
84 * @return The created preparer.
85 * @throws TilesException If something goes wrong during instantiation, or
86 * if the created object is not an instance of <code>ViewPreparer</code>.
87 */
88 protected ViewPreparer createPreparer(String name) throws TilesException {
89
90 if (LOG.isDebugEnabled()) {
91 LOG.debug("Creating ViewPreparer '" + name + "' . . .");
92 }
93
94 Object instance = ClassUtil.instantiate(name, true);
95 LOG.debug("ViewPreparer created successfully");
96 return (ViewPreparer) instance;
97
98 }
99 }