X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.procore.server.environment%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fprocore%2Fserver%2Fenvironment%2Fwindows%2FMsi.java;fp=bundles%2Forg.simantics.db.procore.server.environment%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fprocore%2Fserver%2Fenvironment%2Fwindows%2FMsi.java;h=85c83a51c3a54f99d73305290dc9c642c064b387;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.procore.server.environment/src/org/simantics/db/procore/server/environment/windows/Msi.java b/bundles/org.simantics.db.procore.server.environment/src/org/simantics/db/procore/server/environment/windows/Msi.java new file mode 100644 index 000000000..85c83a51c --- /dev/null +++ b/bundles/org.simantics.db.procore.server.environment/src/org/simantics/db/procore/server/environment/windows/Msi.java @@ -0,0 +1,245 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.procore.server.environment.windows; + +import java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLDecoder; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; + +import org.simantics.db.procore.server.environment.ExecutionEnvironment; +import org.simantics.db.procore.server.environment.ExecutionEnvironmentException; + +/** + * Provides Java access to Windows Msi API functions: + *
true
to use {@link File#deleteOnExit()}
+ * on the resulting file. Note that this does not guarantee that the
+ * file is deleted when the JVM exits
+ * @return the resulting file
+ * @throws FileNotFoundException
+ */
+ private static File copyResource(URL url, File targetFile) throws IOException, FileNotFoundException {
+ FileOutputStream os = null;
+ InputStream is = null;
+ try {
+ if (targetFile.exists())
+ targetFile.delete();
+
+ is = url.openStream();
+ int read;
+ byte [] buffer = new byte [16384];
+ os = new FileOutputStream (targetFile);
+ while ((read = is.read (buffer)) != -1) {
+ os.write(buffer, 0, read);
+ }
+ os.close ();
+ is.close ();
+
+ return targetFile;
+ } finally {
+ uncheckedClose(os);
+ uncheckedClose(is);
+ }
+ }
+
+
+ private static void uncheckedClose(Closeable closeable) {
+ try {
+ if (closeable != null)
+ closeable.close();
+ } catch (IOException e) {
+ //ignore
+ }
+ }
+
+ private static File extractLib(URL libURL, String libName) throws FileNotFoundException, IOException {
+ String tmpDirStr = System.getProperty("java.io.tmpdir");
+ if (tmpDirStr == null)
+ throw new NullPointerException("java.io.tmpdir property is null");
+ File tmpDir = new File(tmpDirStr);
+ File libFile = new File(tmpDir, libName);
+
+ if (libFile.exists()) {
+ if (libFile.isFile()) {
+ try {
+ byte[] origSum = computeSum(libURL);
+ byte[] targetSum = computeSum(libFile);
+ if (Arrays.equals(origSum, targetSum))
+ return libFile;
+ } catch (NoSuchAlgorithmException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ return copyResource(libURL, libFile);
+ }
+
+ public static byte[] computeSum(InputStream in) throws IOException {
+ if (in == null)
+ throw new IllegalArgumentException("Input cannot be null!");
+
+ try {
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ byte[] data = new byte[64 * 1024];
+
+ while (true) {
+ int read = in.read(data);
+ if (read == -1) {
+ return md.digest();
+ }
+ md.update(data, 0, read);
+ }
+ } catch (NoSuchAlgorithmException e) {
+ // Should not be possible for MD5
+ throw new IOException(e);
+ }
+ }
+
+ public static byte[] computeSum(File f) throws IOException, NoSuchAlgorithmException {
+ InputStream in = null;
+ try {
+ in = new FileInputStream(f);
+ return computeSum(in);
+ } finally {
+ if (in != null)
+ in.close();
+ }
+ }
+
+ public static byte[] computeSum(URL url) throws IOException, NoSuchAlgorithmException {
+ InputStream in = null;
+ try {
+ in = url.openStream();
+ return computeSum(in);
+ } finally {
+ if (in != null)
+ in.close();
+ }
+ }
+
+}
\ No newline at end of file