001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*- 002 * 003 * Copyright © 2020 microBean™. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * 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 014 * implied. See the License for the specific language governing 015 * permissions and limitations under the License. 016 */ 017package org.microbean.settings; 018 019import java.lang.annotation.Annotation; 020 021import java.util.Collections; 022import java.util.Set; 023 024import javax.enterprise.context.ApplicationScoped; 025 026/** 027 * A {@link Source} that retrieves values from environment variables. 028 * 029 * @author <a href="https://about.me/lairdnelson" 030 * target="_parent">Laird Nelson</a> 031 * 032 * @see #getValue(String, Set) 033 */ 034@ApplicationScoped 035public class EnvironmentVariablesSource extends Source { 036 037 038 /* 039 * Constructors. 040 */ 041 042 043 /** 044 * Creates a new {@link EnvironmentVariablesSource}. 045 */ 046 public EnvironmentVariablesSource() { 047 super(); 048 } 049 050 051 /* 052 * Instance methods. 053 */ 054 055 056 /** 057 * Returns a {@link Value} suitable for the supplied {@code name} by 058 * making use of the {@link System#getenv(String)} method. 059 * 060 * @param name the name of the setting; must not be {@code null} 061 * 062 * @param qualifiers a {@link Set} of {@link Annotation}s qualifying 063 * the request; the {@link Value} returned by this method will 064 * {@linkplain Value#getQualifiers() have} a subset of these 065 * qualifiers; must not be {@code null} 066 * 067 * @return a suitable {@link Value}, or {@code null} if an 068 * invocation of {@link System#getenv(String)} with the supplied 069 * {@code name} returns {@code null} 070 * 071 * @exception NullPointerException if either {@code name} or {@code 072 * qualifiers} is {@code null} 073 * 074 * @threadsafety This method is safe for concurrent use by multiple 075 * threads. 076 * 077 * @idempotency No guarantees with respect to idempotency are made 078 * about this method. 079 * 080 * @nullability This method may return {@code null}. 081 */ 082 @Override 083 public Value getValue(final String name, final Set<Annotation> qualifiers) { 084 final Value returnValue; 085 final String stringValue = System.getenv(name); 086 if (stringValue == null) { 087 returnValue = null; 088 } else { 089 returnValue = new Value(this, name, Collections.emptySet(), false, stringValue); 090 } 091 return returnValue; 092 } 093 094}