Removing API registration logic from mobile plugin implementation

merge-requests/1/head
prabathabey 10 years ago
parent 83fbfc98be
commit 340cf2a003

@ -66,7 +66,6 @@
org.wso2.carbon.core,
org.wso2.carbon.utils.*,
org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.apimgt.*;
</Import-Package>
<Export-Package>
!org.wso2.carbon.device.mgt.mobile.internal,
@ -117,14 +116,6 @@
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.common</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.api</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.impl</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>

@ -1,87 +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.mobile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.core.ServerStartupObserver;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.config.APIConfig;
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager;
import org.wso2.carbon.device.mgt.mobile.util.DeviceManagementAPIPublisherUtil;
import java.util.List;
public class MobileDeviceManagementStartupObserver implements ServerStartupObserver {
private static final Log log = LogFactory.getLog(MobileDeviceManagementStartupObserver.class);
public void completingServerStartup() {
}
public void completedServerStartup() {
try {
this.initAPIConfigs();
/* Publish all mobile device management related JAX-RS services as APIs */
this.publishAPIs();
} catch (DeviceManagementException e) {
log.error("Error occurred while publishing Mobile Device Management related APIs", e);
}
}
private void initAPIConfigs() throws DeviceManagementException {
if (log.isDebugEnabled()) {
log.debug("Initializing Mobile Device Management related APIs");
}
List<APIConfig> apiConfigs =
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
getApiPublisherConfig().getAPIs();
for (APIConfig apiConfig : apiConfigs) {
try {
APIProvider provider =
APIManagerFactory.getInstance().getAPIProvider(apiConfig.getOwner());
apiConfig.init(provider);
} catch (APIManagementException e) {
throw new DeviceManagementException("Error occurred while initializing API Config '" +
apiConfig.getName() + "'", e);
}
}
}
private void publishAPIs() throws DeviceManagementException {
if (log.isDebugEnabled()) {
log.debug("Publishing Mobile Device Management related APIs");
}
List<APIConfig> apiConfigs =
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
getApiPublisherConfig().getAPIs();
for (APIConfig apiConfig : apiConfigs) {
DeviceManagementAPIPublisherUtil.publishAPI(apiConfig);
if (log.isDebugEnabled()) {
log.debug("Successfully published API '" + apiConfig.getName() + "' with the context '" +
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'");
}
}
}
}

@ -1,101 +0,0 @@
/*
* Copyright (c) 2014, 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.mobile.config;
import org.wso2.carbon.apimgt.api.APIProvider;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement(name = "API")
public class APIConfig {
private String name;
private String owner;
private String context;
private String endpoint;
private String version;
private String transports;
private APIProvider provider;
public void init(APIProvider provider) {
this.provider = provider;
}
@XmlTransient
public APIProvider getProvider() {
return provider;
}
@XmlElement(name = "Name", nillable = false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "Owner", nillable = false)
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
@XmlElement(name = "Context", nillable = false)
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
@XmlElement(name = "Endpoint", nillable = false)
public String getEndpoint() {
return endpoint;
}
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
@XmlElement(name = "Version", nillable = false)
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@XmlElement(name = "Transports", nillable = false)
public String getTransports() {
return transports;
}
public void setTransports(String transports) {
this.transports = transports;
}
}

@ -1,41 +0,0 @@
/*
* Copyright (c) 2014, 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.mobile.config;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "APIPublisher")
public class APIPublisherConfig {
private List<APIConfig> apis;
@XmlElementWrapper(name = "APIs", nillable = false, required = true)
@XmlElement(name = "API", nillable = false)
public List<APIConfig> getAPIs() {
return apis;
}
public void setAPIs(List<APIConfig> apis) {
this.apis = apis;
}
}

@ -28,7 +28,6 @@ import javax.xml.bind.annotation.XmlRootElement;
public final class MobileDeviceManagementConfig {
private MobileDeviceManagementRepository mobileDeviceMgtRepository;
private APIPublisherConfig apiPublisherConfig;
@XmlElement(name = "ManagementRepository", nillable = false)
public MobileDeviceManagementRepository getMobileDeviceMgtRepository() {
@ -40,13 +39,4 @@ public final class MobileDeviceManagementConfig {
this.mobileDeviceMgtRepository = mobileDeviceMgtRepository;
}
@XmlElement(name = "APIPublisher")
public APIPublisherConfig getApiPublisherConfig() {
return apiPublisherConfig;
}
public void setApiPublisherConfig(APIPublisherConfig apiPublisherConfig) {
this.apiPublisherConfig = apiPublisherConfig;
}
}

@ -21,13 +21,8 @@ package org.wso2.carbon.device.mgt.mobile.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.*;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.mobile.DataSourceListener;
import org.wso2.carbon.device.mgt.mobile.config.APIConfig;
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager;
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig;
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
@ -35,7 +30,6 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManager;
import org.wso2.carbon.device.mgt.mobile.util.DeviceManagementAPIPublisherUtil;
import java.util.ArrayList;
import java.util.List;
@ -83,11 +77,6 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B
bundleContext.registerService(DeviceManager.class.getName(),
new WindowsDeviceManager(), null);
/* Initialize all API configurations with corresponding API Providers */
this.initAPIConfigs();
/* Publish all mobile device management related JAX-RS services as APIs */
this.publishAPIs();
if (log.isDebugEnabled()) {
log.debug("Mobile Device Management Service bundle is activated");
}
@ -107,10 +96,6 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B
windowsServiceRegRef.unregister();
bundleContext.removeBundleListener(this);
/* Removing all APIs published upon start-up for mobile device management related JAX-RS
services */
this.removeAPIs();
} catch (Throwable e) {
log.error("Error occurred while de-activating Mobile Device Management bundle", e);
}
@ -137,39 +122,5 @@ public class MobileDeviceManagementBundleActivator implements BundleActivator, B
return dataSourceListeners;
}
private void initAPIConfigs() throws DeviceManagementException {
List<APIConfig> apiConfigs =
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
getApiPublisherConfig().getAPIs();
for (APIConfig apiConfig : apiConfigs) {
try {
APIProvider provider =
APIManagerFactory.getInstance().getAPIProvider(apiConfig.getOwner());
apiConfig.init(provider);
} catch (APIManagementException e) {
throw new DeviceManagementException(
"Error occurred while initializing API Config '" +
apiConfig.getName() + "'", e);
}
}
}
private void publishAPIs() throws DeviceManagementException {
List<APIConfig> apiConfigs =
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
getApiPublisherConfig().getAPIs();
for (APIConfig apiConfig : apiConfigs) {
DeviceManagementAPIPublisherUtil.publishAPI(apiConfig);
}
}
private void removeAPIs() throws DeviceManagementException {
List<APIConfig> apiConfigs =
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
getApiPublisherConfig().getAPIs();
for (APIConfig apiConfig : apiConfigs) {
DeviceManagementAPIPublisherUtil.removeAPI(apiConfig);
}
}
}

@ -23,12 +23,8 @@ import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService;
import org.wso2.carbon.core.ServerStartupObserver;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.spi.DeviceManager;
import org.wso2.carbon.device.mgt.mobile.MobileDeviceManagementStartupObserver;
import org.wso2.carbon.device.mgt.mobile.config.APIConfig;
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager;
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig;
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
@ -37,19 +33,10 @@ import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.ios.IOSDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManager;
import org.wso2.carbon.device.mgt.mobile.util.DeviceManagementAPIPublisherUtil;
import java.util.List;
/**
* @scr.component name="org.wso2.carbon.device.mgt.mobile.impl.internal.MobileDeviceManagementServiceComponent"
* immediate="true"
* @scr.reference name="api.manager.config.service"
* interface="org.wso2.carbon.apimgt.impl.APIManagerConfigurationService"
* cardinality="1..1"
* policy="dynamic"
* bind="setAPIManagerConfigurationService"
* unbind="unsetAPIManagerConfigurationService"
* <p/>
* Adding reference to API Manager Configuration service is an unavoidable hack to get rid of NPEs thrown while
* initializing APIMgtDAOs attempting to register APIs programmatically. APIMgtDAO needs to be proper cleaned up
@ -102,8 +89,6 @@ public class MobileDeviceManagementServiceComponent {
windowsServiceRegRef =
bundleContext.registerService(DeviceManager.class.getName(), new WindowsDeviceManager(), null);
serverStartupObserverRef = bundleContext.registerService(ServerStartupObserver.class,
new MobileDeviceManagementStartupObserver(), null);
if (log.isDebugEnabled()) {
log.debug("Mobile Device Management Service Component has been successfully activated");
}
@ -122,9 +107,6 @@ public class MobileDeviceManagementServiceComponent {
windowsServiceRegRef.unregister();
serverStartupObserverRef.unregister();
/* Removing all APIs published upon start-up for mobile device management related JAX-RS
services */
this.removeAPIs();
if (log.isDebugEnabled()) {
log.debug(
"Mobile Device Management Service Component has been successfully de-activated");
@ -134,21 +116,5 @@ public class MobileDeviceManagementServiceComponent {
}
}
private void removeAPIs() throws DeviceManagementException {
List<APIConfig> apiConfigs =
MobileDeviceConfigurationManager.getInstance().getMobileDeviceManagementConfig().
getApiPublisherConfig().getAPIs();
for (APIConfig apiConfig : apiConfigs) {
DeviceManagementAPIPublisherUtil.removeAPI(apiConfig);
}
}
protected void setAPIManagerConfigurationService(APIManagerConfigurationService service) {
//do nothing
}
protected void unsetAPIManagerConfigurationService(APIManagerConfigurationService service) {
//do nothing
}
}

@ -1,112 +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.mobile.util;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.api.model.API;
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
import org.wso2.carbon.apimgt.api.model.APIStatus;
import org.wso2.carbon.apimgt.api.model.URITemplate;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.mobile.config.APIConfig;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class DeviceManagementAPIPublisherUtil {
enum HTTPMethod {
GET, POST, DELETE, PUT, OPTIONS
}
private static List<HTTPMethod> httpMethods;
static {
httpMethods = new ArrayList<HTTPMethod>();
httpMethods.add(HTTPMethod.GET);
httpMethods.add(HTTPMethod.POST);
httpMethods.add(HTTPMethod.DELETE);
httpMethods.add(HTTPMethod.PUT);
httpMethods.add(HTTPMethod.OPTIONS);
}
public static void publishAPI(APIConfig config) throws DeviceManagementException {
APIProvider provider = config.getProvider();
APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion());
API api = new API(id);
try {
api.setContext(config.getContext());
api.setUrl(config.getVersion());
api.setUriTemplates(getURITemplates(config.getEndpoint(),
APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN));
api.setVisibility(APIConstants.API_GLOBAL_VISIBILITY);
api.addAvailableTiers(provider.getTiers());
api.setEndpointSecured(false);
api.setStatus(APIStatus.PUBLISHED);
api.setTransports(config.getTransports());
provider.addAPI(api);
} catch (APIManagementException e) {
throw new DeviceManagementException("Error occurred while registering the API", e);
}
}
public static void removeAPI(APIConfig config) throws DeviceManagementException {
try {
APIProvider provider = config.getProvider();
APIIdentifier id = new APIIdentifier(config.getOwner(), config.getName(), config.getVersion());
provider.deleteAPI(id);
} catch (APIManagementException e) {
throw new DeviceManagementException("Error occurred while removing API", e);
}
}
private static Set<URITemplate> getURITemplates(String endpoint, String authType) {
Set<URITemplate> uriTemplates = new LinkedHashSet<URITemplate>();
if (APIConstants.AUTH_NO_AUTHENTICATION.equals(authType)) {
for (HTTPMethod method : httpMethods) {
URITemplate template = new URITemplate();
template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
template.setHTTPVerb(method.toString());
template.setResourceURI(endpoint);
template.setUriTemplate("/*");
uriTemplates.add(template);
}
} else {
for (HTTPMethod method : httpMethods) {
URITemplate template = new URITemplate();
if (HTTPMethod.OPTIONS.equals(method)) {
template.setAuthType(APIConstants.AUTH_NO_AUTHENTICATION);
} else {
template.setAuthType(APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN);
}
template.setHTTPVerb(method.toString());
template.setResourceURI(endpoint);
template.setUriTemplate("/*");
uriTemplates.add(template);
}
}
return uriTemplates;
}
}
Loading…
Cancel
Save