From c134cc7e25c46bfe817712680900e4c3098f559c Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 28 Jul 2015 20:38:02 +0530 Subject: [PATCH] Fixing license manager initialization related issues and code cleanup --- .../DeviceManagementPluginRepository.java | 54 +++++++++++++------ .../DeviceManagementServiceComponent.java | 18 ++++++- .../DeviceManagerStartupListener.java} | 6 +-- .../pom.xml | 10 ++++ .../mgt/GenericArtifactManagerFactory.java | 8 +-- .../license/mgt/LicenseManagementService.java | 38 ------------- .../mgt/RegistryBasedLicenseManager.java | 7 ++- 7 files changed, 78 insertions(+), 63 deletions(-) rename components/device-mgt/{org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java => org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java} (84%) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 4c276405ff..ae94f52811 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -17,46 +17,70 @@ */ package org.wso2.carbon.device.mgt.core; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagerStartupListener; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -public class DeviceManagementPluginRepository { +public class DeviceManagementPluginRepository implements DeviceManagerStartupListener { private Map providers; + private boolean isInited; + private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class); public DeviceManagementPluginRepository() { - providers = new HashMap(); + providers = Collections.synchronizedMap(new HashMap()); + DeviceManagementServiceComponent.registerStartupListener(this); } public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { String deviceType = provider.getType(); - try { - /* Initializing Device Management Service Provider */ - provider.init(); - DeviceManagerUtil.registerDeviceType(deviceType); - } catch (DeviceManagementException e) { - throw new DeviceManagementException("Error occurred while adding device management provider '" + - deviceType + "'"); + synchronized (providers) { + try { + if (isInited) { + /* Initializing Device Management Service Provider */ + provider.init(); + DeviceManagerUtil.registerDeviceType(deviceType); + } + } catch (DeviceManagementException e) { + throw new DeviceManagementException("Error occurred while adding device management provider '" + + deviceType + "'", e); + } + providers.put(deviceType, provider); } - providers.put(deviceType, provider); } public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { - String deviceType = provider.getType(); - providers.remove(deviceType); + providers.remove(provider.getType()); } public DeviceManagementService getDeviceManagementService(String type) { return providers.get(type); } - public Collection getDeviceManagementProviders(){ - return providers.values(); + @Override + public void notifyObserver() { + synchronized (providers) { + for (DeviceManagementService provider : providers.values()) { + try { + provider.init(); + DeviceManagerUtil.registerDeviceType(provider.getType()); + } catch (Throwable e) { + /* Throwable is caught intentionally as failure of one plugin - due to invalid start up parameters, + etc - should not block the initialization of other device management providers */ + log.error("Error occurred while initializing device management provider '" + + provider.getType() + "'", e); + } + } + this.isInited = true; + } } } 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 4da10c562f..6acdd01d5b 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 @@ -95,8 +95,9 @@ public class DeviceManagementServiceComponent { private DeviceManagementPluginRepository pluginRepository = new DeviceManagementPluginRepository(); private static final Object LOCK = new Object(); - private static List listeners = new ArrayList(); - private static List deviceManagers = new ArrayList(); + private static List listeners = new ArrayList<>(); + private static List deviceManagers = new ArrayList<>(); + private static List startupListeners = new ArrayList<>(); @SuppressWarnings("unused") protected void activate(ComponentContext componentContext) { @@ -130,6 +131,9 @@ public class DeviceManagementServiceComponent { /* Registering declarative service instances exposed by DeviceManagementServiceComponent */ this.registerServices(componentContext); + /* This is a workaround to initialize all Device Management Service Providers after the initialization + * of Device Management Service component in order to avoid bundle start up order related complications */ + notifyStartupListeners(); if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } @@ -312,4 +316,14 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setConfigurationContextService(null); } + public static void registerStartupListener(DeviceManagerStartupListener startupListener) { + startupListeners.add(startupListener); + } + + public static void notifyStartupListeners() { + for (DeviceManagerStartupListener startupListener : startupListeners) { + startupListener.notifyObserver(); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java similarity index 84% rename from components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java index 79e6986406..29f9069ffc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagerStartupListener.java @@ -16,10 +16,10 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.extensions.license.mgt; - -public class LicenseManagementUtil { +package org.wso2.carbon.device.mgt.core.internal; +public interface DeviceManagerStartupListener { + void notifyObserver(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml index 94f880c99c..03ebbf7385 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/pom.xml @@ -56,6 +56,16 @@ org.wso2.carbon.device.mgt.extensions.* + + org.wso2.carbon.governance.api.*, + javax.xml.namespace, + org.wso2.carbon.context, + org.wso2.carbon.device.mgt.common.license.mgt, + org.wso2.carbon.registry.api, + org.wso2.carbon.registry.core, + org.wso2.carbon.registry.core.exceptions, + org.wso2.carbon.registry.core.session + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java index a5bea93668..1b52502a36 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/GenericArtifactManagerFactory.java @@ -22,8 +22,10 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.governance.api.generic.GenericArtifactManager; +import org.wso2.carbon.governance.api.util.GovernanceUtils; import org.wso2.carbon.registry.api.Registry; import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.registry.core.session.UserRegistry; import java.util.HashMap; import java.util.Map; @@ -32,14 +34,14 @@ public class GenericArtifactManagerFactory { private static Map tenantArtifactManagers = new HashMap(); - private static final Object lock = new Object(); + private static final Object LOCK = new Object(); public static GenericArtifactManager getTenantAwareGovernanceArtifactManager( Registry registry) throws LicenseManagementException { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GenericArtifactManager artifactManager; - synchronized (lock) { + synchronized (LOCK) { artifactManager = tenantArtifactManagers.get(tenantId); if (artifactManager == null) { @@ -54,7 +56,7 @@ public class GenericArtifactManagerFactory { return artifactManager; } catch (RegistryException e) { throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " + - "associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'"); + "associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java deleted file mode 100644 index 2a417cf799..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/LicenseManagementService.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.extensions.license.mgt; - -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.license.mgt.LicenseManager; - -public class LicenseManagementService implements LicenseManager { - - @Override - public License getLicense(String deviceType, String languageCode) throws LicenseManagementException { - //return DeviceManagementDataHolder.getInstance().getLicenseManager().getLicense(deviceType, languageCode); - return null; - } - - @Override - public void addLicense(String deviceType, License license) throws LicenseManagementException { - //return DeviceManagementDataHolder.getInstance().getLicenseManager().addLicense(deviceType, license); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java index 019259d532..b5b092ddb2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/RegistryBasedLicenseManager.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.mgt.extensions.license.mgt; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; @@ -41,9 +43,10 @@ public class RegistryBasedLicenseManager implements LicenseManager { private Registry registry; private GenericArtifactManager artifactManager; - public RegistryBasedLicenseManager(Registry registry) { + public RegistryBasedLicenseManager() { + Registry registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_GOVERNANCE); if (registry == null) { - throw new IllegalArgumentException("Registry instance provided is null. Hence, " + + throw new IllegalArgumentException("Registry instance retrieved is null. Hence, " + "'Registry based license manager cannot be initialized'"); } this.registry = registry;