KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ixenon > free > nodes > CreateRuntimeBatch


1 /* $Id$
2  *
3  * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 //
17
// DESCRIPTION
18
//
19
// Peter Pilgrim
20
// Tue Jan 26 19:55:32 GMT 1999
21
//
22
// RCS HEADER ``CreateRuntimeBatch.java''
23
//
24
// $Author$
25
// $Date$
26
// $Source$
27
// $Revision$ $State$ $Locker$
28
//
29

30 package ixenon.free.nodes;
31
32 import java.io.*;
33 import java.util.*;
34 import java.awt.*;
35 import java.awt.event.*;
36
37 import javax.swing.*; // For JTree
38
import javax.swing.event.*; // For events
39
import javax.swing.tree.*; // MutableTreeNode
40

41 import ixenon.free.swing.*; // for `ResetProgressBar' & `UpdateProgressBar'
42
import ixenon.free.install.*; // for FreeInstallerApplication
43

44 /**
45  * This <code>Installable</code> tree node creates the
46  * runtime batch file node installable.
47  * This class creates the following files for UNIX and NT
48  *
49  * <UL>
50  * <LI> run-<I>ApplicationBatch</I> </LI><BR>
51  * A Bourne shell / Korn shell script launcher
52  * <LI> <I>ApplicationBatch</I>.bat </LI><BR>
53  * A MS DOS command batch file launcher
54  * </UL>
55  */

56 public class CreateRuntimeBatch extends AbstractInstallable
57 implements Installable
58 {
59     public final static int SLEEP_TIME=75; // Sleep time
60

61     protected File rootDirectory;
62     protected File binaryDirectory;
63     protected String JavaDoc envHomeVar;
64     protected String JavaDoc rootFilename;
65     protected String JavaDoc mainClass;
66     protected String JavaDoc [] jarFileList;
67     
68     /**
69      * Construct a installable
70      *
71      * @param nodeName the name of the installable node
72      * @param rootDir the destination root directory
73      * @param binDir the destination binary directory
74      * @param envHomeVar the name of the environment variable which
75      * points to the root of the distribution.
76      * @param rootFilename the root filename without the suffix
77      * @param mainClass the full package name of the class which runs the application
78      * which has the <B>public static void main( String {} args )</B> method
79      * e.g. com.sun.java.compiler
80      * @param jarFileList an array jar of zip filename.
81      */

82     public CreateRuntimeBatch ( String JavaDoc nodeName,
83                 String JavaDoc rootDir,
84                 String JavaDoc binDir,
85                 String JavaDoc envHomeVar,
86                 String JavaDoc rootFilename,
87                 String JavaDoc mainClass,
88                 String JavaDoc [] jarFileList )
89     {
90     this( nodeName, new File(rootDir), new File(binDir),
91           envHomeVar, rootFilename, mainClass, jarFileList );
92     }
93     
94     /**
95      * Construct a installable
96      *
97      * @param nodeName the name of the installable node
98      * @param rootDir the destination directory
99      * @param binDir the destination binary directory
100      * @param envHomeVar the name of the environment variable which
101      * points to the root of the distribution.
102      * @param rootFilename the root filename without the suffix
103      * @param mainClass the full package name of the class which runs the application
104      * which has the <B>public static void main( String {} args )</B> method
105      * e.g. com.sun.java.compiler
106      * @param jarFileList an array jar of zip filename.
107      */

108     public CreateRuntimeBatch( String JavaDoc nodeName,
109                    File rootDir,
110                    File binDir,
111                    String JavaDoc envHomeVar,
112                    String JavaDoc rootFilename,
113                    String JavaDoc mainClass,
114                    String JavaDoc [] jarFileList )
115     {
116     super( nodeName, null, true /*allowsChildren*/ );
117
118     if (envHomeVar == null)
119         envHomeVar = "THISAPPHOME";
120     rootDirectory = rootDir;
121     binaryDirectory = binDir;
122     this.envHomeVar = envHomeVar;
123     this.rootFilename = rootFilename;
124     this.mainClass = mainClass;
125     this.jarFileList = jarFileList;
126     }
127     
128     /**
129      * create or retrieve the visual component of the installable
130      * if one exists. Otherwise return null.
131      */

132     public Component getVisualComponent()
133     {
134     return (null);
135     }
136     
137     /**
138      * create or retrieve the configurable component of the installable
139      * if one exists. Otherwise return null object.
140      */

141     public Component getConfigurableComponent()
142     {
143     return (null);
144     }
145
146     /**
147      * method to install by copying files from the source
148      * directory to target directory
149      *
150      * @exception <code>InstallException</code> if there is an
151      * problem trying to install the entity.
152      */

153     public void install() throws InstallException
154     {
155     FreeInstallerApplication theApp =
156         FreeInstallerApplication.getInstance();
157
158     if ( !rootDirectory.exists() )
159         rootDirectory.mkdirs();
160
161     if ( !rootDirectory.isDirectory() )
162         throw new InstallException(
163         "no such root directory:`"+rootDirectory.getAbsolutePath()+"'" );
164
165     if ( !binaryDirectory.exists() )
166         binaryDirectory.mkdirs();
167
168     if ( !binaryDirectory.isDirectory() )
169         throw new InstallException(
170         "no such binary directory:`"+binaryDirectory.getAbsolutePath()+"'" );
171
172     // Get the selected files to copy
173
theApp.getProgressFrame().setStatusText( getNodeName() );
174
175     JProgressBar pbar = theApp.getProgressFrame().getProgressBarNode();
176     int index = 0;
177     SwingUtilities.invokeLater( new ResetProgressBar( pbar, 0, 2, 0 ) );
178     
179     // OLD CODE: pbar.setValue(j+1);
180
try { Thread.sleep(SLEEP_TIME); } catch (InterruptedException JavaDoc ie) { ; }
181
182     createNTDosBatch();
183     SwingUtilities.invokeLater( new UpdateProgressBar( pbar, ++index ) );
184
185     createUnixShellScript();
186     SwingUtilities.invokeLater( new UpdateProgressBar( pbar, ++index ) );
187     }
188
189     /** Create a Windows/DOS batch launcher */
190     protected void createNTDosBatch() throws InstallException
191     {
192     File dstFile = new File( binaryDirectory, rootFilename+".bat" );
193
194     // Check if file exists, if true then delete it.
195
if (dstFile.exists() )
196         dstFile.delete();
197     
198     PrintWriter pwriter=null;
199     FileWriter fwriter=null;
200     
201     FreeInstallerApplication theApp =
202         FreeInstallerApplication.getInstance();
203
204     String JavaDoc fileSep = "\\"; // System.getProperty("file.separator");
205
String JavaDoc pathSep = ";"; // System.getProperty("path.separator");
206

207     // Add another one to the uninstallable
208
if (generateUninstall)
209         uninstallable.addFile( dstFile );
210
211     theApp.printInfo( "Creating a NT/DOS batch command file: `" + dstFile.getPath()+"'" );
212     try {
213         fwriter = new FileWriter( dstFile );
214         pwriter = new PrintWriter( fwriter );
215
216         pwriter.println( "REM Windows/DOS Batch file");
217         pwriter.println( "REM for "+theApp.getProductName() + " by " + theApp.getCompanyName() +" [c]" );
218         pwriter.println( "REM Automagically generated by FreeInstaller V"+ApplicationVersion.getVersionString() );
219         pwriter.println();
220         
221         // Set the installation home directory for the application
222
// File prefixDir = theApp.getMainFrame().getPrefix();
223
pwriter.println( "REM set "+envHomeVar+"=<path to the application>" );
224         pwriter.println( "set "+envHomeVar+"="+rootDirectory.getPath() );
225
226         // Prepend the PATH e.g "set PATH C:\jdk1.2\bin:%PATH%"
227
pwriter.println( "set PATH="+binaryDirectory.getPath()+pathSep+"%PATH%");
228
229         // Prepend the CLASSPATH e.g "C:\jdk1.2\lib\classes:%CLASSPATH%"
230
// Also must allow this class to specify jars and zip files.
231
String JavaDoc finalClassPath = "%"+envHomeVar+"%"+fileSep+"lib";
232         
233         for (int k=0; k<jarFileList.length; ++k)
234         if ( jarFileList[k] != null)
235             finalClassPath += InstallUtilities.convertUnixPathToNTPath( pathSep+jarFileList[k] );
236
237         finalClassPath += pathSep+"%CLASSPATH%";
238
239         // Set the application resource path
240
String JavaDoc appresPath =
241         InstallUtilities.convertUnixPathToNTPath(
242             "%USRPROFILE%"+pathSep+System.getProperty("user.home")+
243             pathSep+theApp.getMainFrame().getPrefix()+fileSep+"lib" );
244         
245         pwriter.println( "");
246         pwriter.println( "java -Dappres.path=\""+appresPath+"\" -classpath \""+finalClassPath+"\" "+mainClass );
247         pwriter.println( "");
248         pwriter.println( "@echo if Java does not run by search path, specify the full path to");
249         pwriter.println( "@echo the Sun JDK or Java Runtime Environment in this batch command.");
250         pwriter.println( "");
251         pwriter.println( "REM pause");
252         
253         pwriter.flush();
254         InstallUtilities.setFilePermissions( dstFile, InstallConstants.INST_EXE_PERMS );
255
256     }
257     catch (IOException ioe) {
258         throw new InstallException(
259         "I/O exception occurred writing file:"+ dstFile.getPath()+
260         "\ndetails:"+ioe.getMessage() );
261     }
262     finally {
263         if (fwriter != null) try { fwriter.close(); } catch (Exception JavaDoc e) { ; }
264         if (pwriter != null) try { pwriter.close(); } catch (Exception JavaDoc e) { ; }
265     }
266     }
267
268     
269     /** Create a Unix shell script launcher */
270     protected void createUnixShellScript() throws InstallException
271     {
272     File dstFile = new File( binaryDirectory, "run-"+rootFilename );
273
274     // Check if file exists, if true then delete it.
275
if (dstFile.exists() )
276         dstFile.delete();
277     
278     PrintWriter pwriter=null;
279     FileWriter fwriter=null;
280     
281     FreeInstallerApplication theApp =
282         FreeInstallerApplication.getInstance();
283
284     String JavaDoc fileSep = System.getProperty("file.separator");
285     String JavaDoc pathSep = System.getProperty("path.separator");
286     
287     theApp.printInfo( "Creating a UNIX Shell script file: `" + dstFile.getPath()+"'" );
288     // Add another one to the uninstallable
289
if (generateUninstall)
290         uninstallable.addFile( dstFile );
291
292     try {
293         fwriter = new FileWriter( dstFile );
294         pwriter = new PrintWriter( fwriter );
295
296         pwriter.println( "#!/bin/sh");
297         pwriter.println( "export PATH || exec /bin/sh $0 $argv:q");
298         pwriter.println();
299         pwriter.println( "#\n# DESCRIPTION:\n#\tA shell script batch to launch:" );
300               
301         pwriter.println( "#\t" +theApp.getProductName() + " by " + theApp.getCompanyName() +" [c]" );
302         pwriter.println( "#\tAutomagically generated by FreeInstaller V"+ApplicationVersion.getVersionString() );
303         pwriter.println( "#");
304         pwriter.println();
305         
306         // Set the installation home directory for the application
307
// File prefixDir = theApp.getMainFrame().getPrefix();
308
pwriter.println( "## "+envHomeVar+"=<path to the application>" );
309         pwriter.println( ""+envHomeVar+"="+rootDirectory.getPath() );
310         pwriter.println( "## export "+envHomeVar+"" );
311
312         // Prepend the PATH e.g "set PATH C:\jdk1.2\bin:%PATH%"
313
pwriter.println( "PATH="+binaryDirectory.getPath()+pathSep+"${PATH}");
314         pwriter.println( "export PATH" );
315         
316         // Prepend the CLASSPATH e.g "C:\jdk1.2\lib\classes:%CLASSPATH%"
317
// Also must allow this class to specify jars and zip files.
318
String JavaDoc finalClassPath = "${"+envHomeVar+"}"+fileSep+"lib";
319         
320         for (int k=0; k<jarFileList.length; ++k)
321         if (jarFileList[k] != null)
322             finalClassPath += pathSep+jarFileList[k];
323
324         finalClassPath += pathSep+"${CLASSPATH}";
325         // Set the application resource path
326
String JavaDoc appresPath =
327         InstallUtilities.convertNTPathToUnixPath(
328             "${HOME}" +pathSep+
329             theApp.getMainFrame().getPrefix()+fileSep+"lib" );
330         
331         pwriter.println( "");
332         pwriter.println( "java -Dappres.path=\""+appresPath+"\" -Denv=\"`env`\" -classpath \""+finalClassPath+"\" "+mainClass );
333         pwriter.println( "status=$?\n");
334         pwriter.println( "echo if Java does not run by search path, specify the full path to");
335         pwriter.println( "echo the Sun JDK or Java Runtime Environment in this script file.");
336         pwriter.println( "");
337         pwriter.println( "## echo \"Please press [RETURN] to continue\"");
338         pwriter.println( "## read DUMMYVARIABLE");
339         pwriter.println( "exit $status\n# end");
340         
341         pwriter.flush();
342         InstallUtilities.setFilePermissions( dstFile, InstallConstants.INST_EXE_PERMS );
343     }
344     catch (IOException ioe) {
345         throw new InstallException(
346         "I/O exception occurred writing file:"+ dstFile.getPath()+
347         "\ndetails:"+ioe.getMessage() );
348     }
349     finally {
350         if (fwriter != null) try { fwriter.close(); } catch (Exception JavaDoc e) { ; }
351         if (pwriter != null) try { pwriter.close(); } catch (Exception JavaDoc e) { ; }
352     }
353
354     }
355
356     /**
357      * method to perform the cleanup of content if cancelled, or aborted
358      */

359     public void cleanup() throws InstallException
360     {
361     // empty
362
}
363
364 }
365
366 // fini
367
Popular Tags