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

    1   /*
    2    * $Id: TilesAccess.java 797628 2009-07-24 19:38:14Z 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.access;
   22   
   23   import java.lang.reflect.Method;
   24   
   25   import org.apache.tiles.TilesApplicationContext;
   26   import org.apache.tiles.TilesContainer;
   27   import org.apache.tiles.reflect.ClassUtil;
   28   import org.slf4j.Logger;
   29   import org.slf4j.LoggerFactory;
   30   
   31   
   32   /**
   33    * Provides static access to the tiles container.
   34    *
   35    * @version $Rev: 797628 $ $Date: 2009-07-24 21:38:14 +0200 (ven, 24 lug 2009) $
   36    */
   37   public final class TilesAccess {
   38   
   39       /**
   40        * Constructor, private to avoid instantiation.
   41        */
   42       private TilesAccess() {
   43       }
   44   
   45       /**
   46        * The name of the attribute to use when getting and setting the container
   47        * object in a context.
   48        */
   49       public static final String CONTAINER_ATTRIBUTE =
   50           "org.apache.tiles.CONTAINER";
   51   
   52       /**
   53        * The name of the attribute to get the {@link TilesApplicationContext} from
   54        * a context.
   55        */
   56       private static final String CONTEXT_ATTRIBUTE =
   57           "org.apache.tiles.APPLICATION_CONTEXT";
   58   
   59       /**
   60        * Finds and returns the default Tiles container object, if it was
   61        * previously initialized.
   62        *
   63        * @param context The (application) context object to use.
   64        * @return The container if it has been configured previously, otherwise
   65        * <code>null</code>.
   66        * @see #setContainer(Object, TilesContainer)
   67        * @deprecated Use one of the environment-specific Utilities (e.g.
   68        * ServletUtil).
   69        */
   70       @Deprecated
   71       public static TilesContainer getContainer(Object context) {
   72           return (TilesContainer) getAttribute(context, CONTAINER_ATTRIBUTE);
   73       }
   74   
   75       /**
   76        * Configures the default container to be used in the application.
   77        *
   78        * @param context The Tiles application context object to use.
   79        * @param container The container object to set.
   80        * @throws TilesAccessException If something goes wrong during manipulation of the
   81        * context.
   82        * @since 2.1.2
   83        */
   84       public static void setContainer(TilesApplicationContext context,
   85               TilesContainer container) {
   86           setContainer(context, container, CONTAINER_ATTRIBUTE);
   87       }
   88   
   89       /**
   90        * Configures the container to be used in the application.
   91        *
   92        * @param context The Tiles application context object to use.
   93        * @param container The container object to set.
   94        * @param key The key under which the container will be stored.
   95        * @throws TilesAccessException If something goes wrong during manipulation of the
   96        * context.
   97        * @since 2.1.2
   98        */
   99       public static void setContainer(TilesApplicationContext context,
  100               TilesContainer container, String key) {
  101           Logger log = LoggerFactory.getLogger(TilesAccess.class);
  102           if (key == null) {
  103               key = CONTAINER_ATTRIBUTE;
  104           }
  105   
  106           if (container == null) {
  107               if (log.isInfoEnabled()) {
  108                   log.info("Removing TilesContext for context: " + context.getClass().getName());
  109               }
  110               context.getApplicationScope().remove(key);
  111           } else {
  112               if (log.isInfoEnabled()) {
  113                   log.info("Publishing TilesContext for context: " + context.getClass().getName());
  114               }
  115               context.getApplicationScope().put(key, container);
  116           }
  117       }
  118   
  119       /**
  120        * Configures the default container to be used in the application.
  121        *
  122        * @param context The (application) context object to use.
  123        * @param container The container object to set.
  124        * @throws TilesAccessException If something goes wrong during manipulation of the
  125        * context.
  126        * @deprecated Use {@link #setContainer(TilesApplicationContext, TilesContainer)}.
  127        */
  128       @Deprecated
  129       public static void setContainer(Object context, TilesContainer container) {
  130           Logger log = LoggerFactory.getLogger(TilesAccess.class);
  131           if (container == null) {
  132               if (log.isInfoEnabled()) {
  133                   log.info("Removing TilesContext for context: " + context.getClass().getName());
  134               }
  135               removeAttribute(context, CONTAINER_ATTRIBUTE);
  136           }
  137           if (container != null && log.isInfoEnabled()) {
  138               log.info("Publishing TilesContext for context: " + context.getClass().getName());
  139           }
  140           setAttribute(context, CONTAINER_ATTRIBUTE, container);
  141       }
  142   
  143       /**
  144        * Returns the Tiles application context object.
  145        *
  146        * @param context The (application) context to use.
  147        * @return The required Tiles application context.
  148        * @deprecated Use one of the environment-specific Utilities (e.g.
  149        * ServletUtil).
  150        */
  151       @Deprecated
  152       public static TilesApplicationContext getApplicationContext(Object context) {
  153           TilesContainer container = getContainer(context);
  154           if (container != null) {
  155               return container.getApplicationContext();
  156           }
  157           return (TilesApplicationContext) getAttribute(context, CONTEXT_ATTRIBUTE);
  158       }
  159   
  160       /**
  161        * Returns an attribute from a context.
  162        *
  163        * @param context The context object to use.
  164        * @param attributeName The name of the attribute to search for.
  165        * @return The object, that is the value of the specified attribute.
  166        */
  167       private static Object getAttribute(Object context, String attributeName) {
  168           Class<?> contextClass = context.getClass();
  169           Method attrMethod = ClassUtil.getForcedAccessibleMethod(
  170                   contextClass, "getAttribute", String.class);
  171           return ClassUtil.invokeMethod(context, attrMethod, attributeName);
  172       }
  173   
  174       /**
  175        * Sets an attribute in a context.
  176        *
  177        * @param context The context object to use.
  178        * @param name The name of the attribute to set.
  179        * @param value The value of the attribute to set.
  180        * @throws TilesAccessException If something goes wrong during setting the
  181        * attribute.
  182        */
  183       private static void setAttribute(Object context, String name, Object value) {
  184           Class<?> contextClass = context.getClass();
  185           Method attrMethod = ClassUtil.getForcedAccessibleMethod(
  186                   contextClass, "setAttribute", String.class, Object.class);
  187           ClassUtil.invokeMethod(context, attrMethod, name, value);
  188       }
  189   
  190       /**
  191        * Removes an attribute from a context.
  192        *
  193        * @param context The context object to use.
  194        * @param name The name of the attribute to remove.
  195        * @throws TilesAccessException If something goes wrong during removal.
  196        */
  197       private static void removeAttribute(Object context, String name) {
  198           Class<?> contextClass = context.getClass();
  199           Method attrMethod = ClassUtil.getForcedAccessibleMethod(
  200                   contextClass, "removeAttribute", String.class);
  201           ClassUtil.invokeMethod(context, attrMethod, name);
  202       }
  203   }

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