Tutorials to .com

Tutorials to .com » Jsp » Skills » Installation, upgrade, and uninstall the central part (java in preparation)

Installation, upgrade, and uninstall the central part (java in preparation)

Print View , by: iSee ,Total views: 23 ,Word Count: 2178 ,Date: Tue, 25 Aug 2009 Time: 10:06 PM

Installation program of work: the source files are copied to the appropriate directory.
Upgrade program of work: the source file, the purpose of the updated file to overwrite the file, additional files are copied to the appropriate directory.
Uninstall program of work: the program contents of the folder to delete.

For the above, write a simple installer
(Main document: InstallSheildImpl, recursive way the folder traversal)
/************************************************* **********************
* Module: InstallSheild.java
* Author: Administrator
* Created: 2004-12-13 22:37:53
* Purpose: Setup program interface for a unified approach called.
************************************************** *********************/
package org.heixue.test.install;

/ **
* @ Author Administrator
* /
public interface InstallSheild (
public final static int INSTALL = 1;
public final static int UPDATE = 2;
public final static int UNINSTALL = 3;
public void install (int type, String srcFold, String destFold) throws InstallException;
)


/************************************************* **********************
* Module: InstallSheildImpl.java
* Author: Administrator
* Created: 2004-12-13 22:48:20
* Purpose: Setup program implementation
************************************************** *********************/
package org.heixue.test.install;

import java.io. *;

/ / import org.heixue.util.Log;
import org.heixue.util.FileLog;
import org.heixue.util.file.FileCopy;

/ **
* @ Author Administrator
* /
public class InstallSheildImpl implements InstallSheild (
private String srcFold = null;
private String destFold = null;
private FileLog log = null;

/ **
*
* /
public InstallSheildImpl () (
)

/ *
* @ See org.heixue.test.update.InstallSheild # install (java.lang.String, java.lang.String, int)
* /
public void install (int type, String srcFold, String destFold) throws InstallException (
this.srcFold = srcFold;
this.destFold = destFold;
if (Config.getOut ()! = null)
log = new FileLog (Config.getOut ());
else
log = new FileLog (System.out);
if (destFold == null) throw new InstallException ( "you do not set the purpose of the folder location!");
switch (type) (
case InstallSheild.INSTALL: if (srcFold == null) throw new InstallException ( "you do not set the source folder location!"); doInstall (); break;
case InstallSheild.UPDATE: if (srcFold == null) throw new InstallException ( "you do not set the source folder location!"); doUpdate (); break;
case InstallSheild.UNINSTALL: doUninstall (); break;
default: throw new InstallException ( "do not do this!");
)
)

/ **
*:
* # Perpose: installation program, mainly for copies of documents.
* /
public void doInstall () throws InstallException (
if (srcFold == null) throw new InstallException ( "you do not set the source folder location!");
if (destFold == null) throw new InstallException ( "you do not set the purpose of the folder location!");
File file1 = new File (srcFold);
File file2 = new File (destFold);
if (! file2.exists ()) file2.mkdir ();
installFiles ("","");
)
private void installFiles (String src, String dest) throws InstallException (
File file1 = new File (srcFold, src);
File file2 = new File (destFold, dest);
if (file1.isFile ()) (
log.info (file2.getPath ());
FileCopy.copyByFile (file1, file2);
) else if (file1.isDirectory ()) (
if (! file2.exists ()) file2.mkdir ();
log.info (file2.getPath ());
File [] fs = file1.listFiles ();
for (int i = 0; i <fs.length; i + +) (
String strPath = fs [i]. GetPath (). Substring (srcFold.length () +1);
installFiles (strPath, strPath);
)
) else (
throw new InstallException ( "The file or directory does not exist!");
)
)
/ **
*:
* # Perpose: the upgrade process, according to the file creation date to determine whether the update
* /
public void doUpdate () throws InstallException (
if (srcFold == null) throw new InstallException ( "you do not set the source folder location!");
if (destFold == null) throw new InstallException ( "you do not set the purpose of the folder location!");
File file1 = new File (srcFold);
File file2 = new File (destFold);
if (! file2.exists ()) file2.mkdir ();
updateFiles ("","");
)
private void updateFiles (String src, String dest) throws InstallException (
File file1 = new File (srcFold, src);
File file2 = new File (destFold, dest);
if (file1.isFile ()) (
if (! file2.exists () | | file1.lastModified ()> file2.lastModified ()) (
log.info (file2.getPath ());
FileCopy.copyByFile (file1, file2);
)
) else if (file1.isDirectory ()) (
if (! file2.exists ()) file2.mkdir ();
log.info (file2.getPath ());
File [] fs = file1.listFiles ();
for (int i = 0; i <fs.length; i + +) (
String strPath = fs [i]. GetPath (). Substring (srcFold.length () +1);
updateFiles (strPath, strPath);
)
) else (
throw new InstallException ( "The file or directory does not exist!");
)
)
/ **
*:
* # Perpose: uninstall program, the purpose of the folder to delete all the files under the
* /
public void doUninstall () throws InstallException (
if (destFold == null) throw new InstallException ( "you do not set the purpose of the folder location!");
deleteFiles ("");
)
private void deleteFiles (String dest) throws InstallException (
File file1 = new File (destFold, dest);
if (file1.isFile ()) (
file1.delete ();
log.info (file1.getPath ());
) else if (file1.isDirectory ()) (
File [] fs = file1.listFiles ();
for (int i = 0; i <fs.length; i + +) (
String strPath = fs [i]. GetPath (). Substring (srcFold.length () +1);
deleteFiles (strPath);
)
file1.delete ();
log.info (file1.getPath ());
) else (
throw new InstallException ( "The file or directory does not exist!");
)
)

public static void main (String [] args) throws InstallException (
InstallSheildImpl isi = new InstallSheildImpl ();
isi.install (InstallSheild.INSTALL, "d: \ \ test", "d: \ \ test2");
/ / isi.install (InstallSheild.UPDATE, "d: \ \ test", "d: \ \ test2");
/ / isi.install (InstallSheild.UNINSTALL, "d: \ \ test", "d: \ \ test2");
)
)

/************************************************* **********************
* Module: Config.java
* Author: Administrator
* Created: 2004-12-14 10:24:06
* Purpose: some configuration information
************************************************** *********************/
package org.heixue.test.install;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

/ **
* @ Author Administrator
* /
public class Config (
private static int installType = 0;
private static String srcFold = null;
private static String destFold = null;
private static OutputStream out = null;
private static Config _config = null;

/ **
*
* /
public void initialize (int type, String src, String dest, String logPath) throws FileNotFoundException (
if (logPath! = null)
out = new FileOutputStream (logPath);
initialize (type, src, dest, out);
)
/ **
* @ Param type: the type of installation
* @ Param src: the source folder
* @ Param dest: the destination folder
* @ Param o: log stream output
* /
public void initialize (int type, String src, String dest, OutputStream o) (
installType = type;
srcFold = src;
destFold = dest;
out = o;
)

/ **
* @ Return:
* # Perpose:
* /
public static String getDestFold () (
return destFold;
)

/ **
* @ Return:
* # Perpose:
* /
public static int getInstallType () (
return installType;
)

/ **
* @ Return:
* # Perpose:
* /
public static OutputStream getOut () (
return out;
)

/ **
* @ Return:
* # Perpose:
* /
public static String getSrcFold () (
return srcFold;
)

/ **
* @ Return:
* # Perpose:
* /
public static Config getInstance () (
if (_config == null)
_config = new Config ();
return _config;
)

)



/************************************************* **********************
* Module: InstallException.java
* Author: Administrator
* Created: 2004-12-13 22:53:25
* Purpose: the installation process anomalies
************************************************** *********************/
package org.heixue.test.install;

/ **
* @ Author Administrator
* /
public class InstallException extends Exception (

/ **
*
* /
public InstallException () (
super ();
)

public InstallException (String reason) (
super (reason);
)

)


/************************************************* **********************
* Module: Install.java
* Author: Administrator
* Created: 2004-12-14 10:53:28
* Purpose: Setup file type, enter the parameters: the type of installation, the source folder, destination folder, the log file
************************************************** *********************/
package org.heixue.test.install;

import java.io.FileNotFoundException;

/ **
* @ Author Administrator
* /
public class Install (

/ **
*
* /
public Install () (
super ();
/ / TODO Auto-generated constructor stub
)
public static void main (String [] args) throws FileNotFoundException, InstallException (
int installType = 0;
String srcFold = null;
String destFold = null;
String logPath = null;
if (args.length == 4) (
if (args [0]. equals ( "install")) (
installType = InstallSheild.INSTALL;
) else if (args [0]. equals ( "update")) (
installType = InstallSheild.UPDATE;
) else if (args [0]. equals ( "uninstall")) (
installType = InstallSheild.UNINSTALL;
) else (
)
srcFold = args [1];
destFold = args [2];
logPath = args [3];
if (logPath.equals ( "null"))
logPath = null;
Config.getInstance (). Initialize (installType, srcFold, destFold, logPath);
InstallSheild is = new InstallSheildImpl ();
is.install (installType, srcFold, destFold);
) else (
System.out.println ( "command line: java Install Type srcFold destFold logPath");
System.out.println ( "eg: java Install install d: \ \ test d: \ \ test2 d: \ \ install.log");
)
)

)

////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////////////////////////
Under which the two tools

/************************************************* **********************
* Module: FileCopy.java
* Author: Administrator
* Created: 2004-12-6 22:20:15
* Purpose: copy the file
************************************************** *********************/
package org.heixue.util.file;

import java.io. *;

/ **
* @ Author Administrator
* /
public class FileCopy (

/ **
*
* /
public FileCopy () (
)
public static boolean copy (String src, String dest) (
try (
/ / instance the File as file_in and file_out
java.io.File file_in = new java.io.File (src);
java.io.File file_out = new java.io.File (dest);
FileInputStream in1 = new FileInputStream (file_in);
FileOutputStream out1 = new FileOutputStream (file_out);
byte [] bytes = new byte [1024];
int c;
while ((c = in1.read (bytes ))!=- 1)
out1.write (bytes, 0, c);
in1.close ();
out1.close ();
return (true); / / if success then return true
)
catch (Exception e)
(
System.out.println ( "Error!");
return (false); / / if fail then return false
)
)

public static boolean copyByFile (File src, File dest) (
try (
/ / instance the File as file_in and file_out
FileInputStream in1 = new FileInputStream (src);
FileOutputStream out1 = new FileOutputStream (dest);
byte [] bytes = new byte [1024];
int c;
while ((c = in1.read (bytes ))!=- 1)
out1.write (bytes, 0, c);
in1.close ();
out1.close ();
return (true); / / if success then return true
)
catch (Exception e)
(
System.out.println (e.toString ());
return (false); / / if fail then return false
)
)

)


/************************************************* **********************
* Module: FileLog.java
* Author: Administrator
* Created: 2004-12-6 22:20:15
* Purpose: generate the log file
************************************************** *********************/
package org.heixue.util;

import java.io.FileNotFoundException;
import java.io.IOException;

/ **
* @ Author heixue
*
* /
public class FileLog (

private String logFilePath = "/ usr / tomcat_log.txt ";//" c: \ \ tomcat_log.txt"
java.io.OutputStream outputStream = null;

/ **
*
* /
public FileLog () (
super ();
)
/ **
* @ Param stream: output stream
* /
public FileLog (java.io.OutputStream stream) (
outputStream = stream;
)
/ **
* @ Param filepath: the file path
* /
public FileLog (String filepath) (
super ();
try (
outputStream = new java.io.FileOutputStream (filepath, true);
) Catch (FileNotFoundException e) (
e.printStackTrace ();
)
)
public void error (String str) (
java.util.Date date = new java.util.Date ();
String time = (date.getYear () +1900 )+"-"+( date.getMonth () +1 )+"-"+ date.getDate () + "" + date.getHours ()+":"+ date.getMinutes ()+":"+ date.getSeconds ();
str = "error:" + time +"-->"+ str + "\ n";
if (outputStream! = null) try (
this.outputStream.write (str.getBytes ());
this.outputStream.flush ();
) Catch (IOException e) (
e.printStackTrace ();
)
)
public void info (String str) (
java.util.Date date = new java.util.Date ();
String time = (date.getYear () +1900 )+"-"+( date.getMonth () +1 )+"-"+ date.getDate () + "" + date.getHours ()+":"+ date.getMinutes ()+":"+ date.getSeconds ();
str = "info:" + time +"-->"+ str + "\ n";
if (outputStream! = null) try (
this.outputStream.write (str.getBytes ());
this.outputStream.flush ();
) Catch (IOException e) (
e.printStackTrace ();
)
)

public static void main (String [] args) (
/ / FileLog fl = new FileLog ();
/ / fl.setOutputStream (System.out);
/ / fl.error ( "error occours! contrarian");

FileLog fl = new FileLog ( "c: / log.txt");
fl.error ( "error occours! contrarian");
)
/ **
* @ Return
* /
public String getLogFilePath () (
return logFilePath;
)

/ **
* @ Param string
* /
public void setLogFilePath (String string) (
logFilePath = string;
)

/ **
* @ Return
* /
public java.io.OutputStream getOutputStream () (
return outputStream;
)

/ **
* @ Param stream
* /
public void setOutputStream (java.io.OutputStream stream) (
outputStream = stream;
)

)



Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.