mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
Fixing the issue where the data-sources are not initalized by the time the mobile device management component is initialized
This commit is contained in:
parent
6ca7ee6c78
commit
decbcc83fc
@ -66,6 +66,7 @@
|
|||||||
org.wso2.carbon.core,
|
org.wso2.carbon.core,
|
||||||
org.wso2.carbon.utils.*,
|
org.wso2.carbon.utils.*,
|
||||||
org.wso2.carbon.device.mgt.common.*,
|
org.wso2.carbon.device.mgt.common.*,
|
||||||
|
org.wso2.carbon.ndatasource.core
|
||||||
</Import-Package>
|
</Import-Package>
|
||||||
<Export-Package>
|
<Export-Package>
|
||||||
!org.wso2.carbon.device.mgt.mobile.internal,
|
!org.wso2.carbon.device.mgt.mobile.internal,
|
||||||
@ -116,6 +117,10 @@
|
|||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.testng</groupId>
|
<groupId>org.testng</groupId>
|
||||||
<artifactId>testng</artifactId>
|
<artifactId>testng</artifactId>
|
||||||
|
|||||||
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* *
|
||||||
|
* * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
* *
|
||||||
|
* * Licensed 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;
|
||||||
|
|
||||||
|
public class DataSourceNotAvailableException extends RuntimeException {
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
private static final long serialVersionUID = 2021891706072918866L;
|
||||||
|
|
||||||
|
public DataSourceNotAvailableException(String message, Exception nestedException) {
|
||||||
|
super(message, nestedException);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSourceNotAvailableException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSourceNotAvailableException(String message) {
|
||||||
|
super(message);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataSourceNotAvailableException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.message = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -76,4 +76,5 @@ public class MobileDeviceManagementDAOException extends Exception {
|
|||||||
public void setErrorMessage(String errorMessage) {
|
public void setErrorMessage(String errorMessage) {
|
||||||
this.message = errorMessage;
|
this.message = errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,10 +21,13 @@ package org.wso2.carbon.device.mgt.mobile.dao;
|
|||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
|
import org.wso2.carbon.device.mgt.mobile.DataSourceListener;
|
||||||
|
import org.wso2.carbon.device.mgt.mobile.DataSourceNotAvailableException;
|
||||||
import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition;
|
import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition;
|
||||||
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
|
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
|
||||||
import org.wso2.carbon.device.mgt.mobile.dao.impl.*;
|
import org.wso2.carbon.device.mgt.mobile.dao.impl.*;
|
||||||
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
|
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementServiceComponent;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
@ -35,94 +38,104 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class MobileDeviceManagementDAOFactory {
|
public class MobileDeviceManagementDAOFactory {
|
||||||
|
|
||||||
private static DataSource dataSource;
|
private static DataSource dataSource;
|
||||||
private static MobileDataSourceConfig mobileDataSourceConfig;
|
private static MobileDataSourceConfig dsConfig;
|
||||||
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class);
|
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class);
|
||||||
|
private static boolean isInitialized;
|
||||||
|
|
||||||
public MobileDeviceManagementDAOFactory() {
|
public static void init() throws DeviceManagementException {
|
||||||
|
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(dsConfig);
|
||||||
|
isInitialized = true;
|
||||||
|
// MobileDeviceManagementServiceComponent.registerDataSourceListener(new DataSourceListener() {
|
||||||
|
// @Override
|
||||||
|
// public void notifyObserver() {
|
||||||
|
// try {
|
||||||
|
// initDataSource();
|
||||||
|
// } catch (DeviceManagementException e) {
|
||||||
|
// log.error("Error occurred while registering data source");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* Resolve data source from the data source definition.
|
||||||
|
*
|
||||||
|
* @param config Mobile data source configuration
|
||||||
|
* @return data source resolved from the data source definition
|
||||||
|
*/
|
||||||
|
private 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.getJndiLookupDefinition();
|
||||||
|
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 void init() {
|
public static MobileDeviceDAO getMobileDeviceDAO() {
|
||||||
try {
|
assertDataSourceInitialization();
|
||||||
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(mobileDataSourceConfig);
|
return new MobileDeviceDAOImpl(dataSource);
|
||||||
} catch (DeviceManagementException e) {
|
}
|
||||||
log.error("Exception occurred while initializing the mobile datasource.",e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public static MobileOperationDAO getMobileOperationDAO() {
|
||||||
* Resolve data source from the data source definition.
|
assertDataSourceInitialization();
|
||||||
*
|
return new MobileOperationDAOImpl(dataSource);
|
||||||
* @param config Mobile data source configuration
|
}
|
||||||
* @return data source resolved from the data source definition
|
|
||||||
*/
|
|
||||||
private 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.getJndiLookupDefinition();
|
|
||||||
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 MobileDeviceDAO getMobileDeviceDAO() {
|
public static MobileOperationPropertyDAO getMobileOperationPropertyDAO() {
|
||||||
return new MobileDeviceDAOImpl(dataSource);
|
assertDataSourceInitialization();
|
||||||
}
|
return new MobileOperationPropertyDAOImpl(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
public static MobileOperationDAO getMobileOperationDAO() {
|
public static MobileDeviceOperationMappingDAO getMobileDeviceOperationDAO() {
|
||||||
return new MobileOperationDAOImpl(dataSource);
|
assertDataSourceInitialization();
|
||||||
}
|
return new MobileDeviceOperationMappingDAOImpl(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
public static MobileOperationPropertyDAO getMobileOperationPropertyDAO() {
|
public static MobileFeatureDAO getFeatureDAO() {
|
||||||
return new MobileOperationPropertyDAOImpl(dataSource);
|
assertDataSourceInitialization();
|
||||||
}
|
return new MobileFeatureDAOImpl(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
public static MobileDeviceOperationMappingDAO getMobileDeviceOperationDAO() {
|
public static MobileFeaturePropertyDAO getFeaturePropertyDAO() {
|
||||||
return new MobileDeviceOperationMappingDAOImpl(dataSource);
|
assertDataSourceInitialization();
|
||||||
}
|
return new MobileFeaturePropertyDAOImpl(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
public static MobileFeatureDAO getFeatureDAO() {
|
public static void setDatSourceConfig(MobileDataSourceConfig dsConfig) {
|
||||||
return new MobileFeatureDAOImpl(dataSource);
|
MobileDeviceManagementDAOFactory.dsConfig = dsConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MobileFeaturePropertyDAO getFeaturePropertyDAO() {
|
public static DataSource getDataSource() {
|
||||||
return new MobileFeaturePropertyDAOImpl(dataSource);
|
return dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MobileDataSourceConfig getMobileDeviceManagementConfig() {
|
private static void assertDataSourceInitialization() {
|
||||||
return mobileDataSourceConfig;
|
if (!isInitialized) {
|
||||||
}
|
throw new DataSourceNotAvailableException("Mobile device management metadata repository datasource " +
|
||||||
|
"is not initialized");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void setMobileDataSourceConfig(
|
|
||||||
MobileDataSourceConfig mobileDataSourceConfig) {
|
|
||||||
MobileDeviceManagementDAOFactory.mobileDataSourceConfig =
|
|
||||||
mobileDataSourceConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static DataSource getDataSource() {
|
|
||||||
return dataSource;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@ -65,7 +65,7 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B
|
|||||||
MobileDataSourceConfig dsConfig =
|
MobileDataSourceConfig dsConfig =
|
||||||
config.getMobileDeviceMgtRepository().getMobileDataSourceConfig();
|
config.getMobileDeviceMgtRepository().getMobileDataSourceConfig();
|
||||||
|
|
||||||
MobileDeviceManagementDAOFactory.setMobileDataSourceConfig(dsConfig);
|
MobileDeviceManagementDAOFactory.setDatSourceConfig(dsConfig);
|
||||||
|
|
||||||
androidServiceRegRef =
|
androidServiceRegRef =
|
||||||
bundleContext.registerService(DeviceManager.class.getName(),
|
bundleContext.registerService(DeviceManager.class.getName(),
|
||||||
|
|||||||
@ -25,6 +25,7 @@ import org.osgi.framework.ServiceRegistration;
|
|||||||
import org.osgi.service.component.ComponentContext;
|
import org.osgi.service.component.ComponentContext;
|
||||||
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
||||||
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
|
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
|
||||||
|
import org.wso2.carbon.device.mgt.mobile.DataSourceListener;
|
||||||
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager;
|
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager;
|
||||||
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig;
|
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig;
|
||||||
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
|
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
|
||||||
@ -33,10 +34,17 @@ import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
|
|||||||
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManager;
|
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManager;
|
||||||
import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManager;
|
import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManager;
|
||||||
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManager;
|
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManager;
|
||||||
|
import org.wso2.carbon.ndatasource.core.DataSourceService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @scr.component name="org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementServiceComponent"
|
* @scr.component name="org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementServiceComponent"
|
||||||
* immediate="true"
|
* immediate="true"
|
||||||
|
* @scr.reference name="org.wso2.carbon.ndatasource"
|
||||||
|
* interface="org.wso2.carbon.ndatasource.core.DataSourceService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setDataSourceService"
|
||||||
|
* unbind="unsetDataSourceService"
|
||||||
* <p/>
|
* <p/>
|
||||||
* Adding reference to API Manager Configuration service is an unavoidable hack to get rid of NPEs thrown while
|
* Adding reference to API Manager Configuration service is an unavoidable hack to get rid of NPEs thrown while
|
||||||
* initializing APIMgtDAOs attempting to register APIs programmatically. APIMgtDAO needs to be proper cleaned up
|
* initializing APIMgtDAOs attempting to register APIs programmatically. APIMgtDAO needs to be proper cleaned up
|
||||||
@ -47,7 +55,6 @@ public class MobileDeviceManagementServiceComponent {
|
|||||||
private ServiceRegistration androidServiceRegRef;
|
private ServiceRegistration androidServiceRegRef;
|
||||||
private ServiceRegistration iOSServiceRegRef;
|
private ServiceRegistration iOSServiceRegRef;
|
||||||
private ServiceRegistration windowsServiceRegRef;
|
private ServiceRegistration windowsServiceRegRef;
|
||||||
private ServiceRegistration serverStartupObserverRef;
|
|
||||||
|
|
||||||
private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class);
|
private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class);
|
||||||
|
|
||||||
@ -65,8 +72,7 @@ public class MobileDeviceManagementServiceComponent {
|
|||||||
MobileDataSourceConfig dsConfig =
|
MobileDataSourceConfig dsConfig =
|
||||||
config.getMobileDeviceMgtRepository().getMobileDataSourceConfig();
|
config.getMobileDeviceMgtRepository().getMobileDataSourceConfig();
|
||||||
|
|
||||||
MobileDeviceManagementDAOFactory.setMobileDataSourceConfig(dsConfig);
|
MobileDeviceManagementDAOFactory.setDatSourceConfig(dsConfig);
|
||||||
MobileDeviceManagementDAOFactory.init();
|
|
||||||
String setupOption = System.getProperty("setup");
|
String setupOption = System.getProperty("setup");
|
||||||
if (setupOption != null) {
|
if (setupOption != null) {
|
||||||
if (log.isDebugEnabled()) {
|
if (log.isDebugEnabled()) {
|
||||||
@ -121,5 +127,18 @@ public class MobileDeviceManagementServiceComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void setDataSourceService(DataSourceService dataSourceService) {
|
||||||
|
/* This is to avoid mobile device management component getting initialized before the underlying datasources
|
||||||
|
are registered */
|
||||||
|
try {
|
||||||
|
MobileDeviceManagementDAOFactory.init();
|
||||||
|
} catch (DeviceManagementException e) {
|
||||||
|
log.error("Error occurred while initializing mobile device management repository datasource", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetDataSourceService(DataSourceService dataSourceService) {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
7
pom.xml
7
pom.xml
@ -196,8 +196,13 @@
|
|||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>
|
||||||
|
<version>${carbon.kernel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--CDM core dependencies-->
|
<!-- Device Management Core dependencies -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.wso2.carbon.devicemgt</groupId>
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user