]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.procore.server.environment/src/org/simantics/db/procore/server/environment/ServerEnvironment.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.procore.server.environment / src / org / simantics / db / procore / server / environment / ServerEnvironment.java
diff --git a/bundles/org.simantics.db.procore.server.environment/src/org/simantics/db/procore/server/environment/ServerEnvironment.java b/bundles/org.simantics.db.procore.server.environment/src/org/simantics/db/procore/server/environment/ServerEnvironment.java
new file mode 100644 (file)
index 0000000..4a0290d
--- /dev/null
@@ -0,0 +1,129 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.procore.server.environment;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.net.URL;\r
+import java.util.Arrays;\r
+\r
+import org.eclipse.core.runtime.FileLocator;\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.simantics.db.procore.server.environment.windows.Msi;\r
+import org.simantics.db.procore.server.environment.windows.Product;\r
+import org.simantics.db.procore.server.environment.windows.ProductCodes;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class ServerEnvironment {\r
+\r
+    // http://msdn.microsoft.com/en-us/library/aa368542(v=vs.85).aspx\r
+    private static final int ERROR_SUCCESS                 = 0;\r
+    private static final int ERROR_SUCCESS_REBOOT_REQUIRED = 3010;\r
+\r
+    /**\r
+     * Checks whether the current running environment has all the necessary\r
+     * components installed to run the ProCore database server.\r
+     * \r
+     * @throws ExecutionEnvironmentException if dependencies for running the\r
+     *         database server are not met\r
+     */\r
+    public static void ensureServerDependenciesMet() throws ExecutionEnvironmentException {\r
+        ExecutionEnvironment env = ExecutionEnvironment.calculate();\r
+        switch (env.os) {\r
+            case WINDOWS: {\r
+                switch (env.arch) {\r
+                    case X86:\r
+                        Msi.checkOneOfProductsInstalled(ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174);\r
+                        break;\r
+                    case X86_64:\r
+                        Msi.checkProductsInstalled(ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X64_KB2467174);\r
+                        break;\r
+                }\r
+                break;\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Checks whether the current running environment has all the necessary\r
+     * components installed to run the ProCore database server.\r
+     * \r
+     * @throws ExecutionEnvironmentException if dependencies for running the\r
+     *         database server are not met\r
+     */\r
+    public static void tryInstallDependencies(IProgressMonitor monitor) throws InstallException {\r
+        ExecutionEnvironment env = ExecutionEnvironment.calculate();\r
+        switch (env.os) {\r
+            case WINDOWS: {\r
+                switch (env.arch) {\r
+                    case X86:\r
+                        runInstaller(monitor, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174, "VC90.2008.SP1.KB2467174.redist.x86.exe");\r
+                        break;\r
+                    case X86_64:\r
+                        runInstaller(monitor, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174, "VC90.2008.SP1.KB2467174.redist.x86.exe");\r
+                        runInstaller(monitor, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X64_KB2467174, "VC90.2008.SP1.KB2467174.redist.x64.exe");\r
+                        break;\r
+                }\r
+                break;\r
+            }\r
+        }\r
+    }\r
+\r
+    private static void runInstaller(IProgressMonitor monitor, Product product, String installerPath) throws InstallException {\r
+        try {\r
+            URL url = FileLocator.find(new URL("platform:/plugin/org.simantics.db.procore.server.environment/" + installerPath));\r
+            URL fileUrl = FileLocator.toFileURL(url);\r
+            final File file = new File(fileUrl.getFile());\r
+            System.out.println(file);\r
+\r
+            Process installer = new ProcessBuilder("cmd.exe", "/C", file.toString(), "/qb").start();\r
+            while (true) {\r
+                try {\r
+                    int exitValue = installer.exitValue();\r
+                    //System.out.println("installation done, exit code: " + exitValue);\r
+                    switch (exitValue) {\r
+                        case ERROR_SUCCESS:\r
+                            return;\r
+                        case ERROR_SUCCESS_REBOOT_REQUIRED:\r
+                            throw new RebootRequiredException(Arrays.asList(product));\r
+                    }\r
+                    throw new InstallException("Installation of " + product.getDescription() + " failed with error code " + exitValue, Arrays.asList(product));\r
+                } catch (IllegalThreadStateException e) {\r
+//                    if (monitor.isCanceled()) {\r
+//                        installer.destroy();\r
+//                    }\r
+                    // Not done yet.\r
+                    try {\r
+                        //System.out.println("sleeping");\r
+                        Thread.sleep(250);\r
+                    } catch (InterruptedException ie) {\r
+                    }\r
+                }\r
+            }\r
+        } catch (IOException e) {\r
+            throw new InstallException(e, Arrays.asList(product));\r
+        }\r
+    }\r
+\r
+    // -------------------------------------------------------------------------\r
+\r
+    public static void main(String[] args) {\r
+        try {\r
+            ensureServerDependenciesMet();\r
+        } catch (ExecutionEnvironmentException e) {\r
+            e.printStackTrace();\r
+        }\r
+    }\r
+\r
+}\r