This project has retired. For details please refer to its
Attic page.
ChainedTilesApplicationContextFactory 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.Initializable;
30 import org.apache.tiles.TilesApplicationContext;
31 import org.apache.tiles.awareness.AbstractTilesApplicationContextFactoryAware;
32 import org.apache.tiles.reflect.ClassUtil;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /***
37 * Implementation for TilesApplicationContextFactory, that creates a chain of
38 * sub-factories, trying each one until it returns a not-null value.
39 *
40 * @version $Rev: 797540 $ $Date: 2009-07-24 17:42:00 +0200 (ven, 24 lug 2009) $
41 * @since 2.1.1
42 * @deprecated Create an instance of {@link TilesApplicationContext} yourself,
43 * by implementing {@link org.apache.tiles.startup.TilesInitializer} or
44 * extending {@link org.apache.tiles.startup.AbstractTilesInitializer} and
45 * overriding <code>createTilesApplicationContext</code> method.<br>
46 * Moreover, it does not make sense to "try" if your application is
47 * servlet-based, portlet-based, etc. You know it, right?
48 */
49 public class ChainedTilesApplicationContextFactory extends
50 AbstractTilesApplicationContextFactory implements Initializable {
51
52 /***
53 * Factory class names initialization parameter to use.
54 *
55 * @since 2.1.1
56 */
57 public static final String FACTORY_CLASS_NAMES =
58 "org.apache.tiles.context.ChainedTilesApplicationContextFactory.FACTORY_CLASS_NAMES";
59
60 /***
61 * The default class names to instantiate that compose the chain..
62 *
63 * @since 2.1.1
64 */
65 public static final String[] DEFAULT_FACTORY_CLASS_NAMES = {
66 "org.apache.tiles.servlet.context.ServletTilesApplicationContextFactory",
67 "org.apache.tiles.portlet.context.PortletTilesApplicationContextFactory"};
68
69 /***
70 * The logging object.
71 */
72 private final Logger log = LoggerFactory
73 .getLogger(ChainedTilesApplicationContextFactory.class);
74
75 /***
76 * The Tiles context factories composing the chain.
77 */
78 private List<AbstractTilesApplicationContextFactory> factories;
79
80 /***
81 * Sets the factories to be used.
82 *
83 * @param factories The factories to be used.
84 */
85 public void setFactories(
86 List<AbstractTilesApplicationContextFactory> factories) {
87 this.factories = factories;
88 }
89
90 /*** {@inheritDoc} */
91 public void init(Map<String, String> configParameters) {
92 String[] classNames = null;
93 String classNamesString = configParameters.get(FACTORY_CLASS_NAMES);
94 if (classNamesString != null) {
95 classNames = classNamesString.split("//s*,//s*");
96 }
97 if (classNames == null || classNames.length <= 0) {
98 classNames = DEFAULT_FACTORY_CLASS_NAMES;
99 }
100
101 factories = new ArrayList<AbstractTilesApplicationContextFactory>();
102 for (int i = 0; i < classNames.length; i++) {
103 try {
104 Class<? extends AbstractTilesApplicationContextFactory> clazz = ClassUtil
105 .getClass(classNames[i],
106 AbstractTilesApplicationContextFactory.class);
107 AbstractTilesApplicationContextFactory factory = clazz
108 .newInstance();
109 if (factory instanceof AbstractTilesApplicationContextFactoryAware) {
110 ((AbstractTilesApplicationContextFactoryAware) factory)
111 .setApplicationContextFactory(this);
112 }
113 factories.add(factory);
114 } catch (ClassNotFoundException e) {
115
116
117 log.warn("Cannot find TilesContextFactory class "
118 + classNames[i]);
119 if (log.isDebugEnabled()) {
120 log.debug("Cannot find TilesContextFactory class "
121 + classNames[i], e);
122 }
123 } catch (InstantiationException e) {
124 throw new IllegalArgumentException(
125 "Cannot instantiate TilesFactoryClass " + classNames[i],
126 e);
127 } catch (IllegalAccessException e) {
128 throw new IllegalArgumentException(
129 "Cannot access TilesFactoryClass " + classNames[i]
130 + " default constructor", e);
131 }
132 }
133 }
134
135 /*** {@inheritDoc} */
136 public TilesApplicationContext createApplicationContext(Object context) {
137 TilesApplicationContext retValue = null;
138
139 for (Iterator<AbstractTilesApplicationContextFactory> factoryIt = factories
140 .iterator(); factoryIt.hasNext() && retValue == null;) {
141 retValue = factoryIt.next().createApplicationContext(context);
142 }
143
144 if (retValue == null) {
145 throw new IllegalArgumentException(
146 "Cannot find a factory to create the application context");
147 }
148
149 return retValue;
150 }
151 }