All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.windowsazure.serviceruntime.XmlRoleEnvironmentDataDeserializer Maven / Gradle / Ivy

There is a newer version: 0.4.6
Show newest version
/**
 * Copyright Microsoft Corporation
 * 
 * Licensed 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 com.microsoft.windowsazure.serviceruntime;

import java.io.InputStream;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 * 
 */
class XmlRoleEnvironmentDataDeserializer implements RoleEnvironmentDataDeserializer {
    public XmlRoleEnvironmentDataDeserializer() {
    }

    @Override
    public RoleEnvironmentData deserialize(InputStream stream) {
        try {
            JAXBContext context = JAXBContext.newInstance(RoleEnvironmentInfo.class.getPackage().getName());
            Unmarshaller unmarshaller = context.createUnmarshaller();

            @SuppressWarnings("unchecked")
            RoleEnvironmentInfo environmentInfo = ((JAXBElement) unmarshaller.unmarshal(stream))
                    .getValue();

            Map configurationSettings = translateConfigurationSettings(environmentInfo);
            Map localResources = translateLocalResources(environmentInfo);
            RoleInstance currentInstance = translateCurrentInstance(environmentInfo);
            Map roles = translateRoles(environmentInfo, currentInstance, environmentInfo
                    .getCurrentInstance().getRoleName());

            return new RoleEnvironmentData(environmentInfo.getDeployment().getId(), configurationSettings,
                    localResources, currentInstance, roles, environmentInfo.getDeployment().isEmulated());
        }
        catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    private Map translateConfigurationSettings(RoleEnvironmentInfo environmentInfo) {
        Map configurationSettings = new HashMap();

        for (ConfigurationSettingInfo settingInfo : environmentInfo.getCurrentInstance().getConfigurationSettings()
                .getConfigurationSetting()) {
            configurationSettings.put(settingInfo.getName(), settingInfo.getValue());
        }

        return configurationSettings;
    }

    private Map translateLocalResources(RoleEnvironmentInfo environmentInfo) {
        Map localResources = new HashMap();

        for (LocalResourceInfo resourceInfo : environmentInfo.getCurrentInstance().getLocalResources()
                .getLocalResource()) {
            localResources.put(resourceInfo.getName(),
                    new LocalResource(resourceInfo.getSizeInMB(), resourceInfo.getName(), resourceInfo.getPath()));
        }

        return localResources;
    }

    private Map translateRoles(RoleEnvironmentInfo environmentInfo, RoleInstance currentInstance,
            String currentRole) {
        Map roles = new HashMap();

        for (RoleInfo roleInfo : environmentInfo.getRoles().getRole()) {
            Map instances = translateRoleInstances(roleInfo.getInstances());

            if (roleInfo.getName().equals(currentRole)) {
                instances.put(currentInstance.getId(), currentInstance);
            }

            Role role = new Role(roleInfo.getName(), instances);

            for (RoleInstance instance : role.getInstances().values()) {
                instance.setRole(role);
            }

            roles.put(roleInfo.getName(), role);
        }

        if (!roles.containsKey(currentRole)) {
            Map instances = new HashMap();

            instances.put(currentInstance.getId(), currentInstance);

            Role singleRole = new Role(currentRole, instances);

            currentInstance.setRole(singleRole);

            roles.put(currentRole, singleRole);
        }

        return roles;
    }

    private Map translateRoleInstances(RoleInstancesInfo instancesInfo) {
        Map roleInstances = new HashMap();

        for (RoleInstanceInfo instanceInfo : instancesInfo.getInstance()) {
            RoleInstance instance = new RoleInstance(instanceInfo.getId(), instanceInfo.getFaultDomain(),
                    instanceInfo.getUpdateDomain(), translateRoleInstanceEndpoints(instanceInfo.getEndpoints()));

            for (RoleInstanceEndpoint endpoint : instance.getInstanceEndpoints().values()) {
                endpoint.setRoleInstance(instance);
            }

            roleInstances.put(instance.getId(), instance);
        }

        return roleInstances;
    }

    private Map translateRoleInstanceEndpoints(EndpointsInfo endpointsInfo) {
        Map endpoints = new HashMap();

        for (EndpointInfo endpointInfo : endpointsInfo.getEndpoint()) {
            RoleInstanceEndpoint endpoint = new RoleInstanceEndpoint(endpointInfo.getProtocol().toString(),
                    new InetSocketAddress(endpointInfo.getAddress(), endpointInfo.getPort()));

            endpoints.put(endpointInfo.getName(), endpoint);
        }

        return endpoints;
    }

    private RoleInstance translateCurrentInstance(RoleEnvironmentInfo environmentInfo) {
        CurrentRoleInstanceInfo currentInstanceInfo = environmentInfo.getCurrentInstance();
        RoleInstance currentInstance = new RoleInstance(currentInstanceInfo.getId(),
                currentInstanceInfo.getFaultDomain(), currentInstanceInfo.getUpdateDomain(),
                translateRoleInstanceEndpoints(environmentInfo.getCurrentInstance().getEndpoints()));

        for (RoleInstanceEndpoint endpoint : currentInstance.getInstanceEndpoints().values()) {
            endpoint.setRoleInstance(currentInstance);
        }

        return currentInstance;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy