Merge branch 'device-type-extension' into 'master'

Re-usability of Device Type Plugin DAO Manager

See merge request entgra/carbon-device-mgt!196
revert-70aa11f8
Dharmakeerthi Lasantha 5 years ago
commit 9f7addebef

@ -34,6 +34,7 @@
*/
package org.wso2.carbon.device.mgt.extensions.device.type.template;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
@ -58,10 +59,12 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceDAOD
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeDeployerPayloadException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.feature.ConfigurationBasedFeatureManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypePluginConstants;
import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypeUtils;
import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager;
import org.wso2.carbon.device.mgt.extensions.spi.DeviceTypePluginExtensionService;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.utils.CarbonUtils;
@ -213,6 +216,35 @@ public class DeviceTypeManager implements DeviceManager {
}
}
}
setDeviceTypePluginManager();
}
/**
* Set device type plugin DAO manager of each device type in a HashMap which can then be used via individual
* device type plugin in working with its DAO components
*/
private void setDeviceTypePluginManager() {
if (StringUtils.isNotEmpty(deviceType)) {
if (deviceTypePluginDAOManager != null) {
DeviceTypePluginExtensionService deviceTypeManagerExtensionService =
new DeviceTypePluginExtensionServiceImpl();
try {
deviceTypeManagerExtensionService.addPluginDAOManager(deviceType, deviceTypePluginDAOManager);
} catch (DeviceTypePluginExtensionException e) {
String msg = "Error occurred while saving DeviceTypePluginDAOManager for device type: "
+ deviceType;
log.error(msg);
throw new DeviceTypeDeployerPayloadException(msg);
}
} else {
log.warn("Could not save DeviceTypePluginDAOManager for device type: " + deviceType +
" since DeviceTypePluginDAOManager is null.");
}
} else {
String msg = "Could not save DeviceTypePluginDAOManager since device type is null or empty.";
log.error(msg);
throw new DeviceTypeDeployerPayloadException(msg);
}
}
@Override
@ -307,15 +339,11 @@ public class DeviceTypeManager implements DeviceManager {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
}
} catch (DeviceTypeMgtPluginException e) {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
} catch (DeviceTypeMgtPluginException ex) {
String msg = "Error occurred while roll back the device enrol transaction :" +
device.toString();
log.warn(msg, ex);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
String msg = "Error while enrolling the " + deviceType + " device : " + device.getDeviceIdentifier();
throw new DeviceManagementException(msg, e);
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
return status;
}
@ -334,16 +362,12 @@ public class DeviceTypeManager implements DeviceManager {
status = deviceTypePluginDAOManager.getDeviceDAO().updateDevice(device);
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
} catch (DeviceTypeMgtPluginException e) {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
} catch (DeviceTypeMgtPluginException mobileDAOEx) {
String msg = "Error occurred while roll back the update device transaction :" +
device.toString();
log.warn(msg, mobileDAOEx);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
String msg = "Error while updating the enrollment of the " + deviceType + " device : " +
device.getDeviceIdentifier();
throw new DeviceManagementException(msg, e);
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
return status;
}
@ -378,13 +402,7 @@ public class DeviceTypeManager implements DeviceManager {
deviceId.getId();
throw new DeviceManagementException(msg, e);
} finally {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
} catch (DeviceTypeMgtPluginException e) {
String msg = "Error occurred while closing the transaction to check device " +
deviceId.getId() + " is enrolled.";
log.warn(msg, e);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
return isEnrolled;
}
@ -419,12 +437,7 @@ public class DeviceTypeManager implements DeviceManager {
throw new DeviceManagementException(
"Error occurred while fetching the " + deviceType + " device: '" + deviceId.getId() + "'", e);
} finally {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
} catch (DeviceTypeMgtPluginException e) {
String msg = "Error occurred while closing the transaction to get device " + deviceId.getId();
log.warn(msg, e);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
return device;
}
@ -447,14 +460,11 @@ public class DeviceTypeManager implements DeviceManager {
status = deviceTypePluginDAOManager.getDeviceDAO().updateDevice(updatedDevice);
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
} catch (DeviceTypeMgtPluginException e) {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
} catch (DeviceTypeMgtPluginException transactionException) {
String msg = "Error occurred while rolling back transaction for device: " + deviceId.getId();
log.warn(msg, transactionException);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
throw new DeviceManagementException(
"Error occurred while fetching the " + deviceType + " device: '" + deviceId.getId() + "'", e);
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
}
return status;
@ -544,15 +554,12 @@ public class DeviceTypeManager implements DeviceManager {
status = deviceTypePluginDAOManager.getDeviceDAO().updateDevice(existingDevice);
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
} catch (DeviceTypeMgtPluginException e) {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
} catch (DeviceTypeMgtPluginException e1) {
log.warn("Error occurred while roll back the update device info transaction : '" +
device.toString() + "'", e1);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
throw new DeviceManagementException(
"Error occurred while updating the " + deviceType + " device: '" +
device.getDeviceIdentifier() + "'", e);
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
return status;
}
@ -572,12 +579,7 @@ public class DeviceTypeManager implements DeviceManager {
} catch (DeviceTypeMgtPluginException e) {
throw new DeviceManagementException("Error occurred while fetching all " + deviceType + " devices", e);
} finally {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
} catch (DeviceTypeMgtPluginException e) {
String msg = "Error occurred while closing the transaction to get all devices.";
log.warn(msg, e);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
return devices;
}
@ -600,15 +602,12 @@ public class DeviceTypeManager implements DeviceManager {
status = deviceTypePluginDAOManager.getDeviceDAO().deleteDevice(existingDevice);
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().commitTransaction();
} catch (DeviceTypeMgtPluginException e) {
try {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
} catch (DeviceTypeMgtPluginException e1) {
log.warn("Error occurred while roll back the delete device info transaction : '" +
device.toString() + "'", e1);
}
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().rollbackTransaction();
throw new DeviceManagementException(
"Error occurred while deleting the " + deviceType + " device: '" +
device.getDeviceIdentifier() + "'", e);
} finally {
deviceTypePluginDAOManager.getDeviceTypeDAOHandler().closeConnection();
}
return status;
}

@ -0,0 +1,71 @@
/*
* Copyright (c) 2019, Entgra (Pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.extensions.device.type.template;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException;
import org.wso2.carbon.device.mgt.extensions.spi.DeviceTypePluginExtensionService;
import java.util.HashMap;
import java.util.Map;
public class DeviceTypePluginExtensionServiceImpl implements DeviceTypePluginExtensionService {
private static final Log log = LogFactory.getLog(DeviceTypePluginExtensionServiceImpl.class);
private static volatile Map<String, DeviceTypePluginDAOManager> pluginDAOManagers = new HashMap<>();
@Override
public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager)
throws DeviceTypePluginExtensionException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (pluginDAOManager == null) {
String msg = "Cannot save DeviceTypePluginDAOManager against tenant id " + tenantId
+ " and device type: " + deviceType + " since DeviceTypePluginDAOManager is null";
log.error(msg);
throw new DeviceTypePluginExtensionException(msg);
}
if (!pluginDAOManagers.containsKey(tenantId + deviceType)) {
if (log.isDebugEnabled()) {
log.debug("Saving DeviceTypePluginDAOManager against tenant id " + tenantId +
" and device type: " + deviceType);
}
pluginDAOManagers.put(tenantId + deviceType, pluginDAOManager);
}
}
@Override
public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) throws DeviceTypePluginExtensionException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (pluginDAOManagers.containsKey(tenantId + deviceType)) {
if (log.isDebugEnabled()) {
log.debug("Retrieving DeviceTypePluginDAOManager against tenant id " + tenantId +
" and device type: " + deviceType);
}
return pluginDAOManagers.get(tenantId + deviceType);
} else {
String msg = "DeviceTypePluginDAOManager could not be found against tenant id " + tenantId +
" and device type: " + deviceType;
log.error(msg);
throw new DeviceTypePluginExtensionException(msg);
}
}
}

@ -1,7 +1,25 @@
/*
* Copyright (c) 2019, Entgra (Pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.extensions.device.type.template.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeDeployerPayloadException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException;
@ -31,7 +49,26 @@ public class DeviceTypeDAOHandler {
Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(datasourceName);
} catch (NamingException e) {
throw new DeviceTypeDeployerPayloadException("Error while looking up the data source: " + datasourceName, e);
String msg = "Error while looking up the data source: " + datasourceName;
log.error(msg, e);
throw new DeviceTypeDeployerPayloadException(msg, e);
}
}
public void openConnection() throws DeviceTypeMgtPluginException {
try {
Connection conn = currentConnection.get();
if (conn != null) {
String msg = "Database connection has already been obtained.";
log.error(msg);
throw new IllegalTransactionStateException(msg);
}
conn = dataSource.getConnection();
currentConnection.set(conn);
} catch (SQLException e) {
String msg = "Failed to get a database connection.";
log.error(msg, e);
throw new DeviceTypeMgtPluginException(msg, e);
}
}
@ -41,7 +78,9 @@ public class DeviceTypeDAOHandler {
conn.setAutoCommit(false);
currentConnection.set(conn);
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while retrieving datasource connection", e);
String msg = "Error occurred while retrieving datasource connection";
log.error(msg, e);
throw new DeviceTypeMgtPluginException(msg, e);
}
}
@ -50,58 +89,58 @@ public class DeviceTypeDAOHandler {
try {
currentConnection.set(dataSource.getConnection());
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while retrieving data source connection", e);
String msg = "Error occurred while retrieving data source connection";
log.error(msg, e);
throw new DeviceTypeMgtPluginException(msg, e);
}
}
return currentConnection.get();
}
public void commitTransaction() throws DeviceTypeMgtPluginException {
public void commitTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
String msg = "No connection is associated with the current transaction. This might have ideally been " +
"caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods";
log.error(msg);
throw new IllegalStateException(msg);
}
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.commit();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence commit "
+ "has not been attempted");
}
}
conn.commit();
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while committing the transaction", e);
} finally {
closeConnection();
String msg = "Error occurred while committing the transaction.";
log.error(msg, e);
}
}
public void closeConnection() throws DeviceTypeMgtPluginException {
public void closeConnection() {
Connection con = currentConnection.get();
if (con != null) {
try {
con.close();
} catch (SQLException e) {
log.error("Error occurred while close the connection");
String msg = "Error occurred while close the connection";
log.error(msg, e);
}
}
currentConnection.remove();
}
public void rollbackTransaction() throws DeviceTypeMgtPluginException {
public void rollbackTransaction() {
Connection conn = currentConnection.get();
if (conn == null) {
String msg = "No connection is associated with the current transaction. This might have ideally been " +
"caused by not properly initiating the transaction via " +
"'beginTransaction'/'openConnection' methods";
log.error(msg);
throw new IllegalStateException(msg);
}
try {
Connection conn = currentConnection.get();
if (conn != null) {
conn.rollback();
} else {
if (log.isDebugEnabled()) {
log.debug("Datasource connection associated with the current thread is null, hence rollback "
+ "has not been attempted");
}
}
conn.rollback();
} catch (SQLException e) {
throw new DeviceTypeMgtPluginException("Error occurred while rollback the transaction", e);
} finally {
closeConnection();
String msg = "Error occurred while roll-backing the transaction.";
log.error(msg, e);
}
}
}

@ -0,0 +1,12 @@
package org.wso2.carbon.device.mgt.extensions.device.type.template.exception;
public class DeviceTypePluginExtensionException extends Exception {
public DeviceTypePluginExtensionException(String msg) {
super(msg);
}
public DeviceTypePluginExtensionException(String msg, Throwable cause) {
super(msg, cause);
}
}

@ -14,6 +14,23 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*
* Copyright (c) 2019, Entgra (Pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.extensions.internal;
@ -23,6 +40,8 @@ import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService;
import org.wso2.carbon.device.mgt.extensions.device.type.template.DeviceTypeGeneratorServiceImpl;
import org.wso2.carbon.device.mgt.extensions.device.type.template.DeviceTypePluginExtensionServiceImpl;
import org.wso2.carbon.device.mgt.extensions.spi.DeviceTypePluginExtensionService;
import org.wso2.carbon.ndatasource.core.DataSourceService;
import org.wso2.carbon.registry.core.service.RegistryService;
@ -50,6 +69,8 @@ public class DeviceTypeExtensionServiceComponent {
}
ctx.getBundleContext()
.registerService(DeviceTypeGeneratorService.class, new DeviceTypeGeneratorServiceImpl(), null);
ctx.getBundleContext().registerService(DeviceTypePluginExtensionService.class,
new DeviceTypePluginExtensionServiceImpl(), null);
if (log.isDebugEnabled()) {
log.debug("Device Type Extension Service Component successfully activated");
}

@ -0,0 +1,45 @@
/*
* Copyright (c) 2019, Entgra (Pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.extensions.spi;
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException;
/**
* This represents the device type plugin extension service which can be used by any device type plugin implementation
* intended to use the same plugin DAO instances to be used with its plugin level DAO components
*/
public interface DeviceTypePluginExtensionService {
/**
* Save device type specific DeviceTypePluginDAOManager in a HashMap againast tenant ID and device type
* @param deviceType - Type of the device (i.e; android, ios, windows)
* @param pluginDAOManager - Device type plugin DAO manager instance to be saved against device type
* @throws DeviceTypePluginExtensionException when pluginDAOManager is null
*/
void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager)
throws DeviceTypePluginExtensionException;
/**
* Retrieve the DeviceTypePluginDAOManager instance against tenant ID and given device type
* @param deviceType - Type of the device (i.e; android, ios, windows)
* @return an Instance of {@link DeviceTypePluginDAOManager}
* @throws DeviceTypePluginExtensionException when pluginDAOManager cannot be found
*/
DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) throws DeviceTypePluginExtensionException;
}
Loading…
Cancel
Save