Completing Android feature management implementation and adding IOS feature management implementation

revert-dabc3590
prabathabey 10 years ago
parent befecac320
commit 649feeea2a

@ -76,8 +76,16 @@ public class AndroidFeatureManager implements FeatureManager {
@Override
public List<Feature> getFeatures() throws DeviceManagementException {
try {
return featureDAO.getFeatures();
FeatureManagementDAOFactory.beginTransaction();
List<Feature> 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);
}
}

@ -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();

@ -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<Feature> getFeatures() throws DeviceManagementException {
return null;
}
@Override
public boolean removeFeature(String s) throws DeviceManagementException {
return false;
}
}

@ -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<Feature> getFeatures() throws FeatureManagementDAOException;
}

@ -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;
}
}

@ -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<Connection> currentConnection = new ThreadLocal<Connection>();
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;
}
}

@ -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<Feature> getFeatures() throws FeatureManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Feature> features = new ArrayList<Feature>();
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);
}
}
}
Loading…
Cancel
Save