|
|
|
@ -37,136 +37,119 @@ import java.util.List;
|
|
|
|
|
*/
|
|
|
|
|
public class MobileDeviceManagementDAOUtil {
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOUtil.class);
|
|
|
|
|
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOUtil.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolve data source from the data source definition.
|
|
|
|
|
*
|
|
|
|
|
* @param config Mobile data source configuration
|
|
|
|
|
* @return data source resolved from the data source definition
|
|
|
|
|
*/
|
|
|
|
|
public static DataSource resolveDataSource(MobileDataSourceConfig config) {
|
|
|
|
|
DataSource dataSource = null;
|
|
|
|
|
if (config == null) {
|
|
|
|
|
throw new RuntimeException("Device Management Repository data source configuration " +
|
|
|
|
|
"is null and thus, is not initialized");
|
|
|
|
|
}
|
|
|
|
|
JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion();
|
|
|
|
|
if (jndiConfig != null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Initializing Device Management Repository data source using the JNDI " +
|
|
|
|
|
"Lookup Definition");
|
|
|
|
|
}
|
|
|
|
|
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
|
|
|
|
|
jndiConfig.getJndiProperties();
|
|
|
|
|
if (jndiPropertyList != null) {
|
|
|
|
|
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
|
|
|
|
|
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
|
|
|
|
jndiProperties.put(prop.getName(), prop.getValue());
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
dataSource = MobileDeviceManagementDAOUtil.lookupDataSource(
|
|
|
|
|
jndiConfig.getJndiName(), jndiProperties);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Error in looking up data source: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
log.error(e);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
dataSource = MobileDeviceManagementDAOUtil.lookupDataSource(
|
|
|
|
|
jndiConfig.getJndiName(), null);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Error in looking up data source: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
log.error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dataSource;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Resolve data source from the data source definition.
|
|
|
|
|
*
|
|
|
|
|
* @param config Mobile data source configuration
|
|
|
|
|
* @return data source resolved from the data source definition
|
|
|
|
|
*/
|
|
|
|
|
public static DataSource resolveDataSource(MobileDataSourceConfig config) throws DeviceManagementException {
|
|
|
|
|
DataSource dataSource = null;
|
|
|
|
|
if (config == null) {
|
|
|
|
|
throw new RuntimeException("Device Management Repository data source configuration " +
|
|
|
|
|
"is null and thus, is not initialized");
|
|
|
|
|
}
|
|
|
|
|
JNDILookupDefinition jndiConfig = config.getJndiLookupDefintion();
|
|
|
|
|
if (jndiConfig != null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Initializing Device Management Repository data source using the JNDI " +
|
|
|
|
|
"Lookup Definition");
|
|
|
|
|
}
|
|
|
|
|
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
|
|
|
|
|
jndiConfig.getJndiProperties();
|
|
|
|
|
if (jndiPropertyList != null) {
|
|
|
|
|
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
|
|
|
|
|
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
|
|
|
|
jndiProperties.put(prop.getName(), prop.getValue());
|
|
|
|
|
}
|
|
|
|
|
dataSource =
|
|
|
|
|
MobileDeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
|
|
|
|
|
} else {
|
|
|
|
|
dataSource = MobileDeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dataSource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DataSource lookupDataSource(String dataSourceName,
|
|
|
|
|
final Hashtable<Object, Object> jndiProperties)
|
|
|
|
|
throws DeviceManagementException {
|
|
|
|
|
try {
|
|
|
|
|
if (jndiProperties == null || jndiProperties.isEmpty()) {
|
|
|
|
|
return (DataSource) InitialContext.doLookup(dataSourceName);
|
|
|
|
|
}
|
|
|
|
|
final InitialContext context = new InitialContext(jndiProperties);
|
|
|
|
|
return (DataSource) context.doLookup(dataSourceName);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new DeviceManagementException(
|
|
|
|
|
"Error in looking up data source: " + e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static DataSource lookupDataSource(String dataSourceName,
|
|
|
|
|
final Hashtable<Object, Object> jndiProperties)
|
|
|
|
|
throws DeviceManagementException {
|
|
|
|
|
try {
|
|
|
|
|
if (jndiProperties == null || jndiProperties.isEmpty()) {
|
|
|
|
|
return (DataSource) InitialContext.doLookup(dataSourceName);
|
|
|
|
|
}
|
|
|
|
|
final InitialContext context = new InitialContext(jndiProperties);
|
|
|
|
|
return (DataSource) context.lookup(dataSourceName);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
String msg = "Error in looking up data source: " + e.getMessage();
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
|
|
|
|
if (rs != null) {
|
|
|
|
|
try {
|
|
|
|
|
rs.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
log.warn("Error occurred while closing result set", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (stmt != null) {
|
|
|
|
|
try {
|
|
|
|
|
stmt.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
log.warn("Error occurred while closing prepared statement", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
conn.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
log.warn("Error occurred while closing database connection", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
|
|
|
|
if (rs != null) {
|
|
|
|
|
try {
|
|
|
|
|
rs.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
log.warn("Error occurred while closing result set", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (stmt != null) {
|
|
|
|
|
try {
|
|
|
|
|
stmt.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
log.warn("Error occurred while closing prepared statement", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (conn != null) {
|
|
|
|
|
try {
|
|
|
|
|
conn.close();
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
log.warn("Error occurred while closing database connection", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the creation of mobile device management schema if -Dsetup has provided.
|
|
|
|
|
*
|
|
|
|
|
* @param dataSource Mobile data source
|
|
|
|
|
*/
|
|
|
|
|
public static void createDataSource(DataSource dataSource) {
|
|
|
|
|
String setupOption = System.getProperty("setup");
|
|
|
|
|
if (setupOption != null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug(
|
|
|
|
|
"-Dsetup is enabled. Mobile Device management repository schema initialization is about " +
|
|
|
|
|
"to begin");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
MobileDeviceManagementDAOUtil.setupMobileDeviceManagementSchema(dataSource);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
log.error(
|
|
|
|
|
"Exception occurred while initializing mobile device management database schema",
|
|
|
|
|
e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Initializes the creation of mobile device management schema if -Dsetup has provided.
|
|
|
|
|
*
|
|
|
|
|
* @param dataSource Mobile data source
|
|
|
|
|
*/
|
|
|
|
|
public static void createDataSource(DataSource dataSource) {
|
|
|
|
|
String setupOption = System.getProperty("setup");
|
|
|
|
|
if (setupOption != null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug(
|
|
|
|
|
"-Dsetup is enabled. Mobile Device management repository schema initialization is about " +
|
|
|
|
|
"to begin");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
MobileDeviceManagementDAOUtil.setupMobileDeviceManagementSchema(dataSource);
|
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
|
log.error("Exception occurred while initializing mobile device management database schema", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates the mobile device management schema.
|
|
|
|
|
*
|
|
|
|
|
* @param dataSource Mobile data source
|
|
|
|
|
*/
|
|
|
|
|
public static void setupMobileDeviceManagementSchema(DataSource dataSource) throws
|
|
|
|
|
DeviceManagementException {
|
|
|
|
|
MobileDeviceManagementSchemaInitializer initializer =
|
|
|
|
|
new MobileDeviceManagementSchemaInitializer(dataSource);
|
|
|
|
|
log.info("Initializing mobile device management repository database schema");
|
|
|
|
|
try {
|
|
|
|
|
initializer.createRegistryDatabase();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new DeviceManagementException(
|
|
|
|
|
"Error occurred while initializing Mobile Device Management " +
|
|
|
|
|
"database schema", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Creates the mobile device management schema.
|
|
|
|
|
*
|
|
|
|
|
* @param dataSource Mobile data source
|
|
|
|
|
*/
|
|
|
|
|
public static void setupMobileDeviceManagementSchema(DataSource dataSource) throws
|
|
|
|
|
DeviceManagementException {
|
|
|
|
|
MobileDeviceManagementSchemaInitializer initializer =
|
|
|
|
|
new MobileDeviceManagementSchemaInitializer(dataSource);
|
|
|
|
|
log.info("Initializing mobile device management repository database schema");
|
|
|
|
|
try {
|
|
|
|
|
initializer.createRegistryDatabase();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
throw new DeviceManagementException("Error occurred while initializing Mobile Device Management " +
|
|
|
|
|
"database schema", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|