This project has retired. For details please refer to its Attic page.
BasicPreparerFactory xref
View Javadoc

1   /*
2    * $Id: BasicPreparerFactory.java 791161 2009-07-04 18:53:36Z apetrelli $
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;
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  }