1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.boot; |
12 | |
|
13 | |
import org.mule.util.ClassUtils; |
14 | |
import org.mule.util.FileUtils; |
15 | |
import org.mule.util.JarUtils; |
16 | |
import org.mule.util.StringUtils; |
17 | |
|
18 | |
import java.io.BufferedReader; |
19 | |
import java.io.File; |
20 | |
import java.io.FileReader; |
21 | |
import java.io.IOException; |
22 | |
import java.io.InputStreamReader; |
23 | |
import java.util.LinkedHashMap; |
24 | |
|
25 | |
import org.apache.commons.logging.Log; |
26 | |
import org.apache.commons.logging.LogFactory; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public final class LicenseHandler |
33 | |
{ |
34 | 0 | private static final Log logger = LogFactory.getLog(LicenseHandler.class); |
35 | |
|
36 | |
private static final int MAX_ROWS_TO_DISPLAY = 80; |
37 | |
|
38 | |
private static final String DEFAULT_LICENSE_TYPE = "Common Public Attribution License Version 1.0 (CPAL)"; |
39 | |
private static final String DEFAULT_LICENSE_VERSION = "UNKNOWN"; |
40 | |
private static final String VERSION_TEXT_PREFIX = "Version "; |
41 | |
|
42 | |
private static final String LICENSE_TEXT_FILENAME = "LICENSE.txt"; |
43 | |
|
44 | |
private static final String LICENSE_TEXT_JAR_FILE_PATH = "META-INF/mule/" + LICENSE_TEXT_FILENAME; |
45 | |
private static final String LICENSE_PROPERTIES_JAR_FILE_PATH = "META-INF/mule/license.props"; |
46 | |
|
47 | |
private LicenseHandler() |
48 | 0 | { |
49 | |
|
50 | 0 | } |
51 | |
|
52 | |
public static boolean isLicenseAccepted() throws Exception |
53 | |
{ |
54 | 0 | return ClassUtils.getResource(LICENSE_PROPERTIES_JAR_FILE_PATH, LicenseHandler.class) != null; |
55 | |
} |
56 | |
|
57 | |
public static File getLicenseFile() |
58 | |
{ |
59 | 0 | return new File(MuleBootstrapUtils.getMuleHomeFile(), LICENSE_TEXT_FILENAME); |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public static boolean getAcceptance() |
71 | |
{ |
72 | |
boolean hasAccepted; |
73 | |
|
74 | |
try |
75 | |
{ |
76 | 0 | if (!getLicenseFile().exists() || !MuleBootstrapUtils.getMuleLibDir().exists()) |
77 | |
{ |
78 | 0 | System.out.println("\nYour Mule installation seems to be incomplete. Please try downloading it again from http://mule.mulesource.org/display/MULE/Download and start again."); |
79 | 0 | hasAccepted = false; |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | 0 | System.out.println("\n\nPlease read over the following license agreement carefully:\n\n"); |
84 | |
|
85 | 0 | LicenseInfo licenseInfo = readLicenseFileAndDisplayToStdout(); |
86 | |
|
87 | 0 | hasAccepted = askUserForAcceptance(); |
88 | |
|
89 | 0 | if (hasAccepted) |
90 | |
{ |
91 | 0 | saveLicenseInfo(licenseInfo); |
92 | |
} |
93 | |
} |
94 | |
} |
95 | 0 | catch (Exception e) |
96 | |
{ |
97 | 0 | hasAccepted = false; |
98 | 0 | System.out.println("\nSorry, we encountered an error in processing your license agreement - please try again."); |
99 | 0 | e.printStackTrace(); |
100 | 0 | } |
101 | |
|
102 | 0 | return hasAccepted; |
103 | |
} |
104 | |
|
105 | |
public static void saveLicenseInfo(LicenseInfo licenseInfo) throws Exception |
106 | |
{ |
107 | 0 | if (licenseInfo != null && !isLicenseAccepted()) |
108 | |
{ |
109 | 0 | if (!MuleBootstrapUtils.getMuleLibDir().canWrite()) |
110 | |
{ |
111 | 0 | throw new Exception("No write permissions for " + MuleBootstrapUtils.MULE_LOCAL_JAR_FILENAME + |
112 | |
" to MULE_HOME. If you are using MULE_BASE and multiple deployments, please ask your administrator to run Mule for the first time."); |
113 | |
} |
114 | 0 | File tempJarFile = createTempLicenseJarFile(licenseInfo); |
115 | 0 | MuleBootstrapUtils.getMuleLocalJarFile().delete(); |
116 | 0 | FileUtils.renameFile(tempJarFile, MuleBootstrapUtils.getMuleLocalJarFile()); |
117 | |
} |
118 | 0 | } |
119 | |
|
120 | |
private static LicenseInfo readLicenseFileAndDisplayToStdout() throws IOException |
121 | |
{ |
122 | 0 | String licenseType = null; |
123 | 0 | String licenseVersion = null; |
124 | |
|
125 | |
BufferedReader stdin; |
126 | 0 | BufferedReader fileReader = null; |
127 | |
|
128 | |
try |
129 | |
{ |
130 | 0 | stdin = new BufferedReader(new InputStreamReader(System.in)); |
131 | 0 | fileReader = new BufferedReader(new FileReader(getLicenseFile())); |
132 | 0 | int row = 1; |
133 | |
|
134 | 0 | while (fileReader.ready()) |
135 | |
{ |
136 | 0 | String line = fileReader.readLine(); |
137 | |
|
138 | 0 | if (row == 1 && line.length() > 0) |
139 | |
{ |
140 | 0 | licenseType = line; |
141 | |
} |
142 | 0 | if (row == 2 && line.startsWith(VERSION_TEXT_PREFIX)) |
143 | |
{ |
144 | 0 | licenseVersion = line.substring(VERSION_TEXT_PREFIX.length()); |
145 | |
} |
146 | |
|
147 | 0 | if ((row % MAX_ROWS_TO_DISPLAY) == 0) |
148 | |
{ |
149 | 0 | System.out.print("\nHit return to continue ... "); |
150 | 0 | stdin.readLine(); |
151 | |
} |
152 | |
|
153 | 0 | System.out.println(line); |
154 | 0 | row++; |
155 | 0 | } |
156 | |
} |
157 | |
finally |
158 | |
{ |
159 | 0 | if (fileReader != null) |
160 | |
{ |
161 | |
try |
162 | |
{ |
163 | 0 | fileReader.close(); |
164 | |
} |
165 | 0 | catch (Exception ignore) |
166 | |
{ |
167 | 0 | logger.debug("Error closing fileReader: " + ignore.getMessage()); |
168 | 0 | } |
169 | |
} |
170 | |
} |
171 | |
|
172 | 0 | return new LicenseInfo(licenseType, licenseVersion); |
173 | |
} |
174 | |
|
175 | |
private static boolean askUserForAcceptance() throws IOException |
176 | |
{ |
177 | 0 | BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); |
178 | |
|
179 | 0 | System.out.print("\n\nDo you accept the terms and conditions of this license agreement [y/n]?"); |
180 | |
|
181 | 0 | final String input = StringUtils.defaultString(stdin.readLine()); |
182 | |
|
183 | 0 | boolean hasAcccepted = input.toLowerCase().startsWith("y"); |
184 | |
|
185 | 0 | if (!hasAcccepted) |
186 | |
{ |
187 | 0 | System.out.println("\nSorry, until you accept the terms and conditions of this EULA, you won't be able to start Mule"); |
188 | |
} |
189 | |
|
190 | 0 | return hasAcccepted; |
191 | |
} |
192 | |
|
193 | |
private static File createTempLicenseJarFile(LicenseInfo licenseInfo) throws Exception |
194 | |
{ |
195 | 0 | LinkedHashMap jarEntries = new LinkedHashMap(); |
196 | 0 | jarEntries.put(LICENSE_PROPERTIES_JAR_FILE_PATH, licenseInfo.toString()); |
197 | 0 | jarEntries.put(LICENSE_TEXT_JAR_FILE_PATH, getLicenseFile()); |
198 | |
|
199 | 0 | File tempJar = File.createTempFile(MuleBootstrapUtils.MULE_LOCAL_JAR_FILENAME, null); |
200 | |
|
201 | |
try |
202 | |
{ |
203 | 0 | JarUtils.createJarFileEntries(tempJar, jarEntries); |
204 | |
} |
205 | 0 | catch (IOException ioe) |
206 | |
{ |
207 | 0 | if (tempJar != null) |
208 | |
{ |
209 | 0 | throw new Exception("Unable to create temporary jar file to " + tempJar.getAbsolutePath()); |
210 | |
} |
211 | |
else |
212 | |
{ |
213 | 0 | throw new Exception("Unable to create temporary jar file for " + MuleBootstrapUtils.MULE_LOCAL_JAR_FILENAME); |
214 | |
} |
215 | 0 | } |
216 | |
|
217 | 0 | return tempJar; |
218 | |
} |
219 | |
|
220 | |
public static class LicenseInfo |
221 | |
{ |
222 | 0 | private String licenseType = DEFAULT_LICENSE_TYPE; |
223 | 0 | private String licenseVersion = DEFAULT_LICENSE_VERSION; |
224 | 0 | private String licenseDate = (new java.util.Date()).toString(); |
225 | |
|
226 | |
public LicenseInfo() |
227 | 0 | { |
228 | |
|
229 | 0 | } |
230 | |
|
231 | |
public LicenseInfo(String licenseType, String licenseVersion) |
232 | 0 | { |
233 | 0 | if (StringUtils.isNotBlank(licenseType)) |
234 | |
{ |
235 | 0 | this.licenseType = licenseType; |
236 | |
} |
237 | 0 | if (StringUtils.isNotBlank(licenseVersion)) |
238 | |
{ |
239 | 0 | this.licenseVersion = licenseVersion; |
240 | |
} |
241 | 0 | } |
242 | |
|
243 | |
public String toString() |
244 | |
{ |
245 | 0 | return "LicenseType=" + licenseType + "\n" + |
246 | |
"LicenseVersion=" + licenseVersion + "\n" + |
247 | |
"LicenseDate=" + licenseDate + "\n"; |
248 | |
} |
249 | |
} |
250 | |
} |