1 /* 2 * $Id: ScopeVariableResolverFactory.java 1049688 2010-12-15 20:15:41Z 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.mvel; 23 24 import java.util.Map; 25 26 import org.apache.tiles.context.TilesRequestContextHolder; 27 import org.apache.tiles.request.Request; 28 import org.mvel2.integration.VariableResolver; 29 30 /** 31 * Resolves beans stored in request, session and application scopes. 32 * 33 * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $ 34 * @since 2.2.0 35 */ 36 public class ScopeVariableResolverFactory extends 37 ReadOnlyVariableResolverFactory { 38 39 /** 40 * The length of the scope suffix: "Scope". 41 */ 42 private static final int SCOPE_SUFFIX_LENGTH = 5; 43 44 /** 45 * Constructor. 46 * 47 * @param requestHolder The Tiles request holder. 48 * @since 2.2.0 49 */ 50 public ScopeVariableResolverFactory(TilesRequestContextHolder requestHolder) { 51 super(requestHolder); 52 } 53 54 /** {@inheritDoc} */ 55 @Override 56 public VariableResolver createVariableResolver(String name) { 57 return new ScopeVariableResolver(name); 58 } 59 60 /** {@inheritDoc} */ 61 public boolean isTarget(String name) { 62 Request request = requestHolder.getTilesRequestContext(); 63 if (name.endsWith("Scope")) { 64 String scopeName = name.substring(0, name.length() - SCOPE_SUFFIX_LENGTH); 65 for (String availableScope : request.getAvailableScopes()) { 66 if (scopeName.equals(availableScope)) { 67 return true; 68 } 69 } 70 } 71 return false; 72 } 73 74 /** 75 * Resolves a single attribute stored in request, session or application scope. 76 * 77 * @version $Rev: 1049688 $ $Date: 2010-12-16 07:15:41 +1100 (Thu, 16 Dec 2010) $ 78 * @since 2.2.0 79 */ 80 private class ScopeVariableResolver extends ReadOnlyVariableResolver { 81 82 /** 83 * Constructor. 84 * 85 * @param name The name of the attribute. 86 * @since 2.2.0 87 */ 88 public ScopeVariableResolver(String name) { 89 super(name); 90 } 91 92 /** {@inheritDoc} */ 93 @SuppressWarnings("rawtypes") 94 public Class getType() { 95 return Map.class; 96 } 97 98 /** {@inheritDoc} */ 99 public Object getValue() { 100 Request request = requestHolder.getTilesRequestContext(); 101 return request.getContext(name.substring(0, name.length() - SCOPE_SUFFIX_LENGTH)); 102 } 103 } 104 }