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