use of org.eclipse.kura.KuraException in project kura by eclipse.
the class HttpDownloadCountingOutputStream method startWork.
@Override
public void startWork() throws KuraException {
this.executor = Executors.newSingleThreadExecutor();
this.future = this.executor.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
URL localUrl = null;
boolean shouldAuthenticate = false;
try {
shouldAuthenticate = HttpDownloadCountingOutputStream.this.options.getUsername() != null && HttpDownloadCountingOutputStream.this.options.getPassword() != null && !(HttpDownloadCountingOutputStream.this.options.getUsername().trim().isEmpty() && !HttpDownloadCountingOutputStream.this.options.getPassword().trim().isEmpty());
if (shouldAuthenticate) {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(HttpDownloadCountingOutputStream.this.options.getUsername(), HttpDownloadCountingOutputStream.this.options.getPassword().toCharArray());
}
});
}
localUrl = new URL(HttpDownloadCountingOutputStream.this.m_downloadURL);
URLConnection urlConnection = localUrl.openConnection();
int connectTimeout = getConnectTimeout();
int readTimeout = getPropReadTimeout();
urlConnection.setConnectTimeout(connectTimeout);
urlConnection.setReadTimeout(readTimeout);
testConnectionProtocol(urlConnection);
HttpDownloadCountingOutputStream.this.is = localUrl.openStream();
String s = urlConnection.getHeaderField("Content-Length");
s_logger.info("Content-lenght: " + s);
setTotalBytes(s != null ? Integer.parseInt(s) : -1);
postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), 0, HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.IN_PROGRESS, null);
int bufferSize = getBufferSize();
if (bufferSize == 0 && getTotalBytes() > 0) {
int newSize = Math.round(HttpDownloadCountingOutputStream.this.totalBytes / 100 * 1);
bufferSize = newSize;
setBufferSize(newSize);
} else if (bufferSize == 0) {
int newSize = 1024 * 4;
bufferSize = newSize;
setBufferSize(newSize);
}
long numBytes = IOUtils.copyLarge(HttpDownloadCountingOutputStream.this.is, HttpDownloadCountingOutputStream.this, new byte[bufferSize]);
postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), numBytes, HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.COMPLETED, null);
} catch (IOException e) {
postProgressEvent(HttpDownloadCountingOutputStream.this.options.getClientId(), getByteCount(), HttpDownloadCountingOutputStream.this.totalBytes, DOWNLOAD_STATUS.FAILED, e.getMessage());
throw new KuraConnectException(e);
} finally {
if (HttpDownloadCountingOutputStream.this.is != null) {
try {
HttpDownloadCountingOutputStream.this.is.close();
} catch (IOException e) {
}
}
try {
close();
} catch (IOException e) {
}
localUrl = null;
if (shouldAuthenticate) {
Authenticator.setDefault(null);
}
}
return null;
}
});
try {
this.future.get();
} catch (ExecutionException ex) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, ex);
} catch (InterruptedException ex) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, ex);
}
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class InstallImpl method installDp.
public void installDp(DeploymentPackageInstallOptions options, File dpFile) throws KuraException {
SafeProcess proc = null;
try {
installDeploymentPackageInternal(dpFile);
installCompleteAsync(options, dpFile.getName());
s_logger.info("Install completed!");
if (options.isReboot()) {
Thread.sleep(options.getRebootDelay());
proc = ProcessUtil.exec("reboot");
}
} catch (Exception e) {
s_logger.info("Install failed!");
installFailedAsync(options, dpFile.getName(), e);
} finally {
if (proc != null) {
ProcessUtil.destroy(proc);
}
}
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class InstallImpl method sendSysUpdateSuccess.
private void sendSysUpdateSuccess() throws KuraException {
s_logger.info("Ready to send success after install");
File installDir = new File(this.m_installPersistanceDir);
if (installDir != null) {
for (File fileEntry : installDir.listFiles()) {
if (fileEntry.isFile() && fileEntry.getName().endsWith(PERSISTANCE_SUFFIX)) {
// &&
// fileEntry.getName().contains(verificationFile.getName()
Properties downloadProperties = loadInstallPersistance(fileEntry);
String deployUrl = downloadProperties.getProperty(DeploymentPackageDownloadOptions.METRIC_DP_DOWNLOAD_URI);
String dpName = downloadProperties.getProperty(DeploymentPackageOptions.METRIC_DP_NAME);
String dpVersion = downloadProperties.getProperty(DeploymentPackageOptions.METRIC_DP_VERSION);
String clientId = downloadProperties.getProperty(DeploymentPackageOptions.METRIC_DP_CLIENT_ID);
Long jobId = Long.valueOf(downloadProperties.getProperty(DeploymentPackageOptions.METRIC_JOB_ID));
String fileSystemFileName = downloadProperties.getProperty(PERSISTANCE_FILE_NAME);
String requestClientId = downloadProperties.getProperty(CloudDeploymentHandlerV2.METRIC_REQUESTER_CLIENT_ID);
DeploymentPackageDownloadOptions deployOptions = new DeploymentPackageDownloadOptions(deployUrl, dpName, dpVersion);
deployOptions.setClientId(clientId);
deployOptions.setJobId(jobId);
deployOptions.setRequestClientId(requestClientId);
try {
installCompleteAsync(deployOptions, fileSystemFileName);
s_logger.info("Sent install complete");
fileEntry.delete();
break;
} catch (KuraException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR);
}
}
}
}
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class InstallImpl method sendSysUpdateFailure.
private void sendSysUpdateFailure() throws KuraException {
File installDir = new File(this.m_installPersistanceDir);
if (installDir != null) {
for (final File fileEntry : installDir.listFiles()) {
if (fileEntry.isFile() && fileEntry.getName().endsWith(PERSISTANCE_SUFFIX)) {
// &&
// fileEntry.getName().contains(verificationFile.getName())
Properties downloadProperties = loadInstallPersistance(fileEntry);
String deployUrl = downloadProperties.getProperty(DeploymentPackageDownloadOptions.METRIC_DP_DOWNLOAD_URI);
String dpName = downloadProperties.getProperty(DeploymentPackageOptions.METRIC_DP_NAME);
String dpVersion = downloadProperties.getProperty(DeploymentPackageOptions.METRIC_DP_VERSION);
String clientId = downloadProperties.getProperty(DeploymentPackageOptions.METRIC_DP_CLIENT_ID);
Long jobId = Long.valueOf(downloadProperties.getProperty(DeploymentPackageOptions.METRIC_JOB_ID));
String fileSystemFileName = downloadProperties.getProperty(PERSISTANCE_FILE_NAME);
String requestClientId = downloadProperties.getProperty(CloudDeploymentHandlerV2.METRIC_REQUESTER_CLIENT_ID);
DeploymentPackageDownloadOptions deployOptions = new DeploymentPackageDownloadOptions(deployUrl, dpName, dpVersion);
deployOptions.setClientId(clientId);
deployOptions.setJobId(jobId);
deployOptions.setRequestClientId(requestClientId);
try {
installFailedAsync(deployOptions, fileSystemFileName, new KuraException(KuraErrorCode.INTERNAL_ERROR));
s_logger.info("Sent install failed");
fileEntry.delete();
break;
} catch (KuraException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR);
}
}
}
}
}
use of org.eclipse.kura.KuraException in project kura by eclipse.
the class InstallImpl method installSh.
public void installSh(DeploymentPackageOptions options, File shFile) throws KuraException {
updateInstallPersistance(shFile.getName(), options);
// Script Exec
SafeProcess proc = null;
try {
proc = ProcessUtil.exec("chmod 700 " + shFile.getCanonicalPath());
} catch (IOException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR);
} finally {
if (proc != null) {
ProcessUtil.destroy(proc);
}
}
SafeProcess proc2 = null;
try {
proc2 = ProcessUtil.exec(shFile.getCanonicalPath());
} catch (IOException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR);
} finally {
if (proc2 != null) {
ProcessUtil.destroy(proc2);
}
}
}
Aggregations