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

org.xlcloud.console.polling.StackPolling Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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 org.xlcloud.console.polling;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;
import org.xlcloud.console.controllers.request.RequestParameters;
import org.xlcloud.console.extensions.scope.ViewScoped;
import org.xlcloud.console.repository.DataRepository;
import org.xlcloud.console.wrappers.ExtendedState;
import org.xlcloud.console.wrappers.StateWrapper;
import org.xlcloud.rest.client.cache.RequestCacheConfig;
import org.xlcloud.rest.exception.BaseException;
import org.xlcloud.rest.exception.ForbiddenException;
import org.xlcloud.service.Session;
import org.xlcloud.service.Stack;

/**
 * Bean used by polling mechanism for {@link Stack} statuses
 * 
 * @author Michał Kamiński
 */
@Named
@ViewScoped
public class StackPolling {

    @Inject
    RequestParameters requestParameters;

    @Inject
    RequestCacheConfig cacheConfig;

    private static final Logger LOG = Logger.getLogger(StackPolling.class);

    Map stacksState = Collections.synchronizedMap(new HashMap());

    @Inject
    private DataRepository dataRepository;

    /**
     * Init stack poller with the list of Stacks. All stacks has by default
     * state retrieving and {@link StackPolling}
     * {@link #refreshStackStatus(Stack)} must be call to retrieve real status
     * from XMS
     * 
     * @param stacks
     */
    public void init(List stacks) {
        stacksState.clear();
        for (Stack stack : stacks) {
            stacksState.put(stack.getId(), StateWrapper.wrapState(ExtendedState.RETRIEVING));
        }
    }

    /**
     * Init stack poller with the single stacks. All stacks has by default state
     * retrieving and {@link StackPolling} {@link #refreshStackStatus(Stack)}
     * must be call to retrieve real status from XMS
     * 
     * @param stacks
     */
    public void init(Stack stack) {
        stacksState.clear();
        stacksState.put(stack.getId(), StateWrapper.wrapState(ExtendedState.RETRIEVING));
    }

    /**
     * Refresh stack status if current status is transient. it adds stopPoller
     * flag to response args, enable to stop poller
     * 
     * @param stack
     */
    public void refreshStackStatus(Stack stack) {
        StateWrapper stackState = stacksState.get(stack.getId());
        if (stackState != null && !stackState.isTransient()) {
            requestParameters.addCallbackParameter("stopPoller", true);
            return;
        }
        try {
            if (!ExtendedState.RETRIEVING.value().equals(stackState.getStateValue())) {
                cacheConfig.setDirectRequest();
            }
            Session session = dataRepository.getStackDetails(stack.getId());
            StateWrapper retrievedState = StateWrapper.wrapSession(session);
            stacksState.put(stack.getId(), retrievedState);
            if (!retrievedState.isTransient()) {
                requestParameters.addCallbackParameter("stopPoller", true);
            }
        } catch (ForbiddenException ex) {
            stacksState.put(stack.getId(), StateWrapper.wrapState(ExtendedState.FORBIDDEN_TO_RETRIEVE));
            requestParameters.addCallbackParameter("stopPoller", true);
        } catch (BaseException ex) {
            stacksState.put(stack.getId(), StateWrapper.wrapState(ExtendedState.FAILED_TO_RETRIEVE));
            requestParameters.addCallbackParameter("stopPoller", true);
        }
    }

    /**
     * 
     * Returns current status of stack from map
     * @param stateId
     * @return
     */
    public StateWrapper getState(Long stateId) {
        return stacksState.get(stateId);
    }

    /**
     * 
     * Refreshes Stack Status to its default: retrieving
     * @param stackId
     */
    public void refreshStatusToDefault(Long stackId) {
        stacksState.put(stackId, StateWrapper.wrapState(ExtendedState.RETRIEVING));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy