} else {
soFar.add(configID);
}
// Download and install the main artifact
Artifact[] matches = configManager.getArtifactResolver().queryArtifacts(configID);
PluginArtifactType instance = null;
if (matches.length == 0) {
// not present, needs to be downloaded
monitor.getResults().setCurrentMessage("Downloading " + configID);
monitor.getResults().setCurrentFilePercent(-1);
OpenResult result = null;
for (SourceRepository repository : repos) {
result = repository.open(configID, monitor);
if (result != null) {
break;
}
}
if (result == null) {
throw new IllegalArgumentException("Could not find " + configID + " in any repo: " + repos);
}
// Check if the result is already in server's repository (maybe the actual configId is not what was expected?)
if (configManager.getArtifactResolver().queryArtifacts(result.getArtifact()).length > 0) {
String msg = "Not downloading " + configID + ". Query for " + configID + " resulted in " + result.getArtifact()
+ " which is already available in server's repository.";
monitor.getResults().setCurrentMessage(msg);
log.info(msg);
result.close();
return;
}
try {
File tempFile = result.getFile();
if (tempFile == null) {
log.error("Null filehandle was returned for " + configID);
throw new IllegalArgumentException("Null filehandle was returned for " + configID);
}
PluginType pluginData = metadata.get(configID);
// Only bother with the hash if we got it from a source other than the download file itself
HashType hash = pluginData == null ? null : pluginData.getPluginArtifact().get(0).getHash();
if (hash != null) {
String actual = ConfigurationStoreUtil.getActualChecksum(tempFile, hash.getType());
if (!actual.equals(hash.getValue())) {
log.error("File download incorrect (expected {} hash {} but got {})", new String[]{hash.getType(), hash.getValue(), actual});
throw new IOException("File download incorrect (expected " + hash.getType() + " hash " + hash.getValue() + " but got " + actual + ")");
}
}
// See if the download file has plugin metadata and use it in preference to what is in the catalog.
try {
PluginType realPluginData = GeronimoSourceRepository.extractPluginMetadata(tempFile);
if (realPluginData != null) {
pluginData = realPluginData;
}
} catch (Exception e) {
log.error("Unable to read plugin metadata: {}", e.getMessage());
throw (IOException) new IOException("Unable to read plugin metadata: " + e.getMessage()).initCause(e);
}
if (pluginData != null) { // it's a plugin, not a plain JAR
if (!validatePlugin(pluginData)) {
monitor.getResults().addSkippedConfigID(new MissingDependencyException("already installed", configID, (Stack<Artifact>)null));
return;
}
instance = pluginData.getPluginArtifact().get(0);
}
monitor.getResults().setCurrentMessage("Copying " + result.getArtifact() + " to the repository");
result.install(writeableRepo, monitor);
if (dependency) {
monitor.getResults().addDependencyInstalled(configID);
configID = result.getArtifact();
} else {
configID = result.getArtifact();
monitor.getResults().addInstalledConfigID(configID);
}
if (pluginData != null) {
log.debug("Installed plugin with moduleId={} and name={}", pluginData.getPluginArtifact().get(0).getModuleId(), pluginData.getName());
} else {
log.debug("Installed artifact={}", configID);
}
} finally {
//todo probably not needede
result.close();
}
} else {
if (dependency) {
monitor.getResults().addDependencyPresent(configID);
} else {
monitor.getResults().addInstalledConfigID(configID);
}
}
// Download and install the dependencies
try {
if (!configID.isResolved()) {
// See if something's running
for (int i = matches.length - 1; i >= 0; i--) {
Artifact match = matches[i];
if (configStore.containsConfiguration(match) && configManager.isRunning(match)) {
log.debug("Found required configuration={} and it is running", match);
return; // its dependencies must be OK
} else {
log.debug("Either required configuration={} is not installed or it is not running", match);
}
}
// Go with something that's installed
configID = matches[matches.length - 1];
}
ConfigurationData data = null;
if (configStore.containsConfiguration(configID)) {
if (configManager.isRunning(configID)) {
return; // its dependencies must be OK
}
log.debug("Loading configuration={}", configID);
data = configStore.loadConfiguration(configID);
}
// Download the dependencies
parentStack.push(configID);
if (instance == null) {
PluginType currentPlugin = getPluginMetadata(configID);
if (currentPlugin != null) {
instance = currentPlugin.getPluginArtifact().get(0);
}
}
if (instance == null) {
//no plugin metadata, guess with something else
Dependency[] dependencies = data == null ? getDependencies(writeableRepo, configID) : getDependencies(data);
for (Dependency dep : dependencies) {
Artifact artifact = dep.getArtifact();
log.debug("Attempting to download dependency={} for configuration={}", artifact, configID);
downloadArtifact(artifact, metadata, repos, username, password, monitor, soFar, parentStack, true, servers, loadOverride);
}
} else {
//rely on plugin metadata if present.
List<DependencyType> deps = instance.getDependency();
for (DependencyType dep : deps) {
Artifact artifact = toArtifact(dep);
log.debug("Attempting to download dependency={} for configuration={}", artifact, configID);
downloadArtifact(artifact, metadata, repos, username, password, monitor, soFar, parentStack, true, servers, loadOverride & dep.isStart());
}