-
-
Save Romain-P/53057c21131d2e599e96 to your computer and use it in GitHub Desktop.
Look at case: MacOsX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.heatup.core; | |
import org.heatup.utils.OsCheck; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.io.*; | |
import java.net.URISyntaxException; | |
import java.nio.channels.Channels; | |
import java.nio.channels.FileChannel; | |
import java.util.prefs.Preferences; | |
public class Main extends JFrame { | |
public static void main(String[] args) throws URISyntaxException { | |
deployingSystemLook(); | |
//checking for admin privileges | |
if(elevateApplication()) return; | |
final AppManager manager = new AppManager(); | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
manager.start(); | |
} | |
}); | |
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { | |
public void run() { | |
manager.terminate(); | |
} | |
})); | |
} | |
private static void deployingSystemLook() { | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} catch (Exception e) { | |
} | |
} | |
private static boolean elevateApplication() { | |
if(!runAsAdministrator()) { | |
OsCheck.OSType type = OsCheck.getOperatingSystemType(); | |
switch (type) { | |
case Windows: { | |
try { | |
File file = File.createTempFile("Elevate", ".dat"); | |
FileChannel channel = new FileOutputStream(file).getChannel(); | |
channel.transferFrom(Channels.newChannel(Main.class.getClassLoader().getResourceAsStream("Elevator")), 0, 199552); | |
channel.close(); | |
Runtime.getRuntime().exec(String.format("%s javaw -jar %s", | |
file.getPath(), | |
Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().substring(1))); | |
} catch (Exception e) {} | |
break; | |
} | |
case MacOS: { | |
try { | |
File executor = File.createTempFile("Executor",".sh"); | |
PrintWriter writer = new PrintWriter(executor, "UTF-8"); | |
writer.println("#!/bin/bash"); | |
writer.println(); | |
writer.println("java -jar $*"); | |
writer.close(); | |
File elevator = File.createTempFile("Elevator",".sh"); | |
writer = new PrintWriter(elevator, "UTF-8"); | |
writer.println("#!/bin/bash"); | |
writer.println(); | |
writer.println(String.format("osascript -e \"do shell script \\\"%s $*\\\" with administrator privileges\"", | |
executor.getPath())); | |
writer.close(); | |
Runtime.getRuntime().exec(String.format("%s %s", | |
elevator.getPath(), | |
Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath())); | |
} catch (Exception e) {} | |
break; | |
} | |
case Linux: { | |
try { | |
Runtime.getRuntime().exec(String.format("gksudo java -jar %s", | |
Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().substring(1))); | |
} catch (Exception e) {} | |
break; | |
} | |
case Other: return false; | |
} | |
return true; | |
} | |
return false; | |
} | |
private static boolean runAsAdministrator() { | |
try{ | |
Preferences prefs = Preferences.systemRoot(); | |
prefs.put("foo", "bar"); | |
prefs.remove("foo"); | |
prefs.flush(); | |
return true; | |
}catch(Exception e){ | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment