This project has retired. For details please refer to its
Attic page.
ChainedTilesRequestContextFactory xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.context;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.tiles.TilesApplicationContext;
30 import org.apache.tiles.awareness.TilesRequestContextFactoryAware;
31 import org.apache.tiles.reflect.ClassUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /***
36 * Default implementation for TilesRequestContextFactory, that creates a chain
37 * of sub-factories, trying each one until it returns a not-null value.
38 *
39 * @version $Rev: 904529 $ $Date: 2010-01-29 16:50:30 +0100 (ven, 29 gen 2010) $
40 * @since 2.1.1
41 */
42 public class ChainedTilesRequestContextFactory implements TilesRequestContextFactory {
43
44 /***
45 * Factory class names initialization parameter to use.
46 *
47 * @since 2.1.1
48 */
49 public static final String FACTORY_CLASS_NAMES =
50 "org.apache.tiles.context.ChainedTilesRequestContextFactory.FACTORY_CLASS_NAMES";
51
52 /***
53 * The default class names to instantiate that compose the chain..
54 *
55 * @since 2.1.1
56 */
57 public static final String[] DEFAULT_FACTORY_CLASS_NAMES = {
58 "org.apache.tiles.servlet.context.ServletTilesRequestContextFactory",
59 "org.apache.tiles.portlet.context.PortletTilesRequestContextFactory",
60 "org.apache.tiles.jsp.context.JspTilesRequestContextFactory" };
61
62 /***
63 * The logging object.
64 */
65 private final Logger log = LoggerFactory
66 .getLogger(ChainedTilesRequestContextFactory.class);
67
68 /***
69 * The Tiles context factories composing the chain.
70 */
71 private List<TilesRequestContextFactory> factories;
72
73 /***
74 * Sets the factories to be used.
75 *
76 * @param factories The factories to be used.
77 */
78 public void setFactories(List<TilesRequestContextFactory> factories) {
79 this.factories = factories;
80 injectParentTilesContextFactory();
81 }
82
83 /*** {@inheritDoc} */
84 public void init(Map<String, String> configParameters) {
85 String[] classNames = null;
86 String classNamesString = configParameters.get(FACTORY_CLASS_NAMES);
87 if (classNamesString != null) {
88 classNames = classNamesString.split("//s*,//s*");
89 }
90 if (classNames == null || classNames.length <= 0) {
91 classNames = DEFAULT_FACTORY_CLASS_NAMES;
92 }
93
94 factories = new ArrayList<TilesRequestContextFactory>();
95 for (int i = 0; i < classNames.length; i++) {
96 try {
97 Class<? extends TilesRequestContextFactory> clazz = ClassUtil
98 .getClass(classNames[i],
99 TilesRequestContextFactory.class);
100 TilesRequestContextFactory factory = clazz.newInstance();
101 factories.add(factory);
102 } catch (ClassNotFoundException e) {
103
104
105 log.info("Cannot find TilesRequestContextFactory class {},"
106 + " skipping support for the managed platform",
107 classNames[i]);
108 if (log.isDebugEnabled()) {
109 log.debug("Cannot find TilesRequestContextFactory class "
110 + classNames[i], e);
111 }
112 } catch (InstantiationException e) {
113 throw new IllegalArgumentException(
114 "Cannot instantiate TilesRequestContextFactory class " + classNames[i],
115 e);
116 } catch (IllegalAccessException e) {
117 throw new IllegalArgumentException(
118 "Cannot access TilesRequestContextFactory class " + classNames[i]
119 + " default constructor", e);
120 }
121 }
122 injectParentTilesContextFactory();
123 }
124
125 /*** {@inheritDoc} */
126 public TilesRequestContext createRequestContext(
127 TilesApplicationContext context, Object... requestItems) {
128 TilesRequestContext retValue = null;
129
130 for (Iterator<TilesRequestContextFactory> factoryIt = factories
131 .iterator(); factoryIt.hasNext() && retValue == null;) {
132 retValue = factoryIt.next().createRequestContext(context,
133 requestItems);
134 }
135
136 if (retValue == null) {
137 throw new IllegalArgumentException(
138 "Cannot find a factory to create the request context");
139 }
140
141 return retValue;
142 }
143
144 /***
145 * Injects this context factory to all chained context factories.
146 */
147 protected void injectParentTilesContextFactory() {
148 for (TilesRequestContextFactory factory : factories) {
149 if (factory instanceof TilesRequestContextFactoryAware) {
150 ((TilesRequestContextFactoryAware) factory)
151 .setRequestContextFactory(this);
152 }
153 }
154 }
155 }