throw new IllegalArgumentException("Inputstream may not be null");
}
try {
if (!m_semaphore.tryAcquire(TIMEOUT)) {
throw new DeploymentException(CODE_TIMEOUT, "Timeout exceeded while waiting to install deployment package (" + TIMEOUT + " ms)");
}
}
catch (InterruptedException ie) {
throw new DeploymentException(CODE_TIMEOUT, "Thread interrupted");
}
File tempPackage = null;
StreamDeploymentPackage source = null;
AbstractDeploymentPackage target = null;
boolean succeeded = false;
try {
JarInputStream jarInput = null;
File tempIndex = null;
File tempContents = null;
try {
File tempDir = m_context.getDataFile(TEMP_DIR);
tempDir.mkdirs();
tempPackage = File.createTempFile(TEMP_PREFIX, TEMP_POSTFIX, tempDir);
tempPackage.delete();
tempPackage.mkdirs();
tempIndex = new File(tempPackage, PACKAGEINDEX_FILE);
tempContents = new File(tempPackage, PACKAGECONTENTS_DIR);
tempContents.mkdirs();
}
catch (IOException e) {
m_log.log(LogService.LOG_ERROR, "Error writing package to disk", e);
throw new DeploymentException(CODE_OTHER_ERROR, "Error writing package to disk", e);
}
try {
jarInput = new ContentCopyingJarInputStream(sourceInput, tempIndex, tempContents);
if (jarInput.getManifest() == null) {
m_log.log(LogService.LOG_ERROR, "Stream does not contain a valid deployment package: missing manifest!");
throw new DeploymentException(CODE_MISSING_HEADER, "No manifest present in deployment package!");
}
}
catch (IOException e) {
m_log.log(LogService.LOG_ERROR, "Stream does not contain a valid Jar", e);
throw new DeploymentException(CODE_NOT_A_JAR, "Stream does not contain a valid Jar", e);
}
source = new StreamDeploymentPackage(jarInput, m_context, this);
String dpSymbolicName = source.getName();
target = getExistingOrEmptyDeploymentPackage(dpSymbolicName);
// Fire an event that we're about to install a new package
sendStartedEvent(source, target);
// Assert that:
// the source has no bundles that exists in other packages than the target.
verifyNoResourcesShared(source, target);
if (source.isFixPackage()) {
// Assert that:
// a. the version of the target matches the required fix-package range;
// b. all missing source bundles are present in the target.
verifyFixPackage(source, target);
}
else {
// Assert that:
// no missing resources or bundles are declared.
verifySourcePackage(source);
}
try {
m_session = new DeploymentSessionImpl(source, target, createInstallCommandChain(), this);
m_session.call(false /* ignoreExceptions */);
}
catch (DeploymentException de) {
throw de;
}
String dpInstallBaseDirectory = PACKAGE_DIR + File.separator + dpSymbolicName;
File targetContents = m_context.getDataFile(dpInstallBaseDirectory + File.separator + PACKAGECONTENTS_DIR);
File targetIndex = m_context.getDataFile(dpInstallBaseDirectory + File.separator + PACKAGEINDEX_FILE);
if (source.isFixPackage()) {
try {
Utils.merge(targetIndex, targetContents, tempIndex, tempContents);
}
catch (IOException e) {
m_log.log(LogService.LOG_ERROR, "Could not merge source fix package with target deployment package", e);
throw new DeploymentException(CODE_OTHER_ERROR, "Could not merge source fix package with target deployment package", e);
}
}
else {
File targetPackage = m_context.getDataFile(dpInstallBaseDirectory);
targetPackage.mkdirs();
if (!Utils.replace(targetPackage, tempPackage)) {
throw new DeploymentException(CODE_OTHER_ERROR, "Could not replace " + targetPackage + " with " + tempPackage);
}
}
FileDeploymentPackage fileDeploymentPackage = null;
try {
fileDeploymentPackage = new FileDeploymentPackage(targetIndex, targetContents, m_context, this);
m_packages.put(dpSymbolicName, fileDeploymentPackage);
}
catch (IOException e) {
m_log.log(LogService.LOG_ERROR, "Could not create installed deployment package from disk", e);
throw new DeploymentException(CODE_OTHER_ERROR, "Could not create installed deployment package from disk", e);
}
// Since we're here, it means everything went OK, so we might as well raise our success flag...
succeeded = true;