1 /*
2 * $Id: BasicPreparerFactory.java 1310865 2012-04-07 21:01:22Z nlebas $
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21 package org.apache.tiles.preparer.factory;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.tiles.preparer.ViewPreparer;
27 import org.apache.tiles.request.Request;
28 import org.apache.tiles.request.reflect.ClassUtil;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
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: 1310865 $ $Date: 2012-04-08 07:01:22 +1000 (Sun, 08 Apr 2012) $
39 */
40 public class BasicPreparerFactory implements PreparerFactory {
41
42 /**
43 * The logging object.
44 */
45 private final Logger log = LoggerFactory
46 .getLogger(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 */
69 public ViewPreparer getPreparer(String name, Request context) {
70
71 if (!preparers.containsKey(name)) {
72 preparers.put(name, createPreparer(name));
73 }
74
75 return preparers.get(name);
76 }
77
78 /**
79 * Creates a view preparer for the given name.
80 *
81 * @param name The name of the preparer.
82 * @return The created preparer.
83 */
84 protected ViewPreparer createPreparer(String name) {
85
86 log.debug("Creating ViewPreparer '{}' . . .", name);
87
88 Object instance = ClassUtil.instantiate(name, true);
89 log.debug("ViewPreparer created successfully");
90 return (ViewPreparer) instance;
91
92 }
93 }