001/* 002 * Copyright 2002-2013 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.context.expression; 018 019import org.springframework.core.env.Environment; 020import org.springframework.expression.AccessException; 021import org.springframework.expression.EvaluationContext; 022import org.springframework.expression.PropertyAccessor; 023import org.springframework.expression.TypedValue; 024 025/** 026 * Read-only EL property accessor that knows how to retrieve keys 027 * of a Spring {@link Environment} instance. 028 * 029 * @author Chris Beams 030 * @since 3.1 031 */ 032public class EnvironmentAccessor implements PropertyAccessor { 033 034 @Override 035 public Class<?>[] getSpecificTargetClasses() { 036 return new Class<?>[] {Environment.class}; 037 } 038 039 /** 040 * Can read any {@link Environment}, thus always returns true. 041 * @return true 042 */ 043 @Override 044 public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException { 045 return true; 046 } 047 048 /** 049 * Access the given target object by resolving the given property name against the given target 050 * environment. 051 */ 052 @Override 053 public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { 054 return new TypedValue(((Environment) target).getProperty(name)); 055 } 056 057 /** 058 * Read-only: returns {@code false}. 059 */ 060 @Override 061 public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { 062 return false; 063 } 064 065 /** 066 * Read-only: no-op. 067 */ 068 @Override 069 public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { 070 } 071 072}