Beispiel #1
0
  public synchronized Instance cloneInstance(
      String name, String cloneName, InstanceSettings settings) throws Exception {
    if (instances.get(cloneName) != null) {
      throw new IllegalArgumentException("Instance " + cloneName + " already exists");
    }
    Instance instance = instances.get(name);
    if (instance == null) {
      throw new IllegalArgumentException("Instance " + name + " not found");
    }
    if (instance.isRoot()) {
      throw new IllegalArgumentException("You can't clone the root instance");
    }
    if (instance.getPid() != 0) {
      throw new IllegalStateException("Instance not stopped");
    }

    println(
        Ansi.ansi()
            .a("Cloning instance ")
            .a(Ansi.Attribute.INTENSITY_BOLD)
            .a(name)
            .a(Ansi.Attribute.RESET)
            .a(" into ")
            .a(Ansi.Attribute.INTENSITY_BOLD)
            .a(cloneName)
            .toString());
    // define the clone instance location
    String cloneLocationPath = settings.getLocation() != null ? settings.getLocation() : name;
    File cloneLocation = new File(cloneLocationPath);
    if (!cloneLocation.isAbsolute()) {
      cloneLocation = new File(storageLocation, cloneLocationPath);
    }
    // copy instance directory
    String locationPath = instance.getLocation();
    File location = new File(locationPath);
    copy(location, cloneLocation);
    // create the properties map including the instance name, location, ssh and rmi port numbers
    HashMap<String, String> props = new HashMap<String, String>();
    props.put(name, cloneName);
    props.put(locationPath, cloneLocationPath);
    if (settings.getSshPort() > 0)
      props.put(
          new Integer(instance.getSshPort()).toString(),
          new Integer(settings.getSshPort()).toString());
    if (settings.getRmiRegistryPort() > 0)
      props.put(
          new Integer(instance.getRmiRegistryPort()).toString(),
          new Integer(settings.getRmiRegistryPort()).toString());
    if (settings.getRmiServerPort() > 0)
      props.put(
          new Integer(instance.getRmiServerPort()).toString(),
          new Integer(settings.getRmiServerPort()).toString());
    // filtering clone files
    filterResource(cloneLocation, "etc/customer.properties", props);
    filterResource(cloneLocation, "etc/org.apache.karaf.management.cfg", props);
    filterResource(cloneLocation, "etc/org.apache.karaf.shell.cfg", props);
    filterResource(cloneLocation, "etc/org.ops4j.pax.logging.cfg", props);
    filterResource(cloneLocation, "etc/system.properties", props);
    filterResource(cloneLocation, "bin/karaf", props);
    filterResource(cloneLocation, "bin/start", props);
    filterResource(cloneLocation, "bin/stop", props);
    filterResource(cloneLocation, "bin/karaf.bat", props);
    filterResource(cloneLocation, "bin/start.bat", props);
    filterResource(cloneLocation, "bin/stop.bat", props);
    // create and add the clone instance in the registry
    String javaOpts = settings.getJavaOpts();
    if (javaOpts == null || javaOpts.length() == 0) {
      javaOpts = "-server -Xmx512M -Dcom.sun.management.jmxremote";
    }
    Instance cloneInstance =
        new InstanceImpl(this, name, cloneLocation.toString(), settings.getJavaOpts());
    instances.put(name, instance);
    saveState();
    return cloneInstance;
  }