Fixing license manager initialization related issues and code cleanup

revert-70aa11f8
prabathabey 9 years ago
parent ca38cc9f51
commit c134cc7e25

@ -17,46 +17,70 @@
*/ */
package org.wso2.carbon.device.mgt.core; 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.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; 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 org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.util.Collection; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class DeviceManagementPluginRepository { public class DeviceManagementPluginRepository implements DeviceManagerStartupListener {
private Map<String, DeviceManagementService> providers; private Map<String, DeviceManagementService> providers;
private boolean isInited;
private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class);
public DeviceManagementPluginRepository() { public DeviceManagementPluginRepository() {
providers = new HashMap<String, DeviceManagementService>(); providers = Collections.synchronizedMap(new HashMap<String, DeviceManagementService>());
DeviceManagementServiceComponent.registerStartupListener(this);
} }
public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { public void addDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
String deviceType = provider.getType(); String deviceType = provider.getType();
synchronized (providers) {
try { try {
if (isInited) {
/* Initializing Device Management Service Provider */ /* Initializing Device Management Service Provider */
provider.init(); provider.init();
DeviceManagerUtil.registerDeviceType(deviceType); DeviceManagerUtil.registerDeviceType(deviceType);
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
throw new DeviceManagementException("Error occurred while adding device management provider '" + throw new DeviceManagementException("Error occurred while adding device management provider '" +
deviceType + "'"); deviceType + "'", e);
} }
providers.put(deviceType, provider); providers.put(deviceType, provider);
} }
}
public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException {
String deviceType = provider.getType(); providers.remove(provider.getType());
providers.remove(deviceType);
} }
public DeviceManagementService getDeviceManagementService(String type) { public DeviceManagementService getDeviceManagementService(String type) {
return providers.get(type); return providers.get(type);
} }
public Collection<DeviceManagementService> getDeviceManagementProviders(){ @Override
return providers.values(); 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;
}
} }
} }

@ -95,8 +95,9 @@ public class DeviceManagementServiceComponent {
private DeviceManagementPluginRepository pluginRepository = new DeviceManagementPluginRepository(); private DeviceManagementPluginRepository pluginRepository = new DeviceManagementPluginRepository();
private static final Object LOCK = new Object(); private static final Object LOCK = new Object();
private static List<PluginInitializationListener> listeners = new ArrayList<PluginInitializationListener>(); private static List<PluginInitializationListener> listeners = new ArrayList<>();
private static List<DeviceManagementService> deviceManagers = new ArrayList<DeviceManagementService>(); private static List<DeviceManagementService> deviceManagers = new ArrayList<>();
private static List<DeviceManagerStartupListener> startupListeners = new ArrayList<>();
@SuppressWarnings("unused") @SuppressWarnings("unused")
protected void activate(ComponentContext componentContext) { protected void activate(ComponentContext componentContext) {
@ -130,6 +131,9 @@ public class DeviceManagementServiceComponent {
/* Registering declarative service instances exposed by DeviceManagementServiceComponent */ /* Registering declarative service instances exposed by DeviceManagementServiceComponent */
this.registerServices(componentContext); 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()) { if (log.isDebugEnabled()) {
log.debug("Device management core bundle has been successfully initialized"); log.debug("Device management core bundle has been successfully initialized");
} }
@ -312,4 +316,14 @@ public class DeviceManagementServiceComponent {
DeviceManagementDataHolder.getInstance().setConfigurationContextService(null); DeviceManagementDataHolder.getInstance().setConfigurationContextService(null);
} }
public static void registerStartupListener(DeviceManagerStartupListener startupListener) {
startupListeners.add(startupListener);
}
public static void notifyStartupListeners() {
for (DeviceManagerStartupListener startupListener : startupListeners) {
startupListener.notifyObserver();
}
}
} }

@ -16,10 +16,10 @@
* under the License. * under the License.
* *
*/ */
package org.wso2.carbon.device.mgt.extensions.license.mgt; package org.wso2.carbon.device.mgt.core.internal;
public class LicenseManagementUtil {
public interface DeviceManagerStartupListener {
void notifyObserver();
} }

@ -56,6 +56,16 @@
<Export-Package> <Export-Package>
org.wso2.carbon.device.mgt.extensions.* org.wso2.carbon.device.mgt.extensions.*
</Export-Package> </Export-Package>
<Import-Package>
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
</Import-Package>
</instructions> </instructions>
</configuration> </configuration>
</plugin> </plugin>

@ -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.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException;
import org.wso2.carbon.governance.api.generic.GenericArtifactManager; 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.api.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -32,14 +34,14 @@ public class GenericArtifactManagerFactory {
private static Map<Integer, GenericArtifactManager> tenantArtifactManagers = private static Map<Integer, GenericArtifactManager> tenantArtifactManagers =
new HashMap<Integer, GenericArtifactManager>(); new HashMap<Integer, GenericArtifactManager>();
private static final Object lock = new Object(); private static final Object LOCK = new Object();
public static GenericArtifactManager getTenantAwareGovernanceArtifactManager( public static GenericArtifactManager getTenantAwareGovernanceArtifactManager(
Registry registry) throws LicenseManagementException { Registry registry) throws LicenseManagementException {
try { try {
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
GenericArtifactManager artifactManager; GenericArtifactManager artifactManager;
synchronized (lock) { synchronized (LOCK) {
artifactManager = artifactManager =
tenantArtifactManagers.get(tenantId); tenantArtifactManagers.get(tenantId);
if (artifactManager == null) { if (artifactManager == null) {
@ -54,7 +56,7 @@ public class GenericArtifactManagerFactory {
return artifactManager; return artifactManager;
} catch (RegistryException e) { } catch (RegistryException e) {
throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " + throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " +
"associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'"); "associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'", e);
} }
} }

@ -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);
}
}

@ -19,6 +19,8 @@
package org.wso2.carbon.device.mgt.extensions.license.mgt; 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.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.license.mgt.License; 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.LicenseManagementException;
@ -41,9 +43,10 @@ public class RegistryBasedLicenseManager implements LicenseManager {
private Registry registry; private Registry registry;
private GenericArtifactManager artifactManager; private GenericArtifactManager artifactManager;
public RegistryBasedLicenseManager(Registry registry) { public RegistryBasedLicenseManager() {
Registry registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_GOVERNANCE);
if (registry == null) { 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'"); "'Registry based license manager cannot be initialized'");
} }
this.registry = registry; this.registry = registry;

Loading…
Cancel
Save