PageRenderTime 79ms CodeModel.GetById 68ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/installer/InstallThread.java

#
Java | 136 lines | 88 code | 25 blank | 23 comment | 9 complexity | 7333f09a37fdcd7347a27aed2545b5d9 MD5 | raw file
  1/*
  2 * InstallThread.java
  3 * Copyright (C) 1999, 2000 Slava Pestov
  4 *
  5 * This program is free software; you can redistribute it and/or
  6 * modify it under the terms of the GNU General Public License
  7 * as published by the Free Software Foundation; either version 2
  8 * of the License, or any later version.
  9 *
 10 * This program is distributed in the hope that it will be useful,
 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13 * GNU General Public License for more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program; if not, write to the Free Software
 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 18 */
 19
 20package installer;
 21
 22import java.io.*;
 23import java.util.Vector;
 24
 25/*
 26 * The thread that performs installation.
 27 */
 28public class InstallThread extends Thread
 29{
 30	public InstallThread(Install installer, Progress progress,
 31		String installDir, String binDir, int size, Vector components)
 32	{
 33		super("Install thread");
 34
 35		this.installer = installer;
 36		this.progress = progress;
 37		this.installDir = installDir;
 38		this.binDir = binDir;
 39		this.size = size;
 40		this.components = components;
 41
 42		buf = new byte[32768];
 43	}
 44
 45	public void setProgress(Progress progress)
 46	{
 47		this.progress = progress;
 48	}
 49
 50	public void run()
 51	{
 52		progress.setMaximum(size * 1024);
 53
 54		try
 55		{
 56			for(int i = 0; i < components.size(); i++)
 57			{
 58				installComponent((String)components.elementAt(i));
 59			}
 60
 61			// create it in case it doesn't already exist
 62			if(binDir != null)
 63				OperatingSystem.getOperatingSystem().mkdirs(binDir);
 64
 65			OperatingSystem.getOperatingSystem().createScript(
 66				installer,installDir,binDir,
 67				installer.getProperty("app.name"));
 68		}
 69		catch(IOException io)
 70		{
 71			progress.error(io.toString());
 72			return;
 73		}
 74
 75		progress.done();
 76	}
 77
 78	// private members
 79	private Install installer;
 80	private Progress progress;
 81	private String installDir;
 82	private String binDir;
 83	private int size;
 84	private Vector components;
 85	private byte[] buf;
 86
 87	private void installComponent(String name) throws IOException
 88	{
 89		BufferedReader fileList = new BufferedReader(
 90			new InputStreamReader(getClass()
 91			.getResourceAsStream(name)));
 92
 93		String fileName;
 94		while((fileName = fileList.readLine()) != null)
 95		{
 96			String outfile = installDir + File.separatorChar
 97				+ fileName.replace('/',File.separatorChar);
 98
 99			InputStream in = new BufferedInputStream(
100				getClass().getResourceAsStream("/" + fileName));
101
102			if(in == null)
103				throw new FileNotFoundException(fileName);
104
105			copy(in,outfile);
106			in.close();
107		}
108
109		fileList.close();
110	}
111
112	private void copy(InputStream in, String outfile) throws IOException
113	{
114		File outFile = new File(outfile);
115
116		OperatingSystem.getOperatingSystem().mkdirs(outFile.getParent());
117
118		BufferedOutputStream out = new BufferedOutputStream(
119			new FileOutputStream(outFile));
120
121		int count;
122
123		for(;;)
124		{
125			count = in.read(buf,0,buf.length);
126			if(count == -1)
127				break;
128
129			out.write(buf,0,count);
130			progress.advance(count);
131		}
132
133		in.close();
134		out.close();
135	}
136}