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