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.io.File;
23  import java.math.BigInteger;
24  import java.net.URI;
25  import java.security.MessageDigest;
26  
27  /**
28   * @author Hans Dockter
29   */
30  public class PathAssembler
31  {
32      public static final String MAVEN_USER_HOME_STRING = "MAVEN_USER_HOME";
33  
34      public static final String PROJECT_STRING = "PROJECT";
35  
36      private File mavenUserHome;
37  
38      public PathAssembler()
39      {
40      }
41  
42      public PathAssembler( File mavenUserHome )
43      {
44          this.mavenUserHome = mavenUserHome;
45      }
46  
47      /**
48       * Determines the local locations for the distribution to use given the supplied configuration.
49       *
50       * @param configuration a wrapper configuration
51       * @return the local distribution
52       */
53      public LocalDistribution getDistribution( WrapperConfiguration configuration )
54      {
55          String baseName = getDistName( configuration.getDistribution() );
56          String distName = removeExtension( baseName );
57          String rootDirName = rootDirName( distName, configuration );
58          File distDir = new File( getBaseDir( configuration.getDistributionBase() ),
59                                   configuration.getDistributionPath() + "/" + rootDirName );
60          File distZip = new File( getBaseDir( configuration.getZipBase() ),
61                                   configuration.getZipPath() + "/" + rootDirName + "/" + baseName );
62          return new LocalDistribution( distDir, distZip );
63      }
64  
65      private String rootDirName( String distName, WrapperConfiguration configuration )
66      {
67          String urlHash = getMd5Hash( configuration.getDistribution().toString() );
68          return String.format( "%s/%s", distName, urlHash );
69      }
70  
71      private String getMd5Hash( String string )
72      {
73          try
74          {
75              MessageDigest messageDigest = MessageDigest.getInstance( "MD5" );
76              byte[] bytes = string.getBytes();
77              messageDigest.update( bytes );
78              return new BigInteger( 1, messageDigest.digest() ).toString( 32 );
79          }
80          catch ( Exception e )
81          {
82              throw new RuntimeException( "Could not hash input string.", e );
83          }
84      }
85  
86      private String removeExtension( String name )
87      {
88          int p = name.lastIndexOf( "." );
89          if ( p < 0 )
90          {
91              return name;
92          }
93          return name.substring( 0, p );
94      }
95  
96      private String getDistName( URI distUrl )
97      {
98          String path = distUrl.getPath();
99          int p = path.lastIndexOf( "/" );
100         if ( p < 0 )
101         {
102             return path;
103         }
104         return path.substring( p + 1 );
105     }
106 
107     private File getBaseDir( String base )
108     {
109         if ( base.equals( MAVEN_USER_HOME_STRING ) )
110         {
111             return mavenUserHome;
112         }
113         else if ( base.equals( PROJECT_STRING ) )
114         {
115             return new File( System.getProperty( "user.dir" ) );
116         }
117         else
118         {
119             throw new RuntimeException( "Base: " + base + " is unknown" );
120         }
121     }
122 
123     /**
124      * Local distribution
125      */
126     public class LocalDistribution
127     {
128         private final File distZip;
129 
130         private final File distDir;
131 
132         public LocalDistribution( File distDir, File distZip )
133         {
134             this.distDir = distDir;
135             this.distZip = distZip;
136         }
137 
138         /**
139          * Returns the location to install the distribution into.
140          * @return the location to install the distribution into
141          */
142         public File getDistributionDir()
143         {
144             return distDir;
145         }
146 
147         /**
148          * Returns the location to install the distribution ZIP file to.
149          * @return the location to install the distribution ZIP file to
150          */
151         public File getZipFile()
152         {
153             return distZip;
154         }
155     }
156 }