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.juneau.commons.svl.vars; 018 019import java.nio.file.*; 020 021import org.apache.juneau.commons.settings.*; 022import org.apache.juneau.commons.svl.*; 023 024/** 025 * Dotenv file variable resolver. 026 * 027 * <p> 028 * The format for this var is <js>"$DE{key[,default]}"</js>. 029 * 030 * <p> 031 * Values are resolved from {@link DotenvPropertySource} using either: 032 * <ul class='spaced-list'> 033 * <li>The default dotenv path resolution when using the no-arg constructor. 034 * <li>The path provided via {@link #create(Path)}. 035 * </ul> 036 */ 037public class DotenvVar extends DefaultingVar { 038 039 /** The name of this variable. */ 040 public static final String NAME = "DE"; 041 042 /** 043 * Creates a {@link DotenvVar} bound to a specific dotenv file path. 044 * 045 * @param path The dotenv file path. Can be <jk>null</jk>, in which case no properties are loaded. 046 * @return A new {@link DotenvVar} instance. 047 */ 048 public static DotenvVar create(Path path) { 049 return new DotenvVar(path); 050 } 051 052 private final DotenvPropertySource source; 053 054 /** 055 * Constructor. 056 */ 057 public DotenvVar() { 058 super(NAME); 059 this.source = new DotenvPropertySource(); 060 } 061 062 private DotenvVar(Path path) { 063 super(NAME); 064 this.source = new DotenvPropertySource(path); 065 } 066 067 @Override /* Overridden from Var */ 068 public String resolve(VarResolverSession session, String key) { 069 var result = source.get(key); 070 return result.isPresent() ? result.value().orElse(null) : null; 071 } 072}