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 static org.apache.juneau.internal.CollectionUtils.*; 017 018import java.util.*; 019import java.util.concurrent.*; 020 021import org.apache.juneau.reflect.*; 022 023/** 024 * Configurable properties on the {@link VarResolver} class. 025 * 026 * <p> 027 * Used to associate {@link Var Vars} and context objects with {@link VarResolver VarResolvers}. 028 * 029 * <ul class='seealso'> 030 * <li class='link'>{@doc juneau-svl.VarResolvers} 031 * </ul> 032 */ 033public class VarResolverContext { 034 035 private final Class<?>[] vars; 036 private final Map<String,Var> varMap; 037 private final Map<String,Object> contextObjects; 038 039 /** 040 * Constructor. 041 * 042 * @param vars The Var classes used for resolving string variables. 043 * @param contextObjects Read-only context objects. 044 */ 045 public VarResolverContext(Class<? extends Var>[] vars, Map<String,Object> contextObjects) { 046 047 this.vars = Arrays.copyOf(vars, vars.length); 048 049 Map<String,Var> m = new ConcurrentSkipListMap<>(); 050 for (Class<?> c : vars) { 051 ClassInfo ci = ClassInfo.of(c); 052 if (! ci.isChildOf(Var.class)) 053 throw new VarResolverException("Invalid variable class ''{0}''. Must extend from Var.", ci); 054 Var v = castOrCreate(Var.class, c); 055 m.put(v.getName(), v); 056 } 057 058 this.varMap = unmodifiableMap(m); 059 this.contextObjects = immutableMap(contextObjects); 060 } 061 062 /** 063 * Returns an unmodifiable map of {@link Var Vars} associated with this context. 064 * 065 * @return A map whose keys are var names (e.g. <js>"S"</js>) and values are {@link Var} instances. 066 */ 067 protected Map<String,Var> getVarMap() { 068 return varMap; 069 } 070 071 /** 072 * Returns an array of variables define in this variable resolver context. 073 * 074 * @return A new array containing the variables in this context. 075 */ 076 protected Class<?>[] getVars() { 077 return Arrays.copyOf(vars, vars.length); 078 } 079 080 /** 081 * Returns the context object with the specified name. 082 * 083 * @param name The name of the context object. 084 * @return The context object, or <jk>null</jk> if no context object is specified with that name. 085 */ 086 protected Object getContextObject(String name) { 087 return contextObjects == null ? null : contextObjects.get(name); 088 } 089 090 /** 091 * Returns the context map of this variable resolver context. 092 * 093 * @return An unmodifiable map of the context objects of this variable resolver context. 094 */ 095 protected Map<String,Object> getContextObjects() { 096 return contextObjects; 097 } 098}