1 package org.apache.maven.wrapper;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
49
50
51
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
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
140
141
142 public File getDistributionDir()
143 {
144 return distDir;
145 }
146
147
148
149
150
151 public File getZipFile()
152 {
153 return distZip;
154 }
155 }
156 }