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(); }
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; }