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

1   /*
2    * $Id: ChainedTilesApplicationContextFactory.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.Initializable;
27  import org.apache.tiles.TilesApplicationContext;
28  import org.apache.tiles.awareness.AbstractTilesApplicationContextFactoryAware;
29  import org.apache.tiles.reflect.ClassUtil;
30  
31  import java.util.ArrayList;
32  import java.util.Iterator;
33  import java.util.List;
34  import java.util.Map;
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: 774104 $ $Date: 2009-05-12 23:44:39 +0200 (mar, 12 mag 2009) $
41   * @since 2.1.1
42   */
43  public class ChainedTilesApplicationContextFactory extends
44          AbstractTilesApplicationContextFactory implements Initializable {
45  
46      /***
47       * Factory class names initialization parameter to use.
48       *
49       * @since 2.1.1
50       */
51      public static final String FACTORY_CLASS_NAMES =
52          "org.apache.tiles.context.ChainedTilesApplicationContextFactory.FACTORY_CLASS_NAMES";
53  
54      /***
55       * The default class names to instantiate that compose the chain..
56       *
57       * @since 2.1.1
58       */
59      public static final String[] DEFAULT_FACTORY_CLASS_NAMES = {
60              "org.apache.tiles.servlet.context.ServletTilesApplicationContextFactory",
61              "org.apache.tiles.portlet.context.PortletTilesApplicationContextFactory"};
62  
63      /***
64       * The logging object.
65       */
66      private final Log log = LogFactory
67              .getLog(ChainedTilesApplicationContextFactory.class);
68  
69      /***
70       * The Tiles context factories composing the chain.
71       */
72      private List<AbstractTilesApplicationContextFactory> factories;
73  
74      /***
75       * Sets the factories to be used.
76       *
77       * @param factories The factories to be used.
78       */
79      public void setFactories(
80              List<AbstractTilesApplicationContextFactory> factories) {
81          this.factories = factories;
82      }
83  
84      /*** {@inheritDoc} */
85      public void init(Map<String, String> configParameters) {
86          String[] classNames = null;
87          String classNamesString = configParameters.get(FACTORY_CLASS_NAMES);
88          if (classNamesString != null) {
89              classNames = classNamesString.split("//s*,//s*");
90          }
91          if (classNames == null || classNames.length <= 0) {
92              classNames = DEFAULT_FACTORY_CLASS_NAMES;
93          }
94  
95          factories = new ArrayList<AbstractTilesApplicationContextFactory>();
96          for (int i = 0; i < classNames.length; i++) {
97              try {
98                  Class<? extends AbstractTilesApplicationContextFactory> clazz = ClassUtil
99                          .getClass(classNames[i],
100                                 AbstractTilesApplicationContextFactory.class);
101                 AbstractTilesApplicationContextFactory factory = clazz
102                         .newInstance();
103                 if (factory instanceof AbstractTilesApplicationContextFactoryAware) {
104                     ((AbstractTilesApplicationContextFactoryAware) factory)
105                             .setApplicationContextFactory(this);
106                 }
107                 factories.add(factory);
108             } catch (ClassNotFoundException e) {
109                 // We log it, because it could be a default configuration class that
110                 // is simply not present.
111                 log.warn("Cannot find TilesContextFactory class "
112                         + classNames[i]);
113                 if (log.isDebugEnabled()) {
114                     log.debug("Cannot find TilesContextFactory class "
115                             + classNames[i], e);
116                 }
117             } catch (InstantiationException e) {
118                 throw new IllegalArgumentException(
119                         "Cannot instantiate TilesFactoryClass " + classNames[i],
120                         e);
121             } catch (IllegalAccessException e) {
122                 throw new IllegalArgumentException(
123                         "Cannot access TilesFactoryClass " + classNames[i]
124                                 + " default constructor", e);
125             }
126         }
127     }
128 
129     /*** {@inheritDoc} */
130     public TilesApplicationContext createApplicationContext(Object context) {
131         TilesApplicationContext retValue = null;
132 
133         for (Iterator<AbstractTilesApplicationContextFactory> factoryIt = factories
134                 .iterator(); factoryIt.hasNext() && retValue == null;) {
135             retValue = factoryIt.next().createApplicationContext(context);
136         }
137 
138         if (retValue == null) {
139             throw new IllegalArgumentException(
140                     "Cannot find a factory to create the application context");
141         }
142 
143         return retValue;
144     }
145 }