Search in sources :

Example 6 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeManager method modifyEnrollment.

@Override
public boolean modifyEnrollment(Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Modifying the sampledevice device enrollment data");
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().updateDevice(device);
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException iotDAOEx) {
            String msg = "Error occurred while roll back the update device transaction :" + device.toString();
            log.warn(msg, iotDAOEx);
        }
        String msg = "Error while updating the enrollment of the sampledevice device : " + device.getDeviceIdentifier();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Example 7 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeUtils method setupDeviceManagementSchema.

public static void setupDeviceManagementSchema() throws DeviceMgtPluginException {
    try {
        Context ctx = new InitialContext();
        DataSource dataSource = (DataSource) ctx.lookup(DeviceTypeConstants.DATA_SOURCE_NAME);
        DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource);
        log.info("Initializing device management repository database schema");
        initializer.createRegistryDatabase();
    } catch (NamingException e) {
        log.error("Error while looking up the data source: " + DeviceTypeConstants.DATA_SOURCE_NAME);
    } catch (Exception e) {
        throw new DeviceMgtPluginException("Error occurred while initializing Iot Device " + "Management database schema", e);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) NamingException(javax.naming.NamingException) DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 8 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeDAOImpl method getDevice.

public Device getDevice(String deviceId) throws DeviceMgtPluginException {
    Connection conn = null;
    PreparedStatement stmt = null;
    Device iotDevice = null;
    ResultSet resultSet = null;
    try {
        conn = DeviceTypeDAO.getConnection();
        String selectDBQuery = "SELECT sampledevice_DEVICE_ID, DEVICE_NAME" + " FROM sampledevice_DEVICE WHERE sampledevice_DEVICE_ID = ?";
        stmt = conn.prepareStatement(selectDBQuery);
        stmt.setString(1, deviceId);
        resultSet = stmt.executeQuery();
        if (resultSet.next()) {
            iotDevice = new Device();
            iotDevice.setName(resultSet.getString(DeviceTypeConstants.DEVICE_PLUGIN_DEVICE_NAME));
            if (log.isDebugEnabled()) {
                log.debug("sampledevice device " + deviceId + " data has been fetched from " + "sampledevice database.");
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while fetching sampledevice device : '" + deviceId + "'";
        log.error(msg, e);
        throw new DeviceMgtPluginException(msg, e);
    } finally {
        DeviceTypeUtils.cleanupResources(stmt, resultSet);
        DeviceTypeDAO.closeConnection();
    }
    return iotDevice;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) SQLException(java.sql.SQLException) Device(org.wso2.carbon.device.mgt.common.Device) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 9 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class ServiceComponent method activate.

protected void activate(ComponentContext ctx) {
    if (log.isDebugEnabled()) {
        log.debug("Activating b Management Service Component");
    }
    try {
        DeviceTypeManagerService deviceTypeManagerService = new DeviceTypeManagerService();
        BundleContext bundleContext = ctx.getBundleContext();
        serviceRegistration = bundleContext.registerService(DeviceManagementService.class.getName(), deviceTypeManagerService, null);
        String setupOption = System.getProperty("setup");
        if (setupOption != null) {
            if (log.isDebugEnabled()) {
                log.debug("-Dsetup is enabled. Iot Device management repository schema " + "initialization is about to begin");
            }
            try {
                DeviceTypeUtils.setupDeviceManagementSchema();
            } catch (DeviceMgtPluginException e) {
                log.error("Exception occurred while initializing device management database " + "schema", e);
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("b Management Service Component has been successfully activated");
        }
    } catch (Throwable e) {
        log.error("Error occurred while activating Current Sensor Management Service " + "Component", e);
    }
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceTypeManagerService(org.wso2.iot.sampledevice.plugin.impl.DeviceTypeManagerService) BundleContext(org.osgi.framework.BundleContext)

Example 10 with DeviceMgtPluginException

use of org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException in project product-iots by wso2.

the class DeviceTypeManager method enrollDevice.

@Override
public boolean enrollDevice(Device device) throws DeviceManagementException {
    boolean status;
    try {
        if (log.isDebugEnabled()) {
            log.debug("Enrolling a new sampledevice device : " + device.getDeviceIdentifier());
        }
        DeviceTypeDAO.beginTransaction();
        status = deviceTypeDAO.getDeviceTypeDAO().addDevice(device);
        DeviceTypeDAO.commitTransaction();
    } catch (DeviceMgtPluginException e) {
        try {
            DeviceTypeDAO.rollbackTransaction();
        } catch (DeviceMgtPluginException iotDAOEx) {
            log.warn("Error occurred while roll back the device enrol transaction :" + device.toString(), iotDAOEx);
        }
        String msg = "Error while enrolling the sampledevice device : " + device.getDeviceIdentifier();
        log.error(msg, e);
        throw new DeviceManagementException(msg, e);
    }
    return status;
}
Also used : DeviceMgtPluginException(org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException) DeviceManagementException(org.wso2.carbon.device.mgt.common.DeviceManagementException)

Aggregations

DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)12 SQLException (java.sql.SQLException)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)5 DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)4 ResultSet (java.sql.ResultSet)2 Device (org.wso2.carbon.device.mgt.common.Device)2 ArrayList (java.util.ArrayList)1 Context (javax.naming.Context)1 InitialContext (javax.naming.InitialContext)1 NamingException (javax.naming.NamingException)1 DataSource (javax.sql.DataSource)1 BundleContext (org.osgi.framework.BundleContext)1 DeviceTypeManagerService (org.wso2.iot.sampledevice.plugin.impl.DeviceTypeManagerService)1