Exemplo n.º 1
0
  public synchronized void renameInstance(String oldName, String newName) throws Exception {
    if (instances.get(newName) != null) {
      throw new IllegalArgumentException("Instance " + newName + " already exists");
    }
    Instance instance = instances.get(oldName);
    if (instance == null) {
      throw new IllegalArgumentException("Instance " + oldName + " not found");
    }
    if (instance.isRoot()) {
      throw new IllegalArgumentException("You can't rename the root instance");
    }
    if (instance.getPid() != 0) {
      throw new IllegalStateException("Instance not stopped");
    }

    println(
        Ansi.ansi()
            .a("Renaming instance ")
            .a(Ansi.Attribute.INTENSITY_BOLD)
            .a(oldName)
            .a(Ansi.Attribute.RESET)
            .a(" to ")
            .a(Ansi.Attribute.INTENSITY_BOLD)
            .a(newName)
            .a(Ansi.Attribute.RESET)
            .toString());
    // remove the old instance
    instances.remove(oldName);
    // update instance
    instance.setName(newName);
    // rename directory
    String oldLocationPath = instance.getLocation();
    File oldLocation = new File(oldLocationPath);
    String basedir = oldLocation.getParent();
    File newLocation = new File(basedir, newName);
    oldLocation.renameTo(newLocation);
    // update the instance location
    instance.setLocation(newLocation.getPath());
    // create the properties map including the instance name and instance location
    HashMap<String, String> props = new HashMap<String, String>();
    props.put(oldName, newName);
    props.put(oldLocationPath, newLocation.getPath());
    // replace all references to the "old" name by the new one in etc/system.properties
    // NB: it's replacement to avoid to override the user's changes
    filterResource(newLocation, "etc/system.properties", props);
    // replace all references to the "old" name by the new one in bin/karaf
    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);
    // add the renamed instances
    instances.put(newName, instance);
    // save instance definition in the instances.properties
    saveState();
  }