Fixing license manager initialization related issues and code cleanup

4.x.x
prabathabey 9 years ago
parent ca38cc9f51
commit c134cc7e25

@ -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<String, DeviceManagementService> providers;
private boolean isInited;
private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class);
public DeviceManagementPluginRepository() {
providers = new HashMap<String, DeviceManagementService>();
providers = Collections.synchronizedMap(new HashMap<String, DeviceManagementService>());
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<DeviceManagementService> 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;
}
}
}

@ -95,8 +95,9 @@ public class DeviceManagementServiceComponent {
private DeviceManagementPluginRepository pluginRepository = new DeviceManagementPluginRepository();
private static final Object LOCK = new Object();
private static List<PluginInitializationListener> listeners = new ArrayList<PluginInitializationListener>();
private static List<DeviceManagementService> deviceManagers = new ArrayList<DeviceManagementService>();
private static List<PluginInitializationListener> listeners = new ArrayList<>();
private static List<DeviceManagementService> deviceManagers = new ArrayList<>();
private static List<DeviceManagerStartupListener> 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();
}
}
}

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

@ -56,6 +56,16 @@
<Export-Package>
org.wso2.carbon.device.mgt.extensions.*
</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>
</configuration>
</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.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<Integer, GenericArtifactManager> tenantArtifactManagers =
new HashMap<Integer, GenericArtifactManager>();
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);
}
}

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

Loading…
Cancel
Save