From a2dc916fd04d48f1960e4ed968c859ca50e34c8f Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 18 Jun 2015 18:32:14 +0530 Subject: [PATCH] Fixing issues in plugin initialization as a result of certain plugins getting initialized after the device management component is done getting configured --- .../DeviceManagementServiceComponent.java | 8 ++++++ .../PluginInitializationListener.java | 27 +++++++++++++++++++ .../DeviceManagementProviderServiceImpl.java | 20 +++++++++++++- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/PluginInitializationListener.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index e2d65269df..26c083ff18 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -108,6 +108,7 @@ public class DeviceManagementServiceComponent { private static final Object LOCK = new Object(); private boolean isInitialized; private List deviceManagementServices = new ArrayList(); + private static List listeners = new ArrayList(); protected void activate(ComponentContext componentContext) { try { @@ -168,6 +169,10 @@ public class DeviceManagementServiceComponent { //do nothing } + public static void registerPluginInitializationListener(PluginInitializationListener listener) { + listeners.add(listener); + } + private void initLicenseManager() throws LicenseManagementException { LicenseConfigurationManager.getInstance().initConfig(); LicenseConfig licenseConfig = @@ -242,6 +247,9 @@ public class DeviceManagementServiceComponent { private void registerDeviceManagementProvider(DeviceManagementService deviceManagementService) { try { this.getPluginRepository().addDeviceManagementProvider(deviceManagementService); + for (PluginInitializationListener listener : listeners) { + listener.notify(deviceManagementService); + } } catch (DeviceManagementException e) { log.error("Error occurred while adding device management provider '" + deviceManagementService.getProviderType() + "'"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/PluginInitializationListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/PluginInitializationListener.java new file mode 100644 index 0000000000..2551b0fe51 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/PluginInitializationListener.java @@ -0,0 +1,27 @@ +/* + * 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.core.internal; + +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; + +public interface PluginInitializationListener { + + void notify(DeviceManagementService deviceManagementService); + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 48d42893cf..f19a23b036 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementRepository; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.email.NotificationMessages; @@ -34,7 +35,9 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.*; import org.wso2.carbon.device.mgt.core.email.EmailConstants; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; +import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import java.io.IOException; @@ -43,7 +46,8 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; -public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService { +public class DeviceManagementProviderServiceImpl implements + DeviceManagementProviderService, PluginInitializationListener { private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; @@ -53,6 +57,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class); public DeviceManagementProviderServiceImpl(DeviceManagementRepository pluginRepository) { + /* Registering a listener to retrieve events when some device management service plugin is installed after + * the component is done getting initialized */ + DeviceManagementServiceComponent.registerPluginInitializationListener(this); + this.pluginRepository = pluginRepository; this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); @@ -627,4 +635,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } + @Override + public void notify(DeviceManagementService deviceManagementService) { + try { + pluginRepository.addDeviceManagementProvider(deviceManagementService); + } catch (DeviceManagementException e) { + log.error("Error occurred while registering device management plugin '" + + deviceManagementService.getProviderType() + "'", e); + } + } + }