PageRenderTime 66ms CodeModel.GetById 58ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/installer/Install.java

#
Java | 163 lines | 121 code | 20 blank | 22 comment | 19 complexity | 9fcbab995b98d55101eea629d539d3fe 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.*;
 17import javax.swing.*;
 18import java.io.*;
 19import java.util.Properties;
 20import java.security.*;
 21import java.net.URL;
 22
 23public class Install
 24{
 25	/**
 26	 * detects wether the installer is running from a path
 27	 * containing exclamation marks.
 28	 * This has been reported as a cause of failure on Linux and MS Windows :
 29	 * see bug #2065330 - Installer doesn't run on dir having ! as last char in name.
 30	 */
 31	private static boolean isRunningFromExclam()
 32	{
 33		Class me = Install.class;
 34		ProtectionDomain domaine = me.getProtectionDomain();
 35		CodeSource source = domaine.getCodeSource();
 36		URL mySource = source.getLocation();
 37		// In fact the check is more restrictive than required :
 38		// a problem occurs only when the ! is at the end of directory
 39		return mySource.toString().contains("!");		
 40	}
 41	
 42	private static void errorAndExit(boolean isGUI, String message)
 43	{
 44		if(isGUI)
 45		{
 46			JTextArea messageCnt = new JTextArea(message);
 47			JOptionPane.showMessageDialog(null,
 48				messageCnt,
 49				"jEdit installer error...", JOptionPane.ERROR_MESSAGE); 
 50		}
 51		else
 52		{
 53			System.err.println(message);
 54		}
 55		System.exit(1);
 56	}
 57	
 58	public static void main(String[] args)
 59	{
 60		boolean isGUI = args.length == 0;
 61		
 62		String javaVersion = System.getProperty("java.version");
 63		if(javaVersion.compareTo("1.6") < 0)
 64		{
 65			errorAndExit(isGUI,
 66					  "You are running Java version "
 67					+ javaVersion + " from "+System.getProperty("java.vendor")+".\n"
 68					+"This installer requires Java 1.6 or later.");
 69		}
 70
 71		if(isRunningFromExclam())
 72		{
 73			errorAndExit(isGUI,
 74					  "You are running the installer from a directory containing exclamation marks."
 75					+ "\nIt is a known cause of failure of the installer"
 76					+ "\n(http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4523159 for the curious ones)."
 77					+ "\nPlease move the installer somewhere else and run it again.");
 78		}
 79
 80		if(isGUI)
 81			new SwingInstall();
 82		else if(args.length == 1 && args[0].equals("text"))
 83			new ConsoleInstall();
 84		else if(args.length >= 2 && args[0].equals("auto"))
 85			new NonInteractiveInstall(args);
 86		else
 87		{
 88			System.err.println("Usage:");
 89			System.err.println("java -jar <installer JAR>");
 90			System.err.println("java -jar <installer JAR> text");
 91			System.err.println("java -jar <installer JAR> auto"
 92				+ " <install dir> [unix-script=<dir>] [unix-man=<dir>]");
 93			System.err.println("text parameter starts installer in text-only mode.");
 94			System.err.println("auto parameter starts installer in non-interactive mode.");
 95		}
 96	}
 97
 98	public Install()
 99	{
100		props = new Properties();
101		try
102		{
103			InputStream in = getClass().getResourceAsStream("/installer/install.props");
104			props.load(in);
105			in.close();
106		}
107		catch(IOException io)
108		{
109			System.err.println("Error loading 'install.props':");
110			io.printStackTrace();
111		}
112
113		buf = new byte[32768];
114	}
115
116	public String getProperty(String name)
117	{
118		return props.getProperty(name);
119	}
120
121	public int getIntegerProperty(String name)
122	{
123		try
124		{
125			return Integer.parseInt(props.getProperty(name));
126		}
127		catch(Exception e)
128		{
129			return -1;
130		}
131	}
132
133	public void copy(InputStream in, String outfile, Progress progress)
134		throws IOException
135	{
136		File outFile = new File(outfile);
137
138		OperatingSystem.getOperatingSystem().mkdirs(outFile.getParent());
139
140		BufferedOutputStream out = new BufferedOutputStream(
141			new FileOutputStream(outFile));
142
143		int count;
144
145		for(;;)
146		{
147			count = in.read(buf,0,Math.min(in.available(),buf.length));
148			if(count == -1 || count == 0)
149				break;
150
151			out.write(buf,0,count);
152			if(progress != null)
153				progress.advance(count);
154		}
155
156		//in.close();
157		out.close();
158	}
159
160	// private members
161	private Properties props;
162	private byte[] buf;
163}