forked from community/device-mgt-core
parent
fc80514d74
commit
c40af0de7a
@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
* 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.wso2.carbon.apimgt.api.APIProvider;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlTransient;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* 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.wso2.carbon.apimgt.api.APIManagementException;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.API;
|
||||||
|
import org.wso2.carbon.apimgt.api.model.APIIdentifier;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface APIPublisherService {
|
||||||
|
|
||||||
|
void publishAPI(API api) throws APIManagementException;
|
||||||
|
|
||||||
|
void removeAPI(APIIdentifier id) throws APIManagementException;
|
||||||
|
|
||||||
|
void publishAPIs(List<API> apis) throws APIManagementException;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* 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.api.model.APIIdentifier;
|
||||||
|
import org.wso2.carbon.apimgt.impl.APIManagerFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class APIPublisherServiceImpl implements APIPublisherService {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(APIPublisherServiceImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publishAPI(API api) throws APIManagementException {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Publishing API '" + api.getId() + "'");
|
||||||
|
}
|
||||||
|
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(api.getApiOwner());
|
||||||
|
provider.addAPI(api);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Successfully published API '" + api.getId() + "' with the context '" +
|
||||||
|
api.getContext() + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAPI(APIIdentifier id) throws APIManagementException {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Removing API '" + id.getApiName() + "'");
|
||||||
|
}
|
||||||
|
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(id.getProviderName());
|
||||||
|
provider.deleteAPI(id);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("API '" + id.getApiName() + "' has been successfully removed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publishAPIs(List<API> apis) throws APIManagementException {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Publishing a batch of APIs");
|
||||||
|
}
|
||||||
|
for (API api : apis) {
|
||||||
|
this.publishAPI(api);
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Successfully published the batch of APIs");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* 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.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.util.DeviceManagerUtil;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class APIRegistrationStartupObserver implements ServerStartupObserver {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(APIRegistrationStartupObserver.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void completingServerStartup() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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 = APIPublisherConfig.getInstance().getApiConfigs();
|
||||||
|
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 = APIPublisherConfig.getInstance().getApiConfigs();
|
||||||
|
for (APIConfig apiConfig : apiConfigs) {
|
||||||
|
DeviceManagerUtil.publishAPI(apiConfig);
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Successfully published API '" + apiConfig.getName() + "' with the context '" +
|
||||||
|
apiConfig.getContext() + "' and version '" + apiConfig.getVersion() + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* 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.config.DeviceManagementConfig;
|
||||||
|
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
||||||
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
|
|
||||||
|
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 java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setApiConfig(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 Device Management configuration */
|
||||||
|
JAXBContext ctx = JAXBContext.newInstance(DeviceManagementConfig.class);
|
||||||
|
Unmarshaller unmarshaller = ctx.createUnmarshaller();
|
||||||
|
config = (APIPublisherConfig) unmarshaller.unmarshal(doc);
|
||||||
|
} catch (JAXBException e) {
|
||||||
|
throw new DeviceManagementException("Error occurred while un-marshalling API Publisher Config", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
<!--
|
||||||
|
~ 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.
|
||||||
|
-->
|
||||||
|
<APIPublisherConfig>
|
||||||
|
<APIs>
|
||||||
|
<API>
|
||||||
|
<Name>appmanager</Name>
|
||||||
|
<Owner>admin</Owner>
|
||||||
|
<Context>enrollment</Context>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
|
<Endpoint>http://localhost:9763/</Endpoint>
|
||||||
|
<Transports>http,https</Transports>
|
||||||
|
</API>
|
||||||
|
</APIs>
|
||||||
|
</APIPublisherConfig>
|
Loading…
Reference in new issue