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 4c276405ffe..ae94f528118 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 4da10c562f4..6acdd01d5ba 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 79e69864064..29f9069ffcf 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 94f880c99cd..03ebbf73856 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 a5bea93668e..1b52502a361 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 2a417cf7998..00000000000 --- 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 019259d5324..b5b092ddb29 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;