1 /* 2 * $Id: TilesContextELResolver.java 816924 2009-09-19 13:45:40Z 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.el; 22 23 import java.beans.FeatureDescriptor; 24 import java.util.Iterator; 25 26 import javax.el.BeanELResolver; 27 import javax.el.ELContext; 28 29 import org.apache.tiles.TilesApplicationContext; 30 import org.apache.tiles.context.TilesRequestContext; 31 import org.apache.tiles.util.CombinedBeanInfo; 32 33 /** 34 * Resolves properties of {@link TilesRequestContext} and 35 * {@link TilesApplicationContext}. 36 * 37 * @version $Rev: 816924 $ $Date: 2009-09-19 15:45:40 +0200 (sab, 19 set 2009) $ 38 * @since 2.2.1 39 */ 40 public class TilesContextELResolver extends BeanELResolver { 41 42 /** 43 * The beaninfos about {@link TilesRequestContext} and {@link TilesApplicationContext}. 44 */ 45 private CombinedBeanInfo requestBeanInfo = new CombinedBeanInfo( 46 TilesRequestContext.class, TilesApplicationContext.class); 47 48 /** {@inheritDoc} */ 49 @Override 50 public Class<?> getCommonPropertyType(ELContext context, Object base) { 51 // only resolve at the root of the context 52 if (base != null) { 53 return null; 54 } 55 56 return String.class; 57 } 58 59 /** {@inheritDoc} */ 60 @Override 61 public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, 62 Object base) { 63 // only resolve at the root of the context 64 if (base != null) { 65 return null; 66 } 67 68 return requestBeanInfo.getDescriptors().iterator(); 69 } 70 71 /** {@inheritDoc} */ 72 @Override 73 public Class<?> getType(ELContext context, Object base, Object property) { 74 // only resolve at the root of the context 75 if (base != null) { 76 return null; 77 } 78 79 Class<?> retValue = null; 80 if (requestBeanInfo.getProperties(TilesRequestContext.class).contains(property)) { 81 TilesRequestContext request = (TilesRequestContext) context 82 .getContext(TilesRequestContext.class); 83 retValue = super.getType(context, request, property); 84 } else if (requestBeanInfo.getProperties(TilesApplicationContext.class).contains(property)) { 85 TilesApplicationContext applicationContext = (TilesApplicationContext) context 86 .getContext(TilesApplicationContext.class); 87 retValue = super.getType(context, applicationContext, property); 88 } 89 90 if (retValue != null) { 91 context.setPropertyResolved(true); 92 } 93 94 return retValue; 95 } 96 97 /** {@inheritDoc} */ 98 @Override 99 public Object getValue(ELContext context, Object base, Object property) { 100 // only resolve at the root of the context 101 if (base != null) { 102 return null; 103 } 104 105 Object retValue = null; 106 107 if (requestBeanInfo.getProperties(TilesRequestContext.class).contains(property)) { 108 TilesRequestContext request = (TilesRequestContext) context 109 .getContext(TilesRequestContext.class); 110 retValue = super.getValue(context, request, property); 111 } else if (requestBeanInfo.getProperties(TilesApplicationContext.class) 112 .contains(property)) { 113 TilesApplicationContext applicationContext = (TilesApplicationContext) context 114 .getContext(TilesApplicationContext.class); 115 retValue = super.getValue(context, applicationContext, property); 116 } 117 118 if (retValue != null) { 119 context.setPropertyResolved(true); 120 } 121 122 return retValue; 123 } 124 125 /** {@inheritDoc} */ 126 @Override 127 public boolean isReadOnly(ELContext context, Object base, Object property) { 128 if (context == null) { 129 throw new NullPointerException(); 130 } 131 132 return true; 133 } 134 135 /** {@inheritDoc} */ 136 @Override 137 public void setValue(ELContext context, Object base, Object property, 138 Object value) { 139 // Does nothing for the moment. 140 } 141 }