--- /dev/null
+package org.simantics.db.server.internal;\r
+\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.net.URL;\r
+\r
+import org.eclipse.core.runtime.FileLocator;\r
+import org.eclipse.core.runtime.Path;\r
+import org.eclipse.core.runtime.Plugin;\r
+import org.osgi.framework.Bundle;\r
+import org.osgi.framework.BundleContext;\r
+\r
+public class Activator extends Plugin {\r
+\r
+ public static final String PLUGIN_ID = "org.simantics.db.server";\r
+ static Activator activator;\r
+\r
+ @Override\r
+ public void start(BundleContext context) throws Exception {\r
+ activator = this;\r
+ super.start(context);\r
+ }\r
+\r
+ @Override\r
+ public void stop(BundleContext context) throws Exception {\r
+ super.stop(context);\r
+ activator = null;\r
+ }\r
+\r
+ public static Activator get() {\r
+ return activator;\r
+ }\r
+\r
+ private static final String ROOT = "/";\r
+\r
+ public static File getRoot()\r
+ throws FileNotFoundException, IOException {\r
+ Bundle b = Activator.get().getBundle();\r
+ URL source = FileLocator.find(b, new Path(ROOT), null);\r
+ URL fileSource = FileLocator.toFileURL(source);\r
+ if (fileSource == source)\r
+ throw new FileNotFoundException(ROOT + "could not be made available from bundle " + b.getSymbolicName());\r
+ File root = new File(fileSource.getFile());\r
+ return root;\r
+ }\r
+ public static File getServerFolder()\r
+ throws FileNotFoundException, IOException {\r
+ final String suffix = calculateSuffix();\r
+ final String name = getRoot() + File.separator + suffix;\r
+ File folder = new File(name);\r
+ if (!folder.isDirectory()) {\r
+ String wd = new File(".").getAbsolutePath();\r
+ throw new FileNotFoundException("No directory for " + name + " current directory=" + wd + ".");\r
+ }\r
+ return folder;\r
+ }\r
+ public static String getExeSuffix() {\r
+ String osName = System.getProperty("os.name").toLowerCase();\r
+ if (osName.startsWith("windows"))\r
+ return ".exe";\r
+ else\r
+ return "";\r
+ }\r
+ private static String calculateSuffix() throws FileNotFoundException {\r
+ String osArch = System.getProperty("os.arch").toLowerCase();\r
+ String osName = System.getProperty("os.name").toLowerCase();\r
+ if (is64BitOS()) {\r
+ if (osName.startsWith("windows"))\r
+ return "win32.x86_64";\r
+ else if (osName.startsWith("linux"))\r
+ return "linux.x86_64";\r
+ else if (osName.startsWith("darwin"))\r
+ return "darwin.x86_64";\r
+ } else if (osArch.equals("i386") || osArch.equals("i586") || osArch.equals("i686") || osArch.equals("x86")) {\r
+ if (osName.startsWith("windows"))\r
+ return "win32.x86";\r
+ else if (osName.startsWith("linux"))\r
+ return "linux.x86";\r
+ }\r
+ throw new FileNotFoundException("Unsupported architecture " + osArch + " and/or operating system " + osName);\r
+ }\r
+\r
+ private static boolean is64BitOS() {\r
+ if (System.getProperty("os.name").toLowerCase().contains("windows")) {\r
+ // Even though os.arch reports a 32-bit system this might be because\r
+ // we are running 32-bit Java. Therefore check the OS architecture.\r
+ // Note that WMI would be the correct way to check this, but this\r
+ // works pretty well also.\r
+ return System.getenv("ProgramFiles(x86)") != null;\r
+ }\r
+ return System.getProperty("os.arch").indexOf("64") != -1;\r
+ }\r
+}\r