10
10
import java .io .FileInputStream ;
11
11
import java .io .FileOutputStream ;
12
12
import java .io .IOException ;
13
+ import java .io .InputStream ;
13
14
import java .lang .reflect .Method ;
14
15
import java .net .Authenticator ;
16
+ import java .net .HttpURLConnection ;
15
17
import java .net .URL ;
16
18
import java .net .URLClassLoader ;
17
19
import java .nio .channels .Channels ;
24
26
public class Start {
25
27
26
28
private static final String PROJECT_NAME = "grails-wrapper" ;
27
- private static final String BASE_URL = "http://repo.grails.org/grails/core/org/grails/" + PROJECT_NAME ;
29
+ private static final String WRAPPER_PATH = "/org/grails/" + PROJECT_NAME ;
30
+ private static final String DEFAULT_GRAILS_CORE_ARTIFACTORY_BASE_URL = "https://repo.grails.org/grails/core" ;
28
31
private static final File WRAPPER_DIR = new File (System .getProperty ("user.home" ) + "/.grails/wrapper" );
29
32
private static final File NO_VERSION_JAR = new File (WRAPPER_DIR , PROJECT_NAME + ".jar" );
30
33
31
- private static String getVersion () throws SAXException , ParserConfigurationException {
34
+ private static String getGrailsCoreArtifactoryBaseUrl () {
35
+ String baseUrl = System .getProperty ("grails.core.artifactory.baseUrl" );
36
+ if (baseUrl != null ) {
37
+ return baseUrl ;
38
+ }
39
+ baseUrl = System .getenv ("GRAILS_CORE_ARTIFACTORY_BASE_URL" );
40
+ if (baseUrl != null ) {
41
+ return baseUrl ;
42
+ }
43
+ return DEFAULT_GRAILS_CORE_ARTIFACTORY_BASE_URL ;
44
+ }
45
+
46
+ private static String getVersion () {
32
47
try {
33
48
SAXParserFactory factory = SAXParserFactory .newInstance ();
34
49
SAXParser saxParser = factory .newSAXParser ();
35
50
FindReleaseHandler findReleaseHandler = new FindReleaseHandler ();
36
- saxParser .parse (new URL (BASE_URL + "/maven-metadata.xml" ).openStream (), findReleaseHandler );
37
-
51
+ final String mavenMetadataFileUrl = getGrailsCoreArtifactoryBaseUrl () + WRAPPER_PATH + "/maven-metadata.xml" ;
52
+ HttpURLConnection conn = createHttpURLConnection (mavenMetadataFileUrl );
53
+ saxParser .parse (conn .getInputStream (), findReleaseHandler );
38
54
return findReleaseHandler .getVersion ();
39
55
} catch (Exception e ) {
40
56
if (!NO_VERSION_JAR .exists ()) {
@@ -46,6 +62,13 @@ private static String getVersion() throws SAXException, ParserConfigurationExcep
46
62
}
47
63
}
48
64
65
+ private static HttpURLConnection createHttpURLConnection (String mavenMetadataFileUrl ) throws IOException {
66
+ final URL url = new URL (mavenMetadataFileUrl );
67
+ HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
68
+ conn .setInstanceFollowRedirects (true );
69
+ return conn ;
70
+ }
71
+
49
72
50
73
private static boolean updateJar (String version ) {
51
74
@@ -57,25 +80,28 @@ private static boolean updateJar(String version) {
57
80
WRAPPER_DIR .mkdirs ();
58
81
59
82
try {
60
- File dowloadedJar = File .createTempFile (jarFileName , jarFileExtension );
61
-
62
- URL website = new URL (BASE_URL + "/" + version + "/" + jarFileName + jarFileExtension );
63
- ReadableByteChannel rbc = Channels .newChannel (website .openStream ());
64
- FileOutputStream fos = new FileOutputStream (dowloadedJar );
65
- fos .getChannel ().transferFrom (rbc , 0 , Long .MAX_VALUE );
66
- fos .close ();
67
-
68
- Files .move (dowloadedJar .getAbsoluteFile ().toPath (), NO_VERSION_JAR .getAbsoluteFile ().toPath (), REPLACE_EXISTING );
83
+ File downloadedJar = File .createTempFile (jarFileName , jarFileExtension );
69
84
70
- success = true ;
71
- } catch (Exception e ) {
85
+ final String wrapperUrl = getGrailsCoreArtifactoryBaseUrl () + WRAPPER_PATH + "/" + version + "/" + jarFileName + jarFileExtension ;
86
+ HttpURLConnection conn = createHttpURLConnection (wrapperUrl );
87
+ success = downloadWrapperJar (downloadedJar , conn .getInputStream ());
88
+ } catch (Exception e ) {
72
89
System .out .println ("There was an error downloading the wrapper jar" );
73
90
e .printStackTrace ();
74
91
}
75
92
76
93
return success ;
77
94
}
78
95
96
+ private static boolean downloadWrapperJar (File downloadedJar , InputStream inputStream ) throws IOException {
97
+ ReadableByteChannel rbc = Channels .newChannel (inputStream );
98
+ try (FileOutputStream fos = new FileOutputStream (downloadedJar )) {
99
+ fos .getChannel ().transferFrom (rbc , 0 , Long .MAX_VALUE );
100
+ }
101
+ Files .move (downloadedJar .getAbsoluteFile ().toPath (), NO_VERSION_JAR .getAbsoluteFile ().toPath (), REPLACE_EXISTING );
102
+ return true ;
103
+ }
104
+
79
105
public static void main (String [] args ) {
80
106
Authenticator .setDefault (new SystemPropertiesAuthenticator ());
81
107
@@ -115,16 +141,16 @@ public static void main(String[] args) {
115
141
if (outputStream != null ) {
116
142
outputStream .close ();
117
143
}
118
- }
144
+ }
119
145
}
120
146
}
121
147
}
122
148
}
123
149
124
- URLClassLoader child = new URLClassLoader (new URL [] {NO_VERSION_JAR .toURI ().toURL ()});
150
+ URLClassLoader child = new URLClassLoader (new URL []{NO_VERSION_JAR .toURI ().toURL ()});
125
151
Class classToLoad = Class .forName ("grails.init.RunCommand" , true , child );
126
152
Method main = classToLoad .getMethod ("main" , String [].class );
127
- main .invoke (null , (Object )args );
153
+ main .invoke (null , (Object ) args );
128
154
129
155
} catch (Exception e ) {
130
156
e .printStackTrace ();
0 commit comments