com.athena.peacock.controller.web.software.SoftwareController.java Source code

Java tutorial

Introduction

Here is the source code for com.athena.peacock.controller.web.software.SoftwareController.java

Source

/* 
 * Athena Peacock Project - Server Provisioning Engine for IDC or Cloud
 * 
 * Copyright (C) 2013 Open Source Consulting, Inc. All rights reserved by Open Source Consulting, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * Revision History
 * Author         Date            Description
 * ---------------   ----------------   ------------
 * Sang-cheon Park   2013. 10. 16.      First Draft.
 */
package com.athena.peacock.controller.web.software;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.athena.peacock.controller.common.provisioning.ProvisioningDetail;
import com.athena.peacock.controller.common.provisioning.ProvisioningHandler;
import com.athena.peacock.controller.web.common.model.GridJsonResponse;
import com.athena.peacock.controller.web.common.model.SimpleJsonResponse;
import com.athena.peacock.controller.web.machine.MachineDto;
import com.athena.peacock.controller.web.user.UserController;
import com.athena.peacock.controller.web.user.UserDto;

/**
 * <pre>
 *   ?   
 * </pre>
 * @author Sang-cheon Park
 * @version 1.0
 */
@Controller
@RequestMapping("/software")
public class SoftwareController {

    protected final Logger logger = LoggerFactory.getLogger(SoftwareController.class);

    @Value("#{contextProperties['repository.url']}")
    private String urlPrefix;

    @Inject
    @Named("softwareRepoService")
    private SoftwareRepoService softwareRepoService;

    @Inject
    @Named("softwareService")
    private SoftwareService softwareService;

    @Inject
    @Named("provisioningHandler")
    private ProvisioningHandler provisioningHandler;

    /**
     * <pre>
     * ? Agent(machineID)?   ? 
     * </pre>
     * @param jsonRes
     * @param machineId
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public @ResponseBody GridJsonResponse list(GridJsonResponse jsonRes, MachineDto machine) throws Exception {
        Assert.isTrue(!StringUtils.isEmpty(machine.getMachineId()), "machineId must not be null.");

        jsonRes.setTotal(softwareService.getSoftwareInstallListCnt(machine));
        jsonRes.setList(softwareService.getSoftwareInstallList(machine));

        return jsonRes;
    }

    /**
     * <pre>
     *  ? 
     * </pre>
     * @param jsonRes
     * @param machineId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getNames")
    public @ResponseBody List<SoftwareRepoDto> getNames() throws Exception {
        return softwareRepoService.getSoftwareNames();
    }

    /**
     * <pre>
     *   
     * </pre>
     * @param jsonRes
     * @param machineId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getVersions")
    public @ResponseBody GridJsonResponse getVersions(GridJsonResponse jsonRes, String softwareName)
            throws Exception {
        Assert.isTrue(!StringUtils.isEmpty(softwareName), "softwareName must not be null.");

        List<SoftwareRepoDto> softwareRepoList = softwareRepoService.getSoftwareVersions(softwareName);

        jsonRes.setTotal(softwareRepoList.size());
        jsonRes.setList(softwareRepoList);

        return jsonRes;
    }

    /**
     * <pre>
     * ?? Agent(machineID)?   
     * </pre>
     * @param jsonRes
     * @param machineId
     * @return
     * @throws Exception
     */
    @RequestMapping("/getInstallLog")
    public @ResponseBody SimpleJsonResponse getInstallLog(SimpleJsonResponse jsonRes, SoftwareDto software)
            throws Exception {
        Assert.isTrue(software.getSoftwareId() != null, "softwareId must not be null.");
        Assert.isTrue(!StringUtils.isEmpty(software.getMachineId()), "machineId must not be null.");
        Assert.notNull(software.getInstallSeq(), "installSeq can not be null.");

        jsonRes.setData(softwareService.getSoftware(software));

        return jsonRes;
    }

    /**
     * <pre>
     * Agent?  
     * </pre>
     * @param jsonRes
     * @param provisioningDetail
     * @return
     * @throws Exception 
     */
    @RequestMapping("/install")
    public @ResponseBody SimpleJsonResponse install(HttpServletRequest request, SimpleJsonResponse jsonRes,
            ProvisioningDetail provisioningDetail) {
        Assert.isTrue(provisioningDetail.getSoftwareId() != null, "softwareId must not be null.");
        Assert.isTrue(!StringUtils.isEmpty(provisioningDetail.getMachineId()), "machineId must not be null.");

        try {
            UserDto userDto = (UserDto) request.getSession().getAttribute(UserController.SESSION_USER_KEY);
            if (userDto != null) {
                provisioningDetail.setUserId(userDto.getUserId());
            }

            //    
            boolean installed = false;
            boolean installedDiffVersion = false;

            SoftwareRepoDto softwareRepo = softwareRepoService.getSoftwareRepo(provisioningDetail.getSoftwareId());
            provisioningDetail.setSoftwareName(softwareRepo.getSoftwareName());
            provisioningDetail.setVersion(softwareRepo.getSoftwareVersion());
            provisioningDetail.setFileLocation(softwareRepo.getFileLocation());
            provisioningDetail.setFileName(softwareRepo.getFileName());

            List<SoftwareDto> softwareList = softwareService
                    .getSoftwareInstallListAll(provisioningDetail.getMachineId());

            for (SoftwareDto software : softwareList) {
                if (software.getSoftwareName().equals(provisioningDetail.getSoftwareName())
                        && software.getInstallYn().equals("Y")) {
                    if (software.getSoftwareVersion().equals(provisioningDetail.getVersion())) {
                        installed = true;
                    } else {
                        installedDiffVersion = true;
                    }
                }
            }

            if (installed) {
                jsonRes.setSuccess(false);
                jsonRes.setMsg("? ? .");
                return jsonRes;
            }

            if (installedDiffVersion) {
                jsonRes.setSuccess(false);
                jsonRes.setMsg(
                        " ?  ? .      .");
                return jsonRes;
            }

            //String urlPrefix = "http://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getContextPath();
            provisioningDetail.setUrlPrefix(urlPrefix);

            provisioningHandler.install(provisioningDetail);
            jsonRes.setMsg(
                    "  ? ?.   ? ?    .");
        } catch (Exception e) {
            jsonRes.setSuccess(false);
            jsonRes.setMsg("   ?.");

            logger.error("Unhandled Expeption has occurred. ", e);
        }

        return jsonRes;
    }

    /**
     * <pre>
     * Agent?  
     * </pre>
     * @param jsonRes
     * @param provisioningDetail
     * @return
     * @throws Exception 
     */
    @RequestMapping("/remove")
    public @ResponseBody SimpleJsonResponse remove(HttpServletRequest request, SimpleJsonResponse jsonRes,
            ProvisioningDetail provisioningDetail) throws Exception {
        Assert.isTrue(provisioningDetail.getSoftwareId() != null, "softwareId must not be null.");
        Assert.isTrue(!StringUtils.isEmpty(provisioningDetail.getMachineId()), "machineId must not be null.");
        Assert.notNull(provisioningDetail.getInstallSeq(), "installSeq can not be null.");

        try {
            UserDto userDto = (UserDto) request.getSession().getAttribute(UserController.SESSION_USER_KEY);
            if (userDto != null) {
                provisioningDetail.setUserId(userDto.getUserId());
            }

            //    
            boolean removed = true;

            SoftwareRepoDto softwareRepo = softwareRepoService.getSoftwareRepo(provisioningDetail.getSoftwareId());
            provisioningDetail.setSoftwareName(softwareRepo.getSoftwareName());
            provisioningDetail.setVersion(softwareRepo.getSoftwareVersion());

            List<SoftwareDto> softwareList = softwareService
                    .getSoftwareInstallListAll(provisioningDetail.getMachineId());

            for (SoftwareDto software : softwareList) {
                if (software.getSoftwareName().equals(provisioningDetail.getSoftwareName())
                        && software.getInstallYn().equals("Y")) {
                    if (software.getSoftwareVersion().equals(provisioningDetail.getVersion())) {
                        removed = false;
                    }
                }
            }

            if (removed) {
                jsonRes.setSuccess(false);
                jsonRes.setMsg("? ? ? ? .");
                return jsonRes;
            }

            provisioningHandler.remove(provisioningDetail);
            jsonRes.setMsg("  ? ?.");
        } catch (Exception e) {
            jsonRes.setSuccess(false);
            jsonRes.setMsg("   ?.");

            logger.error("Unhandled Expeption has occurred. ", e);
        }

        return jsonRes;
    }

    /**
     * <pre>
     * HTTPD   ?? ? ? ? uriworkermap.properties ? workers.properties ? .
     * </pre>
     * @param jsonRes
     * @param account
     * @return
     * @throws Exception
     */
    @RequestMapping("/getConnectorProp")
    public @ResponseBody SimpleJsonResponse getConnectorProp(SimpleJsonResponse jsonRes, String account)
            throws Exception {
        Assert.notNull(account, "account must not be null.");

        //String urlPrefix = "http://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getContextPath();

        Map<String, String> properties = new HashMap<String, String>();

        String uriworkermap = IOUtils
                .toString(new URL(urlPrefix + "/repo/httpd/conf/extra/uriworkermap.properties"), "UTF-8")
                .replaceAll("\\$\\{user\\}", account);
        String workers = IOUtils.toString(new URL(urlPrefix + "/repo/httpd/conf/extra/workers.properties"), "UTF-8")
                .replaceAll("\\$\\{user\\}", account);

        properties.put("uriworkermap", uriworkermap);
        properties.put("workers", workers);

        jsonRes.setData(properties);

        return jsonRes;
    }
}
//end of SoftwareController.java