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 021/** 022 * Configurable properties on the {@link VarResolver} class. 023 * 024 * <p> 025 * Used to associate {@link Var Vars} and context objects with {@link VarResolver VarResolvers}. 026 * 027 * <h5 class='section'>See Also:</h5> 028 * <ul> 029 * <li class='link'><a class="doclink" href="../../../../overview-summary.html#juneau-svl.VarResolvers">Overview > juneau-svl > VarResolvers and VarResolverSessions</a> 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 if (! isParentClass(Var.class, c)) 051 throw new RuntimeException("Invalid variable class. Must extend from Var"); 052 Var v = newInstance(Var.class, c); 053 m.put(v.getName(), v); 054 } 055 056 this.varMap = unmodifiableMap(m); 057 this.contextObjects = immutableMap(contextObjects); 058 } 059 060 /** 061 * Returns an unmodifiable map of {@link Var Vars} associated with this context. 062 * 063 * @return A map whose keys are var names (e.g. <js>"S"</js>) and values are {@link Var} instances. 064 */ 065 protected Map<String,Var> getVarMap() { 066 return varMap; 067 } 068 069 /** 070 * Returns an array of variables define in this variable resolver context. 071 * 072 * @return A new array containing the variables in this context. 073 */ 074 protected Class<?>[] getVars() { 075 return Arrays.copyOf(vars, vars.length); 076 } 077 078 /** 079 * Returns the context object with the specified name. 080 * 081 * @param name The name of the context object. 082 * @return The context object, or <jk>null</jk> if no context object is specified with that name. 083 */ 084 protected Object getContextObject(String name) { 085 return contextObjects == null ? null : contextObjects.get(name); 086 } 087 088 /** 089 * Returns the context map of this variable resolver context. 090 * 091 * @return An unmodifiable map of the context objects of this variable resolver context. 092 */ 093 protected Map<String,Object> getContextObjects() { 094 return contextObjects; 095 } 096}