View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.internal.impl.model;
20  
21  import java.nio.file.Path;
22  
23  import org.apache.maven.api.di.Inject;
24  import org.apache.maven.api.di.Named;
25  import org.apache.maven.api.di.Singleton;
26  import org.apache.maven.api.model.ActivationFile;
27  import org.apache.maven.api.services.model.PathTranslator;
28  import org.apache.maven.api.services.model.ProfileActivationContext;
29  import org.apache.maven.api.services.model.RootLocator;
30  import org.codehaus.plexus.interpolation.AbstractValueSource;
31  import org.codehaus.plexus.interpolation.InterpolationException;
32  import org.codehaus.plexus.interpolation.MapBasedValueSource;
33  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
34  
35  /**
36   * Finds an absolute path for {@link ActivationFile#getExists()} or {@link ActivationFile#getMissing()}
37   *
38   */
39  @Named
40  @Singleton
41  public class ProfileActivationFilePathInterpolator {
42  
43      private final PathTranslator pathTranslator;
44  
45      private final RootLocator rootLocator;
46  
47      @Inject
48      public ProfileActivationFilePathInterpolator(PathTranslator pathTranslator, RootLocator rootLocator) {
49          this.pathTranslator = pathTranslator;
50          this.rootLocator = rootLocator;
51      }
52  
53      /**
54       * Interpolates given {@code path}.
55       *
56       * @return absolute path or {@code null} if the input was {@code null}
57       */
58      public String interpolate(String path, ProfileActivationContext context) throws InterpolationException {
59          if (path == null) {
60              return null;
61          }
62  
63          RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
64  
65          Path basedir = context.getProjectDirectory();
66  
67          if (basedir != null) {
68              interpolator.addValueSource(new AbstractValueSource(false) {
69                  @Override
70                  public Object getValue(String expression) {
71                      if ("basedir".equals(expression) || "project.basedir".equals(expression)) {
72                          return basedir.toAbsolutePath().toString();
73                      }
74                      return null;
75                  }
76              });
77          } else if (path.contains("${basedir}")) {
78              return null;
79          }
80  
81          interpolator.addValueSource(new AbstractValueSource(false) {
82              @Override
83              public Object getValue(String expression) {
84                  if ("rootDirectory".equals(expression)) {
85                      Path root = rootLocator.findMandatoryRoot(basedir);
86                      return root.toFile().getAbsolutePath();
87                  }
88                  return null;
89              }
90          });
91  
92          interpolator.addValueSource(new MapBasedValueSource(context.getProjectProperties()));
93          interpolator.addValueSource(new MapBasedValueSource(context.getUserProperties()));
94          interpolator.addValueSource(new MapBasedValueSource(context.getSystemProperties()));
95  
96          String absolutePath = interpolator.interpolate(path, "");
97  
98          return pathTranslator.alignToBaseDirectory(absolutePath, basedir);
99      }
100 }