PageRenderTime 61ms CodeModel.GetById 55ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/installer/Install.java

#
Java | 122 lines | 92 code | 16 blank | 14 comment | 18 complexity | 56100e746ed2dfdcb3c1e384d787526d MD5 | raw file
  1/*
  2 * Install.java - Main class of the installer
  3 *
  4 * Originally written by Slava Pestov for the jEdit installer project. This work
  5 * has been placed into the public domain. You may use this work in any way and
  6 * for any purpose you wish.
  7 *
  8 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, NOT EVEN THE
  9 * IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, ASSUMES
 10 * _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE RESULTING FROM THE USE, MODIFICATION,
 11 * OR REDISTRIBUTION OF THIS SOFTWARE.
 12 */
 13
 14package installer;
 15
 16import javax.swing.plaf.metal.MetalLookAndFeel;
 17import java.io.*;
 18import java.util.Properties;
 19
 20public class Install
 21{
 22	public static void main(String[] args)
 23	{
 24		String javaVersion = System.getProperty("java.version");
 25		if(javaVersion.compareTo("1.3") < 0)
 26		{
 27			System.err.println("You are running Java version "
 28				+ javaVersion + ".");
 29			System.err.println("This installer requires Java 1.3 or later.");
 30			System.exit(1);
 31		}
 32
 33		if(args.length == 0)
 34		{
 35			if(javaVersion.compareTo("1.5") < 0)
 36				MetalLookAndFeel.setCurrentTheme(new JEditMetalTheme());
 37			new SwingInstall();
 38		}
 39		else if(args.length == 1 && args[0].equals("text"))
 40			new ConsoleInstall();
 41		else if(args.length >= 2 && args[0].equals("auto"))
 42		{
 43			new NonInteractiveInstall(args);
 44		}
 45		else
 46		{
 47			System.err.println("Usage:");
 48			System.err.println("java -jar <installer JAR>");
 49			System.err.println("java -jar <installer JAR> text");
 50			System.err.println("java -jar <installer JAR> auto"
 51				+ " <install dir> [unix-script=<dir>] [unix-man=<dir>]");
 52			System.err.println("text parameter starts installer in text-only mode.");
 53			System.err.println("auto parameter starts installer in non-interactive mode.");
 54		}
 55	}
 56
 57	public Install()
 58	{
 59		props = new Properties();
 60		try
 61		{
 62			InputStream in = getClass().getResourceAsStream("/installer/install.props");
 63			props.load(in);
 64			in.close();
 65		}
 66		catch(IOException io)
 67		{
 68			System.err.println("Error loading 'install.props':");
 69			io.printStackTrace();
 70		}
 71
 72		buf = new byte[32768];
 73	}
 74
 75	public String getProperty(String name)
 76	{
 77		return props.getProperty(name);
 78	}
 79
 80	public int getIntegerProperty(String name)
 81	{
 82		try
 83		{
 84			return Integer.parseInt(props.getProperty(name));
 85		}
 86		catch(Exception e)
 87		{
 88			return -1;
 89		}
 90	}
 91
 92	public void copy(InputStream in, String outfile, Progress progress)
 93		throws IOException
 94	{
 95		File outFile = new File(outfile);
 96
 97		OperatingSystem.getOperatingSystem().mkdirs(outFile.getParent());
 98
 99		BufferedOutputStream out = new BufferedOutputStream(
100			new FileOutputStream(outFile));
101
102		int count;
103
104		for(;;)
105		{
106			count = in.read(buf,0,Math.min(in.available(),buf.length));
107			if(count == -1 || count == 0)
108				break;
109
110			out.write(buf,0,count);
111			if(progress != null)
112				progress.advance(count);
113		}
114
115		//in.close();
116		out.close();
117	}
118
119	// private members
120	private Properties props;
121	private byte[] buf;
122}