Merge pull request #8 from manojgunayadev/master

Licence Management Refactor
revert-70aa11f8
Prabath Abeysekara 10 years ago
commit 22f1f5d9b4

@ -18,35 +18,68 @@ package org.wso2.carbon.device.mgt.common;
public final class DeviceManagementConstants { public final class DeviceManagementConstants {
public static final class DataSourceProperties { public static final class DataSourceProperties {
private DataSourceProperties() { private DataSourceProperties() {
throw new AssertionError(); throw new AssertionError();
} }
public static final String DB_CHECK_QUERY = "SELECT * FROM DM_DEVICE"; public static final String DB_CHECK_QUERY = "SELECT * FROM DM_DEVICE";
public static final String SECURE_VAULT_NS = "http://org.wso2.securevault/configuration"; public static final String SECURE_VAULT_NS = "http://org.wso2.securevault/configuration";
public static final String DEVICE_CONFIG_XML_NAME = "cdm-config.xml"; public static final String DEVICE_CONFIG_XML_NAME = "cdm-config.xml";
} }
public static final class SecureValueProperties { public static final class SecureValueProperties {
private SecureValueProperties() { private SecureValueProperties() {
throw new AssertionError(); throw new AssertionError();
} }
public static final String SECRET_ALIAS_ATTRIBUTE_NAME_WITH_NAMESPACE = "secretAlias"; public static final String SECRET_ALIAS_ATTRIBUTE_NAME_WITH_NAMESPACE = "secretAlias";
public static final String SECURE_VAULT_NS = "http://org.wso2.securevault/configuration"; public static final String SECURE_VAULT_NS = "http://org.wso2.securevault/configuration";
} }
public static final class MobileDeviceTypes { public static final class MobileDeviceTypes {
private MobileDeviceTypes() { private MobileDeviceTypes() {
throw new AssertionError(); throw new AssertionError();
} }
public final static String MOBILE_DEVICE_TYPE_ANDROID = "android"; public final static String MOBILE_DEVICE_TYPE_ANDROID = "android";
public final static String MOBILE_DEVICE_TYPE_IOS = "ios"; public final static String MOBILE_DEVICE_TYPE_IOS = "ios";
public final static String MOBILE_DEVICE_TYPE_WINDOWS = "windows"; public final static String MOBILE_DEVICE_TYPE_WINDOWS = "windows";
} }
public static final class LanguageCodes{ public static final class LanguageCodes {
private LanguageCodes() { throw new AssertionError();}
private LanguageCodes() {
throw new AssertionError();
}
public final static String LANGUAGE_CODE_ENGLISH_US = "en-us"; public final static String LANGUAGE_CODE_ENGLISH_US = "en-us";
public final static String LANGUAGE_CODE_ENGLISH_UK = "en-uk"; public final static String LANGUAGE_CODE_ENGLISH_UK = "en-uk";
} }
public static final class DefaultLicences {
private DefaultLicences() {
throw new AssertionError();
}
public static final String DEFAULT_LICENSE_CONFIG_XML_NAME = "DefaultLicense.xml";
}
public static final class LicenseProperties {
private LicenseProperties() {
throw new AssertionError();
}
public static final String OVERVIEW_PROVIDER = "overview_provider";
public static final String OVERVIEW_NAME = "overview_name";
public static final String OVERVIEW_LANGUAGE = "overview_language";
public static final String OVERVIEW_VERSION = "overview_version";
public static final String VALID_FROM = "overview_validityFrom";
public static final String VALID_TO = "overview_validityTo";
public static final String LICENSE = "overview_license";
}
} }

@ -55,14 +55,12 @@ public class DeviceManagerImpl implements DeviceManager {
public boolean enrollDevice(Device device) throws DeviceManagementException { public boolean enrollDevice(Device device) throws DeviceManagementException {
DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType()); DeviceManagerService dms = this.getPluginRepository().getDeviceManagementProvider(device.getType());
boolean status = dms.enrollDevice(device); boolean status = dms.enrollDevice(device);
try { try {
org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device); org.wso2.carbon.device.mgt.core.dto.Device deviceDto = DeviceManagementDAOUtil.convertDevice(device);
Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType()); Integer deviceTypeId = this.getDeviceTypeDAO().getDeviceTypeIdByDeviceTypeName(device.getType());
deviceDto.setStatus(Status.ACTIVE); deviceDto.setStatus(Status.ACTIVE);
deviceDto.setDeviceTypeId(deviceTypeId); deviceDto.setDeviceTypeId(deviceTypeId);
this.getDeviceDAO().addDevice(deviceDto); this.getDeviceDAO().addDevice(deviceDto);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementDAOException e) {
throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + "'", throw new DeviceManagementException("Error occurred while enrolling the device '" + device.getId() + "'",
e); e);
@ -185,5 +183,4 @@ public class DeviceManagerImpl implements DeviceManager {
public DeviceManagementRepository getPluginRepository() { public DeviceManagementRepository getPluginRepository() {
return pluginRepository; return pluginRepository;
} }
} }

@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.License; import org.wso2.carbon.device.mgt.common.License;
import org.wso2.carbon.device.mgt.common.LicenseManagementException; import org.wso2.carbon.device.mgt.common.LicenseManagementException;
import org.wso2.carbon.governance.api.exception.GovernanceException; import org.wso2.carbon.governance.api.exception.GovernanceException;
@ -34,32 +35,33 @@ import org.wso2.carbon.governance.api.util.GovernanceUtils;
import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry; import org.wso2.carbon.registry.core.session.UserRegistry;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
public class LicenseManagerImpl implements LicenseManager { public class LicenseManagerImpl implements LicenseManager {
private static Log log = LogFactory.getLog(DeviceManagerImpl.class); private static Log log = LogFactory.getLog(DeviceManagerImpl.class);
private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
@Override @Override
public License getLicense(final String deviceType, public License getLicense(final String deviceType, final String languageCodes) throws LicenseManagementException {
final String languageCodes) throws LicenseManagementException {
if (log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("entered get License in license manager impl"); log.debug("entered get License in license manager impl");
} }
// TODO: After completes JAX-RX user login, this need to be change to CarbonContext // TODO: After completes JAX-RX user login, this need to be change to CarbonContext
PrivilegedCarbonContext.getThreadLocalCarbonContext().startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername("admin"); PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername("admin");
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
Registry registry = (UserRegistry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry( Registry registry = (UserRegistry) PrivilegedCarbonContext.getThreadLocalCarbonContext()
RegistryType.USER_GOVERNANCE); .getRegistry(RegistryType.USER_GOVERNANCE);
GenericArtifact[] filteredArtifacts; GenericArtifact[] filteredArtifacts;
License license = new License(); License license = new License();
@ -79,41 +81,44 @@ public class LicenseManagerImpl implements LicenseManager {
}); });
String validFrom; String validFrom;
String validTo; String validTo;
DateFormat format;
Date fromDate; Date fromDate;
Date toDate; Date toDate;
for (GenericArtifact artifact : filteredArtifacts) { for (GenericArtifact artifact : filteredArtifacts) {
if (log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("Overview name:"+artifact.getAttribute("overview_name")); log.debug("Overview name: " +
log.debug("Overview provider:"+artifact.getAttribute("overview_provider")); artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME));
log.debug("Overview Language:"+artifact.getAttribute("overview_language")); log.debug("Overview provider: " +
log.debug("overview_validityFrom:"+artifact.getAttribute("overview_validityFrom")); artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_PROVIDER));
log.debug("overview_validityTo:"+artifact.getAttribute("overview_validityTo")); log.debug("Overview language: " +
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_LANGUAGE));
log.debug("Overview validity from: " +
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM));
log.debug("Overview validity to: " +
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO));
} }
validFrom = artifact.getAttribute("overview_validityFrom"); validFrom = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM);
validTo = artifact.getAttribute("overview_validityTo"); validTo = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO);
format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
try { try {
fromDate = format.parse(validFrom); fromDate = format.parse(validFrom);
toDate = format.parse(validTo); toDate = format.parse(validTo);
if (fromDate.getTime()<= new Date().getTime() && new Date().getTime() <= toDate.getTime()){ if (fromDate.getTime() <= new Date().getTime() && new Date().getTime() <= toDate.getTime()) {
license.setLicenseText(artifact.getAttribute("overview_license")); license.setLicenseText(
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LICENSE));
} }
} catch (ParseException e) { } catch (ParseException e) {
log.error("validFrom:"+ validFrom); log.error("Valid from: " + validFrom);
log.error("validTo:"+validTo); log.error("Valid to: " + validTo);
log.error("Valid date parse error:",e); log.error("Valid date parse error: ", e);
} }
} }
} catch (RegistryException regEx) { } catch (RegistryException regEx) {
log.error("registry exception:",regEx); String errorMsg = "Registry error occurred: ";
throw new LicenseManagementException(); log.error(errorMsg, regEx);
}finally { throw new LicenseManagementException(errorMsg, regEx);
} finally {
PrivilegedCarbonContext.endTenantFlow(); PrivilegedCarbonContext.endTenantFlow();
} }
return license; return license;
} }
} }

@ -51,15 +51,15 @@ public class DeviceManagementDAOTests {
TestDBConfiguration dbConfig = getTestDBConfiguration(dbType); TestDBConfiguration dbConfig = getTestDBConfiguration(dbType);
switch (dbType) { switch (dbType) {
case H2: case H2:
createH2DB(dbConfig); createH2DB(dbConfig);
BasicDataSource testDataSource = new BasicDataSource(); BasicDataSource testDataSource = new BasicDataSource();
testDataSource.setDriverClassName(dbConfig.getDriverClass()); testDataSource.setDriverClassName(dbConfig.getDriverClass());
testDataSource.setUrl(dbConfig.getConnectionUrl()); testDataSource.setUrl(dbConfig.getConnectionUrl());
testDataSource.setUsername(dbConfig.getUserName()); testDataSource.setUsername(dbConfig.getUserName());
testDataSource.setPassword(dbConfig.getPwd()); testDataSource.setPassword(dbConfig.getPwd());
DeviceManagementDAOFactory.init(testDataSource); DeviceManagementDAOFactory.init(testDataSource);
default: default:
} }
} }
@ -133,7 +133,7 @@ public class DeviceManagementDAOTests {
} }
@Test(dependsOnMethods = {"addDeviceTypeTest"}) @Test(dependsOnMethods = { "addDeviceTypeTest" })
public void addDeviceTest() throws DeviceManagementDAOException, DeviceManagementException { public void addDeviceTest() throws DeviceManagementDAOException, DeviceManagementException {
DeviceDAO deviceMgtDAO = DeviceManagementDAOFactory.getDeviceDAO(); DeviceDAO deviceMgtDAO = DeviceManagementDAOFactory.getDeviceDAO();
@ -157,21 +157,26 @@ public class DeviceManagementDAOTests {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
String deviceStatus = null;
try { try {
conn = DeviceManagementDAOFactory.getDataSource().getConnection(); conn = DeviceManagementDAOFactory.getDataSource().getConnection();
stmt = conn.prepareStatement("SELECT ID from DM_DEVICE DEVICE where DEVICE.DEVICE_IDENTIFICATION='111'"); stmt = conn.prepareStatement(
"SELECT ID,STATUS from DM_DEVICE DEVICE where DEVICE.DEVICE_IDENTIFICATION=?");
stmt.setString(1,"111");
rs = stmt.executeQuery(); rs = stmt.executeQuery();
while (rs.next()) { while (rs.next()) {
deviceId = rs.getLong(1); deviceId = rs.getLong(1);
deviceStatus = rs.getString(2);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceManagementDAOException("error in fetch device by device identification id", e); throw new DeviceManagementDAOException("Error in fetch device by device identification id", e);
} finally { } finally {
TestUtils.cleanupResources(conn, stmt, rs); TestUtils.cleanupResources(conn, stmt, rs);
} }
Assert.assertNotNull(deviceId, "Device Id is null"); Assert.assertNotNull(deviceId, "Device Id is null");
Assert.assertNotNull(deviceStatus, "Device status is null");
Assert.assertEquals(deviceStatus, "ACTIVE", "Enroll device status should active");
} }
} }

Loading…
Cancel
Save