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          return Paths.get(distUrl.getPath()).getFileName().toString();
77      }
78  
79      private Path getBaseDir(String base) {
80          if (MAVEN_USER_HOME_STRING.equals(base)) {
81              return mavenUserHome;
82          } else if (PROJECT_STRING.equals(base)) {
83              return Paths.get(System.getProperty("user.dir"));
84          } else {
85              throw new RuntimeException("Base: " + base + " is unknown");
86          }
87      }
88  
89      /**
90       * Local distribution
91       */
92      public static class LocalDistribution {
93          private final Path distZip;
94  
95          private final Path distDir;
96  
97          public LocalDistribution(Path distDir, Path distZip) {
98              this.distDir = distDir;
99              this.distZip = distZip;
100         }
101 
102         /**
103          * Returns the location to install the distribution into.
104          *
105          * @return the location to install the distribution into
106          */
107         public Path getDistributionDir() {
108             return distDir;
109         }
110 
111         /**
112          * Returns the location to install the distribution ZIP file to.
113          *
114          * @return the location to install the distribution ZIP file to
115          */
116         public Path getZipFile() {
117             return distZip;
118         }
119     }
120 }