java-wrapper 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #!/usr/bin/perl -w
  2. # Define version
  3. my $version = "2.0";
  4. # Be strict to avoid messy code
  5. use strict;
  6. # Use FindBin module to get script directory
  7. use FindBin;
  8. # Activate all overrides (if any)
  9. my $args = set_overrides();
  10. # Get script directory
  11. my $cwd = $FindBin::RealBin;
  12. # Get script name
  13. my $scriptname = $FindBin::Script;
  14. # Get the home path variable
  15. my $HOME = $ENV{"HOME"};
  16. # If parameters contains -install
  17. if ("@ARGV" =~ /(-|--)install/)
  18. {
  19. install();
  20. }
  21. # If parameters contains -reinstall
  22. elsif ("@ARGV" =~ /(-|--)reinstall/)
  23. {
  24. reinstall();
  25. }
  26. # If parameters contains -uninstall
  27. elsif ("@ARGV" =~ /(-|--)uninstall/)
  28. {
  29. uninstall();
  30. }
  31. # If parameters contains -help
  32. elsif ("@ARGV" =~ /(-|--)help/)
  33. {
  34. help();
  35. }
  36. # If parameters contains -update
  37. elsif ("@ARGV" =~ /(-|--)update/)
  38. {
  39. update();
  40. }
  41. # Else run main function which redirects java calls properly
  42. else
  43. {
  44. main();
  45. }
  46. sub main
  47. {
  48. # Get the java binary to use
  49. my $java_binary = "$cwd/java-bin";
  50. # Get the library path needed
  51. my $ldpath = unix_findlibrarypath("$java_binary");
  52. # Check if the client jvm is available
  53. my $clientmodecheck = `"$java_binary" -help 2>&1`;
  54. # Make a variable to contain the client parameter(if supported)
  55. my $prms = "";
  56. # If -client parameter is supported
  57. if ($clientmodecheck =~ /-client/)
  58. {
  59. # Add the parameter to the command
  60. $prms = "-client";
  61. }
  62. # If the parameters include -version
  63. if ("@ARGV" =~ /-version/)
  64. {
  65. # Print the java version
  66. system "$ldpath $java_binary $prms @ARGV";
  67. # Print java-wrapper information
  68. print "\nExecuted with the Environment Variable\n$ldpath\nThrough java-wrapper version: $version\nParameters were: $args\n";
  69. # Exit
  70. exit
  71. }
  72. # Execute java and wrap the libraries inside LD_LIBRARY_PATH
  73. system "$ldpath $java_binary $prms $args && exit";
  74. }
  75. #
  76. #---------------------------------------- *** ----------------------------------------
  77. #
  78. sub unix_findlibrarypath
  79. {
  80. # Gets passed data from the function call
  81. my ($binary) = "@_";
  82. # List up the shared library files used by the java binary (not the symlink!) and remove unneeded info
  83. my $lddresult = `ldd $binary | grep libjli.so`;
  84. # Finds the library path from the ldd output line, removing whitespaces before
  85. # and after the path.
  86. $lddresult =~ s/\s*libjli\.so\s*=>\s+(.*)jli\/libjli\.so\s+\(\S+\)\s*$/$1/;
  87. # Return the library path for java
  88. return "LD_LIBRARY_PATH=$lddresult";
  89. }
  90. #
  91. #---------------------------------------- *** ----------------------------------------
  92. #
  93. sub install
  94. {
  95. # Locate all Java binaries installed
  96. my ($location, $binary) = locate_java_installs();
  97. # Check if the script is already installed
  98. my $alreadyinstalledcheck = `ls $location`;
  99. # If the ls shows java-bin then
  100. if ($alreadyinstalledcheck =~ /java-bin/)
  101. {
  102. die "The wrapper is possibly already installed in this java directory!\nPlease run java-wrapper --uninstall first or java-wrapper --reinstall\n\n";
  103. }
  104. # Configure the wrapper
  105. configure_wrapper($location, $binary);
  106. }
  107. #
  108. #---------------------------------------- *** ----------------------------------------
  109. #
  110. sub locate_java_installs
  111. {
  112. # Tell what we are doing
  113. print "Trying to find all java binaries on the computer\nThis MAY take some time\n";
  114. # Find the java binaries on the computer
  115. my $java_binaries = `find / -name "libjli.so" 2>&1 | grep -v Permission`;
  116. # Replace the libjli.so location with the binary location
  117. $java_binaries =~ s/\/lib\/(i386|amd64)\/jli\/libjli.so/\/bin\/java/g;
  118. # Split the list by newlines
  119. my @binaries = split (/\n/, $java_binaries);
  120. # Print the top of the message
  121. print "Please enter the number for the java binary you want this script to use.\n\n";
  122. # Make a counter variable
  123. my $counter = 0;
  124. # For each binary in the array
  125. foreach (@binaries)
  126. {
  127. # Print an option on the screen
  128. print " [$counter] $binaries[$counter]\n";
  129. # Increase counter by 1
  130. $counter += 1;
  131. }
  132. # Print a message and ask for the input
  133. print "\nType in the number for the java binary this script shall use,\nEnter the number and press Enter/Return:";
  134. # Wait for input
  135. my $choosen_binary = <STDIN>;
  136. # Pass the binary to another variable to fetch out the path
  137. my $location = $binaries[$choosen_binary];
  138. # Remove "java" from the end
  139. $location =~ s/\/java$//;
  140. # Return the Java location and the choosen binary to wrap the script around
  141. return ($location, $binaries[$choosen_binary]);
  142. }
  143. #
  144. #---------------------------------------- *** ----------------------------------------
  145. #
  146. sub configure_wrapper
  147. {
  148. my ($location, $binary) = @_;
  149. # Rename the real binary to java-bin
  150. print "Renaming the real binary to java-bin\n";
  151. system "mv -v \"$binary\" \"$location/java-bin\"";
  152. # Copy the script itself to the old binary location
  153. print "\nCopying the wrapper to $location/java\n";
  154. system "cp -v \"$cwd/$scriptname\" \"$binary\"";
  155. }
  156. #
  157. #---------------------------------------- *** ----------------------------------------
  158. #
  159. sub reinstall
  160. {
  161. # Locate all Java binaries installed
  162. my ($location, $binary) = locate_java_installs();
  163. # Check if the script is already installed
  164. my $alreadyinstalledcheck = `ldd $location/java`;
  165. # If the ls shows java-bin then
  166. if ($alreadyinstalledcheck =~ /libjli.so/)
  167. {
  168. # Configure/install the script
  169. configure_wrapper($location, $binary)
  170. }
  171. else
  172. {
  173. # Copy the script itself to the old binary location
  174. print "\nCopying the wrapper to $location/java\n";
  175. system "cp -v \"$cwd/$scriptname\" \"$binary\"";
  176. }
  177. }
  178. #
  179. #---------------------------------------- *** ----------------------------------------
  180. #
  181. sub uninstall
  182. {
  183. print "Preparing uninstall\n";
  184. # Locate all Java binaries installed
  185. my ($location, $binary) = locate_java_installs();
  186. # Check if the script is already installed
  187. my $alreadyinstalledcheck = `ldd $location/java`;
  188. # If the ls shows java-bin then
  189. if ($alreadyinstalledcheck !~ /libjli.so/)
  190. {
  191. # Rename the real binary to java-bin
  192. print "Uninstalling the java-wrapper from $location.\n";
  193. system "mv -v \"$location/java-bin\" \"$binary\"";
  194. }
  195. else
  196. {
  197. print "Nothing to uninstall!\n";
  198. }
  199. }
  200. #
  201. #---------------------------------------- *** ----------------------------------------
  202. #
  203. sub activate_overrides
  204. {
  205. my ($args, $overrides) = @_;
  206. # For each value in the array
  207. foreach my $hashkey (keys $overrides)
  208. {
  209. # If the hashkey starts with _set_
  210. if ($hashkey =~ /^_set_/i)
  211. {
  212. # Go to next loop
  213. next;
  214. }
  215. else
  216. {
  217. # Transfer the current hashkey to $setkey
  218. my $setkey = $hashkey;
  219. # Replace _if_ with _set_
  220. $setkey =~ s/_if_/_set_/i;
  221. # Override the parameter if found
  222. $args =~ s/$overrides->{$hashkey}/$overrides->{$setkey}/ig;
  223. }
  224. }
  225. return $args;
  226. }
  227. #
  228. #---------------------------------------- *** ----------------------------------------
  229. #
  230. sub set_overrides
  231. {
  232. # This is the default overrides that will be used if overrides.pm
  233. # does not exist in ~/.config/java-wrapper
  234. my $overrides = {
  235. _if_maxmem => "-Xmx256m",
  236. _set_maxmem => "-Xmx512m",
  237. _if_stacksize => "-Xss1m",
  238. _set_stacksize => "-Xss2m",
  239. };
  240. ## END OF OVERRIDES
  241. # Make folder for overrides config
  242. system "mkdir -p ".$ENV{"HOME"}."/.config/java-wrapper";
  243. # Get the contents of the folder
  244. my $module_exists = `ls $ENV{"HOME"}/.config/java-wrapper`;
  245. if ($module_exists =~ /overrides.pm/)
  246. {
  247. require $ENV{"HOME"}."/.config/java-wrapper/overrides.pm";
  248. $overrides = overrides::get_overrides();
  249. }
  250. elsif ($module_exists !~ /overrides.pm/)
  251. {
  252. print $module_exists;
  253. WriteFile('package overrides;
  254. # This is a collection of programmable overrides for the java parameters
  255. # _if_overridename is set to(=>) "value",
  256. # _set_overridename set it to(=>) "value",
  257. # NOTE: you can add any if statements if you follow the method above!
  258. $overrides = {
  259. _if_maxmem => "-Xmx256m",
  260. _set_maxmem => "-Xmx512m",
  261. _if_stacksize => "-Xss1m",
  262. _set_stacksize => "-Xss2m",
  263. };
  264. ## END OF OVERRIDES
  265. #
  266. #---------------------------------------- *** ----------------------------------------
  267. #
  268. # Callback, DO NOT TOUCH!
  269. sub get_overrides
  270. {
  271. return $overrides;
  272. }
  273. ', ">", $ENV{"HOME"}."/.config/java-wrapper/overrides.pm");
  274. }
  275. # Create a variable holder for the parameters
  276. my $args = "@ARGV";
  277. # Activate overrides
  278. $args = activate_overrides($args, $overrides);
  279. }
  280. #
  281. #---------------------------------------- *** ----------------------------------------
  282. #
  283. # Write a file from scratch(deletes previous content)
  284. sub WriteFile
  285. {
  286. # Get the passed variables
  287. my ($content, $writemode, $outfile) = @_;
  288. # Open the outfile for Writing/Rewrite
  289. open (my $FILE, "$writemode$outfile");
  290. # Write the content passed to the function to the file
  291. print $FILE "$content\n";
  292. }
  293. #
  294. #---------------------------------------- *** ----------------------------------------
  295. #
  296. sub update
  297. {
  298. # Tell user that we are updating
  299. print "Updating script from $cwd/$scriptname http://dl.dropbox.com/u/11631899/opensource/Perl/java-wrapper/java-wrapper\n";
  300. # Overwrite the script with the newest version
  301. system "wget -O $cwd/$scriptname http://dl.dropbox.com/u/11631899/opensource/Perl/java-wrapper/java-wrapper";
  302. }
  303. #
  304. #---------------------------------------- *** ----------------------------------------
  305. #
  306. sub help
  307. {
  308. # Run java -help
  309. system "$cwd/java-bin -help";
  310. # Print the java-wrapper help
  311. print "\njava-wrapper help:\n\t-install\n\t\tFinds all java locations and installs the wrapper in the\n\t\tJava location of your choice.\n\t-reinstall\n\t\tFinds all Java locations and reinstalls the wrapper in the\n\t\tJava location of your choice.\n\t-uninstall\n\t\tFinds all Java locations and uninstalls the wrapper from the\n\t\tJava location of your choice.\n\t-update\n\t\tDownload and overwrite the script with the newest\n\t\tversion of the wrapper.\n\t-help\n\t\tShows this help text\n";
  312. }
  313. #
  314. #---------------------------------------- *** ----------------------------------------
  315. #