Home » tiles-2.2.1-src » org.apache » tiles » factory » [javadoc | source]

    1   /*
    2    * $Id: KeyedDefinitionsFactoryTilesContainerFactory.java 798944 2009-07-29 15:20:27Z 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.factory;
   22   
   23   import java.util.HashMap;
   24   import java.util.Map;
   25   
   26   import org.apache.tiles.TilesApplicationContext;
   27   import org.apache.tiles.TilesContainer;
   28   import org.apache.tiles.awareness.TilesApplicationContextAware;
   29   import org.apache.tiles.context.TilesRequestContextFactory;
   30   import org.apache.tiles.definition.DefinitionsFactory;
   31   import org.apache.tiles.evaluator.AttributeEvaluator;
   32   import org.apache.tiles.impl.BasicTilesContainer;
   33   import org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer;
   34   import org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.KeyExtractor;
   35   import org.apache.tiles.impl.mgmt.CachingKeyedDefinitionsFactoryTilesContainer;
   36   import org.apache.tiles.mgmt.MutableTilesContainer;
   37   import org.apache.tiles.reflect.ClassUtil;
   38   import org.apache.tiles.renderer.RendererFactory;
   39   
   40   /**
   41    * Factory that creates instances of container that will extend the
   42    * {@link KeyedDefinitionsFactoryTilesContainer} class.
   43    *
   44    * @version $Rev: 798944 $ $Date: 2009-07-29 17:20:27 +0200 (mer, 29 lug 2009) $
   45    * @deprecated This class can be replaced by registering different
   46    * {@link TilesContainer} with different keys, by overriding
   47    * {@link org.apache.tiles.startup.AbstractTilesInitializer}
   48    * <code>getContainerKey</code> method.
   49    */
   50   public class KeyedDefinitionsFactoryTilesContainerFactory extends
   51           TilesContainerFactory {
   52   
   53       /**
   54        * The name of the initialization parameter that will contain a
   55        * comma-separated list of keys to use.
   56        */
   57       public static final String CONTAINER_KEYS_INIT_PARAM =
   58           "org.apache.tiles.factory.KeyedDefinitionsFactoryTilesContainerFactory.KEYS";
   59   
   60       /**
   61        * Init parameter name that contains the class name for the key extractor.
   62        */
   63       public static final String KEY_EXTRACTOR_CLASS_INIT_PARAM =
   64           "org.apache.tiles.impl.KeyedDefinitionsFactoryTilesContainer.KeyExtractor";
   65   
   66       /**
   67        * The application context.
   68        *
   69        * @since 2.1.0
   70        */
   71       protected TilesApplicationContext applicationContext;
   72   
   73       /** {@inheritDoc} */
   74       @Override
   75       public MutableTilesContainer createMutableTilesContainer(
   76               TilesApplicationContext context) {
   77           CachingKeyedDefinitionsFactoryTilesContainer container =
   78               new CachingKeyedDefinitionsFactoryTilesContainer();
   79           initializeContainer(context, container);
   80           return container;
   81       }
   82   
   83       /** {@inheritDoc} */
   84       @Override
   85       public TilesContainer createTilesContainer(TilesApplicationContext context) {
   86           KeyedDefinitionsFactoryTilesContainer container =
   87               new KeyedDefinitionsFactoryTilesContainer();
   88           initializeContainer(context, container);
   89           return container;
   90       }
   91   
   92       /**
   93        * Creates a definitions factory.
   94        * @param context The context object to use.
   95        * @return The newly created definitions factory.
   96        * @throws TilesContainerFactoryException If something goes wrong.
   97        * @deprecated Use
   98        * {@link #createDefinitionsFactory(TilesApplicationContext)}.
   99        */
  100       @Deprecated
  101       public DefinitionsFactory createDefinitionsFactory(Object context) {
  102           if (context instanceof TilesApplicationContext) {
  103               createDefinitionsFactory((TilesApplicationContext) context);
  104           }
  105   
  106           throw new UnsupportedOperationException("Class "
  107                   + context.getClass().getName()
  108                   + " not recognized a TilesApplicationContext");
  109       }
  110   
  111       /**
  112        * Creates a definitions factory.
  113        * @param context The Tiles application context object to use.
  114        * @return The newly created definitions factory.
  115        * @throws TilesContainerFactoryException If something goes wrong.
  116        */
  117       public DefinitionsFactory createDefinitionsFactory(
  118               TilesApplicationContext context) {
  119           DefinitionsFactory retValue;
  120           Map<String, String> config = new HashMap<String, String>(defaultConfiguration);
  121           config.putAll(context.getInitParams());
  122           retValue = (DefinitionsFactory) createFactory(config,
  123                       DEFINITIONS_FACTORY_INIT_PARAM);
  124           if (retValue instanceof TilesApplicationContextAware) {
  125               ((TilesApplicationContextAware) retValue)
  126                       .setApplicationContext(applicationContext);
  127           }
  128   
  129           return retValue;
  130       }
  131   
  132       /** {@inheritDoc} */
  133       @Override
  134       protected void storeContainerDependencies(TilesApplicationContext context,
  135               Map<String, String> initParameters,
  136               Map<String, String> configuration, BasicTilesContainer container) {
  137           super.storeContainerDependencies(context, initParameters, configuration, container);
  138   
  139           String keyExtractorClassName = configuration.get(
  140                   KEY_EXTRACTOR_CLASS_INIT_PARAM);
  141           if (keyExtractorClassName != null
  142                   && container instanceof KeyedDefinitionsFactoryTilesContainer) {
  143               ((KeyedDefinitionsFactoryTilesContainer) container).setKeyExtractor(
  144                       (KeyExtractor) ClassUtil.instantiate(keyExtractorClassName));
  145           }
  146       }
  147   
  148       /** {@inheritDoc} */
  149       @Override
  150       protected void postCreationOperations(TilesRequestContextFactory contextFactory,
  151               TilesApplicationContext tilesContext,
  152               RendererFactory rendererFactory, AttributeEvaluator evaluator,
  153               Map<String, String> initParameters,
  154               Map<String, String> configuration, BasicTilesContainer container) {
  155           super.postCreationOperations(contextFactory, tilesContext,
  156                   rendererFactory, evaluator, initParameters, configuration,
  157                   container);
  158           this.applicationContext = tilesContext;
  159           String keysString = initParameters.get(CONTAINER_KEYS_INIT_PARAM);
  160           if (keysString != null
  161                   && container instanceof KeyedDefinitionsFactoryTilesContainer) {
  162               String[] keys = keysString.split(",");
  163               Map<String, String> initParams = new HashMap<String, String>(initParameters);
  164               for (int i = 0; i < keys.length; i++) {
  165                   String param = initParameters.get(
  166                           KeyedDefinitionsFactoryTilesContainer.DEFINITIONS_CONFIG_PREFIX + keys[i]);
  167                   if (param != null) {
  168                       initParams.put(DefinitionsFactory.DEFINITIONS_CONFIG,
  169                               param);
  170                   } else {
  171                       initParams.remove(DefinitionsFactory.DEFINITIONS_CONFIG);
  172                   }
  173   
  174                   DefinitionsFactory defsFactory =
  175                       (DefinitionsFactory) createFactory(configuration,
  176                               DEFINITIONS_FACTORY_INIT_PARAM);
  177                   if (defsFactory instanceof TilesApplicationContextAware) {
  178                       ((TilesApplicationContextAware) defsFactory).setApplicationContext(tilesContext);
  179                   }
  180   
  181                   defsFactory.init(initParams);
  182                   ((KeyedDefinitionsFactoryTilesContainer) container)
  183                           .setDefinitionsFactory(keys[i], defsFactory);
  184               }
  185           }
  186       }
  187   }

Home » tiles-2.2.1-src » org.apache » tiles » factory » [javadoc | source]