PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/source/net/yacy/kelondro/util/OS.java

https://gitorious.org/yacy
Java | 277 lines | 147 code | 27 blank | 103 comment | 65 complexity | 3075f56cd68ab3e9931322bea00d6ddf MD5 | raw file
Possible License(s): Apache-2.0, JSON, BSD-3-Clause, LGPL-3.0, GPL-2.0, LGPL-2.1
  1. // OS.java
  2. // -------------------------------------------
  3. // (C) by Michael Peter Christen; mc@yacy.net
  4. // first published on http://www.anomic.de
  5. // Frankfurt, Germany, 2004
  6. // last major change: 11.03.2004
  7. //
  8. // This program is free software; you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation; either version 2 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. package net.yacy.kelondro.util;
  22. import java.io.BufferedReader;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.InputStreamReader;
  26. import java.lang.management.ManagementFactory;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Properties;
  32. import java.util.Vector;
  33. import net.yacy.cora.document.encoding.UTF8;
  34. import net.yacy.cora.util.ConcurrentLog;
  35. import net.yacy.cora.util.NumberTools;
  36. import net.yacy.server.serverCore;
  37. public final class OS {
  38. // constants for system identification
  39. public enum System {
  40. MacOSC, // 'classic' Mac OS 7.6.1/8.*/9.*
  41. MacOSX, // all Mac OS X
  42. Unix, // all Unix/Linux type systems
  43. Windows, // all Windows 95/98/NT/2K/XP
  44. Unknown; // any other system
  45. }
  46. // constants for file type identification (Mac only)
  47. public static final String blankTypeString = "____";
  48. // system-identification statics
  49. private static final System systemOS;
  50. public static final boolean isMacArchitecture;
  51. private static final boolean isUnixFS;
  52. public static final boolean canExecUnix;
  53. public static final boolean isWindows;
  54. public static final boolean isWin32;
  55. // calculated system constants
  56. public static int maxPathLength = 65535;
  57. // Macintosh-specific statics
  58. public static final Map<String, String> macFSTypeCache = new HashMap<String, String>();
  59. public static final Map<String, String> macFSCreatorCache = new HashMap<String, String>();
  60. // static initialization
  61. static {
  62. // check operation system type
  63. final Properties sysprop = java.lang.System.getProperties();
  64. final String sysname = sysprop.getProperty("os.name","").toLowerCase();
  65. if (sysname.startsWith("mac os x")) systemOS = System.MacOSX;
  66. else if (sysname.startsWith("mac os")) systemOS = System.MacOSC;
  67. else if (sysname.startsWith("windows")) systemOS = System.Windows;
  68. else if ((sysname.startsWith("linux")) || (sysname.startsWith("unix"))) systemOS = System.Unix;
  69. else systemOS = System.Unknown;
  70. isMacArchitecture = ((systemOS == System.MacOSC) || (systemOS == System.MacOSX));
  71. isUnixFS = ((systemOS == System.MacOSX) || (systemOS == System.Unix));
  72. canExecUnix = ((isUnixFS) || (!((systemOS == System.MacOSC) || (systemOS == System.Windows))));
  73. isWindows = (systemOS == System.Windows);
  74. isWin32 = (isWindows && java.lang.System.getProperty("os.arch", "").contains("x86"));
  75. // set up maximum path length according to system
  76. if (isWindows) maxPathLength = 255; else maxPathLength = 65535;
  77. }
  78. /**
  79. * finds the maximum possible heap (may cause high system load)
  80. * @return heap in -Xmx<i>[heap]</i>m
  81. * @author [DW], 07.02.2009
  82. */
  83. private static int getWin32MaxHeap() {
  84. int maxmem = 1000;
  85. while(checkWin32Heap(maxmem)) maxmem += 100;
  86. while(!checkWin32Heap(maxmem)) maxmem -= 10;
  87. return maxmem;
  88. }
  89. private final static ConcurrentLog memchecklog = new ConcurrentLog("MEMCHECK");
  90. /**
  91. * checks heap (may cause high system load)
  92. * @param mem heap to check in -Xmx<i>[heap]</i>m
  93. * @return true if possible
  94. * @author [DW], 07.02.2009
  95. */
  96. private static boolean checkWin32Heap(final int mem){
  97. String line = "";
  98. final List<String> processArgs = new ArrayList<String>();
  99. processArgs.add("java");
  100. processArgs.add("-Xms4m");
  101. processArgs.add("-Xmx" + Integer.toString(mem) + "m");
  102. try {
  103. line = ConsoleInterface.getLastLineConsoleOutput(processArgs, memchecklog);
  104. } catch (final IOException e) {
  105. return false;
  106. }
  107. return (line.indexOf("space for object heap",0) > -1) ? false : true;
  108. }
  109. public static String infoString() {
  110. String s = "System=";
  111. if (systemOS == System.Unknown) s += "unknown";
  112. else if (systemOS == System.MacOSC) s += "Mac OS Classic";
  113. else if (systemOS == System.MacOSX) s += "Mac OS X";
  114. else if (systemOS == System.Unix) s += "Unix/Linux";
  115. else if (systemOS == System.Windows) s += "Windows";
  116. else s += "unknown";
  117. if (isMacArchitecture) s += ", Mac System Architecture";
  118. if (isUnixFS) s += ", has Unix-like File System";
  119. if (canExecUnix) s += ", can execute Unix-Shell Commands";
  120. return s;
  121. }
  122. /** generates a 2-character string containing information about the OS-type*/
  123. public static String infoKey() {
  124. String s = "";
  125. if (systemOS == System.Unknown) s += "o";
  126. else if (systemOS == System.MacOSC) s += "c";
  127. else if (systemOS == System.MacOSX) s += "x";
  128. else if (systemOS == System.Unix) s += "u";
  129. else if (systemOS == System.Windows) s += "w";
  130. else s += "o";
  131. if (isMacArchitecture) s += "m";
  132. if (isUnixFS) s += "f";
  133. if (canExecUnix) s += "e";
  134. return s;
  135. }
  136. public static void deployScript(final File scriptFile, final String theScript) throws IOException {
  137. FileUtils.copy(UTF8.getBytes(theScript), scriptFile);
  138. if(!isWindows){ // set executable
  139. try {
  140. Runtime.getRuntime().exec("chmod 755 " + scriptFile.getAbsolutePath().replaceAll(" ", "\\ ")).waitFor();
  141. } catch (final InterruptedException e) {
  142. ConcurrentLog.severe("DEPLOY", "deploy of script file failed. file = " + scriptFile.getAbsolutePath(), e);
  143. throw new IOException(e.getMessage());
  144. }
  145. }
  146. }
  147. /**
  148. * use a hack to get the current process PID
  149. * @return the PID of the current java process or -1 if the PID cannot be obtained
  150. */
  151. public static int getPID() {
  152. final String pids = ManagementFactory.getRuntimeMXBean().getName();
  153. final int p = pids.indexOf('@');
  154. return p >= 0 ? NumberTools.parseIntDecSubstring(pids, 0, p) : -1;
  155. }
  156. public static void execAsynchronous(final File scriptFile) throws IOException {
  157. // runs a script as separate thread
  158. String starterFileExtension = null;
  159. String script = null;
  160. if(isWindows){
  161. starterFileExtension = ".starter.bat";
  162. // use /K to debug, /C for release
  163. script = "start /MIN CMD /C \"" + scriptFile.getAbsolutePath() + "\"";
  164. } else { // unix/linux
  165. starterFileExtension = ".starter.sh";
  166. script = "#!/bin/sh" + serverCore.LF_STRING + scriptFile.getAbsolutePath().replaceAll(" ", "\\ ") + " &" + serverCore.LF_STRING;
  167. }
  168. final File starterFile = new File(scriptFile.getAbsolutePath().replaceAll(" ", "\\ ") + starterFileExtension);
  169. deployScript(starterFile, script);
  170. try {
  171. Runtime.getRuntime().exec(starterFile.getAbsolutePath().replaceAll(" ", "\\ ")).waitFor();
  172. } catch (final InterruptedException e) {
  173. throw new IOException(e.getMessage());
  174. }
  175. FileUtils.deletedelete(starterFile);
  176. }
  177. public static Vector<String> execSynchronous(final String command) throws IOException {
  178. // runs a unix/linux command and returns output as Vector of Strings
  179. // this method blocks until the command is executed
  180. final Process p = Runtime.getRuntime().exec(command);
  181. final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
  182. String text;
  183. final Vector<String> output = new Vector<String>();
  184. while ((text = in.readLine()) != null) {
  185. output.add(text);
  186. }
  187. in.close();
  188. return output;
  189. }
  190. public static void main(final String[] args) {
  191. if (args[0].equals("-m")) {
  192. java.lang.System.out.println("Maximum possible memory: " + Integer.toString(getWin32MaxHeap()) + "m");
  193. }
  194. }
  195. }
  196. /*
  197. table of common system properties
  198. comparisment between different operation systems
  199. property |Mac OS 9.22 |Mac OSX 10.1.5 |Windows 98 |Linux Kernel 2.4.22 |
  200. -------------------+----------------------+----------------------+----------------------+----------------------+
  201. file.encoding |MacTEC |MacRoman |Cp1252 |ANSI_X3.4-1968 |
  202. file.separator |/ |/ |\ |/ |
  203. java.class.path |/hdisc/... |. |. |/usr/lib/j2se/ext |
  204. java.class.version |45.3 |47.0 |48.0 |47.0 |
  205. java.home |/hdisc/... |/System/Library/... |C:\PROGRAM\... |/usr/lib/j2se/1.3/jre |
  206. java.vendor |Apple Computer, Inc. |Apple Computer, Inc. |Sun Microsystems Inc. |Blackdown Java-Linux |
  207. java.version |1.1.8 |1.3.1 |1.4.0_02 |1.3.1 |
  208. os.arch |PowerPC |ppc |x86 |i386 |
  209. os.name |Mac OS |Mac OS X |Windows 98 |Linux |
  210. os.version |9.2.2 |10.1.5 |4.10 |2.4.22 |
  211. path.separator |: |: |; |: |
  212. user.dir |/hdisc/... |/mydir/... |C:\mydir\... |/home/public |
  213. user.home |/hdisc/... |/Users/myself |C:\WINDOWS |/home/public |
  214. user.language |de |de |de |en |
  215. user.name |Bob |myself |User |public |
  216. user.timezone |ECT |Europe/Berlin |Europe/Berlin | |
  217. -------------------+----------------------+----------------------+----------------------+----------------------+
  218. */
  219. /*
  220. static struct browser possible_browsers[] = {
  221. {N_("Opera"), "opera"},
  222. {N_("Netscape"), "netscape"},
  223. {N_("Mozilla"), "mozilla"},
  224. {N_("Konqueror"), "kfmclient"},
  225. {N_("Galeon"), "galeon"},
  226. {N_("Firebird"), "mozilla-firebird"},
  227. {N_("Firefox"), "firefox"},
  228. {N_("Gnome Default"), "gnome-open"}
  229. };
  230. new:
  231. command = exec("netscape -remote " "\" openURL(\"%s\",new-window) "", uri);
  232. command = exec("opera -newwindow \"%s\"", uri);
  233. command = exec("opera -newpage \"%s\"", uri);
  234. command = exec("galeon -w \"%s\"", uri);
  235. command = exec("galeon -n \"%s\"", uri);
  236. command = exec("%s -remote \"openURL(\"%s\"," "new-window)\"", web_browser, uri);
  237. command = exec("%s -remote \"openURL(\"%s\"," "new-tab)\"", web_browser, uri);
  238. current:
  239. command = exec("netscape -remote " "\"openURL(\"%s\")\"", uri);
  240. command = exec("opera -remote " "\"openURL(\"%s\")\"", uri);
  241. command = exec("galeon \"%s\"", uri);
  242. command = exec("%s -remote \"openURL(\"%s\")\"", web_browser, uri);
  243. no option:
  244. command = exec("kfmclient openURL \"%s\"", uri);
  245. command = exec("gnome-open \"%s\"", uri);
  246. */