VMProperty: clean-up unnecessary logics
authorSeokYeon Hwang <syeon.hwang@samsung.com>
Mon, 1 Feb 2016 08:25:54 +0000 (17:25 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Tue, 2 Feb 2016 04:26:49 +0000 (13:26 +0900)
Change-Id: I98f0005914c597f4a939e74ddd7e128c8ab4073a
Signed-off-by: SeokYeon Hwang <syeon.hwang@samsung.com>
13 files changed:
src/org/tizen/emulator/manager/platform/Profile.java
src/org/tizen/emulator/manager/platform/ProfileList.java
src/org/tizen/emulator/manager/template/TemplateLoader.java
src/org/tizen/emulator/manager/template/renewal/TemplateLoader.java
src/org/tizen/emulator/manager/ui/VMsMainView.java
src/org/tizen/emulator/manager/vms/Creator.java
src/org/tizen/emulator/manager/vms/DisposableVMKeeper.java
src/org/tizen/emulator/manager/vms/IVMWorker.java
src/org/tizen/emulator/manager/vms/ManagedVMKeeper.java
src/org/tizen/emulator/manager/vms/Modifier.java
src/org/tizen/emulator/manager/vms/VMKeeper.java
src/org/tizen/emulator/manager/vms/VMProperty.java
src/org/tizen/emulator/manager/vms/VMWorkerCommon.java

index 4775c7f..3e2de81 100644 (file)
@@ -131,7 +131,7 @@ public class Profile {
                Path lastCreated = Paths.get(lastCreatedPropertyPath);
                // Use last-created_xxx.xml
                if (Files.exists(lastCreated)) {
-                       return EmulatorManager.getVMKeeper().newProperty(lastCreated);
+                       return EmulatorManager.getVMKeeper().loadProperty(lastCreated);
                }
 
                return null;
@@ -231,6 +231,9 @@ public class Profile {
                                image.addEmulator(vm);
                        }
                }
+
+               // FIXME
+               vm.initVMWorker();
        }
 
        public void removeEmulator(VMProperty vm) {
index 10f5df8..dfddccf 100644 (file)
@@ -283,8 +283,6 @@ public class ProfileList {
                                        continue;
                                }
 
-                               prop.initVMWorker();
-
                                binaryVersion = prop.getBaseImageBinaryVersion();
                                path = prop.getBaseImagePath();
                                base = prop.getPropertyValue().baseImage;
index eece2d1..af8fff5 100644 (file)
@@ -178,7 +178,7 @@ public class TemplateLoader {
 
 
                        } else { // property file
-                               VMProperty defaultProperty = EmulatorManager.getVMKeeper().newProperty(f.toPath());
+                               VMProperty defaultProperty = EmulatorManager.getVMKeeper().loadProperty(f.toPath());
                                if (defaultProperty != null) {
                                        platform.getPropertyList().add(defaultProperty);
 
index c2bd2ce..46e6312 100644 (file)
@@ -87,7 +87,7 @@ public class TemplateLoader {
                                }
 
                        } else { // property file
-                               VMProperty defaultProperty = EmulatorManager.getVMKeeper().newProperty(f.toPath());
+                               VMProperty defaultProperty = EmulatorManager.getVMKeeper().loadProperty(f.toPath());
                                if (defaultProperty != null) {
                                        platform.getPropertyList().add(defaultProperty);
 
index 3702f16..231cbed 100644 (file)
@@ -525,22 +525,6 @@ public class VMsMainView {
        }
 
        public void reloadProperty() {
-               if (getCurrentProperty().getWorker().isModified()) {
-                       MainDialog.refreshVMProperty(currentProperty);
-                       String msg = Messages.getString("VMsMainView.ReloadVMProperty.0"); //$NON-NLS-1$
-                       EMLogger.getLogger().info(msg);
-                       MainDialog.getStatusBar().info(msg);
-                       Display.getCurrent().asyncExec(new Runnable() {
-                               @Override
-                               public void run() {
-                                       try {
-                                               Thread.sleep(2000);
-                                               MainDialog.getStatusBar().reset();
-                                       } catch (InterruptedException e) {
-                                               EMLogger.getLogger().warning(e.getMessage());
-                                       }
-                               }
-                       });
-               }
+               // no nothing
        }
 }
index 09fe5bb..4313f5a 100644 (file)
@@ -108,7 +108,7 @@ public class Creator {
                                throw e;
                        }
 
-                       property = EmulatorManager.getVMKeeper().newProperty(newVM);
+                       property = EmulatorManager.getVMKeeper().createProperty(newVM);
 
                        // Save last created property file.
                        Path lastCreatedpropertyFile = null;
index 8b92ae1..6ad63fc 100644 (file)
@@ -74,7 +74,7 @@ public class DisposableVMKeeper  extends VMKeeper{
                        for (Path path : stream) {
                                if (Files.isRegularFile(path)) {
                                        VMProperty property
-                                                       = newProperty(path);
+                                                       = loadProperty(path);
                                        if (property != null) {
                                                listChanged(property, ChangeType.ADDED);
                                        }
@@ -109,7 +109,7 @@ public class DisposableVMKeeper  extends VMKeeper{
 
                                                                for (Path path : stream) {
                                                                        if (Files.isRegularFile(path)) {
-                                                                               VMProperty property = newProperty(path);
+                                                                               VMProperty property = loadProperty(path);
                                                                                if (property != null) {
                                                                                        listChanged(property, ChangeType.ADDED);
                                                                                        properties.add(property);
index 4062438..8962e21 100644 (file)
@@ -34,7 +34,6 @@ import org.tizen.emulator.manager.vms.exception.VMLauncherException;
 import org.tizen.emulator.manager.vms.exception.VMWorkerException;
 
 public interface IVMWorker {
-       public void sendRemoteLog(String msg);
        public void initLauncher();
 
        public void launchVM() throws VMLauncherException, VMWorkerException;
index a091b57..42c655f 100644 (file)
@@ -57,7 +57,7 @@ public class ManagedVMKeeper extends VMKeeper {
        public synchronized void addVM(Path propertyFilePath) {
                assert Files.isRegularFile(propertyFilePath);
 
-               VMProperty property = newProperty(propertyFilePath);
+               VMProperty property = loadProperty(propertyFilePath);
 
                assert property.getHealth() != Health.CORRUPTED && !properties.contains(property);
 
index 2eea358..ddf13cf 100644 (file)
@@ -30,6 +30,7 @@
 
 package org.tizen.emulator.manager.vms;
 
+import org.tizen.emulator.manager.EmulatorManager;
 import org.tizen.emulator.manager.Messages;
 import org.tizen.emulator.manager.vms.exception.VMWorkerException;
 
@@ -43,7 +44,7 @@ public class Modifier {
                }
                modifyFileShare(newVM);
 
-               property.setPropertyValue(newVM);
+               EmulatorManager.getVMKeeper().modifyProperty(property, newVM);
        }
 
        private static void modifyFileShare(VMPropertyValue newVM) throws VMWorkerException {
index 81896c1..8fb6402 100644 (file)
@@ -131,11 +131,15 @@ public abstract class VMKeeper {
         */
        public abstract Queue<VMProperty> getProperties();
 
-       public VMProperty newProperty(VMPropertyValue value) {
+       public VMProperty createProperty(VMPropertyValue value) {
                return VMProperty.createNew(value);
        }
 
-       public VMProperty newProperty(Path propertyFile) {
+       public VMProperty modifyProperty(VMProperty property, VMPropertyValue value) {
+               return property.modify(value);
+       }
+
+       public VMProperty loadProperty(Path propertyFile) {
                return VMProperty.loadFrom(propertyFile);
        }
 
index 401a14b..3096854 100644 (file)
@@ -86,6 +86,7 @@ public final class VMProperty {
        private VMPropertyValue value = null;
 
        private Health health = Health.NOT_CHECKED;
+       private State preState = State.NOT_SET;
        private State state = State.NOT_SET;
 
        private long lastLaunched;
@@ -108,10 +109,7 @@ public final class VMProperty {
        }
 
        static VMProperty loadFrom(Path propertyFile) {
-               VMProperty property = new VMProperty(propertyFile);
-               property.refresh();
-
-               return property;
+               return new VMProperty(propertyFile);
        }
 
        static VMProperty createNew(VMPropertyValue value) {
@@ -123,14 +121,24 @@ public final class VMProperty {
                Path propertyFile = Paths.get(propertyFilename);
                assert Files.notExists(propertyFile);
 
-               VMProperty property = new VMProperty(propertyFile);
-               property.setPropertyValue(value);
+               return new VMProperty(propertyFile, value);
 
-               return property;
        }
 
        private VMProperty(Path propertyFile) {
                this.propertyFile = propertyFile;
+               refresh();
+       }
+
+       private VMProperty(Path propertyFile, VMPropertyValue value) {
+               this.propertyFile = propertyFile;
+               setPropertyValue(value);
+       }
+
+       public VMProperty modify(VMPropertyValue value) {
+               setPropertyValue(value);
+
+               return this;
        }
 
        private void stateChanged() {
@@ -181,6 +189,7 @@ public final class VMProperty {
                }
        };
 
+       // FIXME: called from Profile class
        public void initVMWorker() {
                ExtensionItem item = null;
                if (getPropertyValue().baseImage == null) {
@@ -211,7 +220,7 @@ public final class VMProperty {
                return value;
        }
 
-       public void setPropertyValue(VMPropertyValue value) {
+       private void setPropertyValue(VMPropertyValue value) {
                this.value = value;
 
                configuration = new VMPropertyConfiguration(value);
@@ -231,9 +240,6 @@ public final class VMProperty {
        }
 
        public VMWorkerCommon getWorker() {
-               if (worker == null) {
-                       initVMWorker();
-               }
                return worker;
        }
 
@@ -251,6 +257,10 @@ public final class VMProperty {
                return true;
        }
 
+       public State getPreState() {
+               return preState;
+       }
+
        public State getState() {
                return state;
        }
@@ -272,11 +282,6 @@ public final class VMProperty {
                        assert this.state != State.RUNNING;
                        break;
                case READY:
-                       if (this.state == State.RUNNING) {
-                               if (worker != null) {
-                                       worker.sendRemoteLog("stop"); //$NON-NLS-1$
-                               }
-                       }
                        break;
                case LAUNCHING:
                        assert this.state == State.READY;
@@ -284,14 +289,12 @@ public final class VMProperty {
                        break;
                case RUNNING:
                        assert this.state == State.LAUNCHING || this.state == State.READY;
-                       if (worker != null) {
-                               worker.sendRemoteLog("start"); //$NON-NLS-1$
-                       }
                        break;
                default:
                        break;
                }
 
+               this.preState = this.state;
                this.state = state;
 
                stateChanged();
index d8097ab..609fd8d 100644 (file)
@@ -48,6 +48,7 @@ import org.tizen.emulator.manager.Messages;
 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.VMProperty.State;
 import org.tizen.emulator.manager.vms.exception.VMLauncherException;
 import org.tizen.emulator.manager.vms.exception.VMWorkerException;
 import org.tizen.emulator.manager.vms.helper.QemuImgProc;
@@ -57,10 +58,25 @@ import org.tizen.emulator.manager.vms.option.IOption;
 public class VMWorkerCommon implements IVMWorker {
        private VMProperty property;
        private ILauncher launcher;
-       private boolean isModified;
 
-       @Override
-       public void sendRemoteLog(String msg) {
+       protected VMWorkerCommon() {
+               // TODO: this logic should move to plugins
+               VMProperty.addStateChangeListener(new StateChangeListener<VMProperty>() {
+                       @Override
+                       public void stateChanged(VMProperty property) {
+                               if (property.getState() == State.READY && property.getPreState() == State.RUNNING) {
+                                       sendRemoteLog("stop"); //$NON-NLS-1$
+                               }
+
+                               if (property.getState() == State.RUNNING) {
+                                       sendRemoteLog("start"); //$NON-NLS-1$
+                               }
+                       }
+               });
+       }
+
+       @Deprecated // All state changed event should be using StateChangeListener
+       protected void sendRemoteLog(String msg) {
        }
 
        @Override
@@ -75,14 +91,10 @@ public class VMWorkerCommon implements IVMWorker {
                return launcher;
        }
 
-       public void setVMProperty(VMProperty property) {
+       void setVMProperty(VMProperty property) {
                this.property = property;
        }
 
-       public VMProperty getProperty() {
-               return property;
-       }
-
        @Override
        public void launchVM() throws VMLauncherException, VMWorkerException {
                launchVM(false, null);
@@ -102,15 +114,6 @@ public class VMWorkerCommon implements IVMWorker {
                                                true);
                        }
 
-                       if (isModified) {
-                               property = EmulatorManager.getVMKeeper().newProperty(
-                                               property.getPropertyFile());
-                               if (property == null) {
-                                       throw new VMWorkerException(
-                                                       Messages.getString("VMWorkerCommon.LaunchError.0"), true); //$NON-NLS-1$
-                               }
-                       }
-
                        // Do argument validation.
                        List<IOption> optList = property.getPropertyValue().baseImage.getOptionList();
                        if (optList == null) {
@@ -150,12 +153,6 @@ public class VMWorkerCommon implements IVMWorker {
                                                true);
                        }
 
-                       if (isModified) {
-                               throw new VMWorkerException(
-                                               Messages.getString("VMWorkerCommon.ModifyError.0"), //$NON-NLS-1$
-                                               true);
-                       }
-
                        isRunningNow();
 
                        Modifier.modify(property, newVM);
@@ -591,15 +588,6 @@ public class VMWorkerCommon implements IVMWorker {
 
                return result;
        }
-
-       /**
-        * If property had been modified by other emulator manager(cli),
-        * this method will be return true.
-        * @return return true if property had been modified
-        */
-       public boolean isModified() {
-               return isModified;
-       }
 }
 
 /*