forked from community/device-mgt-core
parent
bcb2c1ddb1
commit
b906d43cc8
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.common.configuration.mgt;
|
||||
|
||||
public class ConfigurationManagementException extends Exception {
|
||||
private static final long serialVersionUID = -3151279311929070299L;
|
||||
|
||||
private String errorMessage;
|
||||
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
public void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
public ConfigurationManagementException(String msg, Exception nestedEx) {
|
||||
super(msg, nestedEx);
|
||||
setErrorMessage(msg);
|
||||
}
|
||||
|
||||
public ConfigurationManagementException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
setErrorMessage(message);
|
||||
}
|
||||
|
||||
public ConfigurationManagementException(String msg) {
|
||||
super(msg);
|
||||
setErrorMessage(msg);
|
||||
}
|
||||
|
||||
public ConfigurationManagementException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public ConfigurationManagementException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.common.configuration.mgt;
|
||||
|
||||
/**
|
||||
* This represents the tenant configuration management functionality which should be implemented by
|
||||
* the device type plugins.
|
||||
*/
|
||||
public interface TenantConfigurationManagementService {
|
||||
|
||||
/**
|
||||
* Method to add a operation to a device or a set of devices.
|
||||
*
|
||||
* @param tenantConfiguration Operation to be added.
|
||||
* @param resourcePath Registry resource path.
|
||||
* @throws org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException If some unusual behaviour is observed while adding the
|
||||
* configuration.
|
||||
*/
|
||||
public boolean saveConfiguration(TenantConfiguration tenantConfiguration, String resourcePath) throws ConfigurationManagementException;
|
||||
|
||||
/**
|
||||
* Method to retrieve the list of general tenant configurations.
|
||||
*
|
||||
* @param resourcePath Registry resource path.
|
||||
* @throws org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException If some unusual behaviour is observed while fetching the
|
||||
* operation list.
|
||||
*/
|
||||
public TenantConfiguration getConfiguration(String resourcePath) throws ConfigurationManagementException;
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.core.config;
|
||||
|
||||
/**
|
||||
* This class holds the constants used throughout the configuration manager.
|
||||
*/
|
||||
public class ConfigurationManagerConstants {
|
||||
|
||||
public static final class ContentTypes {
|
||||
private ContentTypes() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static final String CONTENT_TYPE_ANY = "*/*";
|
||||
public static final String MEDIA_TYPE_XML = "application/xml";
|
||||
}
|
||||
|
||||
public static final class CharSets {
|
||||
private CharSets() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static final String CHARSET_UTF8 = "UTF8";
|
||||
}
|
||||
}
|
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.core.config.tenant;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration;
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfigurationManagementService;
|
||||
import org.wso2.carbon.device.mgt.core.config.ConfigurationManagerConstants;
|
||||
import org.wso2.carbon.device.mgt.core.config.util.ConfigurationManagerUtil;
|
||||
import org.wso2.carbon.registry.api.Resource;
|
||||
import org.wso2.carbon.registry.api.RegistryException;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* This class implements all the functionality exposed as part of the TenantConfigurationManagementService. Main usage of
|
||||
* this module is, saving/retrieving tenant configurations to the registry.
|
||||
*/
|
||||
public class TenantConfigurationManagementServiceImpl
|
||||
implements TenantConfigurationManagementService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(TenantConfigurationManagementServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public boolean saveConfiguration(TenantConfiguration tenantConfiguration, String resourcePath)
|
||||
throws ConfigurationManagementException {
|
||||
boolean status;
|
||||
try {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Persisting tenant configurations in Registry");
|
||||
}
|
||||
StringWriter writer = new StringWriter();
|
||||
JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class);
|
||||
Marshaller marshaller = context.createMarshaller();
|
||||
marshaller.marshal(tenantConfiguration, writer);
|
||||
|
||||
Resource resource = ConfigurationManagerUtil.getConfigurationRegistry().newResource();
|
||||
resource.setContent(writer.toString());
|
||||
resource.setMediaType(ConfigurationManagerConstants.ContentTypes.MEDIA_TYPE_XML);
|
||||
ConfigurationManagerUtil.putRegistryResource(resourcePath, resource);
|
||||
status = true;
|
||||
} catch (RegistryException e) {
|
||||
throw new ConfigurationManagementException(
|
||||
"Error occurred while persisting the Registry resource of Tenant Configuration : " + e.getMessage(), e);
|
||||
} catch (JAXBException e) {
|
||||
throw new ConfigurationManagementException(
|
||||
"Error occurred while parsing the Tenant configuration : " + e.getMessage(), e);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TenantConfiguration getConfiguration(String resourcePath)
|
||||
throws ConfigurationManagementException {
|
||||
Resource resource;
|
||||
try {
|
||||
resource = ConfigurationManagerUtil.getRegistryResource(resourcePath);
|
||||
if(resource != null){
|
||||
JAXBContext context = JAXBContext.newInstance(TenantConfiguration.class);
|
||||
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||
return (TenantConfiguration) unmarshaller.unmarshal(
|
||||
new StringReader(new String((byte[]) resource.getContent(), Charset
|
||||
.forName(ConfigurationManagerConstants.CharSets.CHARSET_UTF8))));
|
||||
}
|
||||
return new TenantConfiguration();
|
||||
} catch (JAXBException e) {
|
||||
throw new ConfigurationManagementException(
|
||||
"Error occurred while parsing the Tenant configuration : " + e.getMessage(), e);
|
||||
} catch (RegistryException e) {
|
||||
throw new ConfigurationManagementException(
|
||||
"Error occurred while retrieving the Registry resource of Tenant Configuration : " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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.core.config.util;
|
||||
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.registry.api.RegistryException;
|
||||
import org.wso2.carbon.registry.api.Resource;
|
||||
import org.wso2.carbon.registry.core.Registry;
|
||||
|
||||
public class ConfigurationManagerUtil {
|
||||
|
||||
public static Registry getConfigurationRegistry() throws ConfigurationManagementException {
|
||||
try {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
return DeviceManagementDataHolder.getInstance().getRegistryService()
|
||||
.getConfigSystemRegistry(
|
||||
tenantId);
|
||||
} catch (RegistryException e) {
|
||||
throw new ConfigurationManagementException(
|
||||
"Error in retrieving governance registry instance: " +
|
||||
e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Resource getRegistryResource(String path) throws ConfigurationManagementException {
|
||||
try {
|
||||
if(ConfigurationManagerUtil.getConfigurationRegistry().resourceExists(path)){
|
||||
return ConfigurationManagerUtil.getConfigurationRegistry().get(path);
|
||||
}
|
||||
return null;
|
||||
} catch (RegistryException e) {
|
||||
throw new ConfigurationManagementException("Error in retrieving registry resource : " +
|
||||
e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean putRegistryResource(String path,
|
||||
Resource resource)
|
||||
throws ConfigurationManagementException {
|
||||
boolean status;
|
||||
try {
|
||||
ConfigurationManagerUtil.getConfigurationRegistry().beginTransaction();
|
||||
ConfigurationManagerUtil.getConfigurationRegistry().put(path, resource);
|
||||
ConfigurationManagerUtil.getConfigurationRegistry().commitTransaction();
|
||||
status = true;
|
||||
} catch (RegistryException e) {
|
||||
throw new ConfigurationManagementException(
|
||||
"Error occurred while persisting registry resource : " +
|
||||
e.getMessage(), e);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue