1// Copyright 2017 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.
14package com.google.devtools.common.options;
15
16import com.google.common.base.CharMatcher;
17import com.google.common.base.Strings;
18import com.google.common.io.BaseEncoding;
19import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy;
20import com.google.protobuf.InvalidProtocolBufferException;
21import com.google.protobuf.TextFormat;
22
23/**
24 * Parses the given InvocationPolicy string, which may be a base64-encoded binary-serialized
25 * InvocationPolicy message, or a text formatted InvocationPolicy message. Note that the text
26 * format is not backwards compatible as the binary format is.
27 */
28public class InvocationPolicyParser {
29  /**
30   * Parses InvocationPolicy in either of the accepted formats. Returns an empty policy if no policy
31   * is provided.
32   *
33   * @throws com.google.devtools.common.options.OptionsParsingException if the value of
34   *     --invocation_policy is invalid.
35   */
36  public static InvocationPolicy parsePolicy(String policy) throws OptionsParsingException {
37    if (Strings.isNullOrEmpty(policy)) {
38      return InvocationPolicy.getDefaultInstance();
39    }
40
41    try {
42      try {
43        // First try decoding the policy as a base64 encoded binary proto.
44        return InvocationPolicy.parseFrom(
45            BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(policy)));
46      } catch (IllegalArgumentException e) {
47        // If the flag value can't be decoded from base64, try decoding the policy as a text
48        // formatted proto.
49        InvocationPolicy.Builder builder = InvocationPolicy.newBuilder();
50        TextFormat.merge(policy, builder);
51        return builder.build();
52      }
53    } catch (InvalidProtocolBufferException | TextFormat.ParseException e) {
54      throw new OptionsParsingException("Malformed value of --invocation_policy: " + policy, e);
55    }
56  }
57}
58