com.tsp.xlcloud.xsa.ext.rr.SessionsManager.java Source code

Java tutorial

Introduction

Here is the source code for com.tsp.xlcloud.xsa.ext.rr.SessionsManager.java

Source

/*
 * 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 com.tsp.xlcloud.xsa.ext.rr;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import org.xlcloud.rest.exception.InternalErrorException;
import org.xlcloud.rest.exception.ValidationException;
import org.xlcloud.xsa.RenderingSession;
import org.xlcloud.xsa.RenderingSession.ApplicationParameters.Parameter;

/**
 * @author chris
 *
 */
public class SessionsManager {

    private static final Object LOBBY_EXEC_COMMAND = "lobby.sh";

    public RenderingSession createSession(RenderingSession renderingSession) {
        if (StringUtils.isBlank(renderingSession.getApplicationId())) {
            throw new ValidationException("applicationId field cannot be empty");
        }
        if (StringUtils.isBlank(renderingSession.getUserId())) {
            throw new ValidationException("userId field cannot be empty");
        }

        try {
            Runtime.getRuntime().exec(extractCommandFromRenderingSession(renderingSession));
        } catch (IOException e) {
            throw new InternalErrorException("Error occured while calling lobby", e.getMessage());
        }

        // TODO call bash script(s) here
        return renderingSession;
    }

    String extractCommandFromRenderingSession(RenderingSession renderingSession) {
        StringBuilder sb = new StringBuilder();
        sb.append(LOBBY_EXEC_COMMAND);
        sb.append(" --applicationId=").append(renderingSession.getApplicationId());
        sb.append(" --userId=").append(renderingSession.getUserId());

        if (renderingSession.getApplicationParameters() != null) {
            for (Parameter parameter : renderingSession.getApplicationParameters().getParameter()) {
                sb.append(" --").append(parameter.getKey()).append("=").append(parameter.getValue());
            }
        }

        return sb.toString();
    }

}