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 static final String KV_SEPARATOR = "::";
37      
38      private final String argName;
39      
40      private final String argDefaultValue;
41      
42      private final Matcher placeholderMatcher;
43      
44      /**
45       * Parse URL argument line.
46       *
47       * @param line line
48       * @return parsed URL argument line
49       */
50      public static Optional<URLArgumentLine> parse(final String line) {
51          Matcher matcher = URLArgumentLine.PLACEHOLDER_PATTERN.matcher(line);
52          if (!matcher.find()) {
53              return Optional.empty();
54          }
55          String[] parsedArg = matcher.group(1).split(KV_SEPARATOR, 2);
56          return Optional.of(new URLArgumentLine(parsedArg[0], parsedArg[1], matcher));
57      }
58      
59      /**
60       * Replace argument.
61       *
62       * @param type placeholder type
63       * @return replaced argument
64       */
65      public String replaceArgument(final URLArgumentPlaceholderType type) {
66          String argumentValue = getArgumentValue(type);
67          if (!Strings.isNullOrEmpty(argumentValue)) {
68              return placeholderMatcher.replaceAll(argumentValue);
69          }
70          if (!argDefaultValue.isEmpty()) {
71              return placeholderMatcher.replaceAll(argDefaultValue);
72          }
73          String modifiedLineWithSpace = placeholderMatcher.replaceAll("");
74          return modifiedLineWithSpace.substring(0, modifiedLineWithSpace.length() - 1);
75      }
76      
77      private String getArgumentValue(final URLArgumentPlaceholderType type) {
78          if (URLArgumentPlaceholderType.ENVIRONMENT == type) {
79              return System.getenv(argName);
80          }
81          if (URLArgumentPlaceholderType.SYSTEM_PROPS == type) {
82              return System.getProperty(argName);
83          }
84          return null;
85      }
86  }