RTSDK: improve SDK home path find method
authorgyeongmin.ju <gyeongmin.ju@samsung.com>
Thu, 9 Mar 2017 10:07:21 +0000 (19:07 +0900)
committergyeongmin.ju <gyeongmin.ju@samsung.com>
Thu, 9 Mar 2017 14:08:54 +0000 (23:08 +0900)
Change-Id: I0dadcb1ce3a0bff1a4135bd2f11565584b24be21
Signed-off-by: gyeongmin.ju <gyeongmin.ju@samsung.com>
rt-ide/tizen.rt.product.plugin/src/org/tizen/rt/ide/util/SDKUtil.java

index 9cf2ddf..0652bad 100644 (file)
@@ -4,7 +4,8 @@
  * Copyright (c) 2000 - 2017 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Contact:
- * Daeryong Park <bdragon.park@samsung.com>
+ * gyoengmin.ju <gyoengmin.ju@samsung.com>
+ * HyeongSeok Heo <harry.heo@samsung.com>
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  */
 package org.tizen.rt.ide.util;
 
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
 import java.io.UnsupportedEncodingException;
 import java.net.URLDecoder;
+import java.util.Properties;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class SDKUtil {
-
     private static final Logger logger = LoggerFactory.getLogger(SDKUtil.class);
 
+    private final static String SDK_INFO_FILE = "sdk.info";
+    private final static String SDK_INFO_FILE_KEY_INSTALLED_PATH = "TIZEN_SDK_INSTALLED_PATH";
+    private final static String SDK_INFO_FILE_KEY_DATA_PATH = "TIZEN_SDK_DATA_PATH";
+
+    private static String getSdkHomeFromeInfoFile(File sdkInfoFile) {
+        logger.info("Loading from {}...", sdkInfoFile.getAbsolutePath());
+
+        Properties prop = new Properties() {
+            private static final long serialVersionUID = 8616159524414427526L;
+
+            @Override
+            public synchronized void load(Reader reader) throws IOException {
+                BufferedReader in = new BufferedReader(reader);
+                ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+                String nextLine = null;
+                while ((nextLine = in.readLine()) != null) {
+                    // to escape D:\tizen-sdk to D:\\tizen-sdk
+                    out.write(nextLine.replace("\\", "\\\\").getBytes());
+                    out.write("\n".getBytes());
+                }
+
+                InputStream is = new ByteArrayInputStream(out.toByteArray());
+                super.load(new InputStreamReader(is));
+            }
+        };
+
+        try {
+            InputStreamReader reader = new InputStreamReader(new FileInputStream(sdkInfoFile), "UTF-8");
+            prop.load(reader);
+        } catch (IOException e) {
+            logger.error(e.getMessage());
+            return null;
+        }
+
+        String sdkInstallPath = prop.getProperty(SDK_INFO_FILE_KEY_INSTALLED_PATH);
+
+        return sdkInstallPath;
+    }
+
+    private static File lookupSdkInfoFile(File originFile) {
+        if (originFile.isFile()) {
+            originFile = originFile.getParentFile();
+        }
+
+        File sdkInfoFile = null;
+        while (originFile != null) {
+            sdkInfoFile = new File(originFile, SDK_INFO_FILE);
+            if (sdkInfoFile.exists()) {
+                break;
+            }
+            originFile = originFile.getParentFile();
+        }
+
+        return sdkInfoFile;
+    }
+
     public static String getSdkPath() {
         String sdkHome = System.getenv("TIZEN_HOME");
         if (sdkHome == null) {
@@ -42,8 +108,14 @@ public class SDKUtil {
                 Class<?> platform = Class.forName("org.eclipse.core.runtime.Platform");
                 if (platform != null) {
                     sdkHome = org.eclipse.core.runtime.Platform.getInstallLocation().getURL().getPath();
-                    sdkHome = URLDecoder.decode(sdkHome, "UTF-8");
-                    sdkHome = new File(sdkHome).getParent();
+                } else {
+                    sdkHome = SDKUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();
+                }
+
+                sdkHome = URLDecoder.decode(sdkHome, "UTF-8");
+                File sdkInfoFile = lookupSdkInfoFile(new File(sdkHome));
+                if (sdkInfoFile != null) {
+                    sdkHome = getSdkHomeFromeInfoFile(sdkInfoFile);
                 }
             } catch (UnsupportedEncodingException e) {
                 logger.error(e.getMessage(), e);