From 649feeea2ac28a197930105667e5cb21f1ce3dcc Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 26 Mar 2015 22:55:42 +0530 Subject: [PATCH] Completing Android feature management implementation and adding IOS feature management implementation --- .../impl/android/AndroidFeatureManager.java | 17 ++- .../impl/android/dao/impl/FeatureDAOImpl.java | 4 +- .../mobile/impl/ios/IOSFeatureManager.java | 57 ++++++++ .../mgt/mobile/impl/ios/dao/FeatureDAO.java | 35 +++++ .../dao/FeatureManagementDAOException.java | 77 +++++++++++ .../ios/dao/FeatureManagementDAOFactory.java | 93 +++++++++++++ .../impl/ios/dao/impl/FeatureDAOImpl.java | 127 ++++++++++++++++++ 7 files changed, 407 insertions(+), 3 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSFeatureManager.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureDAO.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOException.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOFactory.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/impl/FeatureDAOImpl.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java index 3a1d22550..e0cdf71d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java @@ -76,8 +76,16 @@ public class AndroidFeatureManager implements FeatureManager { @Override public List getFeatures() throws DeviceManagementException { try { - return featureDAO.getFeatures(); + FeatureManagementDAOFactory.beginTransaction(); + List features = featureDAO.getFeatures(); + FeatureManagementDAOFactory.commitTransaction(); + return features; } catch (FeatureManagementDAOException e) { + try { + FeatureManagementDAOFactory.rollbackTransaction(); + } catch (FeatureManagementDAOException e1) { + log.warn("Error occurred while roll-backing the transaction", e); + } throw new DeviceManagementException("Error occurred while retrieving the list of features registered " + "for Android platform", e); } @@ -86,9 +94,16 @@ public class AndroidFeatureManager implements FeatureManager { @Override public boolean removeFeature(String name) throws DeviceManagementException { try { + FeatureManagementDAOFactory.beginTransaction(); featureDAO.removeFeature(name); + FeatureManagementDAOFactory.commitTransaction(); return true; } catch (FeatureManagementDAOException e) { + try { + FeatureManagementDAOFactory.rollbackTransaction(); + } catch (FeatureManagementDAOException e1) { + log.warn("Error occurred while roll-backing the transaction", e); + } throw new DeviceManagementException("Error occurred while removing the feature", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/FeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/FeatureDAOImpl.java index 0641350cf..2d43ea017 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/FeatureDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/FeatureDAOImpl.java @@ -39,7 +39,7 @@ public class FeatureDAOImpl implements FeatureDAO { PreparedStatement stmt = null; try { Connection conn = FeatureManagementDAOFactory.getConnection(); - String sql = "INSERT INTO MBL_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; + String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(sql); stmt.setString(1, feature.getCode()); stmt.setString(2, feature.getName()); @@ -58,7 +58,7 @@ public class FeatureDAOImpl implements FeatureDAO { PreparedStatement stmt = null; try { Connection conn = FeatureManagementDAOFactory.getConnection(); - String sql = "DELETE FROM MBL_FEATURE WHERE CODE = ?"; + String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, code); stmt.execute(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSFeatureManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSFeatureManager.java new file mode 100644 index 000000000..e4ab47ade --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/IOSFeatureManager.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015, 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.mobile.impl.ios; + +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureDAO; +import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOFactory; + +import java.util.List; + +public class IOSFeatureManager implements FeatureManager { + + private FeatureDAO featureDAO; + + public IOSFeatureManager() { + this.featureDAO = FeatureManagementDAOFactory.getFeatureDAO(); + } + + @Override + public boolean addFeature(Feature feature) throws DeviceManagementException { + return false; + } + + @Override + public Feature getFeature(String s) throws DeviceManagementException { + return null; + } + + @Override + public List getFeatures() throws DeviceManagementException { + return null; + } + + @Override + public boolean removeFeature(String s) throws DeviceManagementException { + return false; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureDAO.java new file mode 100644 index 000000000..6c8c745cb --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureDAO.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2015, 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.mobile.impl.ios.dao; + +import org.wso2.carbon.device.mgt.common.Feature; + +import java.util.List; + +public interface FeatureDAO { + + void addFeature(Feature feature) throws FeatureManagementDAOException; + + void removeFeature(String name) throws FeatureManagementDAOException; + + Feature getFeature(String name) throws FeatureManagementDAOException; + + List getFeatures() throws FeatureManagementDAOException; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOException.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOException.java new file mode 100644 index 000000000..5882d2391 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOException.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2015, 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.mobile.impl.ios.dao; + +public class FeatureManagementDAOException extends Exception { + + private String message; + private static final long serialVersionUID = 2021891706072918865L; + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified detail message and + * nested exception. + * + * @param message error message + * @param nestedException exception + */ + public FeatureManagementDAOException(String message, Exception nestedException) { + super(message, nestedException); + setErrorMessage(message); + } + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified detail message + * and cause. + * + * @param message the detail message. + * @param cause the cause of this exception. + */ + public FeatureManagementDAOException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified detail message. + * + * @param message the detail message. + */ + public FeatureManagementDAOException(String message) { + super(message); + setErrorMessage(message); + } + + /** + * Constructs a new MobileDeviceManagementDAOException with the specified and cause. + * + * @param cause the cause of this exception. + */ + public FeatureManagementDAOException(Throwable cause) { + super(cause); + } + + public String getMessage() { + return message; + } + + public void setErrorMessage(String errorMessage) { + this.message = errorMessage; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOFactory.java new file mode 100644 index 000000000..7db86415b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/FeatureManagementDAOFactory.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2015, 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.mobile.impl.ios.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.impl.FeatureDAOImpl; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +public class FeatureManagementDAOFactory { + + private static DataSource dataSource; + private static ThreadLocal currentConnection = new ThreadLocal(); + private static final Log log = LogFactory.getLog(FeatureManagementDAOFactory.class); + + public static FeatureDAO getFeatureDAO() { + return new FeatureDAOImpl(); + } + + public static void init(DataSource dtSource) { + dataSource = dtSource; + } + + public static void beginTransaction() throws FeatureManagementDAOException { + try { + Connection conn = dataSource.getConnection(); + conn.setAutoCommit(false); + currentConnection.set(conn); + } catch (SQLException e) { + throw new FeatureManagementDAOException("Error occurred while retrieving datasource connection", e); + } + } + + public static Connection getConnection() { + return currentConnection.get(); + } + + public static void commitTransaction() throws FeatureManagementDAOException { + 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"); + } + } + } catch (SQLException e) { + throw new FeatureManagementDAOException("Error occurred while committing the transaction", e); + } + } + + public static void rollbackTransaction() throws FeatureManagementDAOException { + 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"); + } + } + } catch (SQLException e) { + throw new FeatureManagementDAOException("Error occurred while rollbacking the transaction", e); + } + } + + public static DataSource getDataSource() { + return dataSource; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/impl/FeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/impl/FeatureDAOImpl.java new file mode 100644 index 000000000..f6bc1fc9f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/ios/dao/impl/FeatureDAOImpl.java @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2015, 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.mobile.impl.ios.dao.impl; + +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureDAO; +import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOException; +import org.wso2.carbon.device.mgt.mobile.impl.ios.dao.FeatureManagementDAOFactory; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class FeatureDAOImpl implements FeatureDAO { + + @Override + public void addFeature(Feature feature) throws FeatureManagementDAOException { + PreparedStatement stmt = null; + try { + Connection conn = FeatureManagementDAOFactory.getConnection(); + String sql = "INSERT INTO IOS_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, feature.getCode()); + stmt.setString(2, feature.getName()); + stmt.setString(3, feature.getDescription()); + stmt.executeUpdate(); + } catch (SQLException e) { + throw new FeatureManagementDAOException("Error occurred while adding feature '" + + feature.getName() + "' into the metadata repository", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void removeFeature(String code) throws FeatureManagementDAOException { + PreparedStatement stmt = null; + try { + Connection conn = FeatureManagementDAOFactory.getConnection(); + String sql = "DELETE FROM IOS_FEATURE WHERE CODE = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, code); + stmt.execute(); + } catch (SQLException e) { + throw new FeatureManagementDAOException("Error occurred while adding feature '" + + code + "' into the metadata repository", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public Feature getFeature(String code) throws FeatureManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + try { + Connection conn = FeatureManagementDAOFactory.getConnection(); + String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM IOS_FEATURE WHERE CODE = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, code); + rs = stmt.executeQuery(); + + Feature feature = null; + if (rs.next()) { + feature = new Feature(); + feature.setId(rs.getInt("ID")); + feature.setCode(rs.getString("CODE")); + feature.setName(rs.getString("NAME")); + feature.setDescription(rs.getString("DESCRIPTION")); + } + return feature; + } catch (SQLException e) { + throw new FeatureManagementDAOException("Error occurred while retrieving feature metadata '" + + code + "' from the feature metadata repository", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + @Override + public List getFeatures() throws FeatureManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs = null; + List features = new ArrayList(); + try { + Connection conn = FeatureManagementDAOFactory.getConnection(); + String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM IOS_FEATURE"; + stmt = conn.prepareStatement(sql); + rs = stmt.executeQuery(); + + while (rs.next()) { + Feature feature = new Feature(); + feature.setId(rs.getInt("ID")); + feature.setCode(rs.getString("CODE")); + feature.setName(rs.getString("NAME")); + feature.setDescription(rs.getString("DESCRIPTION")); + } + return features; + } catch (SQLException e) { + throw new FeatureManagementDAOException("Error occurred while retrieving all feature metadata from the " + + "feature metadata repository", e); + } finally { + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + +}