upload tizen2.0 alpha installmanager source
[sdk/installer/install-manager.git] / InstallManager_java / src / org / tizen / installmanager / lib / Registry.java
index 90d4df9..c9f1902 100644 (file)
@@ -1,30 +1,30 @@
 /*
-*  InstallManager
-*
-* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
-*
-* Contact: 
-* Wooyoung Cho <wooyoung1.cho@samsung.com>
-* Shihyun Kim <shihyun.kim@samsung.com>
-* Taeyoung Son <taeyoung2.son@samsung.com>
-* Yongsung kim <yongsung1.kim@samsung.com>
-* 
+ *  InstallManager
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * Wooyoung Cho <wooyoung1.cho@samsung.com>
+ * Shihyun Kim <shihyun.kim@samsung.com>
+ * Taeyoung Son <taeyoung2.son@samsung.com>
+ * Yongsung kim <yongsung1.kim@samsung.com>
+ 
  * Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-* Contributors:
-* - S-Core Co., Ltd
-*
-*/ 
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
 
 package org.tizen.installmanager.lib;
 
@@ -41,132 +41,228 @@ import org.tizen.installmanager.core.IMFatalException;
 import org.tizen.installmanager.lib.ErrorController.ErrorCode;
 import org.tizen.installmanager.util.PathUtil;
 
-
 /**
  * Manages target path to the registry file.
+ * 
  * @author Shihyun Kim <shihyun.kim@samsung.com>
- *
+ * 
  */
 public class Registry {
-       public static final String REGISTRY_FILE_NAME = "tizensdkpath";
-       public static final String REGISTRY_FILE_PATH = PathUtil.get(Config.CONFIG_HOME, REGISTRY_FILE_NAME);
-       public static final String INSTALLED_PATH_KEY = "TIZEN_SDK_INSTALLED_PATH";
-           
-       private static final OldRegistry oldRegistry = new OldRegistry();
-           /**
-            * Exports target path to the registry file.
-            * @param targetPath
-            */
-    public static void exportInstallPath(String targetPath) {
-       if (targetPath == null || targetPath.isEmpty()) {
-               Log.err("Install path is invalid: "+targetPath+". This might be a bug of IM");
-               throw new IMFatalException(ErrorCode.INVALID_INSTALL_PATH);
-       }
-       
-       String parameter = INSTALLED_PATH_KEY + "=" + targetPath;
-        
-        File file = new File(REGISTRY_FILE_PATH);
-        
-        if (file.exists()) {
-               file.delete();
-        }
-        file.getParentFile().mkdirs();
-        
-       try {
-                       file.createNewFile();                   
+       //installed path.
+       private static final String REGISTRY_FILE_NAME = "tizensdkpath";
+       private static final String REGISTRY_FILE_PATH = PathUtil.get(
+                       Config.CONFIG_HOME, REGISTRY_FILE_NAME);
+       private static final String INSTALLED_PATH_KEY = "TIZEN_SDK_INSTALLED_PATH";
+
+       //installed version
+       private static final String INSTALLED_VERSION_FILE_NAME = "version";
+       private static final String INSTALLED_VERSION_FILE_PATH = PathUtil.get(
+                       Config.CONFIG_HOME, INSTALLED_VERSION_FILE_NAME);
+       private static final String INSTALLED_VERSION_KEY = "INSTALLED_VERSION";
+       
+       //registry information seperator
+       private static final String REGISTRY_SEPERATOR = "=";
+
+       /**
+        * Exports target path to the registry file.
+        * 
+        * @param targetPath
+        */
+       public static void exportInstallPath(String targetPath) {
+               if (targetPath == null || targetPath.isEmpty()) {
+                       Log.err("Install path is invalid: " + targetPath
+                                       + ". This might be a bug of IM");
+                       throw new IMFatalException(ErrorCode.INVALID_INSTALL_PATH);
+               }
+
+               String parameter = INSTALLED_PATH_KEY + REGISTRY_SEPERATOR + targetPath;
+
+               File installedPathFile = null;
+               try {
+                       installedPathFile = PathUtil.makeNewFile(REGISTRY_FILE_PATH);
                } catch (IOException e) {
-                       Log.err("Cannot create file. "+file.getAbsolutePath());
-                       throw new IMFatalException(ErrorCode.CANNOT_REGISTER_TARGET_DIR);
+                       Log.ExceptionLog(e);
+                       return;
                }
-        
-       BufferedWriter bw = null;
-        try {
-            FileWriter fw = new FileWriter(file);
-            bw = new BufferedWriter(fw);
-            bw.write(parameter);
-            bw.flush();
-            
-        } catch (Exception e) {
-               Log.err("Cannot register to file. "+file.getAbsolutePath());
+               
+               if (installedPathFile == null) {
+                       return;
+               }
+
+               BufferedWriter bw = null;
+               try {
+                       FileWriter fw = new FileWriter(installedPathFile);
+                       bw = new BufferedWriter(fw);
+                       bw.write(parameter);
+                       bw.flush();
+
+               } catch (Exception e) {
+                       Log.err("Cannot register to file. " + installedPathFile.getAbsolutePath());
                        throw new IMFatalException(ErrorCode.CANNOT_REGISTER_TARGET_DIR);
-                       
-        } finally {
-               if (bw != null) {
-               try {
+
+               } finally {
+                       if (bw != null) {
+                               try {
                                        bw.close();
-                                       
+
                                } catch (IOException e) {
-                                       // TODO Auto-generated catch block
                                        Log.ExceptionLog(e);
                                }
+                       }
+               }
+       }
+
+       /**
+        * @return path in which SDK is installed. empty string if not found.
+        */
+       public static String getInstalledPath() {
+               return getPathFromRegistryKey(REGISTRY_FILE_PATH, INSTALLED_PATH_KEY);
+       }
+
+       private static String getPathFromRegistryKey(String path, String registryKey) {
+               File file = new File(path);
+
+               if (!file.exists()) {
+                       return "";
+               }
+
+               FileInputStream fis = null;
+               InputStreamReader isr = null;
+               BufferedReader br = null;
+
+               String readLine;
+               String result = "";
+               try {
+                       fis = new FileInputStream(file);
+                       isr = new InputStreamReader(fis);
+                       br = new BufferedReader(isr);
+
+                       while ((readLine = br.readLine()) != null) {
+                               if (readLine.startsWith(registryKey)) {
+                                       int startChar = readLine.indexOf("=");
+                                       if (startChar == -1) {
+                                               Log.err("OldRegistry.getPathFromRegistryKey() Invalid registry file => "
+                                                               + path + ", " + registryKey);
+                                               throw new IMFatalException("Invalid registry file");
+                                       }
+                                       result = readLine.substring(startChar + 1);
+                               }
+                       }
+
+                       return result;
+               } catch (IOException ioe) {
+                       Log.err("OldRegistry.getPathFromRegistryKey() Cannot read install path from .tizensdk file => "
+                                       + path + ", " + registryKey);
+                       throw new IMFatalException(
+                                       "Cannot read install path from .tizensdk file");
+               } finally {
+            if (br != null) {
+                try {
+                       br.close();
+                               } catch (IOException e) {
+                                       Log.ExceptionLog(e);
+                               }               
+            }
+            
+            if (isr != null) {
+                try {
+                                       isr.close();
+                               } catch (IOException e) {
+                                       Log.ExceptionLog(e);
+                               }               
             }
-        }
-    }
-    /**
-     * @return path in which SDK is installed. empty string if not found. 
-     */
-    public static String getInstalledPath() {  
-       return getPathFromRegistryKey(REGISTRY_FILE_PATH, INSTALLED_PATH_KEY);
-    }
-    
-    private static String getPathFromRegistryKey(String path, String registryKey) {
-       File file = new File(path);
-        
-        if (!file.exists()) {
-               return "";
-        }
 
-        FileInputStream fis = null;
-        InputStreamReader isr = null;
-        BufferedReader br = null;
-
-        String readLine;
-        String result = "";
-        try {
-            fis = new FileInputStream(file);
-            isr = new InputStreamReader(fis);
-            br = new BufferedReader(isr);
-
-            while ((readLine = br.readLine()) != null) {
-                if (readLine.startsWith(registryKey)) {
-                    int startChar = readLine.indexOf("=");
-                    if(startChar == -1) {
-                       Log.err("OldRegistry.getPathFromRegistryKey() Invalid registry file => " + path + ", " + registryKey);
-                       throw new IMFatalException("Invalid registry file");
-                    }
-                    result = readLine.substring(startChar+1);
-                }
+            if (fis != null) {
+                try {
+                       fis.close();
+                               } catch (IOException e) {
+                                       Log.ExceptionLog(e);
+                               }               
             }
-            fis.close();
-            isr.close();
-            br.close();
-            return result;
-        } catch (IOException ioe) {
-               Log.err("OldRegistry.getPathFromRegistryKey() Cannot read install path from .tizensdk file => " + path + ", " + registryKey);
-            throw new IMFatalException("Cannot read install path from .tizensdk file");
         }
-    }
-
-    /**
-     * remove target path in the registry file.
-     */
-    public static void removeRegistry() {
-       File file = new File(REGISTRY_FILE_PATH);
-
-       if (file.exists()) {
-               file.delete();
-       }
-    }
-    
-    /**
-     * @return <code>true</code> if old sdk(Tizen SDK) is installed.
-     */
-    public static boolean isOldSDKInstalled() {
-       if (oldRegistry.exists()) {
-               return true;
-       } else {
-               return false;
-       }
-    }
+       }
+
+       /**
+        * remove target path in the registry file.
+        */
+       public static void removeRegistry() {
+               File file = new File(REGISTRY_FILE_PATH);
+
+               if (file.exists()) {
+                       file.delete();
+               }
+               
+               removeOldFiles();
+       }
+       
+       /**
+        * Save installed version.
+        */
+       public static void saveInstallVersion(String version) {
+               File versionFile = null;
+               try {
+                       versionFile = PathUtil.makeNewFile(INSTALLED_VERSION_FILE_PATH);
+               } catch (IOException e) {
+                       Log.ExceptionLog(e);
+                       return;
+               }
+               
+               if (versionFile == null) {
+                       return;
+               }
+               
+               
+               BufferedWriter bw = null;
+               try {
+                       FileWriter fw = new FileWriter(versionFile);
+                       bw = new BufferedWriter(fw);
+                       
+                       String versionInformation = INSTALLED_VERSION_KEY + REGISTRY_SEPERATOR + version;
+                       bw.write(versionInformation);
+                       bw.flush();
+
+               } catch (Exception e) {
+                       Log.ExceptionLog(e);
+                       throw new IMFatalException(ErrorCode.CANNOT_REGISTER_TARGET_DIR);
+
+               } finally {
+                       if (bw != null) {
+                               try {
+                                       bw.close();
+
+                               } catch (IOException e) {
+                                       Log.ExceptionLog(e);
+                               }
+                       }
+               }
+       }
+       
+       /**
+        * Get installed version.
+        * @return
+        */
+       public static String getInstalledVersion() {
+               return getPathFromRegistryKey(INSTALLED_VERSION_FILE_PATH, INSTALLED_VERSION_KEY);
+       }
+       
+       public static boolean isInstallManagerInstalled() {
+               String installedPath = getInstalledPath();
+               
+               if (installedPath == null || installedPath.isEmpty()) {
+                       return false;
+               } else {
+                       return true;
+               }
+       }
+       
+       /**
+        * Temporary code to update.
+        */
+       private static void removeOldFiles() {
+               File file = new File(Config.OLD_CONFIG_HOME);
+               
+               if (file.exists()) {
+                       PathUtil.remove(file);
+               }
+       }
 }