View Javadoc
1   package org.apache.maven.wrapper;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.net.URI;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  
26  /**
27   * @author Hans Dockter
28   */
29  public class PathAssembler
30  {
31      public static final String MAVEN_USER_HOME_STRING = "MAVEN_USER_HOME";
32  
33      public static final String PROJECT_STRING = "PROJECT";
34  
35      private Path mavenUserHome;
36  
37      public PathAssembler()
38      {
39      }
40  
41      public PathAssembler( Path mavenUserHome )
42      {
43          this.mavenUserHome = mavenUserHome;
44      }
45  
46      /**
47       * Determines the local locations for the distribution to use given the supplied configuration.
48       *
49       * @param configuration a wrapper configuration
50       * @return the local distribution
51       */
52      public LocalDistribution getDistribution( WrapperConfiguration configuration )
53      {
54          String baseName = getBaseName( configuration.getDistribution() );
55          String distName = removeExtension( baseName );
56          Path rootDirName = rootDirName( distName, configuration );
57          Path distDir = getBaseDir( configuration.getDistributionBase() )
58                         .resolve( configuration.getDistributionPath() )
59                         .resolve( rootDirName );
60          Path distZip = getBaseDir( configuration.getZipBase() )
61                         .resolve( configuration.getZipPath() )
62                         .resolve( rootDirName )
63                         .resolve( baseName );
64          return new LocalDistribution( distDir, distZip );
65      }
66  
67      private Path rootDirName( String distName, WrapperConfiguration configuration )
68      {
69          String urlHash = getHash( configuration.getDistribution() );
70          return Paths.get( distName, urlHash );
71      }
72  
73      private String getHash( URI path )
74      {
75          return Integer.toHexString( path.hashCode() );
76      }
77  
78      private String removeExtension( String name )
79      {
80          int dot = name.lastIndexOf( "." );
81          return dot > 0 ? name.substring( 0, dot ) : name;
82      }
83  
84      private String getBaseName( URI distUrl )
85      {
86          return Paths.get( distUrl.getPath() ).getFileName().toString();
87      }
88  
89      private Path getBaseDir( String base )
90      {
91          if ( MAVEN_USER_HOME_STRING.equals( base ) )
92          {
93              return mavenUserHome;
94          }
95          else if ( PROJECT_STRING.equals( base ) )
96          {
97              return Paths.get( System.getProperty( "user.dir" ) );
98          }
99          else
100         {
101             throw new RuntimeException( "Base: " + base + " is unknown" );
102         }
103     }
104 
105     /**
106      * Local distribution
107      */
108     public static class LocalDistribution
109     {
110         private final Path distZip;
111 
112         private final Path distDir;
113 
114         public LocalDistribution( Path distDir, Path distZip )
115         {
116             this.distDir = distDir;
117             this.distZip = distZip;
118         }
119 
120         /**
121          * Returns the location to install the distribution into.
122          *
123          * @return the location to install the distribution into
124          */
125         public Path getDistributionDir()
126         {
127             return distDir;
128         }
129 
130         /**
131          * Returns the location to install the distribution ZIP file to.
132          *
133          * @return the location to install the distribution ZIP file to
134          */
135         public Path getZipFile()
136         {
137             return distZip;
138         }
139     }
140 }