few changes added to permissions IoT device type plugins

revert-dabc3590
ayyoob 9 years ago
parent 14aac9f4f6
commit 2d57f252f0

@ -81,7 +81,6 @@ public class AndroidSenseManagerServiceImpl implements AndroidSenseManagerServic
device.setEnrolmentInfo(enrolmentInfo);
boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
if (added) {
APIUtil.registerApiAccessRoles(APIUtil.getAuthenticatedUser());
AndroidConfiguration androidConfiguration = new AndroidConfiguration();
androidConfiguration.setTenantDomain(APIUtil.getAuthenticatedUserTenantDomain());
String mqttEndpoint = MqttConfig.getInstance().getMqttQueueEndpoint();

@ -175,45 +175,4 @@ public class APIUtil {
}
return deviceAccessAuthorizationService;
}
public static void registerApiAccessRoles(String user) {
UserStoreManager userStoreManager = null;
try {
userStoreManager = getUserStoreManager();
String[] userList = new String[]{user};
if (userStoreManager != null) {
String rolesOfUser[] = userStoreManager.getRoleListOfUser(user);
if (!userStoreManager.isExistingRole(Constants.DEFAULT_ROLE_NAME)) {
userStoreManager.addRole(Constants.DEFAULT_ROLE_NAME, userList, Constants.DEFAULT_PERMISSION);
} else if (rolesOfUser != null && Arrays.asList(rolesOfUser).contains(Constants.DEFAULT_ROLE_NAME)) {
return;
} else {
userStoreManager.updateUserListOfRole(Constants.DEFAULT_ROLE_NAME, new String[0], userList);
}
}
} catch (UserStoreException e) {
log.error("Error while creating a role and adding a user for virtual_firealarm.", e);
}
}
public static UserStoreManager getUserStoreManager() {
RealmService realmService;
UserStoreManager userStoreManager;
try {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
realmService = (RealmService) ctx.getOSGiService(RealmService.class, null);
if (realmService == null) {
String msg = "Realm service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
int tenantId = ctx.getTenantId();
userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager();
} catch (UserStoreException e) {
String msg = "Error occurred while retrieving current user store manager";
log.error(msg, e);
throw new IllegalStateException(msg);
}
return userStoreManager;
}
}

@ -24,10 +24,5 @@ import org.wso2.carbon.user.core.Permission;
* This hold the constants related to the device type.
*/
public class Constants {
public static final String DEFAULT_PERMISSION_RESOURCE = "/permission/admin/device-mgt/android_sense/user";
public static final String DEFAULT_ROLE_NAME = "android_sense_user";
public static final Permission DEFAULT_PERMISSION[] = new Permission[]{new Permission(Constants.DEFAULT_PERMISSION_RESOURCE,
"ui.execute")};
public static final String LOCALHOST = "localhost";
}

@ -30,56 +30,56 @@
<!-- Device related APIs -->
<Permission>
<name>Set words</name>
<path>/device-mgt/android_sense/user</path>
<path>/device-mgt/user/operations</path>
<url>/device/*/words</url>
<method>POST</method>
<scope>android_sense_user</scope>
</Permission>
<Permission>
<name>set word threshold information</name>
<path>/device-mgt/android_sense/user</path>
<path>/device-mgt/user/operations</path>
<url>/device/*/words/threshold</url>
<method>POST</method>
<scope>android_sense_user</scope>
</Permission>
<Permission>
<name>delete words</name>
<path>/device-mgt/android_sense/user</path>
<path>/device-mgt/user/operations</path>
<url>/device/*/words</url>
<method>DELETE</method>
<scope>android_sense_user</scope>
</Permission>
<Permission>
<name>get device stats</name>
<path>/device-mgt/android_sense/user</path>
<path>/device-mgt/user/stats</path>
<url>/stats/*/sensors/*</url>
<method>GET</method>
<scope>android_sense_device</scope>
</Permission>
<Permission>
<name>Get device</name>
<path>/device-mgt/android_sense/user</path>
<path>/device-mgt/user/devices/list</path>
<url>/enrollment/devices/*</url>
<method>GET</method>
<scope>android_sense_user</scope>
</Permission>
<Permission>
<name>Add device</name>
<path>/device-mgt/user</path>
<path>/device-mgt/user/devices</path>
<url>/enrollment/devices/*</url>
<method>POST</method>
<scope>android_sense_user</scope>
</Permission>
<Permission>
<name>Remove device</name>
<path>/device-mgt/android_sense/user</path>
<path>/device-mgt/user/devices/remove</path>
<url>/enrollment/devices/*</url>
<method>DELETE</method>
<scope>android_sense_user</scope>
</Permission>
<Permission>
<name>Update device</name>
<path>/device-mgt/android_sense/user</path>
<path>/device-mgt/user/devices/update</path>
<url>/enrollment/devices/*</url>
<method>PUT</method>
<scope>android_sense_user</scope>

@ -246,12 +246,7 @@ public class ArduinoManagerServiceImpl implements ArduinoManagerService {
device.setType(ArduinoConstants.DEVICE_TYPE);
enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser());
device.setEnrolmentInfo(enrolmentInfo);
boolean added = APIUtil.getDeviceManagementService().enrollDevice(device);
if (added) {
APIUtil.registerApiAccessRoles(APIUtil.getAuthenticatedUser());
return true;
}
return false;
return APIUtil.getDeviceManagementService().enrollDevice(device);
} catch (DeviceManagementException e) {
return false;
}

@ -160,47 +160,6 @@ public class APIUtil {
return threadLocalCarbonContext.getTenantDomain();
}
public static void registerApiAccessRoles(String user) {
UserStoreManager userStoreManager = null;
try {
userStoreManager = getUserStoreManager();
String[] userList = new String[]{user};
if (userStoreManager != null) {
String rolesOfUser[] = userStoreManager.getRoleListOfUser(user);
if (!userStoreManager.isExistingRole(Constants.DEFAULT_ROLE_NAME)) {
userStoreManager.addRole(Constants.DEFAULT_ROLE_NAME, userList, Constants.DEFAULT_PERMISSION);
} else if (rolesOfUser != null && Arrays.asList(rolesOfUser).contains(Constants.DEFAULT_ROLE_NAME)) {
return;
} else {
userStoreManager.updateUserListOfRole(Constants.DEFAULT_ROLE_NAME, new String[0], userList);
}
}
} catch (UserStoreException e) {
log.error("Error while creating a role and adding a user for virtual_firealarm.", e);
}
}
public static UserStoreManager getUserStoreManager() {
RealmService realmService;
UserStoreManager userStoreManager;
try {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
realmService = (RealmService) ctx.getOSGiService(RealmService.class, null);
if (realmService == null) {
String msg = "Realm service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
int tenantId = ctx.getTenantId();
userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager();
} catch (UserStoreException e) {
String msg = "Error occurred while retrieving current user store manager";
log.error(msg, e);
throw new IllegalStateException(msg);
}
return userStoreManager;
}
public static DeviceAccessAuthorizationService getDeviceAccessAuthorizationService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
DeviceAccessAuthorizationService deviceAccessAuthorizationService =

@ -1,32 +0,0 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.iot.arduino.service.impl.util;
import org.wso2.carbon.user.core.Permission;
/**
* This hold the constants related to the device type.
*/
public class Constants {
public static final String DEFAULT_PERMISSION_RESOURCE = "/permission/admin/device-mgt/arduino/user";
public static final String DEFAULT_ROLE_NAME = "arduino_user";
public static final Permission DEFAULT_PERMISSION[] = new Permission[]{new Permission(Constants.DEFAULT_PERMISSION_RESOURCE,
"ui.execute")};
}

@ -31,49 +31,49 @@
<Permission>
<name>control bulb</name>
<path>/device-mgt/arduino/user</path>
<path>device-mgt/user/operations</path>
<url>/device/*/bulb</url>
<method>POST</method>
<scope>arduino_user</scope>
</Permission>
<Permission>
<name>get controls</name>
<path>/device-mgt/arduino/user</path>
<path>device-mgt/user/operations</path>
<url>/device/*/controls</url>
<method>POST</method>
<scope>arduino_device</scope>
</Permission>
<Permission>
<name>get temperature stats</name>
<path>/device-mgt/arduino/user</path>
<path>device-mgt/user/stats</path>
<url>/device/stats/*</url>
<method>GET</method>
<scope>arduino_device</scope>
</Permission>
<Permission>
<name>Get device</name>
<path>/device-mgt/arduino/user</path>
<path>/device-mgt/user/devices/list</path>
<url>/enrollment/devices/*</url>
<method>GET</method>
<scope>arduino_user</scope>
</Permission>
<Permission>
<name>Remove device</name>
<path>/device-mgt/arduino/user</path>
<path>/device-mgt/user/devices/remove</path>
<url>/enrollment/devices/*</url>
<method>DELETE</method>
<scope>arduino_user</scope>
</Permission>
<Permission>
<name>Download device</name>
<path>/device-mgt/user</path>
<path>/device-mgt/user/devices</path>
<url>/enrollment/devices/download</url>
<method>GET</method>
<scope>arduino_user</scope>
</Permission>
<Permission>
<name>Update device</name>
<path>/device-mgt/arduino/user</path>
<path>/device-mgt/user/devices/update</path>
<url>/enrollment/devices/*</url>
<method>PUT</method>
<scope>arduino_user</scope>

@ -31,7 +31,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.exception.ArduinoDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.dao.ArduinoDAO;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.dao.ArduinoDAOUtil;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.feature.ArduinoFeatureManager;
import java.util.List;
@ -41,7 +41,7 @@ import java.util.List;
*/
public class ArduinoManager implements DeviceManager {
private static final ArduinoDAO arduinoDAO = new ArduinoDAO();
private static final ArduinoDAOUtil arduinoDAO = new ArduinoDAOUtil();
private static final Log log = LogFactory.getLog(ArduinoManager.class);
private ArduinoFeatureManager arduinoFeatureManager = new ArduinoFeatureManager();
@ -70,12 +70,12 @@ public class ArduinoManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Enrolling a new Arduino device : " + device.getDeviceIdentifier());
}
ArduinoDAO.beginTransaction();
ArduinoDAOUtil.beginTransaction();
status = arduinoDAO.getDeviceDAO().addDevice(device);
ArduinoDAO.commitTransaction();
ArduinoDAOUtil.commitTransaction();
} catch (ArduinoDeviceMgtPluginException e) {
try {
ArduinoDAO.rollbackTransaction();
ArduinoDAOUtil.rollbackTransaction();
} catch (ArduinoDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
log.warn(msg, iotDAOEx);
@ -94,12 +94,12 @@ public class ArduinoManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Modifying the Arduino device enrollment data");
}
ArduinoDAO.beginTransaction();
ArduinoDAOUtil.beginTransaction();
status = arduinoDAO.getDeviceDAO().updateDevice(device);
ArduinoDAO.commitTransaction();
ArduinoDAOUtil.commitTransaction();
} catch (ArduinoDeviceMgtPluginException e) {
try {
ArduinoDAO.rollbackTransaction();
ArduinoDAOUtil.rollbackTransaction();
} catch (ArduinoDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
log.warn(msg, iotDAOEx);
@ -119,12 +119,12 @@ public class ArduinoManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Dis-enrolling Arduino device : " + deviceId);
}
ArduinoDAO.beginTransaction();
ArduinoDAOUtil.beginTransaction();
status = arduinoDAO.getDeviceDAO().deleteDevice(deviceId.getId());
ArduinoDAO.commitTransaction();
ArduinoDAOUtil.commitTransaction();
} catch (ArduinoDeviceMgtPluginException e) {
try {
ArduinoDAO.rollbackTransaction();
ArduinoDAOUtil.rollbackTransaction();
} catch (ArduinoDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
log.warn(msg, iotDAOEx);
@ -222,12 +222,12 @@ public class ArduinoManager implements DeviceManager {
log.debug(
"updating the details of Arduino device : " + deviceIdentifier);
}
ArduinoDAO.beginTransaction();
ArduinoDAOUtil.beginTransaction();
status = arduinoDAO.getDeviceDAO().updateDevice(device);
ArduinoDAO.commitTransaction();
ArduinoDAOUtil.commitTransaction();
} catch (ArduinoDeviceMgtPluginException e) {
try {
ArduinoDAO.rollbackTransaction();
ArduinoDAOUtil.rollbackTransaction();
} catch (ArduinoDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
log.warn(msg, iotDAOEx);

@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.constants.ArduinoConstants;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.exception.ArduinoDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.dao.impl.ArduinoDeviceDAOImpl;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@ -30,13 +30,13 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class ArduinoDAO {
public class ArduinoDAOUtil {
private static final Log log = LogFactory.getLog(ArduinoDAO.class);
private static final Log log = LogFactory.getLog(ArduinoDAOUtil.class);
static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public ArduinoDAO() {
public ArduinoDAOUtil() {
initArduinoDAO();
}
@ -49,8 +49,8 @@ public class ArduinoDAO {
}
}
public ArduinoDeviceDAOImpl getDeviceDAO() {
return new ArduinoDeviceDAOImpl();
public ArduinoDeviceDAO getDeviceDAO() {
return new ArduinoDeviceDAO();
}
public static void beginTransaction() throws ArduinoDeviceMgtPluginException {

@ -16,7 +16,7 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.dao.impl;
package org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -24,7 +24,7 @@ import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.exception.ArduinoDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.util.ArduinoUtils;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.constants.ArduinoConstants;
import org.wso2.carbon.device.mgt.iot.arduino.plugin.impl.dao.ArduinoDAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -35,10 +35,10 @@ import java.util.List;
/**
* Implements CRUD for arduino Devices.
*/
public class ArduinoDeviceDAOImpl {
public class ArduinoDeviceDAO {
private static final Log log = LogFactory.getLog(ArduinoDeviceDAOImpl.class);
private static final Log log = LogFactory.getLog(ArduinoDeviceDAO.class);
public Device getDevice(String deviceId) throws ArduinoDeviceMgtPluginException {
Connection conn = null;
@ -46,7 +46,7 @@ public class ArduinoDeviceDAOImpl {
Device device = null;
ResultSet resultSet = null;
try {
conn = ArduinoDAO.getConnection();
conn = ArduinoDAOUtil.getConnection();
String selectDBQuery =
"SELECT ARDUINO_DEVICE_ID, DEVICE_NAME FROM ARDUINO_DEVICE WHERE ARDUINO_DEVICE_ID = ?";
stmt = conn.prepareStatement(selectDBQuery);
@ -67,7 +67,7 @@ public class ArduinoDeviceDAOImpl {
throw new ArduinoDeviceMgtPluginException(msg, e);
} finally {
ArduinoUtils.cleanupResources(stmt, resultSet);
ArduinoDAO.closeConnection();
ArduinoDAOUtil.closeConnection();
}
return device;
}
@ -77,7 +77,7 @@ public class ArduinoDeviceDAOImpl {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = ArduinoDAO.getConnection();
conn = ArduinoDAOUtil.getConnection();
String createDBQuery =
"INSERT INTO ARDUINO_DEVICE(ARDUINO_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
@ -108,7 +108,7 @@ public class ArduinoDeviceDAOImpl {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = ArduinoDAO.getConnection();
conn = ArduinoDAOUtil.getConnection();
String updateDBQuery =
"UPDATE ARDUINO_DEVICE SET DEVICE_NAME = ? WHERE ARDUINO_DEVICE_ID = ?";
@ -139,7 +139,7 @@ public class ArduinoDeviceDAOImpl {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = ArduinoDAO.getConnection();
conn = ArduinoDAOUtil.getConnection();
String deleteDBQuery =
"DELETE FROM ARDUINO_DEVICE WHERE ARDUINO_DEVICE_ID = ?";
stmt = conn.prepareStatement(deleteDBQuery);
@ -170,7 +170,7 @@ public class ArduinoDeviceDAOImpl {
Device device;
List<Device> devices = new ArrayList<Device>();
try {
conn = ArduinoDAO.getConnection();
conn = ArduinoDAOUtil.getConnection();
String selectDBQuery =
"SELECT ARDUINO_DEVICE_ID, DEVICE_NAME FROM ARDUINO_DEVICE";
stmt = conn.prepareStatement(selectDBQuery);
@ -190,7 +190,7 @@ public class ArduinoDeviceDAOImpl {
throw new ArduinoDeviceMgtPluginException(msg, e);
} finally {
ArduinoUtils.cleanupResources(stmt, resultSet);
ArduinoDAO.closeConnection();
ArduinoDAOUtil.closeConnection();
}
}
}

@ -59,7 +59,7 @@
</Permission>
<Permission>
<name>Get Devices</name>
<path>/device-mgt/user/devices/devices</path>
<path>/device-mgt/user/devices/list</path>
<url>/enrollment/devices</url>
<method>GET</method>
<scope>raspberrypi_user</scope>
@ -73,7 +73,7 @@
</Permission>
<Permission>
<name>Get Temperature Data</name>
<path>/device-mgt/user/operations</path>
<path>/device-mgt/user/stats</path>
<url>/device/stats/*</url>
<method>GET</method>
<scope>raspberrypi_user</scope>

@ -30,7 +30,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.exception.RaspberrypiDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl.dao.RaspberrypiDAO;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl.dao.RaspberrypiDAOUtil;
import java.util.List;
/**
@ -38,7 +38,7 @@ import java.util.List;
*/
public class RaspberrypiManager implements DeviceManager {
private static final RaspberrypiDAO raspberrypiDAO = new RaspberrypiDAO();
private static final RaspberrypiDAOUtil raspberrypiDAO = new RaspberrypiDAOUtil();
private static final Log log = LogFactory.getLog(RaspberrypiManager.class);
@Override
@ -64,12 +64,12 @@ public class RaspberrypiManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Enrolling a new Raspberrypi device : " + device.getDeviceIdentifier());
}
RaspberrypiDAO.beginTransaction();
RaspberrypiDAOUtil.beginTransaction();
status = raspberrypiDAO.getDeviceDAO().addDevice(device);
RaspberrypiDAO.commitTransaction();
RaspberrypiDAOUtil.commitTransaction();
} catch (RaspberrypiDeviceMgtPluginException e) {
try {
RaspberrypiDAO.rollbackTransaction();
RaspberrypiDAOUtil.rollbackTransaction();
} catch (RaspberrypiDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
log.warn(msg, iotDAOEx);
@ -88,12 +88,12 @@ public class RaspberrypiManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Modifying the Raspberrypi device enrollment data");
}
RaspberrypiDAO.beginTransaction();
RaspberrypiDAOUtil.beginTransaction();
status = raspberrypiDAO.getDeviceDAO().updateDevice(device);
RaspberrypiDAO.commitTransaction();
RaspberrypiDAOUtil.commitTransaction();
} catch (RaspberrypiDeviceMgtPluginException e) {
try {
RaspberrypiDAO.rollbackTransaction();
RaspberrypiDAOUtil.rollbackTransaction();
} catch (RaspberrypiDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
log.warn(msg, iotDAOEx);
@ -113,12 +113,12 @@ public class RaspberrypiManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Dis-enrolling Raspberrypi device : " + deviceId);
}
RaspberrypiDAO.beginTransaction();
RaspberrypiDAOUtil.beginTransaction();
status = raspberrypiDAO.getDeviceDAO().deleteDevice(deviceId.getId());
RaspberrypiDAO.commitTransaction();
RaspberrypiDAOUtil.commitTransaction();
} catch (RaspberrypiDeviceMgtPluginException e) {
try {
RaspberrypiDAO.rollbackTransaction();
RaspberrypiDAOUtil.rollbackTransaction();
} catch (RaspberrypiDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
log.warn(msg, iotDAOEx);
@ -216,12 +216,12 @@ public class RaspberrypiManager implements DeviceManager {
log.debug(
"updating the details of Raspberrypi device : " + deviceIdentifier);
}
RaspberrypiDAO.beginTransaction();
RaspberrypiDAOUtil.beginTransaction();
status = raspberrypiDAO.getDeviceDAO().updateDevice(device);
RaspberrypiDAO.commitTransaction();
RaspberrypiDAOUtil.commitTransaction();
} catch (RaspberrypiDeviceMgtPluginException e) {
try {
RaspberrypiDAO.rollbackTransaction();
RaspberrypiDAOUtil.rollbackTransaction();
} catch (RaspberrypiDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
log.warn(msg, iotDAOEx);

@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.exception.RaspberrypiDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl.dao.impl.RaspberrypiDeviceDAOImpl;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@ -30,18 +30,18 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class RaspberrypiDAO {
public class RaspberrypiDAOUtil {
private static final Log log = LogFactory.getLog(RaspberrypiDAO.class);
private static final Log log = LogFactory.getLog(RaspberrypiDAOUtil.class);
static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public RaspberrypiDAO() {
public RaspberrypiDAOUtil() {
initRaspberrypiDAO();
}
public RaspberrypiDeviceDAOImpl getDeviceDAO() {
return new RaspberrypiDeviceDAOImpl();
public RaspberrypiDeviceDAO getDeviceDAO() {
return new RaspberrypiDeviceDAO();
}
public static void initRaspberrypiDAO() {

@ -16,14 +16,13 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl.dao.impl;
package org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.exception.RaspberrypiDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl.util.RaspberrypiUtils;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.impl.dao.RaspberrypiDAO;
import org.wso2.carbon.device.mgt.iot.raspberrypi.plugin.constants.RaspberrypiConstants;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -35,9 +34,9 @@ import java.util.List;
/**
* Implements CRUD for Raspberrypi Devices.
*/
public class RaspberrypiDeviceDAOImpl {
public class RaspberrypiDeviceDAO {
private static final Log log = LogFactory.getLog(RaspberrypiDeviceDAOImpl.class);
private static final Log log = LogFactory.getLog(RaspberrypiDeviceDAO.class);
public Device getDevice(String iotDeviceId) throws RaspberrypiDeviceMgtPluginException {
Connection conn;
@ -45,7 +44,7 @@ public class RaspberrypiDeviceDAOImpl {
Device device = null;
ResultSet resultSet = null;
try {
conn = RaspberrypiDAO.getConnection();
conn = RaspberrypiDAOUtil.getConnection();
String selectDBQuery =
"SELECT RASPBERRYPI_DEVICE_ID, DEVICE_NAME FROM RASPBERRYPI_DEVICE WHERE RASPBERRYPI_DEVICE_ID = ?";
stmt = conn.prepareStatement(selectDBQuery);
@ -66,7 +65,7 @@ public class RaspberrypiDeviceDAOImpl {
throw new RaspberrypiDeviceMgtPluginException(msg, e);
} finally {
RaspberrypiUtils.cleanupResources(stmt, resultSet);
RaspberrypiDAO.closeConnection();
RaspberrypiDAOUtil.closeConnection();
}
return device;
}
@ -76,7 +75,7 @@ public class RaspberrypiDeviceDAOImpl {
Connection conn;
PreparedStatement stmt = null;
try {
conn = RaspberrypiDAO.getConnection();
conn = RaspberrypiDAOUtil.getConnection();
String createDBQuery =
"INSERT INTO RASPBERRYPI_DEVICE(RASPBERRYPI_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
@ -107,7 +106,7 @@ public class RaspberrypiDeviceDAOImpl {
Connection conn;
PreparedStatement stmt = null;
try {
conn = RaspberrypiDAO.getConnection();
conn = RaspberrypiDAOUtil.getConnection();
String updateDBQuery = "UPDATE RASPBERRYPI_DEVICE SET DEVICE_NAME = ? WHERE RASPBERRYPI_DEVICE_ID = ?";
stmt = conn.prepareStatement(updateDBQuery);
stmt.setString(1, device.getName());
@ -136,7 +135,7 @@ public class RaspberrypiDeviceDAOImpl {
Connection conn;
PreparedStatement stmt = null;
try {
conn = RaspberrypiDAO.getConnection();
conn = RaspberrypiDAOUtil.getConnection();
String deleteDBQuery = "DELETE FROM RASPBERRYPI_DEVICE WHERE RASPBERRYPI_DEVICE_ID = ?";
stmt = conn.prepareStatement(deleteDBQuery);
stmt.setString(1, iotDeviceId);
@ -165,7 +164,7 @@ public class RaspberrypiDeviceDAOImpl {
Device device;
List<Device> devices = new ArrayList<>();
try {
conn = RaspberrypiDAO.getConnection();
conn = RaspberrypiDAOUtil.getConnection();
String selectDBQuery = "SELECT RASPBERRYPI_DEVICE_ID, DEVICE_NAME FROM RASPBERRYPI_DEVICE";
stmt = conn.prepareStatement(selectDBQuery);
resultSet = stmt.executeQuery();
@ -185,7 +184,7 @@ public class RaspberrypiDeviceDAOImpl {
throw new RaspberrypiDeviceMgtPluginException(msg, e);
} finally {
RaspberrypiUtils.cleanupResources(stmt, resultSet);
RaspberrypiDAO.closeConnection();
RaspberrypiDAOUtil.closeConnection();
}
}
}

@ -160,47 +160,6 @@ public class APIUtil {
return threadLocalCarbonContext.getTenantDomain();
}
public static UserStoreManager getUserStoreManager() {
RealmService realmService;
UserStoreManager userStoreManager;
try {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
realmService = (RealmService) ctx.getOSGiService(RealmService.class, null);
if (realmService == null) {
String msg = "Realm service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
int tenantId = ctx.getTenantId();
userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager();
} catch (UserStoreException e) {
String msg = "Error occurred while retrieving current user store manager";
log.error(msg, e);
throw new IllegalStateException(msg);
}
return userStoreManager;
}
public static void registerApiAccessRoles(String user) {
UserStoreManager userStoreManager = null;
try {
userStoreManager = getUserStoreManager();
String[] userList = new String[]{user};
if (userStoreManager != null) {
String rolesOfUser[] = userStoreManager.getRoleListOfUser(user);
if (!userStoreManager.isExistingRole(Constants.DEFAULT_ROLE_NAME)) {
userStoreManager.addRole(Constants.DEFAULT_ROLE_NAME, userList, Constants.DEFAULT_PERMISSION);
} else if (rolesOfUser != null && Arrays.asList(rolesOfUser).contains(Constants.DEFAULT_ROLE_NAME)) {
return;
} else {
userStoreManager.updateUserListOfRole(Constants.DEFAULT_ROLE_NAME, new String[0], userList);
}
}
} catch (UserStoreException e) {
log.error("Error while creating a role and adding a user for virtual_firealarm.", e);
}
}
public static DeviceAccessAuthorizationService getDeviceAccessAuthorizationService() {
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
DeviceAccessAuthorizationService deviceAccessAuthorizationService =

@ -1,32 +0,0 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util;
import org.wso2.carbon.user.core.Permission;
/**
* This hold the constants related to the device type.
*/
public class Constants {
public static final String DEFAULT_PERMISSION_RESOURCE = "/permission/admin/device-mgt/virtual_firealarm/user";
public static final String DEFAULT_ROLE_NAME = "virtual_firealarm_user";
public static final Permission DEFAULT_PERMISSION[] = new Permission[]{new Permission(Constants.DEFAULT_PERMISSION_RESOURCE,
"ui.execute")};
}

@ -30,14 +30,14 @@
<!-- Device related APIs -->
<Permission>
<name>Get device</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/devices/list</path>
<url>/enrollment/devices/*</url>
<method>GET</method>
<scope>virtual_firealarm_user</scope>
</Permission>
<Permission>
<name>Remove device</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/devices/remove</path>
<url>/enrollment/devices/*</url>
<method>DELETE</method>
<scope>virtual_firealarm_user</scope>
@ -51,42 +51,42 @@
</Permission>
<Permission>
<name>Update device</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/devices/update</path>
<url>/enrollment/devices/*</url>
<method>POST</method>
<scope>virtual_firealarm_user</scope>
</Permission>
<Permission>
<name>Get Devices</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/devices/list</path>
<url>/enrollment/devices</url>
<method>GET</method>
<scope>virtual_firealarm_user</scope>
</Permission>
<Permission>
<name>Register Device</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/operations</path>
<url>/device/register/*/*/*</url>
<method>POST</method>
<scope>virtual_firealarm_device</scope>
</Permission>
<Permission>
<name>Control Buzz</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/operations</path>
<url>/device/*/buzz</url>
<method>POST</method>
<scope>virtual_firealarm_user</scope>
</Permission>
<Permission>
<name>Push Temperature</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/stats</path>
<url>/device/temperature</url>
<method>POST</method>
<scope>virtual_firealarm_device</scope>
</Permission>
<Permission>
<name>Get Stats</name>
<path>/device-mgt/virtual_firealarm/user</path>
<path>/device-mgt/user/stats</path>
<url>/device/stats/*</url>
<method>GET</method>
<scope>virtual_firealarm_device</scope>

@ -30,7 +30,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
import org.wso2.carbon.device.mgt.common.license.mgt.License;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.dao.VirtualFireAlarmDAO;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.dao.VirtualFireAlarmDAOUtil;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.feature.VirtualFirealarmFeatureManager;
import java.util.List;
@ -40,7 +40,7 @@ import java.util.List;
*/
public class VirtualFireAlarmManager implements DeviceManager {
private static final VirtualFireAlarmDAO virtualFireAlarmDAO = new VirtualFireAlarmDAO();
private static final VirtualFireAlarmDAOUtil virtualFireAlarmDAO = new VirtualFireAlarmDAOUtil();
private static final Log log = LogFactory.getLog(VirtualFireAlarmManager.class);
private FeatureManager virtualFirealarmFeatureManager = new VirtualFirealarmFeatureManager();
@ -67,12 +67,12 @@ public class VirtualFireAlarmManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Enrolling a new Virtual Firealarm device : " + device.getDeviceIdentifier());
}
VirtualFireAlarmDAO.beginTransaction();
VirtualFireAlarmDAOUtil.beginTransaction();
status = virtualFireAlarmDAO.getDeviceDAO().addDevice(device);
VirtualFireAlarmDAO.commitTransaction();
VirtualFireAlarmDAOUtil.commitTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException e) {
try {
VirtualFireAlarmDAO.rollbackTransaction();
VirtualFireAlarmDAOUtil.rollbackTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
log.warn(msg, iotDAOEx);
@ -91,12 +91,12 @@ public class VirtualFireAlarmManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Modifying the Virtual Firealarm device enrollment data");
}
VirtualFireAlarmDAO.beginTransaction();
VirtualFireAlarmDAOUtil.beginTransaction();
status = virtualFireAlarmDAO.getDeviceDAO().updateDevice(device);
VirtualFireAlarmDAO.commitTransaction();
VirtualFireAlarmDAOUtil.commitTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException e) {
try {
VirtualFireAlarmDAO.rollbackTransaction();
VirtualFireAlarmDAOUtil.rollbackTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
log.warn(msg, iotDAOEx);
@ -116,12 +116,12 @@ public class VirtualFireAlarmManager implements DeviceManager {
if (log.isDebugEnabled()) {
log.debug("Dis-enrolling Virtual Firealarm device : " + deviceId);
}
VirtualFireAlarmDAO.beginTransaction();
VirtualFireAlarmDAOUtil.beginTransaction();
status = virtualFireAlarmDAO.getDeviceDAO().deleteDevice(deviceId.getId());
VirtualFireAlarmDAO.commitTransaction();
VirtualFireAlarmDAOUtil.commitTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException e) {
try {
VirtualFireAlarmDAO.rollbackTransaction();
VirtualFireAlarmDAOUtil.rollbackTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
log.warn(msg, iotDAOEx);
@ -219,12 +219,12 @@ public class VirtualFireAlarmManager implements DeviceManager {
log.debug(
"updating the details of Virtual Firealarm device : " + deviceIdentifier);
}
VirtualFireAlarmDAO.beginTransaction();
VirtualFireAlarmDAOUtil.beginTransaction();
status = virtualFireAlarmDAO.getDeviceDAO().updateDevice(device);
VirtualFireAlarmDAO.commitTransaction();
VirtualFireAlarmDAOUtil.commitTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException e) {
try {
VirtualFireAlarmDAO.rollbackTransaction();
VirtualFireAlarmDAOUtil.rollbackTransaction();
} catch (VirtualFirealarmDeviceMgtPluginException iotDAOEx) {
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
log.warn(msg, iotDAOEx);

@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.dao.impl.VirtualFireAlarmDeviceDAOImpl;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@ -30,13 +30,13 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class VirtualFireAlarmDAO {
public class VirtualFireAlarmDAOUtil {
private static final Log log = LogFactory.getLog(VirtualFireAlarmDAO.class);
private static final Log log = LogFactory.getLog(VirtualFireAlarmDAOUtil.class);
static DataSource dataSource;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
public VirtualFireAlarmDAO() {
public VirtualFireAlarmDAOUtil() {
initFireAlarmDAO();
}
@ -49,8 +49,8 @@ public class VirtualFireAlarmDAO {
}
}
public VirtualFireAlarmDeviceDAOImpl getDeviceDAO() {
return new VirtualFireAlarmDeviceDAOImpl();
public VirtualFireAlarmDeviceDAO getDeviceDAO() {
return new VirtualFireAlarmDeviceDAO();
}
public static void beginTransaction() throws VirtualFirealarmDeviceMgtPluginException {

@ -16,14 +16,13 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.dao.impl;
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.constants.VirtualFireAlarmConstants;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.exception.VirtualFirealarmDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.dao.VirtualFireAlarmDAO;
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util.VirtualFireAlarmUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -35,9 +34,9 @@ import java.util.List;
/**
* Implements CRUD for virtual firealarm Devices.
*/
public class VirtualFireAlarmDeviceDAOImpl {
public class VirtualFireAlarmDeviceDAO {
private static final Log log = LogFactory.getLog(VirtualFireAlarmDeviceDAOImpl.class);
private static final Log log = LogFactory.getLog(VirtualFireAlarmDeviceDAO.class);
public Device getDevice(String deviceId) throws VirtualFirealarmDeviceMgtPluginException {
Connection conn = null;
@ -45,7 +44,7 @@ public class VirtualFireAlarmDeviceDAOImpl {
Device device = null;
ResultSet resultSet = null;
try {
conn = VirtualFireAlarmDAO.getConnection();
conn = VirtualFireAlarmDAOUtil.getConnection();
String selectDBQuery =
"SELECT VIRTUAL_FIREALARM_DEVICE_ID, DEVICE_NAME" +
" FROM VIRTUAL_FIREALARM_DEVICE WHERE VIRTUAL_FIREALARM_DEVICE_ID = ?";
@ -67,7 +66,7 @@ public class VirtualFireAlarmDeviceDAOImpl {
throw new VirtualFirealarmDeviceMgtPluginException(msg, e);
} finally {
VirtualFireAlarmUtils.cleanupResources(stmt, resultSet);
VirtualFireAlarmDAO.closeConnection();
VirtualFireAlarmDAOUtil.closeConnection();
}
return device;
@ -78,7 +77,7 @@ public class VirtualFireAlarmDeviceDAOImpl {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = VirtualFireAlarmDAO.getConnection();
conn = VirtualFireAlarmDAOUtil.getConnection();
String createDBQuery =
"INSERT INTO VIRTUAL_FIREALARM_DEVICE(VIRTUAL_FIREALARM_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)";
@ -109,7 +108,7 @@ public class VirtualFireAlarmDeviceDAOImpl {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = VirtualFireAlarmDAO.getConnection();
conn = VirtualFireAlarmDAOUtil.getConnection();
String updateDBQuery =
"UPDATE VIRTUAL_FIREALARM_DEVICE SET DEVICE_NAME = ? WHERE VIRTUAL_FIREALARM_DEVICE_ID = ?";
@ -140,7 +139,7 @@ public class VirtualFireAlarmDeviceDAOImpl {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = VirtualFireAlarmDAO.getConnection();
conn = VirtualFireAlarmDAOUtil.getConnection();
String deleteDBQuery = "DELETE FROM VIRTUAL_FIREALARM_DEVICE WHERE VIRTUAL_FIREALARM_DEVICE_ID = ?";
stmt = conn.prepareStatement(deleteDBQuery);
stmt.setString(1, iotDeviceId);
@ -169,7 +168,7 @@ public class VirtualFireAlarmDeviceDAOImpl {
Device device;
List<Device> devices = new ArrayList<>();
try {
conn = VirtualFireAlarmDAO.getConnection();
conn = VirtualFireAlarmDAOUtil.getConnection();
String selectDBQuery =
"SELECT VIRTUAL_FIREALARM_DEVICE_ID, DEVICE_NAME FROM VIRTUAL_FIREALARM_DEVICE";
stmt = conn.prepareStatement(selectDBQuery);
@ -190,7 +189,7 @@ public class VirtualFireAlarmDeviceDAOImpl {
throw new VirtualFirealarmDeviceMgtPluginException(msg, e);
} finally {
VirtualFireAlarmUtils.cleanupResources(stmt, resultSet);
VirtualFireAlarmDAO.closeConnection();
VirtualFireAlarmDAOUtil.closeConnection();
}
}
}
Loading…
Cancel
Save