refactored issues in codes

revert-dabc3590
hasuniea 9 years ago
parent c064b32ff2
commit 716daee269

@ -1,13 +1,10 @@
/* /*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. * in compliance with the License.
* you may obtain a copy of the License at * you may obtain a copy of the License at
* * http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

@ -1,21 +1,18 @@
/* /*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* * WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except
* Version 2.0 (the "License"); you may not use this file except * in compliance with the License.
* in compliance with the License. * you may obtain a copy of the License at
* 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,
* http://www.apache.org/licenses/LICENSE-2.0 * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* Unless required by applicable law or agreed to in writing, * KIND, either express or implied. See the License for the
* software distributed under the License is distributed on an * specific language governing permissions and limitations
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * under the License.
* 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.mobile.impl.windows; package org.wso2.carbon.device.mgt.mobile.impl.windows;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -101,16 +98,15 @@ public class WindowsDeviceManager implements DeviceManager {
public TenantConfiguration getConfiguration() throws DeviceManagementException { public TenantConfiguration getConfiguration() throws DeviceManagementException {
Resource resource; Resource resource;
try { try {
String windowsRegPath = String windowsTenantRegistryPath =
MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants. MobileDeviceManagementUtil.getPlatformConfigPath(DeviceManagementConstants.
MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS);
resource = MobileDeviceManagementUtil.getRegistryResource(windowsRegPath); resource = MobileDeviceManagementUtil.getRegistryResource(windowsTenantRegistryPath);
JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class); JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class);
Unmarshaller unmarshaller = context.createUnmarshaller(); Unmarshaller unmarshaller = context.createUnmarshaller();
return (TenantConfiguration) unmarshaller.unmarshal( return (TenantConfiguration) unmarshaller.unmarshal(
new StringReader(new String((byte[]) resource.getContent(), Charset new StringReader(new String((byte[]) resource.getContent(), Charset
.forName(MobilePluginConstants.CHARSET_UTF8)))); .forName(MobilePluginConstants.CHARSET_UTF8))));
} catch (MobileDeviceMgtPluginException e) { } catch (MobileDeviceMgtPluginException e) {
throw new DeviceManagementException( throw new DeviceManagementException(
"Error occurred while retrieving the Registry instance : " + e.getMessage(), e); "Error occurred while retrieving the Registry instance : " + e.getMessage(), e);
@ -132,20 +128,12 @@ public class WindowsDeviceManager implements DeviceManager {
log.debug("Modifying the Windows device enrollment data"); log.debug("Modifying the Windows device enrollment data");
} }
WindowsDAOFactory.beginTransaction(); WindowsDAOFactory.beginTransaction();
status = daoFactory.getMobileDeviceDAO() status = daoFactory.getMobileDeviceDAO().updateMobileDevice(mobileDevice);
.updateMobileDevice(mobileDevice);
WindowsDAOFactory.commitTransaction(); WindowsDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { WindowsDAOFactory.rollbackTransaction();
WindowsDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error while updating the enrollment of the Windows device : " +
} catch (MobileDeviceManagementDAOException mobileDAOEx) { device.getDeviceIdentifier(), e);
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
log.warn(msg, mobileDAOEx);
}
String msg = "Error while updating the enrollment of the Windows device : " +
device.getDeviceIdentifier();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} }
return status; return status;
} }
@ -161,16 +149,8 @@ public class WindowsDeviceManager implements DeviceManager {
status = daoFactory.getMobileDeviceDAO().deleteMobileDevice(deviceId.getId()); status = daoFactory.getMobileDeviceDAO().deleteMobileDevice(deviceId.getId());
WindowsDAOFactory.commitTransaction(); WindowsDAOFactory.commitTransaction();
} catch (MobileDeviceManagementDAOException e) { } catch (MobileDeviceManagementDAOException e) {
try { WindowsDAOFactory.rollbackTransaction();
WindowsDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error while removing the Windows device : " + deviceId.getId(), e);
} catch (MobileDeviceManagementDAOException mobileDAOEx) {
String msg = "Error occurred while roll back the device dis enrol transaction :" +
deviceId.toString();
log.warn(msg, mobileDAOEx);
}
String msg = "Error while removing the Windows device : " + deviceId.getId();
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} }
return status; return status;
} }

@ -1,13 +1,10 @@
/* /*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. * in compliance with the License.
* You may obtain a copy of the License at * you may obtain a copy of the License at
* * http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@ -16,7 +13,6 @@
* under the License. * under the License.
*/ */
package org.wso2.carbon.device.mgt.mobile.impl.windows; package org.wso2.carbon.device.mgt.mobile.impl.windows;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;

@ -1,21 +1,18 @@
/* /*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* * WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except
* Version 2.0 (the "License"); you may not use this file except * in compliance with the License.
* in compliance with the License. * you may obtain a copy of the License at
* 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,
* http://www.apache.org/licenses/LICENSE-2.0 * software distributed under the License is distributed on an
* * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* Unless required by applicable law or agreed to in writing, * KIND, either express or implied. See the License for the
* software distributed under the License is distributed on an * specific language governing permissions and limitations
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * under the License.
* 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.mobile.impl.windows.dao; package org.wso2.carbon.device.mgt.mobile.impl.windows.dao;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -41,10 +38,14 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
} }
@Override @Override
public MobileDeviceDAO getMobileDeviceDAO() {return new WindowsDeviceDAOImpl();} public MobileDeviceDAO getMobileDeviceDAO() {
return new WindowsDeviceDAOImpl();
}
@Override @Override
public MobileOperationDAO getMobileOperationDAO() {return null;} public MobileOperationDAO getMobileOperationDAO() {
return null;
}
@Override @Override
public MobileOperationPropertyDAO getMobileOperationPropertyDAO() { public MobileOperationPropertyDAO getMobileOperationPropertyDAO() {
@ -88,7 +89,7 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
return currentConnection.get(); return currentConnection.get();
} }
public static void commitTransaction() throws MobileDeviceManagementDAOException { public static void commitTransaction() {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -100,13 +101,13 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e); log.error("Error occurred while committing the transaction", e);
} finally { } finally {
closeConnection(); closeConnection();
} }
} }
public static void closeConnection() throws MobileDeviceManagementDAOException { public static void closeConnection() {
Connection con = currentConnection.get(); Connection con = currentConnection.get();
try { try {
@ -117,7 +118,7 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
currentConnection.remove(); currentConnection.remove();
} }
public static void rollbackTransaction() throws MobileDeviceManagementDAOException { public static void rollbackTransaction() {
try { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { if (conn != null) {
@ -129,7 +130,8 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e); log.warn("Error occurred while roll-backing the transaction", e);
} finally { } finally {
closeConnection(); closeConnection();
} }

@ -1,4 +1,3 @@
/* /*
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
@ -28,9 +27,12 @@ import org.wso2.carbon.device.mgt.mobile.impl.windows.util.WindowsUtils;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Implements MobileDeviceDAO for Windows Devices. * Implements MobileDeviceDAO for Windows Devices.
@ -41,7 +43,49 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO {
@Override @Override
public MobileDevice getMobileDevice(String mblDeviceId) throws MobileDeviceManagementDAOException { public MobileDevice getMobileDevice(String mblDeviceId) throws MobileDeviceManagementDAOException {
return null; Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
MobileDevice mobileDevice = null;
List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>();
try {
conn = WindowsDAOFactory.getConnection();
String selectDBQuery =
"SELECT MOBILE_DEVICE_ID, CHANNEL_URI, DEVICE_INFO, IMEI, IMSI, " +
"OS_VERSION, DEVICE_MODEL, VENDOR, LATITUDE, LONGITUDE, SERIAL, MAC_ADDRESS, OS_VERSION, DEVICE_NAME " +
"FROM WINDOWS_DEVICE WHERE MOBILE_DEVICE_ID = ?";
stmt = conn.prepareStatement(selectDBQuery);
stmt.setString(1, mblDeviceId);
rs = stmt.executeQuery();
while (rs.next()) {
mobileDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(rs.getString(WindowsPluginConstants.MOBILE_DEVICE_ID));
mobileDevice.setVendor(rs.getString(WindowsPluginConstants.IMEI));
mobileDevice.setLatitude(rs.getString(WindowsPluginConstants.IMSI));
mobileDevice.setLongitude(rs.getString(WindowsPluginConstants.OS_VERSION));
mobileDevice.setImei(rs.getString(WindowsPluginConstants.DEVICE_MODEL));
mobileDevice.setImsi(rs.getString(WindowsPluginConstants.VENDOR));
mobileDevice.setOsVersion(rs.getString(WindowsPluginConstants.LATITUDE));
Map<String, String> propertyMap = new HashMap<String, String>();
propertyMap.put(WindowsPluginConstants.CHANNEL_URI, rs.getString(WindowsPluginConstants.CHANNEL_URI));
propertyMap.put(WindowsPluginConstants.DEVICE_INFO, rs.getString(WindowsPluginConstants.DEVICE_INFO));
propertyMap.put(WindowsPluginConstants.DEVICE_NAME, rs.getString(WindowsPluginConstants.DEVICE_NAME));
mobileDevice.setDeviceProperties(propertyMap);
mobileDevices.add(mobileDevice);
}
if (log.isDebugEnabled()) {
log.debug("All Windows device details have fetched from Windows database.");
}
return mobileDevice;
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while fetching all Windows device data", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
WindowsDAOFactory.closeConnection();
}
} }
@Override @Override
@ -88,10 +132,8 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while adding the Windows device '" + throw new MobileDeviceManagementDAOException("Error occurred while adding the Windows device '" +
mobileDevice.getMobileDeviceId() + "' to the Windows db."; mobileDevice.getMobileDeviceId() + "' to the Windows db.", e);
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -124,7 +166,7 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO {
WindowsPluginConstants.DEVICE_INFO)); WindowsPluginConstants.DEVICE_INFO));
stmt.setString(3, mobileDevice.getImei()); stmt.setString(3, mobileDevice.getImei());
stmt.setString(4, mobileDevice.getImsi()); stmt.setString(4, mobileDevice.getImsi());
stmt.setString(5, mobileDevice.getOsVersion() ); stmt.setString(5, mobileDevice.getOsVersion());
stmt.setString(6, mobileDevice.getModel()); stmt.setString(6, mobileDevice.getModel());
stmt.setString(7, mobileDevice.getVendor()); stmt.setString(7, mobileDevice.getVendor());
stmt.setString(8, mobileDevice.getLatitude()); stmt.setString(8, mobileDevice.getLatitude());
@ -144,10 +186,8 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while modifying the Windows device '" + throw new MobileDeviceManagementDAOException("Error occurred while modifying the Windows device '" +
mobileDevice.getMobileDeviceId() + "' data."; mobileDevice.getMobileDeviceId() + "' data.", e);
log.error(msg, e);
throw new MobileDeviceManagementDAOException(msg, e);
} finally { } finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); MobileDeviceManagementDAOUtil.cleanupResources(stmt, null);
} }
@ -184,6 +224,47 @@ public class WindowsDeviceDAOImpl implements MobileDeviceDAO {
@Override @Override
public List<MobileDevice> getAllMobileDevices() throws MobileDeviceManagementDAOException { public List<MobileDevice> getAllMobileDevices() throws MobileDeviceManagementDAOException {
return null; Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
MobileDevice mobileDevice;
List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>();
try {
conn = WindowsDAOFactory.getConnection();
String selectDBQuery =
"SELECT MOBILE_DEVICE_ID, CHANNEL_URI, DEVICE_INFO, IMEI, IMSI, " +
"OS_VERSION, DEVICE_MODEL, VENDOR, LATITUDE, LONGITUDE, SERIAL, MAC_ADDRESS, OS_VERSION, DEVICE_NAME " +
"FROM WINDOWS_DEVICE";
stmt = conn.prepareStatement(selectDBQuery);
rs = stmt.executeQuery();
while (rs.next()) {
mobileDevice = new MobileDevice();
mobileDevice.setMobileDeviceId(rs.getString(WindowsPluginConstants.MOBILE_DEVICE_ID));
mobileDevice.setVendor(rs.getString(WindowsPluginConstants.IMEI));
mobileDevice.setLatitude(rs.getString(WindowsPluginConstants.IMSI));
mobileDevice.setLongitude(rs.getString(WindowsPluginConstants.OS_VERSION));
mobileDevice.setImei(rs.getString(WindowsPluginConstants.DEVICE_MODEL));
mobileDevice.setImsi(rs.getString(WindowsPluginConstants.VENDOR));
mobileDevice.setOsVersion(rs.getString(WindowsPluginConstants.LATITUDE));
Map<String, String> propertyMap = new HashMap<String, String>();
propertyMap.put(WindowsPluginConstants.CHANNEL_URI, rs.getString(WindowsPluginConstants.CHANNEL_URI));
propertyMap.put(WindowsPluginConstants.DEVICE_INFO, rs.getString(WindowsPluginConstants.DEVICE_INFO));
propertyMap.put(WindowsPluginConstants.DEVICE_NAME, rs.getString(WindowsPluginConstants.DEVICE_NAME));
mobileDevice.setDeviceProperties(propertyMap);
mobileDevices.add(mobileDevice);
}
if (log.isDebugEnabled()) {
log.debug("All Windows device details have fetched from Windows database.");
}
return mobileDevices;
} catch (SQLException e) {
throw new MobileDeviceManagementDAOException("Error occurred while fetching all Windows device data", e);
} finally {
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
WindowsDAOFactory.closeConnection();
}
} }
} }

@ -32,7 +32,7 @@ public class WindowsPluginConstants {
public static final String LATITUDE = "LATITUDE"; public static final String LATITUDE = "LATITUDE";
public static final String LONGITUDE = "LONGITUDE"; public static final String LONGITUDE = "LONGITUDE";
public static final String SERIAL = "SERIAL"; public static final String SERIAL = "SERIAL";
public static final String MAC_ADDRESS ="MAC_ADDRESS"; public static final String MAC_ADDRESS = "MAC_ADDRESS";
public static final String DEVICE_NAME ="DEVICE_NAME"; public static final String DEVICE_NAME = "DEVICE_NAME";
} }

@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.mobile.impl.windows.util;
import java.util.Map; import java.util.Map;
/** /**
*
* Contains utility methods used by Windows plugin. * Contains utility methods used by Windows plugin.
*/ */
public class WindowsUtils { public class WindowsUtils {
@ -26,9 +25,8 @@ public class WindowsUtils {
String deviceProperty = deviceProperties.get(property); String deviceProperty = deviceProperties.get(property);
if (deviceProperty == null) { if (deviceProperty == null) {
return ""; return null;
} }
return deviceProperty; return deviceProperty;
} }
} }

Loading…
Cancel
Save