KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > modules > boot > LicenseHandler


1 /*
2  * $Id $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.modules.boot;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.InputStreamReader JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.FileReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.jar.JarEntry JavaDoc;
21 import java.util.jar.JarFile JavaDoc;
22 import java.util.jar.JarOutputStream JavaDoc;
23 import java.util.zip.ZipEntry JavaDoc;
24
25 /**
26  * This class has methods for displaying the EULA and saving the
27  * license acceptance acknowledgment.
28  */

29 public class LicenseHandler
30 {
31     private File JavaDoc muleHome;
32     private File JavaDoc muleBase;
33     private static int maxRowsToDisplay = 80;
34
35     public static String JavaDoc defaultLicenseType = "MuleSource Public License";
36     public static String JavaDoc defaultLicenseVersion = "UNKNOWN";
37     public static String JavaDoc ackJarName = "mule-local-install.jar";
38     public static String JavaDoc ackLicenseName = "META-INF/mule/LICENSE.txt";
39     public static String JavaDoc ackFileName = "META-INF/mule/license.props";
40     public static String JavaDoc licenseFileName = "LICENSE.txt";
41
42     /**
43      * Constructor for when we only know MULE_HOME
44      * This is the constructor that the GUI installer should use.
45      *
46      * @param muleHome File pointing to MULE_HOME
47      */

48     public LicenseHandler(File JavaDoc muleHome)
49     {
50         this.muleHome = muleHome;
51         this.muleBase = muleHome;
52     }
53
54     /**
55      * Constructor for when we know both MULE_HOME and MULE_BASE
56      * This is the constructor used by MuleBootstrap
57      *
58      * @param muleHome File pointing to MULE_HOME
59      * @param muleBase File pointing to MULE_BASE
60      */

61     public LicenseHandler(File JavaDoc muleHome, File JavaDoc muleBase)
62     {
63         this.muleHome = muleHome;
64         this.muleBase = muleBase;
65     }
66
67     /**
68      * Display the EULA and get the user's acceptance. Note that only
69      * a missing license file or a non-yes answer will cause this to
70      * return false. If the user accepts, but we can't write the license
71      * ack file for some reason, we'll still return true.
72      *
73      * @return boolean whether the license was accepted or not
74      */

75     public boolean getAcceptance()
76     {
77         String JavaDoc licenseType = "";
78         String JavaDoc licenseVersion = "";
79
80         try {
81             File JavaDoc licenseFile = new File JavaDoc(muleHome, licenseFileName);
82             File JavaDoc muleLib = new File JavaDoc(muleHome, "lib/mule");
83
84             // Make sure the license file exists
85
// Also make sure that MULE_HOME/lib/mule exists - it
86
// should, of course, but it doesn't hurt to check since
87
// we'll need that directory later
88

89             if (!licenseFile.exists() || !muleLib.exists())
90             {
91                 System.out.println("\nYour Mule installation seems to be incomplete. Please try downloading it again from http://mule.mulesource.org/wiki/display/MULE/Download and start again.");
92                 return false;
93             }
94
95             System.out.println("\n\nPlease read over the following license agreement carefully:\n\n");
96
97             int row = 1;
98             String JavaDoc input = "";
99
100             BufferedReader JavaDoc is = new BufferedReader JavaDoc(
101             new InputStreamReader JavaDoc(System.in));
102             BufferedReader JavaDoc br = new BufferedReader JavaDoc(
103                     new FileReader JavaDoc(licenseFile));
104
105             while (br.ready())
106             {
107                 String JavaDoc line = br.readLine();
108
109                 if (row == 1) licenseType = line;
110                 if (row == 2 && line.startsWith("Version "))
111                     licenseVersion = line.substring(8);
112
113                 if ((row % maxRowsToDisplay ) == 0)
114                 {
115                     System.out.print("\nHit return to continue ... ");
116                 input = is.readLine();
117                 }
118
119                 System.out.println(line);
120                 row++;
121             }
122
123
124             System.out.print("\n\nDo you accept the terms and conditions of this license agreement [y/n]?");
125             input = is.readLine();
126
127             if (!input.toLowerCase().startsWith("y"))
128             {
129                 System.out.println("\nSorry, until you accept the terms and conditions of this EULA, you won't be able to start Mule");
130                 return false;
131             }
132         }
133         catch (Exception JavaDoc e)
134         {
135             System.out.println("\nSorry, we encountered an error in processing your license agreement - please try again");
136             return false;
137         }
138
139         if (licenseType.equals("")) licenseType = defaultLicenseType;
140         if (licenseVersion.equals("")) licenseVersion = defaultLicenseVersion;
141
142         try
143         {
144             saveLicenseAck(licenseType, licenseVersion);
145         }
146         catch (Exception JavaDoc e)
147         {
148             System.out.println(e);
149         }
150
151         return true;
152     }
153
154     /**
155      * Saves the license acceptance acknowledgement file. This method
156      * should be used by the GUI installer.
157      *
158      * For now, the acknowlegment file is license.props and contains:
159      * LicenseVersion:
160      * LicenseAcceptanceDate:
161      * LicenseType:
162      *
163      * We are also making a copy of the license file into the jar as well
164      *
165      * The logic here is:
166      *
167      * 1. Normally, save the license ack in a jar
168      * in MULE_HOME/lib/mule called "mule-local-install.jar"
169      *
170      * 2. If MULE_HOME/lib/mule is not writable AND
171      * MULE_BASE != MULE_HOME, try to save in MULE_BASE/lib/user
172      *
173      * 3. If MULE_BASE/lib/user is not writable, something is
174      * probably strange and we need a third option ... which is ...
175      * well, I'm sure there is one ...
176      *
177      * @param licenseType type of license - for now, should be just MuleSource Public License
178      * @param licenseVersion version of license - for now, should be 1.1.3
179      *
180      * @throws Exception if there is nowhere to write the file or somehow the jar creation fails (disk full, etc.)
181      */

182     public void saveLicenseAck(String JavaDoc licenseType, String JavaDoc licenseVersion) throws Exception JavaDoc
183     {
184         File JavaDoc muleLib = new File JavaDoc(muleHome, "lib/mule");
185         File JavaDoc tempJar = createAckJarFile(licenseType, licenseVersion);
186
187         // First check if the file exists. We do this in case the license.props
188
// has not yet been loaded into the classpath - which might be the case
189
// with the GUI installer.
190
if (licenseJarExists())
191         {
192             return;
193         }
194
195         if (!muleLib.canWrite()) {
196             // If we can't write to MULE_HOME/lib/mule, try MULE_BASE/lib/user
197
if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile())) {
198                 muleLib = new File JavaDoc(muleBase, "lib/user");
199
200                 if (!muleLib.canWrite())
201                     throw new Exception JavaDoc("No write permissions for " + ackJarName + " in either MULE_HOME or MULE_BASE");
202
203             } else {
204                 throw new Exception JavaDoc("No write permission for " + ackJarName);
205             }
206         }
207
208         // Now we have a directory to create the jar to, so let's rename
209
// the temporary one
210
File JavaDoc newJarFile = new File JavaDoc(muleLib, ackJarName);
211         if (newJarFile.exists())
212             throw new Exception JavaDoc("Unable to rename temporary jar to " + newJarFile.getAbsolutePath() + " a file with this name already exists!");
213
214         if (!tempJar.renameTo(newJarFile))
215             throw new Exception JavaDoc("Unable to rename temporary jar to " + newJarFile.getAbsolutePath());
216     }
217
218     /**
219      * This method checks to see if there is a license jar file already. It
220      * checks both MULE_HOME/lib/mule and, if relevant, MULE_BASE/lib/user.
221      */

222
223     public boolean licenseJarExists()
224     {
225         try
226         {
227             File JavaDoc muleLib = new File JavaDoc(muleHome, "lib/mule");
228             File JavaDoc testJarFile = new File JavaDoc(muleLib, ackJarName);
229             JarFile JavaDoc jar = null;
230
231             if (testJarFile.exists())
232             {
233                 jar = new JarFile JavaDoc(testJarFile);
234             }
235             else
236             {
237                 // Not in MULE_HOME/lib/mule, if MULE_BASE is defined
238
// (and therefore != MULE_HOME), check in MULE_BASE/lib/user
239
if (!muleHome.getCanonicalFile().equals(muleBase.getCanonicalFile()))
240                 {
241                     muleLib = new File JavaDoc(muleBase, "lib/user");
242                     testJarFile = new File JavaDoc(muleLib, ackJarName);
243
244                     if (testJarFile.exists())
245                     {
246                         jar = new JarFile JavaDoc(testJarFile);
247                     }
248                 }
249             }
250
251             if (jar != null)
252             {
253                 ZipEntry JavaDoc entry = jar.getEntry(ackFileName);
254
255                 // The only way this method will return true is if we
256
// find the license.props file in the jar
257
if (entry != null)
258                 {
259                     return true;
260                 }
261             }
262         }
263         catch (Exception JavaDoc e)
264         {
265             System.out.println("Unknown error checking for license jar: " +
266                     e.toString());
267         }
268
269         return false;
270     }
271
272     /**
273      * This method will create a temporary jar file with the
274      * license ack file. It will either return the File or throw
275      * an Exception
276      *
277      * @throws Exception
278      */

279     private File JavaDoc createAckJarFile(String JavaDoc licenseType, String JavaDoc licenseVersion) throws Exception JavaDoc
280     {
281         File JavaDoc tempJar = File.createTempFile(ackJarName, null);
282         File JavaDoc licenseFile = new File JavaDoc(muleHome, licenseFileName);
283         JarOutputStream JavaDoc newJar = null;
284
285         String JavaDoc ackData = "LicenseType=" + licenseType + "\n";
286         ackData += "LicenseVersion=" + licenseVersion + "\n";
287         ackData += "LicenseDate=" + (new java.util.Date JavaDoc()).toString() + "\n";
288
289         try {
290             newJar = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(tempJar));
291         } catch (IOException JavaDoc ioe) {
292             throw new Exception JavaDoc("Unable to create temporary jar file");
293         }
294
295         byte buffer[] = new byte[1024];
296         int bytesRead;
297         FileInputStream JavaDoc fis = null;
298
299         try
300         {
301             fis = new FileInputStream JavaDoc(licenseFile);
302
303             JarEntry JavaDoc entry = new JarEntry JavaDoc(ackFileName);
304             newJar.putNextEntry(entry);
305             newJar.write(ackData.getBytes(), 0, ackData.getBytes().length);
306
307             entry = new JarEntry JavaDoc(ackLicenseName);
308             newJar.putNextEntry(entry);
309
310             while ((bytesRead = fis.read(buffer)) != -1)
311             {
312                 newJar.write(buffer, 0, bytesRead);
313             }
314
315         } catch (IOException JavaDoc ioe) {
316             throw new Exception JavaDoc("Unable to write " + ackFileName +
317                    " to temporary jar file");
318         } finally {
319             if (fis != null) {
320                 try { fis.close(); } catch (Exception JavaDoc e) { }
321             }
322         }
323         
324         try {
325             newJar.close();
326         } catch (IOException JavaDoc ioe) {
327             throw new Exception JavaDoc("Unable to close temporary jar file");
328         }
329
330         return tempJar;
331     }
332
333 }
334
335
Popular Tags