Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 17 | import com.google.common.collect.Interner; |
| 18 | import com.google.devtools.build.lib.concurrent.BlazeInterners; |
| 19 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
| 20 | import com.google.devtools.build.skyframe.AbstractSkyKey; |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.skyframe.SkyFunction; |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.skyframe.SkyFunctionName; |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.skyframe.SkyKey; |
| 24 | import com.google.devtools.build.skyframe.SkyValue; |
| 25 | import java.util.Map; |
Damien Martin-Guillerez | ea459a0 | 2017-02-01 23:32:33 +0000 | [diff] [blame] | 26 | import java.util.concurrent.atomic.AtomicReference; |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 27 | import javax.annotation.Nullable; |
| 28 | |
| 29 | /** The Skyframe function that generates values for variables of the client environment. */ |
| 30 | public final class ClientEnvironmentFunction implements SkyFunction { |
janakr | 5fb2a48 | 2018-03-02 17:48:57 -0800 | [diff] [blame] | 31 | public static Key key(String keyString) { |
| 32 | return ClientEnvironmentFunction.Key.create(keyString); |
| 33 | } |
| 34 | |
| 35 | @AutoCodec.VisibleForSerialization |
| 36 | @AutoCodec |
| 37 | static class Key extends AbstractSkyKey<String> { |
| 38 | private static final Interner<Key> interner = BlazeInterners.newWeakInterner(); |
| 39 | |
| 40 | private Key(String arg) { |
| 41 | super(arg); |
| 42 | } |
| 43 | |
| 44 | @AutoCodec.VisibleForSerialization |
| 45 | @AutoCodec.Instantiator |
| 46 | static Key create(String arg) { |
| 47 | return interner.intern(new Key(arg)); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public SkyFunctionName functionName() { |
| 52 | return SkyFunctions.CLIENT_ENVIRONMENT_VARIABLE; |
| 53 | } |
| 54 | } |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 55 | |
Damien Martin-Guillerez | ea459a0 | 2017-02-01 23:32:33 +0000 | [diff] [blame] | 56 | private final AtomicReference<Map<String, String>> clientEnv; |
| 57 | |
carmi | d6a9828 | 2018-03-13 19:19:16 -0700 | [diff] [blame] | 58 | public ClientEnvironmentFunction(AtomicReference<Map<String, String>> clientEnv) { |
Damien Martin-Guillerez | ea459a0 | 2017-02-01 23:32:33 +0000 | [diff] [blame] | 59 | this.clientEnv = clientEnv; |
| 60 | } |
| 61 | |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 62 | @Nullable |
| 63 | @Override |
Damien Martin-Guillerez | ea459a0 | 2017-02-01 23:32:33 +0000 | [diff] [blame] | 64 | public SkyValue compute(SkyKey key, Environment env) throws InterruptedException { |
| 65 | return new ClientEnvironmentValue(clientEnv.get().get((String) key.argument())); |
Klaus Aehlig | cd708c3 | 2016-09-15 13:48:47 +0000 | [diff] [blame] | 66 | } |
| 67 | } |