001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.configuration2.interpol; 018 019import java.lang.reflect.Field; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.apache.commons.lang3.ClassUtils; 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026 027/** 028 * <p> 029 * A specialized lookup implementation that allows access to constant fields of 030 * classes. 031 * </p> 032 * <p> 033 * Sometimes it is necessary in a configuration file to refer to a constant 034 * defined in a class. This can be done with this lookup implementation. 035 * Variable names passed in must be of the form 036 * {@code mypackage.MyClass.FIELD}. The {@code lookup()} method 037 * will split the passed in string at the last dot, separating the fully 038 * qualified class name and the name of the constant (i.e. <strong>static final</strong>) 039 * member field. Then the class is loaded and the field's value is obtained 040 * using reflection. 041 * </p> 042 * <p> 043 * Once retrieved values are cached for fast access. This class is thread-safe. 044 * It can be used as a standard (i.e. global) lookup object and serve multiple 045 * clients concurrently. 046 * </p> 047 * 048 * @since 1.4 049 * @author <a 050 * href="http://commons.apache.org/configuration/team-list.html">Commons 051 * Configuration team</a> 052 */ 053public class ConstantLookup implements Lookup 054{ 055 /** Constant for the field separator. */ 056 private static final char FIELD_SEPRATOR = '.'; 057 058 /** An internally used cache for already retrieved values. */ 059 private static Map<String, Object> constantCache = new HashMap<>(); 060 061 /** The logger. */ 062 private final Log log = LogFactory.getLog(getClass()); 063 064 /** 065 * Tries to resolve the specified variable. The passed in variable name is 066 * interpreted as the name of a <b>static final</b> member field of a 067 * class. If the value has already been obtained, it can be retrieved from 068 * an internal cache. Otherwise this method will invoke the 069 * {@code resolveField()} method and pass in the name of the class 070 * and the field. 071 * 072 * @param var the name of the variable to be resolved 073 * @return the value of this variable or <b>null</b> if it cannot be 074 * resolved 075 */ 076 @Override 077 public Object lookup(final String var) 078 { 079 if (var == null) 080 { 081 return null; 082 } 083 084 Object result; 085 synchronized (constantCache) 086 { 087 result = constantCache.get(var); 088 } 089 if (result != null) 090 { 091 return result; 092 } 093 094 final int fieldPos = var.lastIndexOf(FIELD_SEPRATOR); 095 if (fieldPos < 0) 096 { 097 return null; 098 } 099 try 100 { 101 final Object value = resolveField(var.substring(0, fieldPos), var 102 .substring(fieldPos + 1)); 103 if (value != null) 104 { 105 synchronized (constantCache) 106 { 107 // In worst case, the value will be fetched multiple times 108 // because of this lax synchronization, but for constant 109 // values this shouldn't be a problem. 110 constantCache.put(var, value); 111 } 112 result = value; 113 } 114 } 115 catch (final Exception ex) 116 { 117 log.warn("Could not obtain value for variable " + var, ex); 118 } 119 120 return result; 121 } 122 123 /** 124 * Clears the shared cache with the so far resolved constants. 125 */ 126 public static void clear() 127 { 128 synchronized (constantCache) 129 { 130 constantCache.clear(); 131 } 132 } 133 134 /** 135 * Determines the value of the specified constant member field of a class. 136 * This implementation will call {@code fetchClass()} to obtain the 137 * {@code java.lang.Class} object for the target class. Then it will 138 * use reflection to obtain the field's value. For this to work the field 139 * must be accessable. 140 * 141 * @param className the name of the class 142 * @param fieldName the name of the member field of that class to read 143 * @return the field's value 144 * @throws Exception if an error occurs 145 */ 146 protected Object resolveField(final String className, final String fieldName) 147 throws Exception 148 { 149 final Class<?> clazz = fetchClass(className); 150 final Field field = clazz.getField(fieldName); 151 return field.get(null); 152 } 153 154 /** 155 * Loads the class with the specified name. If an application has special 156 * needs regarding the class loaders to be used, it can hook in here. This 157 * implementation delegates to the {@code getClass()} method of 158 * Commons Lang's 159 * <code><a href="http://commons.apache.org/lang/api-release/org/apache/commons/lang/ClassUtils.html"> 160 * ClassUtils</a></code>. 161 * 162 * @param className the name of the class to be loaded 163 * @return the corresponding class object 164 * @throws ClassNotFoundException if the class cannot be loaded 165 */ 166 protected Class<?> fetchClass(final String className) throws ClassNotFoundException 167 { 168 return ClassUtils.getClass(className); 169 } 170}