View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.url.core.arg;
19  
20  import com.google.common.base.Strings;
21  import lombok.AccessLevel;
22  import lombok.RequiredArgsConstructor;
23  
24  import java.util.Optional;
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  
28  /**
29   * URL argument line.
30   */
31  @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
32  public final class URLArgumentLine {
33      
34      private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile("\\$\\$\\{(.*?)::(.*?)}");
35      
36      private final Matcher placeholderMatcher;
37      
38      /**
39       * Parse URL argument line.
40       *
41       * @param line line
42       * @return parsed URL argument line
43       */
44      public static Optional<URLArgumentLine> parse(final String line) {
45          Matcher matcher = PLACEHOLDER_PATTERN.matcher(line);
46          if (!matcher.find()) {
47              return Optional.empty();
48          }
49          return Optional.of(new URLArgumentLine(matcher));
50      }
51      
52      /**
53       * Replace argument.
54       *
55       * @param type placeholder type
56       * @return replaced argument
57       */
58      public String replaceArgument(final URLArgumentPlaceholderType type) {
59          placeholderMatcher.reset();
60          StringBuffer result = new StringBuffer();
61          while (placeholderMatcher.find()) {
62              String variableName = placeholderMatcher.group(1);
63              String defaultValue = placeholderMatcher.group(2);
64              String argumentValue = getArgumentValue(variableName, type);
65              if (Strings.isNullOrEmpty(argumentValue)) {
66                  argumentValue = defaultValue;
67              }
68              placeholderMatcher.appendReplacement(result, argumentValue);
69          }
70          placeholderMatcher.appendTail(result);
71          return rightTrim(result);
72      }
73      
74      private String rightTrim(final StringBuffer buffer) {
75          while (buffer.length() > 0 && Character.isWhitespace(buffer.charAt(buffer.length() - 1))) {
76              buffer.deleteCharAt(buffer.length() - 1);
77          }
78          return buffer.toString();
79      }
80      
81      private String getArgumentValue(final String argName, final URLArgumentPlaceholderType type) {
82          if (URLArgumentPlaceholderType.ENVIRONMENT == type) {
83              return System.getenv(argName);
84          }
85          if (URLArgumentPlaceholderType.SYSTEM_PROPS == type) {
86              return System.getProperty(argName);
87          }
88          return null;
89      }
90  }