Profile: add profile class
authorjihye424.kim <jihye424.kim@samsung.com>
Fri, 8 May 2015 09:25:10 +0000 (18:25 +0900)
committerjihye424.kim <jihye424.kim@samsung.com>
Fri, 8 May 2015 10:51:28 +0000 (19:51 +0900)
- profile: manage emulator(vm), base image

Change-Id: Id19574815cf79f8de3ac769d5a8bb7b613c73b92
Signed-off-by: jihye424.kim <jihye424.kim@samsung.com>
common-project/src/org/tizen/emulator/manager/platform/Platform.java
common-project/src/org/tizen/emulator/manager/platform/Profile.java [new file with mode: 0644]
common-project/src/org/tizen/emulator/manager/platform/ProfileList.java [new file with mode: 0644]

index 46fa3c5..6141129 100644 (file)
@@ -52,6 +52,7 @@ public class Platform {
        protected String platformName = null;
        protected String platformPath = null;
        protected String profile = null;
+       protected Profile profileClass = null;
        protected String version = null;
        protected static int id = 1;
        protected boolean isOldVersion = false;
@@ -83,6 +84,17 @@ public class Platform {
                }
        }
 
+       public Platform(Profile profile, String version, String path) {
+               this.profileClass = profile;
+               this.profile = profile.getName();
+               this.version = version;
+               this.platformName = profile + "-" + version;
+               this.platformPath = path;
+
+               loadSkins();
+               loadPlugin();
+       }
+
        public static String getPlatformPath(String platformName) {
                String split[] = platformName.split("-");
                if (split != null && split.length == 2) {
diff --git a/common-project/src/org/tizen/emulator/manager/platform/Profile.java b/common-project/src/org/tizen/emulator/manager/platform/Profile.java
new file mode 100644 (file)
index 0000000..a32e7d9
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Emulator Manager
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * JiHye Kim <jihye424.kim@samsung.com>
+ * Minkee Lee <minkee.lee@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park <sangho1206.park@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.emulator.manager.platform;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.tizen.emulator.manager.vms.VMProperty;
+import org.tizen.emulator.manager.vms.VMProperty.FSImageType;
+
+public class Profile {
+       private String name = null;
+       private List<Platform> platformList = new ArrayList<Platform>();
+       private List<BaseImage> imageList = new ArrayList<BaseImage>();
+       private List<VMProperty> vmList = new ArrayList<VMProperty>();
+
+       // for sort VM list
+       private SortBy sortType = SortBy.DateDes;
+       private boolean isSort = false;
+
+       public Profile(String name) {
+               this.name = name;
+       }
+
+       public String getName() {
+               return name;
+       }
+
+       public void addPlatform(Platform p) {
+               platformList.add(p);
+       }
+
+       public List<Platform> getPlatformList() {
+               return platformList;
+       }
+
+       /**
+        *
+        * @param name profile + "-" + version
+        * @return
+        */
+       public Platform getPlatformByName(String name) {
+               for (Platform p : platformList) {
+                       if (p.getName().equals(name)) {
+                               return p;
+                       }
+               }
+               return null;
+       }
+
+       public Platform getPlatformByVersion(String version) {
+               return getPlatformByName(name + "-" + version);
+       }
+
+       public void addEmulator(VMProperty vm) {
+               vmList.add(vm);
+               //setting BaseImage
+               if (vm.getPropertyValue().baseImage == null) {
+                       if (vm.getImageType().equals(FSImageType.standard)) {
+                               for (BaseImage b : imageList) {
+                                       if (vm.getBaseImageName().equals(b.getName())) {
+                                               vm.getPropertyValue().baseImage = b;
+                                               break;
+                                       }
+                               }
+                       } else {
+                               // TODO: create custom base image
+                               vm.getPropertyValue().baseImage = new BaseImage();
+                       }
+               }
+       }
+
+       public void removeEmulator(VMProperty vm) {
+               vmList.remove(vm);
+       }
+
+       public void clearVMsList() {
+               vmList.clear();
+               isSort = false;
+       }
+
+       public List<VMProperty> getEmulatorList() {
+               if (!isSort) {
+                       sortVMList(sortType);
+                       isSort = true;
+               }
+               return vmList;
+       }
+
+       public void sortVMList(SortBy type) {
+               sortType = type;
+               if (!vmList.isEmpty()) {
+                       vmList = sortType.sort((ArrayList<VMProperty>)vmList);
+               }
+       }
+
+       public SortBy getSortType() {
+               return sortType;
+       }
+
+       public void addBaseImage(BaseImage base) {
+               imageList.add(base);
+       }
+
+       public List<BaseImage> getImageList() {
+               return imageList;
+       }
+
+       public void setImageList(List<BaseImage> imageList) {
+               this.imageList = imageList;
+       }
+}
diff --git a/common-project/src/org/tizen/emulator/manager/platform/ProfileList.java b/common-project/src/org/tizen/emulator/manager/platform/ProfileList.java
new file mode 100644 (file)
index 0000000..12ffe78
--- /dev/null
@@ -0,0 +1,249 @@
+/*
+ * Emulator Manager
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * JiHye Kim <jihye424.kim@samsung.com>
+ * Minkee Lee <minkee.lee@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park <sangho1206.park@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+package org.tizen.emulator.manager.platform;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.tizen.emulator.manager.EmulatorManager;
+import org.tizen.emulator.manager.logging.EMLogger;
+import org.tizen.emulator.manager.resources.FilePathResources;
+import org.tizen.emulator.manager.resources.StringResources;
+import org.tizen.emulator.manager.vms.EmulatorVMList;
+import org.tizen.emulator.manager.vms.VMProperty;
+import org.tizen.emulator.manager.vms.VMProperty.FSImageType;
+import org.tizen.emulator.manager.vms.helper.VMWorkerException;
+
+public class ProfileList {
+       private static ArrayList<Profile> profileList = null;
+       // TODO
+       private static Profile totalProfile = new Profile("all");
+
+       public static ArrayList<Profile> getProfileList() {
+               if (profileList == null) {
+                       initProfileList();
+               }
+               return profileList;
+       }
+
+       public static Profile getTotalProfile() {
+               return totalProfile;
+       }
+
+       private static void initProfileList() {
+               profileList = new ArrayList<Profile>();
+
+               File platforms = new File(FilePathResources.getPlatformsPath());
+               if (!platforms.exists() || !platforms.isDirectory()) {
+                       // TODO: how to handling error
+                       return;
+               }
+
+               for (File platform : platforms.listFiles()) {
+                       if (!platform.isDirectory()) {
+                               continue;
+                       }
+
+                       for (File profile : platform.listFiles()) {
+                               if (profile.isDirectory() && isProfileDir(profile)) {
+                                       Profile p = ProfileList.makeProfile(profile.getName());
+                                       Platform pl = ProfileList.makePlatform (p, // profile
+                                                       platform.getName(), // version
+                                                       profile.getAbsolutePath()); // platform path
+                                       ProfileList.makeBaesImageList(p, pl, profile.getAbsolutePath());
+                               }
+                       }
+               }
+
+               // Check base image version()
+               checkingBaseImageBinaryVersion();
+               // Make emulator list
+               settingVMPropertyList();
+
+               // Remove invalid last-created property file.
+               checkLastCreatedPropertyFile();
+       }
+
+       private static boolean isProfileDir(File profile) {
+               for (File f : profile.listFiles()) {
+                       if (f.isDirectory() && f.getName().equalsIgnoreCase("emulator-images")) {
+                               // this is profile directory
+                               return true;
+                       }
+               }
+               return false;
+       }
+
+       private static Profile makeProfile(String profile) {
+               for (Profile p : profileList) {
+                       if (p.getName().equals(profile)) {
+                               return p;
+                       }
+               }
+
+               Profile p = new Profile(profile);
+               profileList.add(p);
+               return p;
+       }
+
+       private static Platform makePlatform(Profile profile, String version, String path) {
+               if (path == null) {
+                       return null;
+               }
+
+               Platform p = new Platform(profile, version, path);
+               if (p != null) {
+                       profile.addPlatform(p);
+               }
+               return p;
+       }
+
+       private static void makeBaesImageList(Profile profile, Platform platform, String platformPath) {
+               if (platform == null || platformPath == null) {
+                       return;
+               }
+
+               File images = new File(platformPath + FilePathResources.getDefaultImagePath());
+               BaseImage image = null;
+
+               if(images.exists() && images.isDirectory()) {
+                       for (File imageDir : images.listFiles()) {
+                               if (imageDir.isDirectory()) {
+                                       try {
+                                               image = new BaseImage(platform, imageDir);
+                                               //TODO: Default image is added at first index of image list
+                                               //TODO: need sort image
+                                               profile.addBaseImage(image);
+                                               totalProfile.addBaseImage(image);
+                                       } catch (IOException e) {
+                                               EMLogger.getLogger().warning("Failed adding base image.." + StringResources.NEW_LINE
+                                                               + "Base Disk Image Path: " + imageDir.getAbsolutePath() + StringResources.NEW_LINE
+                                                               + "Error: " + e.getMessage());
+                                       }
+                               } else {
+                                       EMLogger.getLogger().info("This is not base image directroy"
+                                                                       + StringResources.NEW_LINE + "Path: " + imageDir.getAbsolutePath());
+                               }
+                       }
+               }
+       }
+
+       public static void settingVMPropertyList() {
+               EmulatorVMList vms = EmulatorVMList.getInstance();
+               vms.refreshProperties();
+
+               for (Profile p : profileList) {
+                       p.clearVMsList();
+               }
+               totalProfile.clearVMsList();
+
+               for (VMProperty prop : (VMProperty[])vms.getProperties()) {
+                       for (Profile profile : profileList) {
+                               if (profile.getName().equals(prop.getImageProfile())) {
+                                       profile.addEmulator(prop);
+                               }
+                       }
+                       // Initialize emulator worker
+                       prop.initVMWorker();
+
+                       totalProfile.addEmulator(prop);
+               }
+       }
+
+       private static void checkingBaseImageBinaryVersion() {
+               // TODO
+               EmulatorVMList vms = EmulatorVMList.getInstance();
+               vms.refreshProperties();
+
+               totalProfile.clearVMsList();
+
+               String binaryVersion = "";
+               String path = "";
+               BaseImage base = null;
+               for (VMProperty prop : (VMProperty[])vms.getProperties()) {
+                       totalProfile.addEmulator(prop);
+                       prop.initVMWorker();
+
+                       if (prop.getImageType() == FSImageType.standard) {
+                               binaryVersion = prop.getConfiguration().getBaseInformation().getDiskImage().getBaseDiskImage().getVersion();
+                               path = prop.getConfiguration().getBaseInformation().getDiskImage().getBaseDiskImage().getValue();
+                               base = prop.getPropertyValue().baseImage;
+                               if (base.getPath().equals(path)
+                                               && (binaryVersion == null || !binaryVersion.equals(base.getBinaryVersion()))) {
+                                       try {
+                                               prop.getWorker().deleteVM();
+                                       } catch (VMWorkerException e) {
+                                               EMLogger.getLogger().warning(e.getMessage());
+                                       }
+                               }
+                       }
+               }
+       }
+
+       private static void checkLastCreatedPropertyFile() {
+               // Get current last-created property file path.
+               List<String> currentPathList = new ArrayList<String>();
+               for (Profile p : profileList) {
+                       for (BaseImage image : p.getImageList()) {
+                               currentPathList.add(image.getLastCreatedPropertyPath());
+                       }
+               }
+
+               // Remove old last-created property.
+               File vms = new File(FilePathResources.getTizenVmsPath());
+               File[] files = vms.listFiles();
+               if (files != null) {
+                       for (File f : files) {
+                               String path = f.getAbsolutePath();
+                               String[] arr = null;
+                               if (EmulatorManager.isWin()) {
+                                       arr = path.split("\\\\");
+                               } else {
+                                       arr =  path.split(File.separator);
+                               }
+                               String fileName = arr[arr.length-1];
+                               if (fileName.startsWith(StringResources.LAST_CREATED_XML_PREFIX)) {
+                                       boolean isOld = true;
+                                       for (String currentPath : currentPathList) {
+                                               if (path.equals(currentPath)) {
+                                                       isOld = false;
+                                               }
+                                       }
+                                       if (isOld) {
+                                               f.delete();
+                                       }
+                               }
+                       }
+               }
+       }
+}