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

1   /*
2    * $Id: ChainedTilesContextFactory.java 774104 2009-05-12 21:44:39Z 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  
22  package org.apache.tiles.context;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.tiles.TilesApplicationContext;
27  import org.apache.tiles.awareness.TilesRequestContextFactoryAware;
28  import org.apache.tiles.reflect.ClassUtil;
29  
30  import java.util.ArrayList;
31  import java.util.Iterator;
32  import java.util.List;
33  import java.util.Map;
34  
35  /***
36   * Default implementation for TilesContextFactory, that creates a chain of
37   * sub-factories, trying each one until it returns a not-null value.
38   *
39   * @version $Rev: 774104 $ $Date: 2009-05-12 23:44:39 +0200 (mar, 12 mag 2009) $
40   * @deprecated Use {@link ChainedTilesApplicationContextFactory}
41   * and {@link ChainedTilesRequestContextFactory}.
42   */
43  public class ChainedTilesContextFactory implements TilesContextFactory {
44  
45      /***
46       * Factory class names initialization parameter to use.
47       *
48       * @deprecated Simply do not use it.
49       */
50      @Deprecated
51      public static final String FACTORY_CLASS_NAMES =
52          "org.apache.tiles.context.ChainedTilesContextFactory.FACTORY_CLASS_NAMES";
53  
54      /***
55       * The default class names to instantiate that compose the chain..
56       *
57       * @deprecated Simply do not use it.
58       */
59      @Deprecated
60      public static final String[] DEFAULT_FACTORY_CLASS_NAMES = {
61              "org.apache.tiles.servlet.context.ServletTilesContextFactory",
62              "org.apache.tiles.portlet.context.PortletTilesContextFactory",
63              "org.apache.tiles.jsp.context.JspTilesContextFactory" };
64  
65      /***
66       * The logging object.
67       */
68      private final Log log = LogFactory
69              .getLog(ChainedTilesContextFactory.class);
70  
71      /***
72       * The Tiles context factories composing the chain.
73       */
74      private List<TilesContextFactory> factories;
75  
76      /***
77       * Sets the factories to be used.
78       *
79       * @param factories The factories to be used.
80       * @deprecated Simply do not use it.
81       */
82      @Deprecated
83      public void setFactories(List<TilesContextFactory> factories) {
84          this.factories = factories;
85      }
86  
87      /*** {@inheritDoc} */
88      public void init(Map<String, String> configParameters) {
89          String[] classNames = null;
90          String classNamesString = configParameters.get(FACTORY_CLASS_NAMES);
91          if (classNamesString != null) {
92              classNames = classNamesString.split("//s*,//s*");
93          }
94          if (classNames == null || classNames.length <= 0) {
95              classNames = DEFAULT_FACTORY_CLASS_NAMES;
96          }
97  
98          factories = new ArrayList<TilesContextFactory>();
99          for (int i = 0; i < classNames.length; i++) {
100             try {
101                 Class<? extends TilesContextFactory> clazz = ClassUtil
102                         .getClass(classNames[i], TilesContextFactory.class);
103                 TilesContextFactory factory = clazz.newInstance();
104                 if (factory instanceof TilesRequestContextFactoryAware) {
105                     ((TilesRequestContextFactoryAware) factory)
106                             .setRequestContextFactory(this);
107                 }
108                 factories.add(factory);
109             } catch (ClassNotFoundException e) {
110                 // We log it, because it could be a default configuration class that
111                 // is simply not present.
112                 log.warn("Cannot find TilesContextFactory class "
113                         + classNames[i]);
114                 if (log.isDebugEnabled()) {
115                     log.debug("Cannot find TilesContextFactory class "
116                             + classNames[i], e);
117                 }
118             } catch (InstantiationException e) {
119                 throw new IllegalArgumentException(
120                         "Cannot instantiate TilesFactoryClass " + classNames[i],
121                         e);
122             } catch (IllegalAccessException e) {
123                 throw new IllegalArgumentException(
124                         "Cannot access TilesFactoryClass " + classNames[i]
125                                 + " default constructor", e);
126             }
127         }
128     }
129 
130     /*** {@inheritDoc} */
131     public TilesApplicationContext createApplicationContext(Object context) {
132         TilesApplicationContext retValue = null;
133 
134         for (Iterator<TilesContextFactory> factoryIt = factories.iterator(); factoryIt
135                 .hasNext() && retValue == null;) {
136             retValue = factoryIt.next().createApplicationContext(context);
137         }
138 
139         if (retValue == null) {
140             throw new IllegalArgumentException(
141                     "Cannot find a factory to create the application context");
142         }
143 
144         return retValue;
145     }
146 
147     /*** {@inheritDoc} */
148     public TilesRequestContext createRequestContext(
149             TilesApplicationContext context, Object... requestItems) {
150         TilesRequestContext retValue = null;
151 
152         for (Iterator<TilesContextFactory> factoryIt = factories.iterator(); factoryIt
153                 .hasNext() && retValue == null;) {
154             retValue = factoryIt.next().createRequestContext(context,
155                     requestItems);
156         }
157 
158         if (retValue == null) {
159             throw new IllegalArgumentException(
160                     "Cannot find a factory to create the request context");
161         }
162 
163         return retValue;
164     }
165 }