PageRenderTime 148ms CodeModel.GetById 136ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 143 lines | 95 code | 25 blank | 23 comment | 9 complexity | cb498de2234ec66eedd95401aef9ab13 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(FileNotFoundException fnf)
 70		{
 71			progress.error("The installer could not create the "
 72				+ "destination directory.\n"
 73				+ "Maybe you do not have write permission?");
 74			return;
 75		}
 76		catch(IOException io)
 77		{
 78			progress.error(io.toString());
 79			return;
 80		}
 81
 82		progress.done();
 83	}
 84
 85	// private members
 86	private Install installer;
 87	private Progress progress;
 88	private String installDir;
 89	private String binDir;
 90	private int size;
 91	private Vector components;
 92	private byte[] buf;
 93
 94	private void installComponent(String name) throws IOException
 95	{
 96		BufferedReader fileList = new BufferedReader(
 97			new InputStreamReader(getClass()
 98			.getResourceAsStream(name)));
 99
100		String fileName;
101		while((fileName = fileList.readLine()) != null)
102		{
103			String outfile = installDir + File.separatorChar
104				+ fileName.replace('/',File.separatorChar);
105
106			InputStream in = new BufferedInputStream(
107				getClass().getResourceAsStream("/" + fileName));
108
109			if(in == null)
110				throw new FileNotFoundException(fileName);
111
112			copy(in,outfile);
113			in.close();
114		}
115
116		fileList.close();
117	}
118
119	private void copy(InputStream in, String outfile) throws IOException
120	{
121		File outFile = new File(outfile);
122
123		OperatingSystem.getOperatingSystem().mkdirs(outFile.getParent());
124
125		BufferedOutputStream out = new BufferedOutputStream(
126			new FileOutputStream(outFile));
127
128		int count;
129
130		for(;;)
131		{
132			count = in.read(buf,0,buf.length);
133			if(count == -1)
134				break;
135
136			out.write(buf,0,count);
137			progress.advance(count);
138		}
139
140		in.close();
141		out.close();
142	}
143}