Search in sources :

Example 11 with KuraException

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);
    }
}
Also used : KuraConnectException(org.eclipse.kura.KuraConnectException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) KuraException(org.eclipse.kura.KuraException) ExecutionException(java.util.concurrent.ExecutionException) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 12 with KuraException

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);
        }
    }
}
Also used : SafeProcess(org.eclipse.kura.core.util.SafeProcess) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) KuraException(org.eclipse.kura.KuraException)

Example 13 with KuraException

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);
                }
            }
        }
    }
}
Also used : DeploymentPackageDownloadOptions(org.eclipse.kura.core.deployment.download.DeploymentPackageDownloadOptions) KuraException(org.eclipse.kura.KuraException) Properties(java.util.Properties) File(java.io.File)

Example 14 with KuraException

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);
                }
            }
        }
    }
}
Also used : DeploymentPackageDownloadOptions(org.eclipse.kura.core.deployment.download.DeploymentPackageDownloadOptions) KuraException(org.eclipse.kura.KuraException) Properties(java.util.Properties) File(java.io.File)

Example 15 with KuraException

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);
        }
    }
}
Also used : KuraException(org.eclipse.kura.KuraException) SafeProcess(org.eclipse.kura.core.util.SafeProcess) IOException(java.io.IOException)

Aggregations

KuraException (org.eclipse.kura.KuraException)315 IOException (java.io.IOException)191 CommConnection (org.eclipse.kura.comm.CommConnection)54 SafeProcess (org.eclipse.kura.core.util.SafeProcess)49 ArrayList (java.util.ArrayList)46 File (java.io.File)44 BufferedReader (java.io.BufferedReader)42 InputStreamReader (java.io.InputStreamReader)30 UnknownHostException (java.net.UnknownHostException)30 FileNotFoundException (java.io.FileNotFoundException)25 NetConfig (org.eclipse.kura.net.NetConfig)23 NetInterfaceAddressConfig (org.eclipse.kura.net.NetInterfaceAddressConfig)20 SupportedUsbModemInfo (org.eclipse.kura.linux.net.modem.SupportedUsbModemInfo)19 UsbModemDevice (org.eclipse.kura.usb.UsbModemDevice)19 Properties (java.util.Properties)18 NetConfigIP4 (org.eclipse.kura.net.NetConfigIP4)17 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)17 StringTokenizer (java.util.StringTokenizer)16 FileOutputStream (java.io.FileOutputStream)15 IP4Address (org.eclipse.kura.net.IP4Address)15