com.sds.acube.jstor.XNDiscXNApi.java Source code

Java tutorial

Introduction

Here is the source code for com.sds.acube.jstor.XNDiscXNApi.java

Source

/*
 * <pre>
 * Copyright (c) 2014 Samsung SDS.
 * All right reserved.
 *
 * This software is the confidential and proprietary information of Samsung
 * SDS. You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement
 * you entered into with Samsung SDS.
 *
 * Author             : Takkies
 * Date                : 2014. 04. 01.
 * Description      : Add XNDisc XNApi
 * </pre>
 */
package com.sds.acube.jstor;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

import org.apache.commons.lang.StringUtils;

import com.sds.acube.ndisc.common.exception.FileException;
import com.sds.acube.ndisc.common.exception.NDiscException;
import com.sds.acube.ndisc.common.exception.NetworkException;
import com.sds.acube.ndisc.model.NFile;
import com.sds.acube.ndisc.xnapi.XNApi;

/**
 * XNDisc XNApi Main
 * 
 * @author Takkies
 * 
 */
public class XNDiscXNApi extends JSTORApi {

    /* XNDisc XNApi ? (version.txt) */
    private static String XNDiscXNApi_PublishingVersion;

    /* XNDisc XNApi ?(version.txt) */
    private static String XNDiscXNApi_PublishingDate;

    /* XNDisc XNApi   */
    public static String XNAPI_VERSION;

    /* XNApi ? */
    private XNApi xnapi = null;

    /* ?   */
    private boolean useCache = false;

    /* ?  */
    private int errorCode;

    /* ?  */
    private String errorMsg;

    /* ? ? ? ?  (? ?? ? , ? ? ?) */
    private String m_sRegFileIDArr[];

    /* ?  ? ?  (  ??  ? ?) */
    private String m_sNewCpyFileIDArr[];

    /* ?  ?  */
    private static final int FILE_ERROR = 7000;

    /* ? ?  */
    private static final int NETWORK_ERROR = 7100;

    /* NDisc ?  */
    private static final int NDISC_ERROR = 7200;

    /* OS  ?? */
    private static String LINE_SEPERATOR = System.getProperty("line.separator");

    static {
        XNAPI_VERSION = "ACUBE JSTOR XNDISC XNAPI " + getXNDiscXNApiVersion() + "(" + getXNDiscXNApiPublshingDate()
                + ")";
        System.setProperty("jstor_api_version", XNAPI_VERSION);
        logger = LogFactory.getLogger("jstorapi");
        StringBuilder smsg = new StringBuilder(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("", 60, "-")).append("?").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.center("", 60, " ")).append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("   Company        : SAMSUNG SDS", 60, " ")).append("")
                .append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("   Product Name   : JSTOR API for ACUBE", 60, " "))
                .append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("   Version        : " + XNAPI_VERSION, 60, " "))
                .append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.center("", 60, " ")).append("").append(LINE_SEPERATOR);
        smsg.append("").append(StringUtils.rightPad("", 60, "-")).append("").append(LINE_SEPERATOR);
        if (logger.isInfoEnabled() || logger.isDebugEnabled()) {
            logger.info(smsg.toString());
        } else {
            System.out.println(smsg.toString());
        }
    }

    /**
     * ??(?  )
     * 
     * @param useCache
     *            ?  
     */
    public XNDiscXNApi(boolean useCache) {
        System.setProperty("xndisc_xnapi_use_cache", Boolean.toString(useCache));
        this.useCache = useCache;
    }

    /**
     * ? ?  
     * 
     * @param errorCode
     *            ?
     * @param errorMsg
     *            ?
     */
    private void setError(int errorCode, String errorMsg) {
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    /**
     * ? 
     */
    @Override
    public int JSTOR_getErrCode() {
        return errorCode;
    }

    /**
     * ? 
     */
    @Override
    public String JSTOR_getErrMsg() {
        return errorMsg;
    }

    /**
     * ?? ? ?  
     */
    @Override
    public String[] JSTOR_getRegFileID() {
        for (int i = 0; i < m_sRegFileIDArr.length; i++) {
            if (m_sRegFileIDArr[i] != null) {
                m_sRegFileIDArr[i] = m_sRegFileIDArr[i].trim();
            }
        }
        return m_sRegFileIDArr;
    }

    /**
     * ? ? ?  
     */
    @Override
    public String[] JSTOR_getNewCpyFileID() {
        for (int i = 0; i < m_sNewCpyFileIDArr.length; i++) {
            if (m_sNewCpyFileIDArr[i] != null) {
                m_sNewCpyFileIDArr[i] = m_sNewCpyFileIDArr[i].trim();
            }
        }
        return m_sNewCpyFileIDArr;
    }

    /**
     * JSTOR XNDisc Server ?
     * 
     * @param sIPAddr
     *            XNDisc Server IP 
     * @param nPortNo
     *            XNDisc Server PORT 
     * @return ?  (?  PORT )
     */
    @Override
    public int JSTOR_Connect(String sIPAddr, int nPortNo) {
        System.out.println("Kiyong TEST XNDiscXNApi.java JSTOR_Connect()");
        int nConnId = -1;
        xnapi = new XNApi(useCache);
        try {
            nConnId = xnapi.XNDisc_Connect(sIPAddr, nPortNo);
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_Connect fail : " + e.getMessage());
        }
        return nConnId;
    }

    /**
     * JSTOR XNDisc Server ? 
     * 
     * @param nConnID
     *            ?  (?  PORT )
     */
    @Override
    public void JSTOR_Disconnect(int nConnID) {
        try {
            xnapi.XNDisc_Disconnect();
            logger.info("JSTOR Disconnect / ConnID : " + nConnID);
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_Disconnect fail : " + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            setError(NDISC_ERROR, e.getMessage());
            logger.error("JSTOR_Disconnect fail : " + e.getMessage());
        }
    }

    /**
     * ? ?
     * 
     * @param nConnID
     *            ?  (?  PORT )
     * @param nNumOfFile
     *            ? ? 
     * @param sInfoRegArr
     *            ? ? <br>
     *            <code>
     *       sInfoRegArr[i][0] => ? ??  (?? ? temporary )
     *       sInfoRegArr[i][1] => ? ?  ?
     *       sInfoRegArr[i][2] => ? ?  (0 : NONE, 1 : (GZIP ?), 2 : ,  3 :  + )
     *       sInfoRegArr[i][3] => FileCDate(option)  ? ?  null ?  
     *            </code>
     * @param nOption
     *            GUI Interface ? (DRM ), 0? , 1? , 10? DRM 
     * @return ? 0, ? -1
     */
    @Override
    public int JSTOR_FileReg(int nConnID, int nNumOfFile, String[][] sInfoRegArr, int nOption) {
        int ret = -1;
        NFile[] nFile = null;
        try {
            nFile = new NFile[nNumOfFile];
            for (int i = 0; i < nNumOfFile; i++) {
                nFile[i] = new NFile();
                nFile[i].setName(sInfoRegArr[i][0]); // ?  
                nFile[i].setVolumeId(Integer.parseInt(sInfoRegArr[i][1])); //   ID
                nFile[i].setStatType(sInfoRegArr[i][2]); // "0"
            }
            if (nOption == 10) { // ? "10" ?  ? ??  .
                if (JSTOR_DecodeDRMFile(nFile) != true) {//   
                    logger.error("JSTOR_DecodeDRMFile() fail");
                    return ret;
                }
            }
            m_sRegFileIDArr = xnapi.XNDISC_FileReg(nFile);
            logger.info("JSTOR_FileReg() success");
            ret = 0;
        } catch (FileException e) {
            e.printStackTrace();
            setError(FILE_ERROR, e.getMessage());
            logger.error("JSTOR_FileReg fail : " + e.getMessage());
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_FileReg fail : " + e.getMessage());
        } catch (NDiscException e) {
            e.printStackTrace();
            setError(NDISC_ERROR, e.getMessage());
            logger.error("JSTOR_FileReg fail : " + e.getMessage());
        }
        return ret;
    }

    /**
     * ?  
     * 
     * @param nConnID
     *            ?  (?  PORT )
     * @param nNumOfFile
     *             ? 
     * @param sInfoGetArr
     *             ?   <br>
     *            <code>
     *     sInfoGetArr[i][0] =>    ? ?
     *     sInfoGetArr[i][1] =>  ?? ?  ? (?  ??  )
     *     sInfoGetArr[i][2] =>   ? Fileter ID (?  ?? Filter ID  ?? )  (0 : NONE, 1 : (GZIP ?), 2 : , 3 :  + , -1 : ?? )
     *             </code>
     * @param nGUIFlag
     *            GUI Interface ? (DRM ), 0? , 1? , 10? DRM 
     * @return ? 0, ? -1
     */
    @Override
    public int JSTOR_FileGet(int nConnID, int nNumOfFile, String[][] sInfoGetArr, int nGUIFlag) {
        int nRet = -1;
        NFile[] nFile = null;
        try {
            nFile = new NFile[nNumOfFile];
            for (int i = 0; i < nNumOfFile; i++) {
                nFile[i] = new NFile();
                nFile[i].setId(sInfoGetArr[i][0]);
                nFile[i].setName(sInfoGetArr[i][1]);
                nFile[i].setStatType(sInfoGetArr[i][2]);
            }
            xnapi.XNDISC_FileGet(nFile);
            logger.info("JSTOR_FileGet() sucess");
            nRet = 0;
        } catch (FileException e) {
            e.printStackTrace();
            setError(FILE_ERROR, e.getMessage());
            logger.error("JSTOR_FileGet fail : " + e.getMessage());
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_FileGet fail : " + e.getMessage());
        } catch (NDiscException e) {
            e.printStackTrace();
            setError(NDISC_ERROR, e.getMessage());
            logger.error("JSTOR_FileGet fail : " + e.getMessage());
        }
        return nRet;
    }

    /**
     * ? ?
     * 
     * @param nConnID
     *            ?  (?  PORT )
     * @param nNumOfFile
     *            ? ? 
     * @param sInfoRepArr
     *            ? ?  <br>
     *            <code>
     *       sInfoRepArr[i][0] => ?? ? ? ?
     *       sInfoRepArr[i][1] => ? ? 
     *       sInfoRepArr[i][2] => ? ?? (0 : NONE, 1 : (GZIP ?), 2 : , 3 :  + , -1 : ?? )
     *            </code>
     * @param nOption
     *            GUI Interface ? (DRM ), 0? , 1? , 10? DRM 
     * @return ? 0, ? -1
     */
    @Override
    public int JSTOR_FileRep(int nConnID, int nNumOfFile, String[][] sInfoRepArr, int nOption) {
        int nRet = -1;
        NFile[] nFile = null;
        try {
            nFile = new NFile[nNumOfFile];
            for (int i = 0; i < nNumOfFile; i++) {
                nFile[i] = new NFile();
                nFile[i].setId(sInfoRepArr[i][0]); //   
                nFile[i].setName(sInfoRepArr[i][1]); // ?  
                nFile[i].setStatType(sInfoRepArr[i][2]); // 0
            }
            if (nOption == 10) { // ? "10" ?  ? ??  .
                if (JSTOR_DecodeDRMFile(nFile) != true) {//   
                    logger.error("JSTOR_DecodeDRMFile() fail");
                    return nRet;
                }
            }
            xnapi.XNDISC_FileRep(nFile);
            logger.info("JSTOR_FileRep() success");
            nRet = 0;
        } catch (FileException e) {
            e.printStackTrace();
            setError(FILE_ERROR, e.getMessage());
            logger.error("JSTOR_FileRep fail : " + e.getMessage());
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_FileRep fail : " + e.getMessage());
        } catch (NDiscException e) {
            e.printStackTrace();
            setError(NDISC_ERROR, e.getMessage());
            logger.error("JSTOR_FileRep fail : " + e.getMessage());
        }
        return nRet;
    }

    /**
     * ? 
     * 
     * @param nConnID
     *            ?  (?  PORT )
     * @param nNumOfFile
     *             ? 
     * @param sInfoDelArr
     *             ? ?  
     * @return ? 0, ? -1
     */
    @Override
    public int JSTOR_FileDel(int nConnID, int nNumOfFile, String[] sInfoDelArr) {
        int nRet = -1;
        NFile[] nFile = null;
        try {
            nFile = new NFile[nNumOfFile];
            for (int i = 0; i < nNumOfFile; i++) {
                nFile[i] = new NFile();
                nFile[i].setId(sInfoDelArr[i]);
            }
            xnapi.XNDISC_FileDel(nFile);
            logger.info("JSTOR_FileDel() success");
            nRet = 0;
        } catch (FileException e) {
            e.printStackTrace();
            setError(FILE_ERROR, e.getMessage());
            logger.error("JSTOR_FileDel fail : " + e.getMessage());
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_FileDel fail : " + e.getMessage());
        } catch (NDiscException e) {
            e.printStackTrace();
            setError(NDISC_ERROR, e.getMessage());
            logger.error("JSTOR_FileDel fail : " + e.getMessage());
        }
        return nRet;
    }

    /**
     * ? 
     * 
     * @param nConnID
     *            ?  (?  PORT )
     * @param nNumOfFile
     *             ? 
     * @param sInfoCpyArr
     *             ?  <br>
     *            <code>
     *       sInfoCpyArr[i][0] => ? ? ? ? 
     *       sInfoCpyArr[i][1] => ? ?  ?
     *       sInfoCpyArr[i][2] => ? ?? (0 : NONE, 1 : (GZIP ?), 2 : , 3 :  + , -1 : ?? )
     *       sInfoCpyArr[i][3] => ? ?  (1 :  , 2 : ? )
     *       sInfoCpyArr[i][4] => ? ?  IP
     *       sInfoCpyArr[i][5] => ? ?  Port
     *             </code>
     * @return ? 0, ? -1
     */
    @Override
    public int JSTOR_FileCpy(int nConnID, int nNumOfFile, String[][] sInfoCpyArr) {
        int nRet = -1;
        NFile[] nFile = null;
        try {
            nFile = new NFile[nNumOfFile];
            for (int i = 0; i < nNumOfFile; i++) {
                nFile[i] = new NFile();
                nFile[i].setId(sInfoCpyArr[i][0]);
                nFile[i].setVolumeId(Integer.parseInt(sInfoCpyArr[i][1]));
                nFile[i].setStatType(sInfoCpyArr[i][2]);
            }
            m_sNewCpyFileIDArr = xnapi.XNDISC_FileCpy(nFile);
            logger.info("JSTOR_FileCpy() success");
            nRet = 0;
        } catch (FileException e) {
            e.printStackTrace();
            setError(FILE_ERROR, e.getMessage());
            logger.error("JSTOR_FileCpy fail : " + e.getMessage());
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_FileCpy fail : " + e.getMessage());
        } catch (NDiscException e) {
            e.printStackTrace();
            setError(NDISC_ERROR, e.getMessage());
            logger.error("JSTOR_FileCpy fail : " + e.getMessage());
        }
        return nRet;
    }

    /**
     * ? ??
     * 
     * @param nConnID
     *            ?  (?  PORT )
     * @param nNumOfFile
     *            ?? ? 
     * @param sInfoMoveArr
     *            ?? ?  <br>
     *            <code>
     *       sInfoCpyArr[i][0] => ??? ? ? ?
     *       sInfoCpyArr[i][1] => ??? ?  ?
     *       sInfoCpyArr[i][2] => ??? ?? (0 : NONE, 1 : (GZIP ?), 2 : , 3 :  + , -1 : ?? ) 
     *       sInfoCpyArr[i][3] => ??? ?  (1 :  , 2 : ? )
     *       sInfoCpyArr[i][4] => ??? ?  IP 
     *       sInfoCpyArr[i][5] => ??? ?  Port
     *             </code>
     * @return ? 0, ? -1
     */
    @Override
    public int JSTOR_FileMov(int nConnID, int nNumOfFile, String[][] sInfoMovArr) {
        int nRet = -1;
        NFile[] nFile = null;
        try {
            nFile = new NFile[nNumOfFile];
            for (int i = 0; i < nNumOfFile; i++) {
                nFile[i] = new NFile();
                nFile[i].setId(sInfoMovArr[i][0]);
                nFile[i].setVolumeId(Integer.parseInt(sInfoMovArr[i][1]));
                nFile[i].setStatType(sInfoMovArr[i][2]);
            }
            xnapi.XNDISC_FileMov(nFile);
            logger.info("JSTOR_FileMov() success");
            nRet = 0;
        } catch (FileException e) {
            e.printStackTrace();
            setError(FILE_ERROR, e.getMessage());
            logger.error("JSTOR_FileMov fail : " + e.getMessage());
        } catch (NetworkException e) {
            e.printStackTrace();
            setError(NETWORK_ERROR, e.getMessage());
            logger.error("JSTOR_FileMov fail : " + e.getMessage());
        } catch (NDiscException e) {
            e.printStackTrace();
            setError(NDISC_ERROR, e.getMessage());
            logger.error("JSTOR_FileMov fail : " + e.getMessage());
        }
        return nRet;
    }

    /**
     * JSTOR commit <br>
     *   API ? .<br>
     * 
     * @param nConnID
     *            ?  (?  PORT )
     */
    @Override
    public int JSTOR_Commit(int nConnID) {
        return 0;
    }

    /**
     * JSTOR rollback <br>
     *   API ? .<br>
     * 
     * @param ?
     *             (?  PORT )
     */
    @Override
    public int JSTOR_Rollback(int nConnID) {
        return 0;
    }

    /**
     * ?  DRM 
     * 
     * @param nFile
     *            NFile  
     * @return ? true, ? false
     */
    private boolean JSTOR_DecodeDRMFile(NFile[] nFile) {
        boolean ret = false;
        try {
            if (m_drmType.equalsIgnoreCase("FASOO_V3.1")) {// Fasoo DRM 
                m_fasooPackager = new FasooPackager();
                String fsdHomeDir = (String) m_drmConfig.get("FSD_HOME_DIR");
                String fsdServerID = (String) m_drmConfig.get("FSD_SERVER_ID");
                for (int i = 0; i < nFile.length; i++) {
                    ret = m_fasooPackager.isFasooPackageFile(nFile[i].getName());
                    if (ret) {
                        ret = m_fasooPackager.doFasooPacakgeFileExtract(nFile[i].getName(), fsdHomeDir,
                                fsdServerID);
                    }
                }
            } else if (m_drmType.startsWith("SOFTCAMP")) {// SoftCamp DRM 
                m_softCampPackager = new SoftCampPackager();
                String keyDir = (String) m_drmConfig.get("SCSL_KEY_DIR");
                String keyFile = (String) m_drmConfig.get("SCSL_KEY_FILE");
                String privID = (String) m_drmConfig.get("SCSL_PRIV_ID");
                if (m_drmType.equalsIgnoreCase("SOFTCAMP_V3.1")) {
                    m_softCampPackager.m_strPathForProperty = (String) m_drmConfig.get("SCSL_PROP_PATH");
                }
                for (int i = 0; i < nFile.length; i++) {
                    ret = m_softCampPackager.doSoftCampFileExtract(nFile[i].getName(),
                            keyDir + File.separator + keyFile, privID);
                }
            }
        } catch (Exception e) {
            logger.error("JSTOR_DecodeDRMFile fail : " + e.getMessage());
        }
        return ret;
    }

    /**
     * XNDisc XNApi ?  
     * 
     * @return XNDisc XNApi 
     */
    public static String getXNDiscXNApiVersion() {
        if (XNDiscXNApi_PublishingVersion == null) {
            readVersionFromFile();
        }
        return XNDiscXNApi_PublishingVersion;
    }

    /**
     * XNDisc XNApi ?? 
     * 
     * @return XNDisc XNApi ??
     */
    public static String getXNDiscXNApiPublshingDate() {
        if (XNDiscXNApi_PublishingDate == null) {
            readVersionFromFile();
        }
        return XNDiscXNApi_PublishingDate;
    }

    /**
     * XNDisc XNApi ?  ?
     */
    private static void readVersionFromFile() {
        XNDiscXNApi_PublishingVersion = "<unknown>";
        XNDiscXNApi_PublishingDate = "<unknown>";
        InputStreamReader isr = null;
        LineNumberReader lnr = null;
        try {
            isr = new InputStreamReader(XNDiscXNApi.class.getResourceAsStream("/com/sds/acube/jstor/version.txt"));
            if (isr != null) {
                lnr = new LineNumberReader(isr);
                String line = null;
                do {
                    line = lnr.readLine();
                    if (line != null) {
                        if (line.startsWith("Publishing-Version=")) {
                            XNDiscXNApi_PublishingVersion = line
                                    .substring("Publishing-Version=".length(), line.length()).trim();
                        } else if (line.startsWith("Publishing-Date=")) {
                            XNDiscXNApi_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                    .trim();
                        }
                    }
                } while (line != null);
                lnr.close();
            }
        } catch (IOException ioe) {
            XNDiscXNApi_PublishingVersion = "<unknown>";
            XNDiscXNApi_PublishingDate = "<unknown>";
        } finally {
            try {
                if (lnr != null) {
                    lnr.close();
                }
                if (isr != null) {
                    isr.close();
                }
            } catch (IOException ioe) {
            }
        }
    }
}