1
2
3
4
5
6
7
8
9
10
11 package org.mule.modules.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 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 {
49
50 }
51
52 public static boolean isLicenseAccepted() throws Exception
53 {
54 return ClassUtils.getResource(LICENSE_PROPERTIES_JAR_FILE_PATH, LicenseHandler.class) != null;
55 }
56
57 public static File getLicenseFile()
58 {
59 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 if (!getLicenseFile().exists() || !MuleBootstrapUtils.getMuleLibDir().exists())
77 {
78 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 hasAccepted = false;
80 }
81 else
82 {
83 System.out.println("\n\nPlease read over the following license agreement carefully:\n\n");
84
85 LicenseInfo licenseInfo = readLicenseFileAndDisplayToStdout();
86
87 hasAccepted = askUserForAcceptance();
88
89 if (hasAccepted)
90 {
91 saveLicenseInfo(licenseInfo);
92 }
93 }
94 }
95 catch (Exception e)
96 {
97 hasAccepted = false;
98 System.out.println("\nSorry, we encountered an error in processing your license agreement - please try again.");
99 e.printStackTrace();
100 }
101
102 return hasAccepted;
103 }
104
105 public static void saveLicenseInfo(LicenseInfo licenseInfo) throws Exception
106 {
107 if (licenseInfo != null && !isLicenseAccepted())
108 {
109 if (!MuleBootstrapUtils.getMuleLibDir().canWrite())
110 {
111 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 File tempJarFile = createTempLicenseJarFile(licenseInfo);
115 MuleBootstrapUtils.getMuleLocalJarFile().delete();
116 FileUtils.renameFile(tempJarFile, MuleBootstrapUtils.getMuleLocalJarFile());
117 }
118 }
119
120 private static LicenseInfo readLicenseFileAndDisplayToStdout() throws IOException
121 {
122 String licenseType = null;
123 String licenseVersion = null;
124
125 BufferedReader stdin;
126 BufferedReader fileReader = null;
127
128 try
129 {
130 stdin = new BufferedReader(new InputStreamReader(System.in));
131 fileReader = new BufferedReader(new FileReader(getLicenseFile()));
132 int row = 1;
133
134 while (fileReader.ready())
135 {
136 String line = fileReader.readLine();
137
138 if (row == 1 && line.length() > 0)
139 {
140 licenseType = line;
141 }
142 if (row == 2 && line.startsWith(VERSION_TEXT_PREFIX))
143 {
144 licenseVersion = line.substring(VERSION_TEXT_PREFIX.length());
145 }
146
147 if ((row % MAX_ROWS_TO_DISPLAY) == 0)
148 {
149 System.out.print("\nHit return to continue ... ");
150 stdin.readLine();
151 }
152
153 System.out.println(line);
154 row++;
155 }
156 }
157 finally
158 {
159 if (fileReader != null)
160 {
161 try
162 {
163 fileReader.close();
164 }
165 catch (Exception ignore)
166 {
167 logger.debug("Error closing fileReader: " + ignore.getMessage());
168 }
169 }
170 }
171
172 return new LicenseInfo(licenseType, licenseVersion);
173 }
174
175 private static boolean askUserForAcceptance() throws IOException
176 {
177 BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
178
179 System.out.print("\n\nDo you accept the terms and conditions of this license agreement [y/n]?");
180
181 final String input = StringUtils.defaultString(stdin.readLine());
182
183 boolean hasAcccepted = input.toLowerCase().startsWith("y");
184
185 if (!hasAcccepted)
186 {
187 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 return hasAcccepted;
191 }
192
193 private static File createTempLicenseJarFile(LicenseInfo licenseInfo) throws Exception
194 {
195 LinkedHashMap jarEntries = new LinkedHashMap();
196 jarEntries.put(LICENSE_PROPERTIES_JAR_FILE_PATH, licenseInfo.toString());
197 jarEntries.put(LICENSE_TEXT_JAR_FILE_PATH, getLicenseFile());
198
199 File tempJar = File.createTempFile(MuleBootstrapUtils.MULE_LOCAL_JAR_FILENAME, null);
200
201 try
202 {
203 JarUtils.createJarFileEntries(tempJar, jarEntries);
204 }
205 catch (IOException ioe)
206 {
207 if (tempJar != null)
208 {
209 throw new Exception("Unable to create temporary jar file to " + tempJar.getAbsolutePath());
210 }
211 else
212 {
213 throw new Exception("Unable to create temporary jar file for " + MuleBootstrapUtils.MULE_LOCAL_JAR_FILENAME);
214 }
215 }
216
217 return tempJar;
218 }
219
220 public static class LicenseInfo
221 {
222 private String licenseType = DEFAULT_LICENSE_TYPE;
223 private String licenseVersion = DEFAULT_LICENSE_VERSION;
224 private String licenseDate = (new java.util.Date()).toString();
225
226 public LicenseInfo()
227 {
228
229 }
230
231 public LicenseInfo(String licenseType, String licenseVersion)
232 {
233 if (StringUtils.isNotBlank(licenseType))
234 {
235 this.licenseType = licenseType;
236 }
237 if (StringUtils.isNotBlank(licenseVersion))
238 {
239 this.licenseVersion = licenseVersion;
240 }
241 }
242
243 public String toString()
244 {
245 return "LicenseType=" + licenseType + "\n" +
246 "LicenseVersion=" + licenseVersion + "\n" +
247 "LicenseDate=" + licenseDate + "\n";
248 }
249 }
250 }