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