]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.server/src/org/simantics/db/server/internal/Activator.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.server / src / org / simantics / db / server / internal / Activator.java
1 package org.simantics.db.server.internal;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileNotFoundException;\r
5 import java.io.IOException;\r
6 import java.net.URL;\r
7 \r
8 import org.eclipse.core.runtime.FileLocator;\r
9 import org.eclipse.core.runtime.Path;\r
10 import org.eclipse.core.runtime.Plugin;\r
11 import org.osgi.framework.Bundle;\r
12 import org.osgi.framework.BundleContext;\r
13 \r
14 public class Activator extends Plugin {\r
15 \r
16     public static final String PLUGIN_ID = "org.simantics.db.server";\r
17     static Activator activator;\r
18 \r
19     @Override\r
20     public void start(BundleContext context) throws Exception {\r
21         activator = this;\r
22         super.start(context);\r
23     }\r
24 \r
25     @Override\r
26     public void stop(BundleContext context) throws Exception {\r
27         super.stop(context);\r
28         activator = null;\r
29     }\r
30 \r
31     public static Activator get() {\r
32         return activator;\r
33     }\r
34 \r
35     private static final String ROOT = "/";\r
36 \r
37     public static File getRoot()\r
38     throws FileNotFoundException, IOException {\r
39         Bundle b = Activator.get().getBundle();\r
40         URL source = FileLocator.find(b, new Path(ROOT), null);\r
41         URL fileSource = FileLocator.toFileURL(source);\r
42         if (fileSource == source)\r
43             throw new FileNotFoundException(ROOT + "could not be made available from bundle " + b.getSymbolicName());\r
44         File root = new File(fileSource.getFile());\r
45         return root;\r
46     }\r
47     public static File getServerFolder()\r
48     throws FileNotFoundException, IOException {\r
49         final String suffix = calculateSuffix();\r
50         final String name = getRoot() + File.separator + suffix;\r
51         File folder = new File(name);\r
52         if (!folder.isDirectory()) {\r
53             String wd = new File(".").getAbsolutePath();\r
54             throw new FileNotFoundException("No directory for " + name + " current directory=" + wd + ".");\r
55         }\r
56         return folder;\r
57     }\r
58     public static String getExeSuffix() {\r
59         String osName = System.getProperty("os.name").toLowerCase();\r
60         if (osName.startsWith("windows"))\r
61             return ".exe";\r
62         else\r
63             return "";\r
64     }\r
65     private static String calculateSuffix() throws FileNotFoundException {\r
66         String osArch = System.getProperty("os.arch").toLowerCase();\r
67         String osName = System.getProperty("os.name").toLowerCase();\r
68         if (is64BitOS()) {\r
69             if (osName.startsWith("windows"))\r
70                 return "win32.x86_64";\r
71             else if (osName.startsWith("linux"))\r
72                 return "linux.x86_64";\r
73             else if (osName.startsWith("darwin"))\r
74                 return "darwin.x86_64";\r
75         } else if (osArch.equals("i386") || osArch.equals("i586") || osArch.equals("i686") || osArch.equals("x86")) {\r
76             if (osName.startsWith("windows"))\r
77                 return "win32.x86";\r
78             else if (osName.startsWith("linux"))\r
79                 return "linux.x86";\r
80         }\r
81         throw new FileNotFoundException("Unsupported architecture " + osArch + " and/or operating system " + osName);\r
82     }\r
83 \r
84     private static boolean is64BitOS() {\r
85         if (System.getProperty("os.name").toLowerCase().contains("windows")) {\r
86             // Even though os.arch reports a 32-bit system this might be because\r
87             // we are running 32-bit Java. Therefore check the OS architecture.\r
88             // Note that WMI would be the correct way to check this, but this\r
89             // works pretty well also.\r
90             return System.getenv("ProgramFiles(x86)") != null;\r
91         }\r
92         return System.getProperty("os.arch").indexOf("64") != -1;\r
93     }\r
94 }\r