View Javadoc

1   /*
2    * $Id: BasicPreparerFactory.java 680124 2008-07-27 15:20:00Z 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 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  }