Coverage Report - org.apache.tiles.el.TilesContextBeanELResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
TilesContextBeanELResolver
100%
55/55
92%
24/26
3.111
 
 1  
 /*
 2  
  * $Id: TilesContextBeanELResolver.java 1291847 2012-02-21 15:09:30Z nlebas $
 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.el;
 22  
 
 23  
 import java.beans.FeatureDescriptor;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 
 29  
 import javax.el.ELContext;
 30  
 import javax.el.ELResolver;
 31  
 
 32  
 import org.apache.tiles.request.Request;
 33  
 
 34  
 /**
 35  
  * Resolves beans in request, session and application scope.
 36  
  *
 37  
  * @version $Rev: 1291847 $ $Date: 2012-02-22 02:09:30 +1100 (Wed, 22 Feb 2012) $
 38  
  * @since 2.2.1
 39  
  */
 40  12
 public class TilesContextBeanELResolver extends ELResolver {
 41  
 
 42  
     /** {@inheritDoc} */
 43  
     @Override
 44  
     public Class<?> getCommonPropertyType(ELContext context, Object base) {
 45  
         // only resolve at the root of the context
 46  2
         if (base != null) {
 47  1
             return null;
 48  
         }
 49  
 
 50  1
         return String.class;
 51  
     }
 52  
 
 53  
     /** {@inheritDoc} */
 54  
     @Override
 55  
     public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
 56  
             Object base) {
 57  1
         List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>();
 58  
 
 59  1
         Request request = (Request) context
 60  
                 .getContext(Request.class);
 61  1
         for (String scope : request.getAvailableScopes()) {
 62  3
             collectBeanInfo(request.getContext(scope), list);
 63  3
         }
 64  1
         return list.iterator();
 65  
     }
 66  
 
 67  
     /** {@inheritDoc} */
 68  
     @Override
 69  
     public Class<?> getType(ELContext context, Object base, Object property) {
 70  5
         if (base != null) {
 71  1
             return null;
 72  
         }
 73  
 
 74  4
         Object obj = findObjectByProperty(context, property);
 75  4
         if (obj != null) {
 76  3
             context.setPropertyResolved(true);
 77  3
             return obj.getClass();
 78  
         }
 79  1
         return null;
 80  
     }
 81  
 
 82  
     /** {@inheritDoc} */
 83  
     @Override
 84  
     public Object getValue(ELContext context, Object base, Object property) {
 85  20
         if (base != null) {
 86  9
             return null;
 87  
         }
 88  
 
 89  11
         Object retValue = findObjectByProperty(context, property);
 90  
 
 91  11
         if (retValue != null) {
 92  11
             context.setPropertyResolved(true);
 93  
         }
 94  
 
 95  11
         return retValue;
 96  
     }
 97  
 
 98  
     /** {@inheritDoc} */
 99  
     @Override
 100  
     public boolean isReadOnly(ELContext context, Object base, Object property) {
 101  2
         if (context == null) {
 102  1
             throw new NullPointerException();
 103  
         }
 104  
 
 105  1
         return true;
 106  
     }
 107  
 
 108  
     /** {@inheritDoc} */
 109  
     @Override
 110  
     public void setValue(ELContext context, Object base, Object property,
 111  
             Object value) {
 112  
         // Does nothing for the moment.
 113  1
     }
 114  
 
 115  
     /**
 116  
      * Collects bean infos from a map's values and filling a list.
 117  
      *
 118  
      * @param map The map containing the bean to be inspected.
 119  
      * @param list The list to fill.
 120  
      * @since 2.2.1
 121  
      */
 122  
     protected void collectBeanInfo(Map<String, ? extends Object> map,
 123  
             List<FeatureDescriptor> list) {
 124  7
         if (map == null || map.isEmpty()) {
 125  1
             return;
 126  
         }
 127  
 
 128  6
         for (Map.Entry<String, ? extends Object> entry : map.entrySet()) {
 129  6
             FeatureDescriptor descriptor = new FeatureDescriptor();
 130  6
             descriptor.setDisplayName(entry.getKey());
 131  6
             descriptor.setExpert(false);
 132  6
             descriptor.setHidden(false);
 133  6
             descriptor.setName(entry.getKey());
 134  6
             descriptor.setPreferred(true);
 135  6
             descriptor.setShortDescription("");
 136  6
             descriptor.setValue("type", String.class);
 137  6
             descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE);
 138  6
             list.add(descriptor);
 139  6
         }
 140  6
     }
 141  
 
 142  
     /**
 143  
      * Finds an object in request, session or application scope, in this order.
 144  
      *
 145  
      * @param context The context to use.
 146  
      * @param property The property used as an attribute name.
 147  
      * @return The found bean, if it exists, or <code>null</code> otherwise.
 148  
      * @since 2.2.1
 149  
      */
 150  
     protected Object findObjectByProperty(ELContext context, Object property) {
 151  18
         Object retValue = null;
 152  
 
 153  18
         Request request = (Request) context
 154  
                 .getContext(Request.class);
 155  
 
 156  18
         String prop = property.toString();
 157  
 
 158  18
         String[] scopes = request.getAvailableScopes().toArray(new String[0]);
 159  18
         int i = 0;
 160  
         do {
 161  35
             retValue = getObject(request.getContext(scopes[i]), prop);
 162  35
             i++;
 163  35
         } while (retValue == null && i < scopes.length);
 164  
 
 165  18
         return retValue;
 166  
     }
 167  
 
 168  
     /**
 169  
      * Returns an object from a map in a null-safe manner.
 170  
      *
 171  
      * @param map The map to use.
 172  
      * @param property The property to use as a key.
 173  
      * @return The object, if present, or <code>null</code> otherwise.
 174  
      * @since 2.2.1
 175  
      */
 176  
     protected Object getObject(Map<String, ? extends Object> map,
 177  
             String property) {
 178  38
         Object retValue = null;
 179  38
         if (map != null) {
 180  37
             retValue = map.get(property);
 181  
         }
 182  38
         return retValue;
 183  
     }
 184  
 }