001/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
002 *
003 * Copyright © 2017-2018 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.configuration.spi;
018
019import java.io.Serializable;
020
021import java.util.Collections;
022import java.util.Map;
023import java.util.Set;
024
025import org.microbean.configuration.api.ConfigurationValue;
026
027/**
028 * An {@link AbstractConfiguration} providing access to {@linkplain
029 * System#getenv(String) environment variabes}.
030 *
031 * @author <a href="https://about.me/lairdnelson"
032 * target="_parent">Laird Nelson</a>
033 *
034 * @see #getValue(Map, String)
035 */
036public final class EnvironmentVariablesConfiguration extends AbstractConfiguration implements Ranked, Serializable {
037
038
039  /*
040   * Static fields.
041   */
042
043  
044  /**
045   * The version of this class for {@linkplain Serializable
046   * serialization} purposes.
047   */
048  private static final long serialVersionUID = 1L;
049
050
051  /*
052   * Constructors.
053   */
054
055
056  /**
057   * Creates a new {@link EnvironmentVariablesConfiguration}.
058   */
059  public EnvironmentVariablesConfiguration() {
060    super();
061  }
062
063
064  /*
065   * Instance methods.
066   */
067  
068
069  @Override
070  public final int getRank() {
071    return 200;
072  }
073
074  
075  /**
076   * Returns a {@link ConfigurationValue} representing the {@linkplain
077   * System#getenv(String) environment variable} identified by the
078   * supplied {@code name}, or {@code null}.
079   *
080   * @param coordinates the configuration coordinates in effect for
081   * the current request; may be {@code null}
082   *
083   * @param name the name of the configuration property for which to
084   * return a {@link ConfigurationValue}; may be {@code null}
085   *
086   * @return a {@link ConfigurationValue}, or {@code null}
087   */
088  @Override
089  public final ConfigurationValue getValue(final Map<String, String> coordinates, final String name) {
090    ConfigurationValue returnValue = null;
091    if (name != null) {
092      final String propertyValue = System.getenv(name);
093      if (propertyValue != null) {
094        returnValue = new ConfigurationValue(this, null /* deliberately null coordinates */, name, propertyValue, false);
095      }
096    }
097    return returnValue;
098  }
099
100  /**
101   * Returns a {@link Set} of the names of all {@link
102   * ConfigurationValue}s that might be returned by this {@link
103   * Configuration}.
104   *
105   * <p>This implementation does not return {@code null}.</p>
106   *
107   * <p>This implementation returns the equivalent of {@link
108   * System#getenv() System.getenv().keySet()}.</p>
109   *
110   * @return a non-{@code null} {@link Set} of names
111   */
112  @Override
113  public final Set<String> getNames() {
114    return System.getenv().keySet();
115  }
116
117  @Override
118  public final int hashCode() {
119    return System.getenv().hashCode();
120  }
121
122  @Override
123  public final boolean equals(final Object other) {
124    return other instanceof EnvironmentVariablesConfiguration;
125  }
126
127  @Override
128  public final String toString() {
129    return System.getenv().toString();
130  }
131
132}