@Test public void testRenameInstance() throws Exception { InstanceServiceImpl service = new InstanceServiceImpl(); service.setStorageLocation(tempFolder.newFolder("instances")); InstanceSettings settings = new InstanceSettings(8122, 1122, 44444, getName(), null, null, null); service.createInstance(getName(), settings, true); service.renameInstance(getName(), getName() + "b", true); assertNotNull(service.getInstance(getName() + "b")); }
public void renameInstance(final String oldName, final String newName, final boolean printOutput) throws Exception { execute(state -> { if (state.instances.get(newName) != null) { throw new IllegalArgumentException("Instance " + newName + " already exists"); } InstanceState instance = state.instances.get(oldName); if (instance == null) { throw new IllegalArgumentException("Instance " + oldName + " not found"); } if (instance.root) { throw new IllegalArgumentException("Root instance cannot be renamed"); } checkPid(instance); if (instance.pid != 0) { throw new IllegalStateException("Instance not stopped"); } println("Renaming instance " + SimpleAnsi.INTENSITY_BOLD + oldName + SimpleAnsi.INTENSITY_NORMAL + " to " + SimpleAnsi.INTENSITY_BOLD + newName + SimpleAnsi.INTENSITY_NORMAL); String oldLocationPath = instance.loc; File oldLocation = new File(oldLocationPath); String basedir = oldLocation.getParent(); File newLocation = new File(basedir, newName); oldLocation.renameTo(newLocation); HashMap<String, String> props = new HashMap<>(); props.put(oldName, newName); props.put(oldLocationPath, newLocation.getPath()); filterResource(newLocation, "etc/system.properties", props); filterResource(newLocation, "bin/karaf", props); filterResource(newLocation, "bin/start", props); filterResource(newLocation, "bin/stop", props); filterResource(newLocation, "bin/karaf.bat", props); filterResource(newLocation, "bin/start.bat", props); filterResource(newLocation, "bin/stop.bat", props); instance.name = newName; instance.loc = newLocation.getPath(); state.instances.put(newName, instance); state.instances.remove(oldName); InstanceImpl proxy = InstanceServiceImpl.this.proxies.remove(oldName); if (proxy == null) { proxy = new InstanceImpl(InstanceServiceImpl.this, newName); } else { proxy.doSetName(newName); } InstanceServiceImpl.this.proxies.put(newName, proxy); return null; }, true); }
InstanceServiceImpl implements InstanceService { public void renameInstance(final String oldName, final String newName, final boolean printOutput) throws Exception { execute(state -> { if (state.instances.get(newName) != null) { throw new IllegalArgumentException("Instance " + newName + " already exists"); } InstanceState instance = state.instances.get(oldName); if (instance == null) { throw new IllegalArgumentException("Instance " + oldName + " not found"); } if (instance.root) { throw new IllegalArgumentException("Root instance cannot be renamed"); } checkPid(instance); if (instance.pid != 0) { throw new IllegalStateException("Instance not stopped"); } println("Renaming instance " + SimpleAnsi.INTENSITY_BOLD + oldName + SimpleAnsi.INTENSITY_NORMAL + " to " + SimpleAnsi.INTENSITY_BOLD + newName + SimpleAnsi.INTENSITY_NORMAL); String oldLocationPath = instance.loc; File oldLocation = new File(oldLocationPath); String basedir = oldLocation.getParent(); File newLocation = new File(basedir, newName); oldLocation.renameTo(newLocation); HashMap<String, String> props = new HashMap<>(); props.put(oldName, newName); props.put(oldLocationPath, newLocation.getPath()); filterResource(newLocation, "etc/system.properties", props); filterResource(newLocation, "bin/karaf", props); filterResource(newLocation, "bin/start", props); filterResource(newLocation, "bin/stop", props); filterResource(newLocation, "bin/karaf.bat", props); filterResource(newLocation, "bin/start.bat", props); filterResource(newLocation, "bin/stop.bat", props); instance.name = newName; instance.loc = newLocation.getPath(); state.instances.put(newName, instance); state.instances.remove(oldName); InstanceImpl proxy = InstanceServiceImpl.this.proxies.remove(oldName); if (proxy == null) { proxy = new InstanceImpl(InstanceServiceImpl.this, newName); } else { proxy.doSetName(newName); } InstanceServiceImpl.this.proxies.put(newName, proxy); return null; }, true); } }
InstanceServiceImpl implements InstanceService { public void renameInstance(final String oldName, final String newName, final boolean printOutput) throws Exception { execute(state -> { if (state.instances.get(newName) != null) { throw new IllegalArgumentException("Instance " + newName + " already exists"); } InstanceState instance = state.instances.get(oldName); if (instance == null) { throw new IllegalArgumentException("Instance " + oldName + " not found"); } if (instance.root) { throw new IllegalArgumentException("Root instance cannot be renamed"); } checkPid(instance); if (instance.pid != 0) { throw new IllegalStateException("Instance not stopped"); } println("Renaming instance " + SimpleAnsi.INTENSITY_BOLD + oldName + SimpleAnsi.INTENSITY_NORMAL + " to " + SimpleAnsi.INTENSITY_BOLD + newName + SimpleAnsi.INTENSITY_NORMAL); String oldLocationPath = instance.loc; File oldLocation = new File(oldLocationPath); String basedir = oldLocation.getParent(); File newLocation = new File(basedir, newName); oldLocation.renameTo(newLocation); HashMap<String, String> props = new HashMap<>(); props.put(oldName, newName); props.put(oldLocationPath, newLocation.getPath()); filterResource(newLocation, "etc/system.properties", props); filterResource(newLocation, "bin/karaf", props); filterResource(newLocation, "bin/start", props); filterResource(newLocation, "bin/stop", props); filterResource(newLocation, "bin/karaf.bat", props); filterResource(newLocation, "bin/start.bat", props); filterResource(newLocation, "bin/stop.bat", props); instance.name = newName; instance.loc = newLocation.getPath(); state.instances.put(newName, instance); state.instances.remove(oldName); InstanceImpl proxy = InstanceServiceImpl.this.proxies.remove(oldName); if (proxy == null) { proxy = new InstanceImpl(InstanceServiceImpl.this, newName); } else { proxy.doSetName(newName); } InstanceServiceImpl.this.proxies.put(newName, proxy); return null; }, true); } InstanceServiceImpl(); }
InstanceServiceImpl implements InstanceService { public void renameInstance(final String oldName, final String newName, final boolean printOutput) throws Exception { execute(state -> { if (state.instances.get(newName) != null) { throw new IllegalArgumentException("Instance " + newName + " already exists"); } InstanceState instance = state.instances.get(oldName); if (instance == null) { throw new IllegalArgumentException("Instance " + oldName + " not found"); } if (instance.root) { throw new IllegalArgumentException("Root instance cannot be renamed"); } checkPid(instance); if (instance.pid != 0) { throw new IllegalStateException("Instance not stopped"); } println("Renaming instance " + SimpleAnsi.INTENSITY_BOLD + oldName + SimpleAnsi.INTENSITY_NORMAL + " to " + SimpleAnsi.INTENSITY_BOLD + newName + SimpleAnsi.INTENSITY_NORMAL); String oldLocationPath = instance.loc; File oldLocation = new File(oldLocationPath); String basedir = oldLocation.getParent(); File newLocation = new File(basedir, newName); oldLocation.renameTo(newLocation); HashMap<String, String> props = new HashMap<>(); props.put(oldName, newName); props.put(oldLocationPath, newLocation.getPath()); filterResource(newLocation, "etc/system.properties", props); filterResource(newLocation, "bin/karaf", props); filterResource(newLocation, "bin/start", props); filterResource(newLocation, "bin/stop", props); filterResource(newLocation, "bin/karaf.bat", props); filterResource(newLocation, "bin/start.bat", props); filterResource(newLocation, "bin/stop.bat", props); instance.name = newName; instance.loc = newLocation.getPath(); state.instances.put(newName, instance); state.instances.remove(oldName); InstanceImpl proxy = InstanceServiceImpl.this.proxies.remove(oldName); if (proxy == null) { proxy = new InstanceImpl(InstanceServiceImpl.this, newName); } else { proxy.doSetName(newName); } InstanceServiceImpl.this.proxies.put(newName, proxy); return null; }, true); } InstanceServiceImpl(); File getStorageLocation(); void setStorageLocation(File storage); long getStopTimeout(); void setStopTimeout(long stopTimeout); synchronized Instance createInstance(final String name, final InstanceSettings settings, final boolean printOutput); Instance[] getInstances(); Instance getInstance(final String name); void startInstance(final String name, final String javaOpts); void restartInstance(final String name, final String javaOpts); void stopInstance(final String name); void destroyInstance(final String name); void renameInstance(final String oldName, final String newName, final boolean printOutput); synchronized Instance cloneInstance(final String name, final String cloneName, final InstanceSettings settings, final boolean printOutput); void changeInstanceSshHost(String name, String host); }
InstanceServiceImpl implements InstanceService { public void renameInstance(final String oldName, final String newName, final boolean printOutput) throws Exception { execute(state -> { if (state.instances.get(newName) != null) { throw new IllegalArgumentException("Instance " + newName + " already exists"); } InstanceState instance = state.instances.get(oldName); if (instance == null) { throw new IllegalArgumentException("Instance " + oldName + " not found"); } if (instance.root) { throw new IllegalArgumentException("Root instance cannot be renamed"); } checkPid(instance); if (instance.pid != 0) { throw new IllegalStateException("Instance not stopped"); } println("Renaming instance " + SimpleAnsi.INTENSITY_BOLD + oldName + SimpleAnsi.INTENSITY_NORMAL + " to " + SimpleAnsi.INTENSITY_BOLD + newName + SimpleAnsi.INTENSITY_NORMAL); String oldLocationPath = instance.loc; File oldLocation = new File(oldLocationPath); String basedir = oldLocation.getParent(); File newLocation = new File(basedir, newName); oldLocation.renameTo(newLocation); HashMap<String, String> props = new HashMap<>(); props.put(oldName, newName); props.put(oldLocationPath, newLocation.getPath()); filterResource(newLocation, "etc/system.properties", props); filterResource(newLocation, "bin/karaf", props); filterResource(newLocation, "bin/start", props); filterResource(newLocation, "bin/stop", props); filterResource(newLocation, "bin/karaf.bat", props); filterResource(newLocation, "bin/start.bat", props); filterResource(newLocation, "bin/stop.bat", props); instance.name = newName; instance.loc = newLocation.getPath(); state.instances.put(newName, instance); state.instances.remove(oldName); InstanceImpl proxy = InstanceServiceImpl.this.proxies.remove(oldName); if (proxy == null) { proxy = new InstanceImpl(InstanceServiceImpl.this, newName); } else { proxy.doSetName(newName); } InstanceServiceImpl.this.proxies.put(newName, proxy); return null; }, true); } InstanceServiceImpl(); File getStorageLocation(); void setStorageLocation(File storage); long getStopTimeout(); void setStopTimeout(long stopTimeout); synchronized Instance createInstance(final String name, final InstanceSettings settings, final boolean printOutput); Instance[] getInstances(); Instance getInstance(final String name); void startInstance(final String name, final String javaOpts); void restartInstance(final String name, final String javaOpts); void stopInstance(final String name); void destroyInstance(final String name); void renameInstance(final String oldName, final String newName, final boolean printOutput); synchronized Instance cloneInstance(final String name, final String cloneName, final InstanceSettings settings, final boolean printOutput); void changeInstanceSshHost(String name, String host); static final String STORAGE_FILE; static final String BACKUP_EXTENSION; static final String DEFAULT_JAVA_OPTS; }