]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.procore.server.environment;\r
13 \r
14 import java.io.File;\r
15 import java.io.IOException;\r
16 import java.net.URL;\r
17 import java.util.Arrays;\r
18 \r
19 import org.eclipse.core.runtime.FileLocator;\r
20 import org.eclipse.core.runtime.IProgressMonitor;\r
21 import org.simantics.db.procore.server.environment.windows.Msi;\r
22 import org.simantics.db.procore.server.environment.windows.Product;\r
23 import org.simantics.db.procore.server.environment.windows.ProductCodes;\r
24 \r
25 /**\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public final class ServerEnvironment {\r
29 \r
30     // http://msdn.microsoft.com/en-us/library/aa368542(v=vs.85).aspx\r
31     private static final int ERROR_SUCCESS                 = 0;\r
32     private static final int ERROR_SUCCESS_REBOOT_REQUIRED = 3010;\r
33 \r
34     /**\r
35      * Checks whether the current running environment has all the necessary\r
36      * components installed to run the ProCore database server.\r
37      * \r
38      * @throws ExecutionEnvironmentException if dependencies for running the\r
39      *         database server are not met\r
40      */\r
41     public static void ensureServerDependenciesMet() throws ExecutionEnvironmentException {\r
42         ExecutionEnvironment env = ExecutionEnvironment.calculate();\r
43         switch (env.os) {\r
44             case WINDOWS: {\r
45                 switch (env.arch) {\r
46                     case X86:\r
47                         Msi.checkOneOfProductsInstalled(ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174);\r
48                         break;\r
49                     case X86_64:\r
50                         Msi.checkProductsInstalled(ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X64_KB2467174);\r
51                         break;\r
52                 }\r
53                 break;\r
54             }\r
55         }\r
56     }\r
57 \r
58     /**\r
59      * Checks whether the current running environment has all the necessary\r
60      * components installed to run the ProCore database server.\r
61      * \r
62      * @throws ExecutionEnvironmentException if dependencies for running the\r
63      *         database server are not met\r
64      */\r
65     public static void tryInstallDependencies(IProgressMonitor monitor) throws InstallException {\r
66         ExecutionEnvironment env = ExecutionEnvironment.calculate();\r
67         switch (env.os) {\r
68             case WINDOWS: {\r
69                 switch (env.arch) {\r
70                     case X86:\r
71                         runInstaller(monitor, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174, "VC90.2008.SP1.KB2467174.redist.x86.exe");\r
72                         break;\r
73                     case X86_64:\r
74                         runInstaller(monitor, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X86_KB2467174, "VC90.2008.SP1.KB2467174.redist.x86.exe");\r
75                         runInstaller(monitor, ProductCodes.VISUAL_CPP_2008_SP1_REDIST_X64_KB2467174, "VC90.2008.SP1.KB2467174.redist.x64.exe");\r
76                         break;\r
77                 }\r
78                 break;\r
79             }\r
80         }\r
81     }\r
82 \r
83     private static void runInstaller(IProgressMonitor monitor, Product product, String installerPath) throws InstallException {\r
84         try {\r
85             URL url = FileLocator.find(new URL("platform:/plugin/org.simantics.db.procore.server.environment/" + installerPath));\r
86             URL fileUrl = FileLocator.toFileURL(url);\r
87             final File file = new File(fileUrl.getFile());\r
88             System.out.println(file);\r
89 \r
90             Process installer = new ProcessBuilder("cmd.exe", "/C", file.toString(), "/qb").start();\r
91             while (true) {\r
92                 try {\r
93                     int exitValue = installer.exitValue();\r
94                     //System.out.println("installation done, exit code: " + exitValue);\r
95                     switch (exitValue) {\r
96                         case ERROR_SUCCESS:\r
97                             return;\r
98                         case ERROR_SUCCESS_REBOOT_REQUIRED:\r
99                             throw new RebootRequiredException(Arrays.asList(product));\r
100                     }\r
101                     throw new InstallException("Installation of " + product.getDescription() + " failed with error code " + exitValue, Arrays.asList(product));\r
102                 } catch (IllegalThreadStateException e) {\r
103 //                    if (monitor.isCanceled()) {\r
104 //                        installer.destroy();\r
105 //                    }\r
106                     // Not done yet.\r
107                     try {\r
108                         //System.out.println("sleeping");\r
109                         Thread.sleep(250);\r
110                     } catch (InterruptedException ie) {\r
111                     }\r
112                 }\r
113             }\r
114         } catch (IOException e) {\r
115             throw new InstallException(e, Arrays.asList(product));\r
116         }\r
117     }\r
118 \r
119     // -------------------------------------------------------------------------\r
120 \r
121     public static void main(String[] args) {\r
122         try {\r
123             ensureServerDependenciesMet();\r
124         } catch (ExecutionEnvironmentException e) {\r
125             e.printStackTrace();\r
126         }\r
127     }\r
128 \r
129 }\r