Removing user-api-publisher-config.xml and making JAX-RS services get auto-published as APIs depending on parameters configured in web.xml of each corresponding web applications

revert-70aa11f8
prabathabey 10 years ago
parent bd75f70a24
commit 6c857fdc4d

@ -83,7 +83,9 @@
org.wso2.carbon.identity.oauth.stub,
org.wso2.carbon.identity.oauth.stub.dto,
org.wso2.carbon.ndatasource.core,
org.apache.catalina
org.apache.catalina,
org.apache.catalina.core,
javax.servlet
</Import-Package>
<Export-Package>
!org.wso2.carbon.device.mgt.core.internal,
@ -212,6 +214,10 @@
<groupId>org.wso2.tomcat</groupId>
<artifactId>tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
</dependency>
</dependencies>
</project>

@ -53,6 +53,7 @@ public class APIConfig {
private String version;
private String transports;
private APIProvider provider;
private boolean isSecured;
public void init() throws DeviceManagementException {
try {
@ -126,4 +127,14 @@ public class APIConfig {
this.transports = transports;
}
@XmlElement(name = "isSecured", required = false)
public boolean isSecured() {
return isSecured;
}
@SuppressWarnings("unused")
public void setSecured(boolean secured) {
isSecured = secured;
}
}

@ -1,82 +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.core.api.mgt;
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.api.model.API;
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.core.api.mgt.config.APIPublisherConfig;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import java.util.List;
/**
* This particular class corresponding to the ServerStartupObserver written for publishing the set of APIs used by
* the device management related components.
*
* Note: Using this particular approach is not a must, had there been a proper programming interface provided by the
* underlying API-Management infrastructure for manipulating the APIs. Even though, there's one, its concrete
* implementation consumes a set of OSGi declarative services for initializing some of its internal states, which
* prevents us from, simply, instantiating the APIPublisher implementation and using for device management related
* tasks. The aforesaid complication lead us to go for this alternative approach to get the same done.
*/
public class APIRegistrationStartupObserver implements ServerStartupObserver {
private static final Log log = LogFactory.getLog(APIRegistrationStartupObserver.class);
@Override
public void completingServerStartup() {
}
@Override
public void completedServerStartup() {
/* Publish all mobile device management related JAX-RS services as APIs */
if (log.isDebugEnabled()) {
log.debug("Publishing all mobile device management related JAX-RS services as APIs");
}
List<APIConfig> apiConfigs = APIPublisherConfig.getInstance().getApiConfigs();
for (APIConfig apiConfig : apiConfigs) {
try {
/* API Config is initialized at this point in order to avoid OSGi declarative services which
the APIManagerComponent depend on, are deployed and initialized before invoking methods in
APIManagerFactory */
apiConfig.init();
API api = DeviceManagerUtil.getAPI(apiConfig);
DeviceManagementDataHolder.getInstance().getApiPublisherService().publishAPI(api);
} catch (Throwable e) {
/* Throwable is caught as none of the RuntimeExceptions that can potentially occur at this point
does not seem to be logged anywhere else within the framework */
log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" +
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'", e);
}
}
if (log.isDebugEnabled()) {
log.debug("End of publishing all mobile device management related JAX-RS services as APIs");
}
}
}

@ -1,110 +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.core.api.mgt.config;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.utils.CarbonUtils;
import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import java.io.File;
import java.util.List;
/**
* This class carries all API configurations used by device management components, that need to be published within
* the underlying API-Management infrastructure.
*/
@XmlRootElement(name = "APIPublisherConfig")
public class APIPublisherConfig {
private List<APIConfig> apiConfigs;
private static APIPublisherConfig config;
private static final Log log = LogFactory.getLog(APIPublisherConfig.class);
private static final String USER_DEFINED_API_CONFIG_PATH =
CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "user-api-publisher-config.xml";
private static final String USER_DEFINED_API_CONFIG_SCHEMA_PATH =
"resources/config/schema/api-publisher-config-schema.xsd";
private static final Object LOCK = new Object();
public static APIPublisherConfig getInstance() {
if (config == null) {
synchronized (LOCK) {
try {
init();
} catch (DeviceManagementException e) {
log.error("Error occurred while initializing API Publisher Config", e);
}
}
}
return config;
}
@XmlElementWrapper(name = "APIs", required = true)
@XmlElement(name = "API", required = true)
public List<APIConfig> getApiConfigs() {
return apiConfigs;
}
@SuppressWarnings("unused")
public void setApiConfigs(List<APIConfig> apiConfigs) {
this.apiConfigs = apiConfigs;
}
private static void init() throws DeviceManagementException {
try {
File publisherConfig = new File(APIPublisherConfig.USER_DEFINED_API_CONFIG_PATH);
Document doc = DeviceManagerUtil.convertToDocument(publisherConfig);
/* Un-marshaling API publisher configuration */
JAXBContext ctx = JAXBContext.newInstance(APIPublisherConfig.class);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
//unmarshaller.setSchema(getSchema());
config = (APIPublisherConfig) unmarshaller.unmarshal(doc);
} catch (JAXBException e) {
throw new DeviceManagementException("Error occurred while un-marshalling API Publisher Config", e);
}
}
private static Schema getSchema() throws DeviceManagementException {
try {
File deviceManagementSchemaConfig = new File(APIPublisherConfig.USER_DEFINED_API_CONFIG_SCHEMA_PATH);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
return factory.newSchema(deviceManagementSchemaConfig);
} catch (SAXException e) {
throw new DeviceManagementException("Error occurred while initializing the schema of " +
"user-api-publisher-config.xml", e);
}
}
}

@ -0,0 +1,131 @@
/*
* 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.core.api.mgt.lifecycle.listener;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.core.StandardContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.apimgt.api.model.API;
import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import javax.servlet.ServletContext;
public class APIPublisherLifecycleListener implements LifecycleListener {
private static final String API_CONFIG_DEFAULT_VERSION = "1.0.0";
private static final Log log = LogFactory.getLog(APIPublisherLifecycleListener.class);
@Override
public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
if (Lifecycle.AFTER_START_EVENT.equals(lifecycleEvent.getType())) {
StandardContext context = (StandardContext) lifecycleEvent.getLifecycle();
ServletContext servletContext = context.getServletContext();
String param = servletContext.getInitParameter("managed-api-enabled");
boolean isManagedApi = (param != null && !"".equals(param)) && Boolean.parseBoolean(param);
if (isManagedApi) {
APIConfig apiConfig = this.buildApiConfig(servletContext);
try {
apiConfig.init();
API api = DeviceManagerUtil.getAPI(apiConfig);
DeviceManagementDataHolder.getInstance().getApiPublisherService().publishAPI(api);
} catch (Throwable e) {
/* Throwable is caught as none of the RuntimeExceptions that can potentially occur at this point
does not seem to be logged anywhere else within the framework */
log.error("Error occurred while publishing API '" + apiConfig.getName() + "' with the context '" +
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'", e);
}
}
}
}
private APIConfig buildApiConfig(ServletContext servletContext) {
APIConfig apiConfig = new APIConfig();
String name = servletContext.getInitParameter("managed-api-name");
if (name == null || "".equals(name)) {
if (log.isDebugEnabled()) {
log.debug("'managed-api-name' attribute is not configured. Therefore, using the default, " +
"which is the name of the web application");
}
name = servletContext.getServletContextName();
}
apiConfig.setName(name);
String version = servletContext.getInitParameter("managed-api-version");
if (version == null || "".equals(version)) {
if (log.isDebugEnabled()) {
log.debug("'managed-api-version' attribute is not configured. Therefore, using the " +
"default, which is '1.0.0'");
}
version = API_CONFIG_DEFAULT_VERSION;
}
apiConfig.setVersion(version);
String context = servletContext.getInitParameter("managed-api-context");
if (context == null || "".equals(context)) {
if (log.isDebugEnabled()) {
log.debug("'managed-api-context' attribute is not configured. Therefore, using the default, " +
"which is the original context assigned to the web application");
}
context = servletContext.getContextPath();
}
apiConfig.setContext(context);
String endpoint = servletContext.getInitParameter("managed-api-endpoint");
if (endpoint == null || "".equals(endpoint)) {
if (log.isDebugEnabled()) {
log.debug("'managed-api-endpoint' attribute is not configured");
}
}
apiConfig.setEndpoint(endpoint);
String owner = servletContext.getInitParameter("managed-api-owner");
if (owner == null || "".equals(owner)) {
if (log.isDebugEnabled()) {
log.debug("'managed-api-owner' attribute is not configured");
}
}
apiConfig.setOwner(owner);
String isSecuredParam = servletContext.getInitParameter("managed-api-isSecured");
boolean isSecured =
(isSecuredParam != null && !"".equals(isSecuredParam)) && Boolean.parseBoolean(isSecuredParam);
apiConfig.setSecured(isSecured);
String transports = servletContext.getInitParameter("managed-api-transports");
if (transports == null || "".equals(transports)) {
if (log.isDebugEnabled()) {
log.debug("'managed-api-transports' attribute is not configured. Therefore using the defaults, " +
"which are 'http' and 'https'");
}
transports = "http,https";
}
apiConfig.setTransports(transports);
return apiConfig;
}
}

@ -35,7 +35,6 @@ import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherService;
import org.wso2.carbon.device.mgt.core.api.mgt.APIPublisherServiceImpl;
import org.wso2.carbon.device.mgt.core.api.mgt.APIRegistrationStartupObserver;
import org.wso2.carbon.device.mgt.core.api.mgt.ApplicationManagementProviderService;
import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagerProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig;
@ -198,8 +197,6 @@ public class DeviceManagementServiceComponent {
DeviceManagementDataHolder.getInstance().setApiPublisherService(publisher);
bundleContext.registerService(APIPublisherService.class, publisher, null);
bundleContext.registerService(ServerStartupObserver.class, new APIRegistrationStartupObserver(), null);
/* Registering App Management service */
try {
AppManagementConfigurationManager.getInstance().initConfig();

Loading…
Cancel
Save