001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.svl; 014 015import static org.apache.juneau.internal.ClassUtils.*; 016import java.util.*; 017import java.util.concurrent.*; 018 019import org.apache.juneau.collections.*; 020import org.apache.juneau.reflect.*; 021 022/** 023 * Configurable properties on the {@link VarResolver} class. 024 * 025 * <p> 026 * Used to associate {@link Var Vars} and context objects with {@link VarResolver VarResolvers}. 027 * 028 * <ul class='seealso'> 029 * <li class='link'>{@doc VarResolvers} 030 * </ul> 031 */ 032public class VarResolverContext { 033 034 private final Class<?>[] vars; 035 private final Map<String,Var> varMap; 036 private final Map<String,Object> contextObjects; 037 038 /** 039 * Constructor. 040 * 041 * @param vars The Var classes used for resolving string variables. 042 * @param contextObjects Read-only context objects. 043 */ 044 public VarResolverContext(Class<? extends Var>[] vars, Map<String,Object> contextObjects) { 045 046 this.vars = Arrays.copyOf(vars, vars.length); 047 048 Map<String,Var> m = new ConcurrentSkipListMap<>(); 049 for (Class<?> c : vars) { 050 ClassInfo ci = ClassInfo.of(c); 051 if (! ci.isChildOf(Var.class)) 052 throw new VarResolverException("Invalid variable class ''{0}''. Must extend from Var.", ci); 053 Var v = castOrCreate(Var.class, c); 054 m.put(v.getName(), v); 055 } 056 057 this.varMap = AMap.unmodifiable(m); 058 this.contextObjects = AMap.unmodifiable(contextObjects); 059 } 060 061 /** 062 * Returns an unmodifiable map of {@link Var Vars} associated with this context. 063 * 064 * @return A map whose keys are var names (e.g. <js>"S"</js>) and values are {@link Var} instances. 065 */ 066 protected Map<String,Var> getVarMap() { 067 return varMap; 068 } 069 070 /** 071 * Returns an array of variables define in this variable resolver context. 072 * 073 * @return A new array containing the variables in this context. 074 */ 075 protected Class<?>[] getVars() { 076 return Arrays.copyOf(vars, vars.length); 077 } 078 079 /** 080 * Returns the context object with the specified name. 081 * 082 * @param name The name of the context object. 083 * @return The context object, or <jk>null</jk> if no context object is specified with that name. 084 */ 085 protected Object getContextObject(String name) { 086 return contextObjects == null ? null : contextObjects.get(name); 087 } 088 089 /** 090 * Returns the context map of this variable resolver context. 091 * 092 * @return An unmodifiable map of the context objects of this variable resolver context. 093 */ 094 protected Map<String,Object> getContextObjects() { 095 return contextObjects; 096 } 097}