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.wrapper;
20  
21  import java.net.URI;
22  import java.nio.file.Path;
23  import java.nio.file.Paths;
24  
25  /**
26   * @author Hans Dockter
27   */
28  public class PathAssembler {
29      public static final String MAVEN_USER_HOME_STRING = "MAVEN_USER_HOME";
30  
31      public static final String PROJECT_STRING = "PROJECT";
32  
33      private Path mavenUserHome;
34  
35      public PathAssembler() {}
36  
37      public PathAssembler(Path mavenUserHome) {
38          this.mavenUserHome = mavenUserHome;
39      }
40  
41      /**
42       * Determines the local locations for the distribution to use given the supplied configuration.
43       *
44       * @param configuration a wrapper configuration
45       * @return the local distribution
46       */
47      public LocalDistribution getDistribution(WrapperConfiguration configuration) {
48          String baseName = getBaseName(configuration.getDistribution());
49          String distName = removeExtension(baseName);
50          Path rootDirName = rootDirName(distName, configuration);
51          Path distDir = getBaseDir(configuration.getDistributionBase())
52                  .resolve(configuration.getDistributionPath())
53                  .resolve(rootDirName);
54          Path distZip = getBaseDir(configuration.getZipBase())
55                  .resolve(configuration.getZipPath())
56                  .resolve(rootDirName)
57                  .resolve(baseName);
58          return new LocalDistribution(distDir, distZip);
59      }
60  
61      private Path rootDirName(String distName, WrapperConfiguration configuration) {
62          String urlHash = getHash(configuration.getDistribution());
63          return Paths.get(distName, urlHash);
64      }
65  
66      private String getHash(URI path) {
67          return Integer.toHexString(path.hashCode());
68      }
69  
70      private String removeExtension(String name) {
71          int dot = name.lastIndexOf(".");
72          return dot > 0 ? name.substring(0, dot) : name;
73      }
74  
75      private String getBaseName(URI distUrl) {
76          if (distUrl.getScheme().equals("file")) {
77              return Paths.get(distUrl).getFileName().toString();
78          } else {
79              return Paths.get(distUrl.getPath()).getFileName().toString();
80          }
81      }
82  
83      private Path getBaseDir(String base) {
84          if (MAVEN_USER_HOME_STRING.equals(base)) {
85              return mavenUserHome;
86          } else if (PROJECT_STRING.equals(base)) {
87              return Paths.get(System.getProperty("user.dir"));
88          } else {
89              throw new RuntimeException("Base: " + base + " is unknown");
90          }
91      }
92  
93      /**
94       * Local distribution
95       */
96      public static class LocalDistribution {
97          private final Path distZip;
98  
99          private final Path distDir;
100 
101         public LocalDistribution(Path distDir, Path distZip) {
102             this.distDir = distDir;
103             this.distZip = distZip;
104         }
105 
106         /**
107          * Returns the location to install the distribution into.
108          *
109          * @return the location to install the distribution into
110          */
111         public Path getDistributionDir() {
112             return distDir;
113         }
114 
115         /**
116          * Returns the location to install the distribution ZIP file to.
117          *
118          * @return the location to install the distribution ZIP file to
119          */
120         public Path getZipFile() {
121             return distZip;
122         }
123     }
124 }