in instance/src/main/java/org/apache/karaf/instance/core/internal/InstanceServiceImpl.java [289:440]
public synchronized Instance createInstance(final String name, final InstanceSettings settings, final boolean printOutput) throws Exception {
return execute(state -> {
if (state.instances.get(name) != null) {
throw new IllegalArgumentException("Instance '" + name + "' already exists");
}
if (!settings.getProfiles().isEmpty()) {
try {
ProfileApplier.verify();
} catch (NoClassDefFoundError error) {
throw new IllegalArgumentException("Profile service package is not available");
}
}
String loc = settings.getLocation() != null ? settings.getLocation() : name;
File karafBase = new File(loc);
if (!karafBase.isAbsolute()) {
karafBase = new File(storageLocation, loc);
}
int sshPort = settings.getSshPort();
if (sshPort <= 0) {
sshPort = ++state.defaultSshPortStart;
}
String sshHost = settings.getAddress();
int rmiRegistryPort = settings.getRmiRegistryPort();
if (rmiRegistryPort <= 0) {
rmiRegistryPort = ++state.defaultRmiRegistryPortStart;
}
int rmiServerPort = settings.getRmiServerPort();
if (rmiServerPort <= 0) {
rmiServerPort = ++state.defaultRmiServerPortStart;
}
logInfo("Creating new instance on SSH port %d and registry port %d / RMI server port %d at: %s",
printOutput, sshPort, rmiRegistryPort, rmiServerPort, karafBase);
mkdir(karafBase, "bin", printOutput);
mkdir(karafBase, "etc", printOutput);
mkdir(karafBase, "etc/scripts", printOutput);
mkdir(karafBase, "system", printOutput);
mkdir(karafBase, "deploy", printOutput);
mkdir(karafBase, "data", printOutput);
Map<String, URL> textResources = new HashMap<>(settings.getTextResources());
Map<String, URL> binaryResources = new HashMap<>(settings.getBinaryResources());
String[] resources =
{
"etc/all.policy",
"etc/config.properties",
"etc/custom.properties",
"etc/distribution.info",
"etc/equinox-debug.properties",
"etc/java.util.logging.properties",
"etc/jmx.acl.cfg",
"etc/jmx.acl.java.lang.Memory.cfg",
"etc/jmx.acl.org.apache.karaf.bundle.cfg",
"etc/jmx.acl.org.apache.karaf.config.cfg",
"etc/jmx.acl.org.apache.karaf.security.jmx.cfg",
"etc/jmx.acl.osgi.compendium.cm.cfg",
"etc/jre.properties",
"etc/keys.properties",
"etc/org.apache.felix.eventadmin.impl.EventAdmin.cfg",
"etc/org.apache.felix.fileinstall-deploy.cfg",
"etc/org.apache.karaf.command.acl.bundle.cfg",
"etc/org.apache.karaf.command.acl.config.cfg",
"etc/org.apache.karaf.command.acl.feature.cfg",
"etc/org.apache.karaf.command.acl.jaas.cfg",
"etc/org.apache.karaf.command.acl.kar.cfg",
"etc/org.apache.karaf.command.acl.scope_bundle.cfg",
"etc/org.apache.karaf.command.acl.shell.cfg",
"etc/org.apache.karaf.command.acl.system.cfg",
"etc/org.apache.karaf.features.repos.cfg",
"etc/org.apache.karaf.jaas.cfg",
"etc/org.apache.karaf.kar.cfg",
"etc/org.apache.karaf.log.cfg",
"etc/org.ops4j.pax.logging.cfg",
"etc/org.ops4j.pax.url.mvn.cfg",
"etc/shell.init.script",
"etc/users.properties",
"etc/scripts/shell.completion.script",
FEATURES_CFG
};
copyResourcesToDir(resources, karafBase, textResources, printOutput);
addFeaturesFromSettings(new File(karafBase, FEATURES_CFG), settings);
// The startup.properties is now generated by the karaf maven plugin, so
// we use the one from the root instance instead of embedding it
File rootEtc = new File(System.getProperty("karaf.etc"));
copy(new File(rootEtc, "startup.properties"), new File(karafBase, "etc/startup.properties"));
// align child with any bundles we have overriden in the root instance
File rootOverrides = new File(rootEtc, "overrides.properties");
if (rootOverrides.exists()) {
copy(rootOverrides, new File(karafBase, "etc/overrides.properties"));
}
HashMap<String, String> props = new HashMap<>();
props.put("${SUBST-KARAF-NAME}", name);
props.put("${SUBST-KARAF-HOME}", System.getProperty("karaf.home"));
props.put("${SUBST-KARAF-BASE}", karafBase.getPath());
props.put("${SUBST-SSH-PORT}", Integer.toString(sshPort));
props.put("${SUBST-SSH-HOST}", sshHost);
props.put("${SUBST-RMI-REGISTRY-PORT}", Integer.toString(rmiRegistryPort));
props.put("${SUBST-RMI-SERVER-PORT}", Integer.toString(rmiServerPort));
String[] filteredResources =
{
"etc/system.properties",
"etc/org.apache.karaf.shell.cfg",
"etc/org.apache.karaf.management.cfg",
"bin/karaf",
"bin/start",
"bin/stop",
"bin/karaf.bat",
"bin/start.bat",
"bin/stop.bat"
};
copyFilteredResourcesToDir(filteredResources, karafBase, textResources, props, printOutput);
try {
makeFileExecutable(new File(karafBase, "bin/karaf"));
makeFileExecutable(new File(karafBase, "bin/start"));
makeFileExecutable(new File(karafBase, "bin/stop"));
} catch (IOException e) {
LOGGER.debug("Could not set file mode on scripts.", e);
}
for (String resource : textResources.keySet()) {
copyFilteredResourceToDir(resource, karafBase, textResources, props, printOutput);
}
for (String resource : binaryResources.keySet()) {
copyBinaryResourceToDir(resource, karafBase, binaryResources, printOutput);
}
if (!settings.getProfiles().isEmpty()) {
ProfileApplier.applyProfiles(karafBase, settings.getProfiles(), printOutput);
}
String javaOpts = settings.getJavaOpts();
if (javaOpts == null || javaOpts.length() == 0) {
javaOpts = DEFAULT_JAVA_OPTS;
}
InstanceState is = new InstanceState();
is.name = name;
is.loc = karafBase.toString();
is.opts = javaOpts;
state.instances.put(name, is);
InstanceImpl instance = new InstanceImpl(InstanceServiceImpl.this, name);
InstanceServiceImpl.this.proxies.put(name, instance);
return instance;
}, true);
}