Search in sources :

Example 16 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class UninstallImpl method uninstaller.

public void uninstaller(DeploymentPackageUninstallOptions options, String packageName) throws KuraException {
    try {
        String name = packageName;
        if (name != null) {
            DeploymentPackage dp = this.m_deploymentAdmin.getDeploymentPackage(name);
            if (dp != null) {
                dp.uninstall();
                String sUrl = CloudDeploymentHandlerV2.s_installImplementation.getDeployedPackages().getProperty(name);
                File dpFile = new File(new URL(sUrl).getPath());
                if (!dpFile.delete()) {
                    s_logger.warn("Cannot delete file at URL: {}", sUrl);
                }
                CloudDeploymentHandlerV2.s_installImplementation.removePackageFromConfFile(name);
            }
            uninstallCompleteAsync(options, name);
            // Reboot?
            deviceReboot(options);
        }
    } catch (Exception e) {
        throw KuraException.internalError(e);
    }
}
Also used : DeploymentPackage(org.osgi.service.deploymentadmin.DeploymentPackage) File(java.io.File) URL(java.net.URL) KuraException(org.eclipse.kura.KuraException)

Example 17 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class XmlConfigPropertiesAdapter method unmarshal.

public Map<String, Object> unmarshal(XmlConfigPropertiesAdapted adaptedPropsAdapted) throws Exception {
    Map<String, Object> properties = new HashMap<String, Object>();
    XmlConfigPropertyAdapted[] adaptedProps = adaptedPropsAdapted.getProperties();
    if (adaptedProps == null) {
        return properties;
    }
    for (XmlConfigPropertyAdapted adaptedProp : adaptedProps) {
        String propName = adaptedProp.getName();
        ConfigPropertyType type = adaptedProp.getType();
        if (type != null) {
            Object propvalue = null;
            if (adaptedProp.getArray() == false) {
                switch(adaptedProp.getType()) {
                    case STRING_TYPE:
                        propvalue = adaptedProp.getValues()[0];
                        break;
                    case LONG_TYPE:
                        propvalue = Long.parseLong(adaptedProp.getValues()[0]);
                        break;
                    case DOUBLE_TYPE:
                        propvalue = Double.parseDouble(adaptedProp.getValues()[0]);
                        break;
                    case FLOAT_TYPE:
                        propvalue = Float.parseFloat(adaptedProp.getValues()[0]);
                        break;
                    case INTEGER_TYPE:
                        propvalue = Integer.parseInt(adaptedProp.getValues()[0]);
                        break;
                    case BYTE_TYPE:
                        propvalue = Byte.parseByte(adaptedProp.getValues()[0]);
                        break;
                    case CHAR_TYPE:
                        String s = adaptedProp.getValues()[0];
                        propvalue = Character.valueOf(s.charAt(0));
                        break;
                    case BOOLEAN_TYPE:
                        propvalue = Boolean.parseBoolean(adaptedProp.getValues()[0]);
                        break;
                    case SHORT_TYPE:
                        propvalue = Short.parseShort(adaptedProp.getValues()[0]);
                        break;
                    case PASSWORD_TYPE:
                        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
                        ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
                        CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
                        propvalue = adaptedProp.getValues()[0];
                        if (adaptedProp.isEncrypted()) {
                            try {
                                propvalue = new Password(cryptoService.decryptAes(((String) propvalue).toCharArray()));
                            } catch (KuraException e) {
                                propvalue = new Password(cryptoService.decodeBase64((String) propvalue));
                            }
                        } else {
                            propvalue = new Password((String) propvalue);
                        }
                        break;
                }
            } else {
                // Starting from 1.2.0 an empty array will never be present in a snapshot.
                if (adaptedProp.getValues() == null) {
                    continue;
                }
                switch(adaptedProp.getType()) {
                    case STRING_TYPE:
                        propvalue = adaptedProp.getValues();
                        break;
                    case LONG_TYPE:
                        Long[] longValues = new Long[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                longValues[i] = Long.parseLong(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = longValues;
                        break;
                    case DOUBLE_TYPE:
                        Double[] doubleValues = new Double[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                doubleValues[i] = Double.parseDouble(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = doubleValues;
                        break;
                    case FLOAT_TYPE:
                        Float[] floatValues = new Float[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                floatValues[i] = Float.parseFloat(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = floatValues;
                        break;
                    case INTEGER_TYPE:
                        Integer[] intValues = new Integer[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                intValues[i] = Integer.parseInt(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = intValues;
                        break;
                    case BYTE_TYPE:
                        Byte[] byteValues = new Byte[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                byteValues[i] = Byte.parseByte(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = byteValues;
                        break;
                    case CHAR_TYPE:
                        Character[] charValues = new Character[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                String s = adaptedProp.getValues()[i];
                                charValues[i] = Character.valueOf(s.charAt(0));
                            }
                        }
                        propvalue = charValues;
                        break;
                    case BOOLEAN_TYPE:
                        Boolean[] booleanValues = new Boolean[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                booleanValues[i] = Boolean.parseBoolean(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = booleanValues;
                        break;
                    case SHORT_TYPE:
                        Short[] shortValues = new Short[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                shortValues[i] = Short.parseShort(adaptedProp.getValues()[i]);
                            }
                        }
                        propvalue = shortValues;
                        break;
                    case PASSWORD_TYPE:
                        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
                        ServiceReference<CryptoService> cryptoServiceRef = bundleContext.getServiceReference(CryptoService.class);
                        CryptoService cryptoService = bundleContext.getService(cryptoServiceRef);
                        Password[] pwdValues = new Password[adaptedProp.getValues().length];
                        for (int i = 0; i < adaptedProp.getValues().length; i++) {
                            if (adaptedProp.getValues()[i] != null) {
                                if (adaptedProp.isEncrypted()) {
                                    try {
                                        pwdValues[i] = new Password(cryptoService.decryptAes(adaptedProp.getValues()[i].toCharArray()));
                                    } catch (KuraException e) {
                                        pwdValues[i] = new Password(cryptoService.decodeBase64(adaptedProp.getValues()[i]));
                                    }
                                } else {
                                    pwdValues[i] = new Password(adaptedProp.getValues()[i]);
                                }
                            }
                        }
                        propvalue = pwdValues;
                        break;
                }
            }
            properties.put(propName, propvalue);
        }
    }
    return properties;
}
Also used : HashMap(java.util.HashMap) ConfigPropertyType(org.eclipse.kura.core.configuration.XmlConfigPropertyAdapted.ConfigPropertyType) CryptoService(org.eclipse.kura.crypto.CryptoService) KuraException(org.eclipse.kura.KuraException) Password(org.eclipse.kura.configuration.Password) BundleContext(org.osgi.framework.BundleContext)

Example 18 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class LinuxNamed method enable.

public boolean enable() throws KuraException {
    try {
        // Check if named is running
        int pid = LinuxProcessUtil.getPid(s_procString);
        if (pid > -1) {
            // If so, disable it
            s_logger.error("DNS server is already running, bringing it down...");
            disable();
        }
        // Start named
        int result = -1;
        if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind start");
        } else if (OS_VERSION.equals(KuraConstants.ReliaGATE_10_05.getImageName() + "_" + KuraConstants.ReliaGATE_10_05.getImageVersion())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind start");
        } else if (OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind9 start");
        } else if (OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind start");
        } else if (OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind9 start");
        } else if (OS_VERSION.equals(KuraConstants.Fedora_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.Reliagate_20_26.getImageName() + "_" + KuraConstants.Reliagate_20_26.getImageVersion())) {
            result = LinuxProcessUtil.start("/bin/systemctl start named");
        } else {
            s_logger.info("Linux named enable fallback");
            result = LinuxProcessUtil.start("/etc/init.d/named start");
        }
        if (result == 0) {
            s_logger.debug("DNS server started.");
            s_logger.trace(this.m_dnsServerConfigIP4.toString());
            return true;
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    return false;
}
Also used : KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) KuraException(org.eclipse.kura.KuraException)

Example 19 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class LinuxNamed method disable.

public boolean disable() throws KuraException {
    try {
        int result = -1;
        // If so, stop it.
        if (OS_VERSION.equals(KuraConstants.Mini_Gateway.getImageName() + "_" + KuraConstants.Mini_Gateway.getImageVersion())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind stop");
        } else if (OS_VERSION.equals(KuraConstants.ReliaGATE_10_05.getImageName() + "_" + KuraConstants.ReliaGATE_10_05.getImageVersion())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind stop");
        } else if (OS_VERSION.equals(KuraConstants.Raspberry_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.BeagleBone.getImageName())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind9 stop");
        } else if (OS_VERSION.equals(KuraConstants.Intel_Edison.getImageName() + "_" + KuraConstants.Intel_Edison.getImageVersion() + "_" + KuraConstants.Intel_Edison.getTargetName())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind stop");
        } else if (OS_VERSION.equals(KuraConstants.ReliaGATE_50_21_Ubuntu.getImageName() + "_" + KuraConstants.ReliaGATE_50_21_Ubuntu.getImageVersion())) {
            result = LinuxProcessUtil.start("/etc/init.d/bind9 stop");
        } else if (OS_VERSION.equals(KuraConstants.Fedora_Pi.getImageName()) || OS_VERSION.equals(KuraConstants.Reliagate_20_26.getImageName() + "_" + KuraConstants.Reliagate_20_26.getImageVersion())) {
            result = LinuxProcessUtil.start("/bin/systemctl stop named");
        } else {
            result = LinuxProcessUtil.start("/etc/init.d/named stop");
        }
        if (result == 0) {
            s_logger.debug("DNS server stopped.");
            s_logger.trace(this.m_dnsServerConfigIP4.toString());
            return true;
        } else {
            s_logger.debug("tried to kill DNS server for interface but it is not running");
        }
    } catch (Exception e) {
        throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
    }
    return true;
}
Also used : KuraException(org.eclipse.kura.KuraException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) KuraException(org.eclipse.kura.KuraException)

Example 20 with KuraException

use of org.eclipse.kura.KuraException in project kura by eclipse.

the class LinuxNamed method writeConfig.

private void writeConfig() throws KuraException {
    try {
        FileOutputStream fos = new FileOutputStream(s_persistentConfigFileName);
        PrintWriter pw = new PrintWriter(fos);
        // build up the file
        if (this.m_dnsServerConfigIP4 == null || this.m_dnsServerConfigIP4.getForwarders() == null || this.m_dnsServerConfigIP4.getAllowedNetworks() == null || this.m_dnsServerConfigIP4.getForwarders().size() == 0 || this.m_dnsServerConfigIP4.getAllowedNetworks().size() == 0) {
            s_logger.debug("writing default named.conf to {} with: {}", s_persistentConfigFileName, this.m_dnsServerConfigIP4.toString());
            pw.print(getDefaultNamedFile());
        } else {
            s_logger.debug("writing custom named.conf to {} with: {}", s_persistentConfigFileName, this.m_dnsServerConfigIP4.toString());
            pw.print(getForwardingNamedFile());
        }
        pw.flush();
        fos.getFD().sync();
        pw.close();
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new KuraException(KuraErrorCode.CONFIGURATION_ERROR, "error while building up new configuration files for dns servers: " + e.getMessage());
    }
}
Also used : KuraException(org.eclipse.kura.KuraException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) KuraException(org.eclipse.kura.KuraException) PrintWriter(java.io.PrintWriter)

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