Example usage for java.util Arrays deepToString

List of usage examples for java.util Arrays deepToString

Introduction

In this page you can find the example usage for java.util Arrays deepToString.

Prototype

public static String deepToString(Object[] a) 

Source Link

Document

Returns a string representation of the "deep contents" of the specified array.

Usage

From source file:org.apache.hadoop.hbase.master.assignment.MergeTableRegionsProcedure.java

/**
 * Prepare merge and do some check//from  www  .j  a  v a  2s.  co m
 * @param env MasterProcedureEnv
 * @throws IOException
 */
private boolean prepareMergeRegion(final MasterProcedureEnv env) throws IOException {
    // Note: the following logic assumes that we only have 2 regions to merge.  In the future,
    // if we want to extend to more than 2 regions, the code needs to modify a little bit.
    //
    CatalogJanitor catalogJanitor = env.getMasterServices().getCatalogJanitor();
    boolean regionAHasMergeQualifier = !catalogJanitor.cleanMergeQualifier(regionsToMerge[0]);
    if (regionAHasMergeQualifier || !catalogJanitor.cleanMergeQualifier(regionsToMerge[1])) {
        String msg = "Skip merging regions " + HRegionInfo.getShortNameToLog(regionsToMerge)
                + ", because region " + (regionAHasMergeQualifier ? regionsToMerge[0].getEncodedName()
                        : regionsToMerge[1].getEncodedName())
                + " has merge qualifier";
        LOG.warn(msg);
        throw new MergeRegionException(msg);
    }

    RegionStates regionStates = env.getAssignmentManager().getRegionStates();
    RegionState regionStateA = regionStates.getRegionState(regionsToMerge[0].getEncodedName());
    RegionState regionStateB = regionStates.getRegionState(regionsToMerge[1].getEncodedName());
    if (regionStateA == null || regionStateB == null) {
        throw new UnknownRegionException(
                regionStateA == null ? regionsToMerge[0].getEncodedName() : regionsToMerge[1].getEncodedName());
    }

    if (!regionStateA.isOpened() || !regionStateB.isOpened()) {
        throw new MergeRegionException(
                "Unable to merge regions not online " + regionStateA + ", " + regionStateB);
    }

    if (!env.getMasterServices().isSplitOrMergeEnabled(MasterSwitchType.MERGE)) {
        String regionsStr = Arrays.deepToString(regionsToMerge);
        LOG.warn("merge switch is off! skip merge of " + regionsStr);
        super.setFailure(getClass().getSimpleName(),
                new IOException("Merge of " + regionsStr + " failed because merge switch is off"));
        return false;
    }

    // Ask the remote regionserver if regions are mergeable. If we get an IOE, report it
    // along w/ the failure so can see why we are not mergeable at this time.
    IOException mergeableCheckIOE = null;
    boolean mergeable = false;
    RegionState current = regionStateA;
    try {
        mergeable = isMergeable(env, current);
    } catch (IOException e) {
        mergeableCheckIOE = e;
    }
    if (mergeable && mergeableCheckIOE == null) {
        current = regionStateB;
        try {
            mergeable = isMergeable(env, current);
        } catch (IOException e) {
            mergeableCheckIOE = e;
        }
    }
    if (!mergeable) {
        IOException e = new IOException(current.getRegion().getShortNameToLog() + " NOT mergeable");
        if (mergeableCheckIOE != null)
            e.initCause(mergeableCheckIOE);
        super.setFailure(getClass().getSimpleName(), e);
        return false;
    }

    return true;
}

From source file:gov.lanl.adore.djatoka.plugin.ExtractPDF.java

/**
 * Get PDF information with pdfinfo:/*  w  w w .j a  v  a2 s.  c  o  m*/
 * - "Pages: X": number of pages;
 * - "Page X size: www.ww hhh.hh": size of each page, in pts.
 * @returns a map:
 * - [Pages][n]
 * - [Page 1][111.11 222.22]
 * - [Page i][www.ww hhh.hh]
 * - [Page n][999.99 1000.00]
 */
private static Map<String, String> getPDFProperties(ImageRecord input) throws DjatokaException {
    logger.debug("Getting PDF info");

    try {
        setPDFCommandsPath();
    } catch (IllegalStateException e) {
        logger.error("Failed to set PDF commands path: ", e);
        throw e;
    }

    HashMap<String, String> pdfProperties = new HashMap<String, String>();

    String sourcePath = null;

    if (input.getImageFile() != null) {
        logger.debug("PDFInfo image file: " + input.getImageFile());
        sourcePath = input.getImageFile();
    } else if (input.getObject() != null && (input.getObject() instanceof InputStream)) {
        FileInputStream fis = null;
        fis = (FileInputStream) input.getObject();
        File in;

        // Copy to tmp file
        try {
            String cacheDir = OpenURLJP2KService.getCacheDir();
            if (cacheDir != null) {
                in = File.createTempFile("tmp", ".pdf", new File(cacheDir));
            } else {
                in = File.createTempFile("tmp", ".pdf");
            }
            in.deleteOnExit();

            FileOutputStream fos = new FileOutputStream(in);
            IOUtils.copyStream(fis, fos);
        } catch (IOException e) {
            logger.error(e, e);
            throw new DjatokaException(e);
        }
        sourcePath = in.getAbsolutePath();
    } else {
        throw new DjatokaException("File not defined and Input Object Type " + input //.getObject().getClass().getName()
                + " is not supported");
    }

    String pdfinfoCmd[] = PDFINFO_COMMAND.clone();
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_BIN] = pdfinfoPath;
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_FIRSTPAGE] = "1";
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_LASTPAGE] = "-1"; // Last page even we not knowing its number.
    pdfinfoCmd[PDFINFO_COMMAND_POSITION_FILE] = sourcePath;
    Process pdfProc = null;
    try {
        ArrayList<MatchResult> pageSizes = new ArrayList<MatchResult>();
        MatchResult pages = null;

        pdfProc = Runtime.getRuntime().exec(pdfinfoCmd);
        BufferedReader lr = new BufferedReader(new InputStreamReader(pdfProc.getInputStream()));
        String line;
        for (line = lr.readLine(); line != null; line = lr.readLine()) {
            Matcher mm1 = PAGES_PATT.matcher(line);
            if (mm1.matches())
                pages = mm1.toMatchResult();
            Matcher mm2 = MEDIABOX_PATT.matcher(line);
            if (mm2.matches())
                pageSizes.add(mm2.toMatchResult());
        }

        int istatus = pdfProc.waitFor();
        if (istatus != 0)
            logger.error("pdfinfo proc failed, exit status=" + istatus + ", file=" + sourcePath);

        if (pages == null) {
            logger.error("Did not find 'Pages' line in output of pdfinfo command: "
                    + Arrays.deepToString(pdfinfoCmd));
            pdfProperties.put("Pages", "0");
        } else {
            //int n = Integer.parseInteger(pages.group(1));
            pdfProperties.put("Pages", pages.group(1));
        }

        if (pageSizes.isEmpty()) {
            logger.error("Did not find \"Page X size\" lines in output of pdfinfo command: "
                    + Arrays.deepToString(pdfinfoCmd));
            throw new IllegalArgumentException("Failed to get pages size of PDF with pdfinfo.");
        } else {
            for (MatchResult mr : pageSizes) {
                String page = mr.group(1);

                float x0 = Float.parseFloat(mr.group(2));
                float y0 = Float.parseFloat(mr.group(3));
                float x1 = Float.parseFloat(mr.group(4));
                float y1 = Float.parseFloat(mr.group(5));
                float w = Math.abs(x1 - x0);
                float h = Math.abs(y1 - y0);
                // Have to scale page sizes by max dpi (MAX_DPI / DEFAULT_DENSITY). Otherwise, BookReader.js will request the wrong zoom level (svc.level).
                float ws = w * MAX_DPI / DEFAULT_DENSITY;
                float hs = h * MAX_DPI / DEFAULT_DENSITY;
                String width = "" + ws; //mr.group(2);
                String height = "" + hs; //mr.group(3);
                pdfProperties.put("Page " + page, width + " " + height);
            }
        }

    } catch (Exception e) {
        logger.error("Failed getting PDF information: ", e);
        throw new DjatokaException("Failed getting PDF information: ", e);
    } finally {
        // Our exec() should just consume one of the streams, but we want to stay safe.
        // http://mark.koli.ch/2011/01/leaky-pipes-remember-to-close-your-streams-when-using-javas-runtimegetruntimeexec.html
        org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getOutputStream());
        org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getInputStream());
        org.apache.commons.io.IOUtils.closeQuietly(pdfProc.getErrorStream());
    }

    return pdfProperties;
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.por.PORFileReader.java

private void decodeSec2(BufferedReader reader) throws IOException {
    dbgLog.fine("***** decodeSec2(): start *****");
    if (reader == null) {
        throw new IllegalArgumentException("decodeSec2: stream == null!");
    }/*from  w  ww .  j a v a2 s .c o m*/

    // Because a 64-bit machine may not save the first 40
    // bytes of a POR file in a way as a 32-bit machine does,
    // the first 5 lines of a POR file is excluded from the read-back
    // file and the new 1st line contains the format mark "SPSSPORT"
    // somewhere in it.

    // mark the start position for the later rewind
    if (reader.markSupported()) {
        reader.mark(100000);
    }

    char[] sixthLineCharArray = new char[80];
    int nbytes_sixthLine = reader.read(sixthLineCharArray);

    String sixthLine = new String(sixthLineCharArray);
    dbgLog.info("sixthLineCharArray=" + Arrays.deepToString(ArrayUtils.toObject(sixthLineCharArray)));
    int signatureLocation = sixthLine.indexOf(POR_MARK);

    if (signatureLocation >= 0) {
        dbgLog.info("format signature was found at:" + signatureLocation);
    } else {
        dbgLog.severe("signature string was not found");
        throw new IOException("signature string was not found");
    }

    // rewind the position to the beginning
    reader.reset();

    // skip bytes up to the signature string
    long skippedBytes = reader.skip(signatureLocation);

    char[] sec2_leader = new char[POR_MARK.length()];
    int nbytes_sec2_leader = reader.read(sec2_leader);

    String leader_string = new String(sec2_leader);

    dbgLog.info("format signature [SPSSPORT] detected=" + leader_string);

    if (leader_string.equals("SPSSPORT")) {
        dbgLog.info("signature was correctly detected");

    } else {
        dbgLog.severe("the format signature is not found at the previously located column");
        throw new IOException("decodeSec2: failed to find the signature string");
    }

    int length_section_2 = LENGTH_SECTION_2;

    char[] Sec2_bytes = new char[length_section_2];

    int nbytes_sec2 = reader.read(Sec2_bytes);

    if (nbytes_sec2 == 0) {
        dbgLog.severe("decodeSec2: reading error");
        throw new IOException("decodeSec2: reading error");
    } else {
        dbgLog.fine("bytes read=" + nbytes_sec2);
    }

    String sec2 = new String(Sec2_bytes);
    dbgLog.fine("sec2[creation date/time]=" + sec2);

    // sec2
    //       0123456789012345678
    //       A8/YYYYMMDD6/HHMMSS
    // thus
    // section2 should has 3 elements

    String[] section2 = StringUtils.split(sec2, '/');

    dbgLog.fine("section2=" + StringUtils.join(section2, "|"));

    String fileCreationDate = null;
    String fileCreationTime = null;
    if ((section2.length == 3) && (section2[0].startsWith("A"))) {
        fileCreationDate = section2[1].substring(0, 7);
        fileCreationTime = section2[2];
    } else {
        dbgLog.severe("decodeSec2: file creation date/time were not correctly detected");
        throw new IOException("decodeSec2: file creation date/time were not correctly detected");
    }
    dbgLog.fine("fileCreationDate=" + fileCreationDate);
    dbgLog.fine("fileCreationTime=" + fileCreationTime);
    smd.getFileInformation().put("fileCreationDate", fileCreationDate);
    smd.getFileInformation().put("fileCreationTime", fileCreationTime);
    smd.getFileInformation().put("varFormat_schema", "SPSS");
}

From source file:org.mmadsen.sim.transmissionlab.analysis.IndividualTraitFrequencyAnalyzer.java

/**
 * calculateTurnover() runs through the prev and current sorted lists of TraitCount
 * objects, and calculates turnover (additions and removals) of traits from "top N"
 * lists (i.e., truncating prev and cur sorted lists if list.size > topNlistsize).
 * The topN cur and prev lists are then intersected, and we return turnover as:
 * (prevsize + cursize) - (2 * intersection.size)
 * @return turnover - double representing turnover, calculated from current data
 *//*from   w w w  . j  av a 2s .  c  o  m*/
private double calculateTurnover() {
    double turnover = 0.0;
    //log.debug("lists should be trimmed to " + topNListSize);

    if (prevSortedTraitCounts == null) {
        // this will happen on the first tick, after that we should be fine
        return 0;
    }

    // given the sorted trait frequencies tallied in the IDataCollector process()
    // method, extract the top N traits and the number of agents that have traits in that top N list
    List prevList = this.getTopNTraits(prevSortedTraitCounts);
    List curList = this.getTopNTraits(curSortedTraitCounts);
    int numAgentsInTopN = this.getNumAgentsInTopN(curSortedTraitCounts);

    log.debug("TFA:  num agents with traits in top N: " + numAgentsInTopN);

    // update the tracking information for how long traits in the TopN spend in each position
    this.updateCumTopNResidenceByTrait(curList);

    // now find the intersection of these two sorted trait ID lists
    Collection intersection = CollectionUtils.intersection(prevList, curList);
    log.debug("TFA:  previous: " + Arrays.deepToString(prevList.toArray()));
    log.debug("TFA:  current: " + Arrays.deepToString(curList.toArray()));
    log.debug("TFA:  intersection: " + Arrays.deepToString(intersection.toArray()));

    // now use the list sizes and the cardinality of the intersection set to calculate turnover
    int prevSize = prevList.size();
    int curSize = curList.size();
    int intersectionSize = intersection.size();
    turnover = (prevSize + curSize) - (2 * intersection.size());
    log.debug("prev size: " + prevSize + " cursize: " + curSize + " intersection size: " + intersectionSize
            + " turnover: " + turnover);

    // add the calculated to the turnover history
    this.turnoverHistory.add(turnover);
    this.agentsInTopNHistory.add((double) numAgentsInTopN);

    return turnover;
}

From source file:com.celements.navigation.Navigation.java

boolean isActiveMenuItem(DocumentReference docRef) {
    DocumentReference currentDocRef = getContext().getDoc().getDocumentReference();
    List<DocumentReference> docParentList = getWebUtilsService().getDocumentParentsList(currentDocRef, true);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("isActiveMenuItem: for [" + docRef + "] with [" + docParentList.size() + "] parents ["
                + Arrays.deepToString(docParentList.toArray(new DocumentReference[0])) + "].");
    }//from  w  w w  . j  a  v  a2 s  .c o  m
    return (docRef != null) && (docParentList.contains(docRef) || docRef.equals(currentDocRef));
}

From source file:it.eng.spagobi.tools.scheduler.dispatcher.MailDocumentDispatchChannel.java

private static List<String> findRecipientsFromDataSet(DispatchContext info, BIObject biobj,
        IDataStore dataStore) throws Exception {
    logger.debug("IN");
    List<String> recipients = new ArrayList();
    if (info.isUseDataSet()) {
        logger.debug("Trigger is configured to send mail to recipients retrieved by a dataset");
        if (dataStore == null || dataStore.isEmpty()) {
            throw new Exception("The dataset in input is empty!! Cannot retrieve recipients from it.");
        }/*from w  ww . j  a  v a2 s.co m*/
        // in this case recipients must be retrieved by the dataset (which the datastore in input belongs to)
        // we must find the parameter value in order to filter the dataset
        String dsParameterLabel = info.getDataSetParameterLabel();
        logger.debug("The dataset will be filtered using the value of the parameter " + dsParameterLabel);
        // looking for the parameter
        List parameters = biobj.getBiObjectParameters();
        BIObjectParameter parameter = null;
        String codeValue = null;
        Iterator parameterIt = parameters.iterator();
        while (parameterIt.hasNext()) {
            BIObjectParameter aParameter = (BIObjectParameter) parameterIt.next();
            if (aParameter.getLabel().equalsIgnoreCase(dsParameterLabel)) {
                parameter = aParameter;
                break;
            }
        }
        if (parameter == null) {
            throw new Exception("The document parameter with label [" + dsParameterLabel
                    + "] was not found. Cannot filter the dataset.");
        }

        // considering the first value of the parameter
        List values = parameter.getParameterValues();
        if (values == null || values.isEmpty()) {
            throw new Exception("The document parameter with label [" + dsParameterLabel
                    + "] has no values. Cannot filter the dataset.");
        }

        codeValue = (String) values.get(0);
        logger.debug("Using value [" + codeValue + "] for dataset filtering...");

        Iterator it = dataStore.iterator();
        while (it.hasNext()) {
            String recipient = null;
            IRecord record = (IRecord) it.next();
            // the parameter value is used to filter on the first dataset field
            IField valueField = (IField) record.getFieldAt(0);
            Object valueObj = valueField.getValue();
            String value = null;
            if (valueObj != null)
                value = valueObj.toString();
            if (codeValue.equals(value)) {
                logger.debug("Found value [" + codeValue + "] on the first field of a record of the dataset.");
                // recipient address is on the second dataset field
                IField recipientField = (IField) record.getFieldAt(1);
                Object recipientFieldObj = recipientField.getValue();
                if (recipientFieldObj != null) {
                    recipient = recipientFieldObj.toString();
                    // in this case recipients can be separated by ","
                    String[] multiRecipients = recipient.split(",");

                    recipients.addAll(Arrays.asList(multiRecipients));

                    logger.debug("DataSet multi recipients found: " + Arrays.deepToString(multiRecipients));
                } else {
                    logger.warn("The second field of the record is null.");
                }
            }
            if (recipient != null) {
                recipients.add(recipient);
            }
        }
        logger.debug("Recipients found from dataset: " + recipients.toArray());
    }
    logger.debug("OUT");
    return recipients;
}

From source file:voldemort.VoldemortClientShell.java

@SuppressWarnings("unchecked")
protected void printObject(Object o) {
    if (o == null) {
        commandOutput.print("null");
    } else if (o instanceof String) {
        commandOutput.print('"');
        commandOutput.print(o);/*w  w  w.ja  va  2  s.c  o m*/
        commandOutput.print('"');
    } else if (o instanceof Date) {
        DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
        commandOutput.print("'");
        commandOutput.print(df.format((Date) o));
        commandOutput.print("'");
    } else if (o instanceof List) {
        List<Object> l = (List<Object>) o;
        commandOutput.print("[");
        for (Object obj : l)
            printObject(obj);
        commandOutput.print("]");
    } else if (o instanceof Map) {
        Map<String, Object> m = (Map<String, Object>) o;
        commandOutput.print('{');
        for (String s : m.keySet()) {
            printObject(s);
            commandOutput.print(':');
            printObject(m.get(s));
            commandOutput.print(", ");
        }
        commandOutput.print('}');
    } else if (o instanceof Object[]) {
        Object[] a = (Object[]) o;
        commandOutput.print(Arrays.deepToString(a));
    } else if (o instanceof byte[]) {
        byte[] a = (byte[]) o;
        commandOutput.print(Arrays.toString(a));
    } else {
        commandOutput.print(o);
    }
}

From source file:org.gytheio.util.exec.RuntimeExec.java

/**
 * Executes the statement that this instance was constructed with an optional
 * timeout after which the command is asked to 
 * /*  w  w w  .  j  av a  2  s . co m*/
 * @param properties the properties that the command might be executed with.
 * <code>null</code> properties will be treated as an empty string for substitution
 * purposes.
 * @param timeoutMs a timeout after which {@link Process#destroy()} is called.
 *        ignored if less than or equal to zero. Note this method does not guarantee
 *        to terminate the process (it is not a kill -9).
 * @param stdOutGobblerFactory the object used to create the output input stream reader
 *        If null the defaultInputStreamReaderThreadFactory will be used
 * @param stdErrGobblerFactory the object used to create the error input stream reader
 *        If null the defaultInputStreamReaderThreadFactory will be used
 * 
 * @return Returns the full execution results
 */
public ExecutionResult execute(Map<String, String> properties,
        InputStreamReaderThreadFactory stdOutGobblerFactory,
        InputStreamReaderThreadFactory stdErrGobblerFactory, final long timeoutMs) {
    int defaultFailureExitValue = errCodes.size() > 0 ? ((Integer) errCodes.toArray()[0]) : 1;

    // check that the command has been set
    if (command == null) {
        throw new GytheioRuntimeException("Runtime command has not been set: \n" + this);
    }

    if (stdOutGobblerFactory == null) {
        stdOutGobblerFactory = defaultInputStreamReaderThreadFactory;
    }
    if (stdErrGobblerFactory == null) {
        stdErrGobblerFactory = defaultInputStreamReaderThreadFactory;
    }

    // create the properties
    Runtime runtime = Runtime.getRuntime();
    Process process = null;
    String[] commandToExecute = null;
    try {
        // execute the command with full property replacement
        commandToExecute = getCommand(properties);
        final Process thisProcess = runtime.exec(commandToExecute, processProperties, processDirectory);
        process = thisProcess;
        if (timeoutMs > 0) {
            final String[] command = commandToExecute;
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // Only try to kill the process if it is still running
                    try {
                        thisProcess.exitValue();
                    } catch (IllegalThreadStateException stillRunning) {
                        if (transformerDebugLogger.isDebugEnabled()) {
                            transformerDebugLogger.debug("Process has taken too long (" + (timeoutMs / 1000)
                                    + " seconds). Killing process " + Arrays.deepToString(command));
                        }
                        thisProcess.destroy();
                    }
                }
            }, timeoutMs);
        }
    } catch (IOException e) {
        // The process could not be executed here, so just drop out with an appropriate error state
        String execOut = "";
        String execErr = e.getMessage();
        int exitValue = defaultFailureExitValue;
        ExecutionResult result = new ExecutionResult(null, commandToExecute, errCodes, exitValue, execOut,
                execErr);
        logFullEnvironmentDump(result);
        return result;
    }

    // create the stream gobblers
    InputStreamReaderThread stdOutGobbler = stdOutGobblerFactory.createInstance(process.getInputStream(),
            charset);
    InputStreamReaderThread stdErrGobbler = stdErrGobblerFactory.createInstance(process.getErrorStream(),
            charset);

    // start gobbling
    stdOutGobbler.start();
    stdErrGobbler.start();

    // wait for the process to finish
    int exitValue = 0;
    try {
        if (waitForCompletion) {
            exitValue = process.waitFor();
        }
    } catch (InterruptedException e) {
        // process was interrupted - generate an error message
        stdErrGobbler.addToBuffer(e.toString());
        exitValue = defaultFailureExitValue;
    }

    if (waitForCompletion) {
        // ensure that the stream gobblers get to finish
        stdOutGobbler.waitForCompletion();
        stdErrGobbler.waitForCompletion();
    }

    // get the stream values
    String execOut = stdOutGobbler.getBuffer();
    String execErr = stdErrGobbler.getBuffer();

    // construct the return value
    ExecutionResult result = new ExecutionResult(process, commandToExecute, errCodes, exitValue, execOut,
            execErr);

    // done
    logFullEnvironmentDump(result);
    return result;
}

From source file:edu.harvard.iq.dvn.ingest.dsb.impl.DvnRDataAnalysisServiceImpl.java

/** *************************************************************
 * Execute an R-based dvn statistical analysis request 
 *
 * @param sro    a DvnRJobRequest object that contains various parameters
 * @return    a Map that contains various information about results
 *///from w ww  .j  a va2s . c om

public Map<String, String> execute(DvnRJobRequest sro) {

    // Step 1. Copy of Rdata file
    // Step 2. Subset Rdata file
    // Step 3. Return the subsetted Rdata file instead of the original

    // set the return object
    Map<String, String> result = new HashMap<String, String>();
    // temporary result
    Map<String, String> tmpResult = new HashMap<String, String>();

    try {
        // Set up an Rserve connection
        dbgLog.info("sro dump:\n" + ToStringBuilder.reflectionToString(sro, ToStringStyle.MULTI_LINE_STYLE));

        dbgLog.fine("RSERVE_USER=" + RSERVE_USER + "[default=rserve]");
        dbgLog.fine("RSERVE_PWD=" + RSERVE_PWD + "[default=rserve]");
        dbgLog.fine("RSERVE_PORT=" + RSERVE_PORT + "[default=6311]");

        RConnection c = new RConnection(RSERVE_HOST, RSERVE_PORT);
        dbgLog.fine("hostname=" + RSERVE_HOST);

        c.login(RSERVE_USER, RSERVE_PWD);
        dbgLog.info("R Version = " + c.eval("R.version$version.string").asString() + "<");
        dbgLog.info("SRO request type: " + sro.getRequestType());

        // check working directories
        // This needs to be done *before* we try to create any files 
        // there!
        setupWorkingDirectories(c);

        // save the data file at the Rserve side
        String infile = sro.getSubsetFileName();

        InputStream inb = new BufferedInputStream(new FileInputStream(infile));

        int bufsize;
        byte[] bffr = new byte[1024];

        RFileOutputStream os = c.createFile(tempFileName);
        while ((bufsize = inb.read(bffr)) != -1) {
            os.write(bffr, 0, bufsize);
        }
        os.close();
        inb.close();

        // save the original data file on the server-side

        // WORKS STARTS HERE
        // os = c.createFile(tempOriginalFileName);

        // Rserve code starts here
        dbgLog.fine("DvnRserveComm: " + "wrkdir=" + wrkdir);
        //dbgLog.fine("DvnRserveComm: "+librarySetup);
        //historyEntry.add(librarySetup);
        //c.voidEval(librarySetup);

        Properties p = System.getProperties();
        String domainRoot = p.getProperty("com.sun.aas.instanceRoot");
        String rFunctionsFileName = domainRoot + "/config/" + DVN_R_DATA_FUNCTIONS;

        dbgLog.fine("Source code for the custom DVN R functions: " + rFunctionsFileName);

        File rFunctionsFile = new File(rFunctionsFileName);
        if (!rFunctionsFile.exists()) {
            throw new IOException("Could not find R source code file " + rFunctionsFileName);
        }

        /* 
         * Send the R code file across:
         */

        inb = new BufferedInputStream(new FileInputStream(rFunctionsFile));

        os = c.createFile(tempRCodeFileName);
        while ((bufsize = inb.read(bffr)) != -1) {
            os.write(bffr, 0, bufsize);
        }
        os.close();
        inb.close();

        /* 
         * And read it in: 
         */

        String newLibrarySetup = "source(\"" + tempRCodeFileName + "\");";
        dbgLog.fine("DvnRserveComm: " + newLibrarySetup);
        historyEntry.add(newLibrarySetup);
        c.voidEval(newLibrarySetup);
        dbgLog.fine("DVN R Code library has been read.");

        // variable type
        /* 
        vartyp <-c(1,1,1)
        */
        // java side
        // int [] jvartyp  = {1,1,1};// = mp.get("vartyp").toArray()
        // int [] jvartyp  = sro.getVariableTypes();
        /*
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0 ; i< jvartyp.length; i++){
        if (i == (jvartyp.length -1)){
            sb.append(String.valueOf(jvartyp[i]));
        } else {
            sb.append(String.valueOf(jvartyp[i])+", ");
        }
                    }
                            
                    // R side
                    historyEntry.add("vartyp<-c(" + sb.toString()+")");
        */

        //c.assign("vartyp", new REXPInteger(jvartyp));
        if ("Download".equals(sro.getRequestType())) {
            /*
             * Note that we want to use the "getVariableTypesWithBoolean method 
             * when the subset is being created for download/conversion; when 
             * we create a SRO object for analysis, we'll still be using 
             * the old getVariableTypes
             * method, that don't recognize Booleans as a distinct class. 
             * So they will be treated simply as numeric categoricals 
             * (factors) with the "TRUE" and "FALSE" labels. But for the purposes
             * of saving the subset in R format, we want to convert these into
             * R "logical" vectors.
             * 
             * TODO: verify what's going to happen to these "logical"
             * variables when we call R package Foreign to convert the 
             * dataset into STATA format. -- L.A. 
             */

            dbgLog.fine("raw variable type=" + sro.getVariableTypesWithBoolean());
            c.assign("vartyp", new REXPInteger(sro.getVariableTypesWithBoolean()));
            String[] tmpt = c.eval("vartyp").asStrings();
            dbgLog.fine("vartyp length=" + tmpt.length + "\t " + StringUtils.join(tmpt, ","));

        } else {
            historyEntry.add("vartyp<-c(" + StringUtils.join(sro.getVariableTypesAsString(), ",") + ")");
            dbgLog.fine("DvnRserveComm: " + "vartyp<-c(" + StringUtils.join(sro.getVariableTypesAsString(), ",")
                    + ")");

            dbgLog.fine("raw variable type=" + sro.getVariableTypes());
            c.assign("vartyp", new REXPInteger(sro.getVariableTypes()));
            String[] tmpt = c.eval("vartyp").asStrings();
            dbgLog.fine(
                    "DvnRserveComm: " + "vartyp length=" + tmpt.length + "\t " + StringUtils.join(tmpt, ","));
        }

        // variable format (date/time)
        /* 
        varFmt<-list();
        c.voidEval("varFmt<-list()");
        */

        Map<String, String> tmpFmt = sro.getVariableFormats();
        dbgLog.fine("DvnRserveComm: " + "tmpFmt=" + tmpFmt);
        if (tmpFmt != null) {
            Set<String> vfkeys = tmpFmt.keySet();
            String[] tmpfk = (String[]) vfkeys.toArray(new String[vfkeys.size()]);
            String[] tmpfv = getValueSet(tmpFmt, tmpfk);
            historyEntry.add("tmpfk<-c(" + StringUtils.join(tmpfk, ", ") + ")");
            dbgLog.fine("DvnRserveComm: " + "tmpfk<-c(" + StringUtils.join(tmpfk, ", ") + ")");

            c.assign("tmpfk", new REXPString(tmpfk));
            historyEntry.add("tmpfv<-c(" + StringUtils.join(tmpfv, ", ") + ")");
            dbgLog.fine("DvnRserveComm: " + "tmpfv<-c(" + StringUtils.join(tmpfv, ", ") + ")");
            c.assign("tmpfv", new REXPString(tmpfv));
            String fmtNamesLine = "names(tmpfv)<- tmpfk";
            historyEntry.add(fmtNamesLine);

            dbgLog.fine("DvnRserveComm: " + fmtNamesLine);
            c.voidEval(fmtNamesLine);

            String fmtValuesLine = "varFmt<- as.list(tmpfv)";
            historyEntry.add(fmtValuesLine);

            dbgLog.fine("DvnRserveComm: " + fmtValuesLine);
            c.voidEval(fmtValuesLine);
        } else {
            String[] varFmtN = {};
            List<String> varFmtV = new ArrayList<String>();
            historyEntry.add("varFmt <- list()");

            dbgLog.fine("DvnRserveComm: " + "varFmt <- list()");
            c.assign("varFmt", new REXPList(new RList(varFmtV, varFmtN)));
        }
        /*
        vnames<-c("race","age","vote")
        */

        // variable names
        // String [] jvnames = {"race","age","vote"};

        String[] jvnamesRaw = sro.getVariableNames();
        String[] jvnames = null;

        //VariableNameFilterForR nf = new VariableNameFilterForR(jvnamesRaw);

        if (sro.hasUnsafedVariableNames) {
            // create  list
            jvnames = sro.safeVarNames;
            dbgLog.fine("renamed=" + StringUtils.join(jvnames, ","));
        } else {
            jvnames = jvnamesRaw;
        }
        //historyEntry.add("vnamnes<-c("+ StringUtils.join(jvnames, ", ")+")");
        String vnQList = DvnDSButil.joinNelementsPerLine(jvnames, true);
        historyEntry.add("vnames<-c(" + vnQList + ")");

        c.assign("vnames", new REXPString(jvnames));

        // confirmation
        String[] tmpjvnames = c.eval("vnames").asStrings();
        dbgLog.fine("DvnRserveComm: " + "vnames:" + StringUtils.join(tmpjvnames, ","));

        /*
        x<-read.table141vdc(file="/tmp/VDC/t.28948.1.tab", 
            col.names=vnames, colClassesx=vartyp, varFormat=varFmt)
        */

        //String datafilename = "/nfs/home/A/asone/java/rcode/t.28948.1.tab";

        // tab-delimited file name = tempFileName

        // vnames = Arrays.deepToString(new REXPString(jvnames).asStrings())
        // vartyp = Arrays.deepToString(new REXPInteger(sro.getUpdatedVariableTypes()).asStrings())
        // varFmt = 
        dbgLog.fine("col names ..... " + Arrays.deepToString(jvnames));
        dbgLog.fine("colClassesX ... " + Arrays.toString(sro.getVariableTypesWithBoolean()));
        dbgLog.fine("varFormat ..... " + tmpFmt);

        String readtableline = "x<-read.table141vdc(file='" + tempFileName
                + "', col.names=vnames, colClassesx=vartyp, varFormat=varFmt )";

        historyEntry.add(readtableline);
        dbgLog.fine("DvnRserveComm: " + "readtable=" + readtableline);

        c.voidEval(readtableline);

        // safe-to-raw variable name
        /* 
          attr(x, "Rsafe2raw")<-list();
        */
        //if (nf.hasRenamedVariables()){
        if (sro.hasUnsafedVariableNames) {
            dbgLog.fine("unsafeVariableNames exist");
            // create  list
            //jvnames = nf.getFilteredVarNames();
            jvnames = sro.safeVarNames;
            String[] rawNameSet = sro.renamedVariableArray;
            String[] safeNameSet = sro.renamedResultArray;

            historyEntry.add("tmpRN<-c(" + StringUtils.join(rawNameSet, ", ") + ")");
            c.assign("tmpRN", new REXPString(rawNameSet));
            historyEntry.add("tmpSN<-c(" + StringUtils.join(safeNameSet, ", ") + ")");
            c.assign("tmpSN", new REXPString(safeNameSet));

            String raw2safevarNameTableLine = "names(tmpRN)<- tmpSN";
            historyEntry.add(raw2safevarNameTableLine);
            c.voidEval(raw2safevarNameTableLine);
            String attrRsafe2rawLine = "attr(x, 'Rsafe2raw')<- as.list(tmpRN)";
            historyEntry.add(attrRsafe2rawLine);
            c.voidEval(attrRsafe2rawLine);
        } else {
            String attrRsafe2rawLine = "attr(x, 'Rsafe2raw')<-list();";
            historyEntry.add(attrRsafe2rawLine);
            c.voidEval(attrRsafe2rawLine);
        }

        //Map<String, String> Rsafe2raw = sro.getRaw2SafeVarNameTable();

        // asIs
        /* 
        for (i in 1:dim(x)[2]){if (attr(x,"var.type")[i] == 0) {
        x[[i]]<-I(x[[i]]);  x[[i]][ x[[i]] == '' ]<-NA  }}
        */

        /* 
         * Commenting out the fragment below: 
         * - this is now being done early on in the read.table141vdc
         *   R function:
                 
        String asIsline  = "for (i in 1:dim(x)[2]){ "+
        "if (attr(x,'var.type')[i] == 0) {" +
        "x[[i]]<-I(x[[i]]);  x[[i]][ x[[i]] == '' ]<-NA  }}";
        historyEntry.add(asIsline);
        c.voidEval(asIsline);
        */

        // replication: copy the data.frame
        String repDVN_Xdupline = "dvnData<-x";
        c.voidEval(repDVN_Xdupline);
        tmpResult.put("dvn_dataframe", "dvnData");

        // recoding line
        if (sro.hasRecodedVariables()) {

            // subsetting 

            // For operations on time values during recoding we need
            // to set the following option (this is to enable millisecond
            // precision time): 
            //      -- L.A., v3.6

            c.voidEval("saved.options <- options(digits.secs = 3)");

            List<String> scLst = sro.getSubsetConditions();
            if (scLst != null) {
                for (String sci : scLst) {
                    dbgLog.fine("sci:" + sci);
                    historyEntry.add(sci);
                    c.voidEval(sci);
                }
                tmpResult.put("subset", "T");
            }
            // recoding

            List<String> rcLst = sro.getRecodeConditions();
            if (rcLst != null) {
                for (String rci : rcLst) {
                    dbgLog.fine("rci:" + rci);

                    historyEntry.add(rci);
                    c.voidEval(rci);
                }

                // subsetting (mutating the data.frame strips non-default attributes 
                // 
                // variable type must be re-attached

                if (!"Download".equals(sro.getRequestType())) {
                    String varTypeNew = "vartyp<-c("
                            + StringUtils.join(sro.getUpdatedVariableTypesAsString(), ",") + ")";
                    historyEntry.add(varTypeNew);
                    dbgLog.fine("updated var Type =" + sro.getUpdatedVariableTypesAsString());
                    c.assign("vartyp", new REXPInteger(sro.getUpdatedVariableTypes()));
                } else {
                    dbgLog.fine("updated var Type =" + sro.getUpdatedVariableTypesWithBooleanAsString());
                    c.assign("vartyp", new REXPInteger(sro.getUpdatedVariableTypesWithBoolean()));
                }

                String reattachVarTypeLine = "attr(x, 'var.type') <- vartyp";
                historyEntry.add(reattachVarTypeLine);

                dbgLog.fine("DvnRserveComm: " + reattachVarTypeLine);
                c.voidEval(reattachVarTypeLine);

                // replication: variable type
                String repDVN_vt = "attr(dvnData, 'var.type') <- vartyp";

                dbgLog.fine("DvnRserveComm: " + repDVN_vt);
                c.voidEval(repDVN_vt);

                // new (recoded) variable names: 
                String varNamesRecoded = "recodedvnames<-c(" + StringUtils.join(sro.getRecodedVarNameSet(), ",")
                        + ")";
                historyEntry.add(varNamesRecoded);
                dbgLog.fine("recoded var names =" + StringUtils.join(sro.getRecodedVarNameSet(), ","));
                c.assign("recodedvnames", sro.getRecodedVarNameSet());

                tmpFmt = sro.getUpdatedVariableFormats();
                dbgLog.fine("DvnRserveComm: " + "(recoded)tmpFmt=" + tmpFmt);
                if (tmpFmt != null) {
                    Set<String> vfkeys = tmpFmt.keySet();
                    String[] tmpfk = (String[]) vfkeys.toArray(new String[vfkeys.size()]);
                    String[] tmpfv = getValueSet(tmpFmt, tmpfk);
                    historyEntry.add("tmpfk<-c(" + StringUtils.join(tmpfk, ", ") + ")");
                    dbgLog.fine("DvnRserveComm: " + "(recoded)tmpfk<-c(" + StringUtils.join(tmpfk, ", ") + ")");

                    c.assign("tmpfk", new REXPString(tmpfk));
                    historyEntry.add("tmpfv<-c(" + StringUtils.join(tmpfv, ", ") + ")");
                    dbgLog.fine("DvnRserveComm: " + "(recoded)tmpfv<-c(" + StringUtils.join(tmpfv, ", ") + ")");
                    c.assign("tmpfv", new REXPString(tmpfv));
                    String fmtNamesLine = "names(tmpfv)<- tmpfk";
                    historyEntry.add(fmtNamesLine);

                    dbgLog.fine("DvnRserveComm: (recoded)" + fmtNamesLine);
                    c.voidEval(fmtNamesLine);

                    String fmtValuesLine = "varFmt<- as.list(tmpfv)";
                    historyEntry.add(fmtValuesLine);

                    dbgLog.fine("DvnRserveComm: (recoded)" + fmtValuesLine);
                    c.voidEval(fmtValuesLine);
                } else {
                    String[] varFmtN = {};
                    List<String> varFmtV = new ArrayList<String>();
                    historyEntry.add("varFmt <- list()");

                    dbgLog.fine("DvnRserveComm: (recoded)" + "varFmt <- list()");
                    c.assign("varFmt", new REXPList(new RList(varFmtV, varFmtN)));
                }

                // run post-processing on the newly created recode variables:
                dbgLog.fine("running transformrecoded()");

                historyEntry.add("transformrecoded()");
                int recodedVarsStartIndex = sro.getVariableNames().length + 1;
                dbgLog.fine("recoded variables start at " + recodedVarsStartIndex);
                c.voidEval("x<-transformrecoded(x, recodedvarsindx=" + recodedVarsStartIndex
                        + ", col.names=c(vnames,recodedvnames), colClassesx=vartyp, varFormat=varFmt)");
            }

            // reset the milliseconds option: 

            c.voidEval("options(saved.options)");
        }

        // variable Id
        /* 
        attr(x, "var.nmbr")<-c("v198057","v198059","v198060")
        */

        // String[] jvarnmbr = {"v198057","v198059","v198060"};
        //String[] jvarnmbr = sro.getVariableIds();
        // after recoding 
        String[] jvarnmbr = sro.getUpdatedVariableIds();

        String viQList = DvnDSButil.joinNelementsPerLine(jvarnmbr, true);
        historyEntry.add("varnmbr <-c(" + viQList + ")");
        c.assign("varnmbr", new REXPString(jvarnmbr));

        String attrVarNmbrLine = "attr(x, 'var.nmbr')<-varnmbr";
        historyEntry.add(attrVarNmbrLine);

        dbgLog.fine("DvnRserveComm: (varnmbr) " + attrVarNmbrLine);
        c.voidEval(attrVarNmbrLine);

        // confirmation
        String[] vno = c.eval("attr(x, 'var.nmbr')").asStrings();
        dbgLog.fine("varNo=" + StringUtils.join(vno, ","));

        // replication: variable number
        String repDVN_vn = "attr(dvnData, 'var.nmbr') <- varnmbr";

        dbgLog.fine("DvnRserveComm: " + repDVN_vn);
        c.voidEval(repDVN_vn);

        // variable labels
        /* 
        attr(x, "var.labels")<-c("race","age","vote")
        */

        // String[] jvarlabels = {"race","age","vote"};
        // String[] jvarlabels = sro.getVariableLabels();
        // after recoding
        String[] jvarlabels = sro.getUpdatedVariableLabels();

        String vlQList = DvnDSButil.joinNelementsPerLine(jvarlabels, true);

        historyEntry.add("varlabels <-c(" + vlQList + ")");

        dbgLog.fine("DvnRserveComm: " + "varlabels <-c(" + vlQList + ")");
        c.assign("varlabels", new REXPString(jvarlabels));

        String attrVarLabelsLine = "attr(x, 'var.labels')<-varlabels";
        historyEntry.add(attrVarLabelsLine);

        dbgLog.fine("DvnRserveComm: " + attrVarLabelsLine);
        c.voidEval(attrVarLabelsLine);

        // confirmation
        String[] vlbl = c.eval("attr(x, 'var.labels')").asStrings();
        dbgLog.fine("varlabels=" + StringUtils.join(vlbl, ","));

        // replication: 
        String repDVN_vl = "attr(dvnData, 'var.labels') <- varlabels";

        dbgLog.fine("DvnRserveComm: " + repDVN_vl);
        c.voidEval(repDVN_vl);

        // --------- start: block to be used for the production code
        // value-label table
        /* 
        VALTABLE<-list()
        VALTABLE[["1"]]<-list(
        "2"="white",
        "1"="others")
        attr(x, 'val.table')<-VALTABLE
        */

        // create the VALTABLE
        String vtFirstLine = "VALTABLE<-list()";
        historyEntry.add(vtFirstLine);

        dbgLog.fine("DvnRserveComm: " + vtFirstLine);
        c.voidEval(vtFirstLine);

        c.voidEval("VALORDER<-list()");

        // vltbl includes both base and recoded cases when it was generated
        Map<String, Map<String, String>> vltbl = sro.getValueTable();
        Map<String, List<String>> orderedCategoryValues = sro.getCategoryValueOrders();
        Map<String, String> rnm2vi = sro.getRawVarNameToVarIdTable();
        String[] updatedVariableIds = sro.getUpdatedVariableIds();

        //for (int j=0;j<jvnamesRaw.length;j++){
        for (int j = 0; j < updatedVariableIds.length; j++) {
            // if this variable has its value-label table,
            // pass its key and value arrays to the Rserve
            // and finalize a value-table at the Rserve

            //String varId = rnm2vi.get(jvnamesRaw[j]);
            String varId = updatedVariableIds[j];

            if (vltbl.containsKey(varId)) {

                Map<String, String> tmp = (HashMap<String, String>) vltbl.get(varId);
                Set<String> vlkeys = tmp.keySet();
                String[] tmpk = (String[]) vlkeys.toArray(new String[vlkeys.size()]);
                String[] tmpv = getValueSet(tmp, tmpk);
                // debug
                dbgLog.fine("tmp:k=" + StringUtils.join(tmpk, ","));
                dbgLog.fine("tmp:v=" + StringUtils.join(tmpv, ","));

                // index number starts from 1(not 0)
                int indx = j + 1;
                dbgLog.fine("index=" + indx);

                if (tmpv.length > 0) {

                    historyEntry.add("tmpk<-c(" + DvnDSButil.joinNelementsPerLine(tmpk, true) + ")");
                    c.assign("tmpk", new REXPString(tmpk));

                    historyEntry.add("tmpv<-c(" + DvnDSButil.joinNelementsPerLine(tmpv, true) + ")");
                    c.assign("tmpv", new REXPString(tmpv));

                    String namesValueLine = "names(tmpv)<- tmpk";
                    historyEntry.add(namesValueLine);
                    c.voidEval(namesValueLine);

                    String sbvl = "VALTABLE[['" + Integer.toString(indx) + "']]" + "<- as.list(tmpv)";
                    dbgLog.fine("frag=" + sbvl);
                    historyEntry.add(sbvl);
                    c.voidEval(sbvl);

                    // confirmation test for j-th variable name
                    REXP jl = c.parseAndEval(sbvl);
                    dbgLog.fine("jl(" + j + ") = " + jl);
                }
            }

            if (orderedCategoryValues != null && orderedCategoryValues.containsKey(varId)) {
                int indx = j + 1;
                List<String> orderList = orderedCategoryValues.get(varId);
                if (orderList != null) {
                    String[] ordv = (String[]) orderList.toArray(new String[orderList.size()]);
                    dbgLog.fine("ordv=" + StringUtils.join(ordv, ","));
                    c.assign("ordv", new REXPString(ordv));
                    String sbvl = "VALORDER[['" + Integer.toString(indx) + "']]" + "<- as.list(ordv)";
                    dbgLog.fine("VALORDER[...]=" + sbvl);
                    historyEntry.add(sbvl);
                    c.voidEval(sbvl);
                } else {
                    dbgLog.fine("NULL orderedCategoryValues list.");
                }
            }
        }

        // debug: confirmation test for value-table
        dbgLog.fine("length of vl=" + c.eval("length(VALTABLE)").asInteger());
        String attrValTableLine = "attr(x, 'val.table')<-VALTABLE";
        historyEntry.add(attrValTableLine);
        c.voidEval(attrValTableLine);

        // replication: value-label table
        String repDVN_vlt = "attr(dvnData, 'val.table') <- VALTABLE";
        c.voidEval(repDVN_vlt);

        // --------- end: block to be used for the production code

        // missing-value list: TO DO
        /*
        MSVLTBL<-list(); attr(x, 'missval.table')<-MSVLTBL
        */
        String msvStartLine = "MSVLTBL<-list();";
        historyEntry.add(msvStartLine);
        c.voidEval(msvStartLine);
        // data structure
        String attrMissvalLine = "attr(x, 'missval.table')<-MSVLTBL";
        historyEntry.add(attrMissvalLine);
        c.voidEval(attrMissvalLine);

        // replication: missing value table
        String repDVN_mvlt = "attr(dvnData, 'missval.table') <- MSVLTBL";
        c.voidEval(repDVN_mvlt);

        // attach attributes(tables) to the data.frame
        /*
        x<-createvalindex(dtfrm=x, attrname='val.index')
        x<-createvalindex(dtfrm=x, attrname='missval.index')
        */
        String createVIndexLine = "x<-createvalindex(dtfrm=x, attrname='val.index');";
        historyEntry.add(createVIndexLine);
        c.voidEval(createVIndexLine);
        String createMVIndexLine = "x<-createvalindex(dtfrm=x, attrname='missval.index');";
        historyEntry.add(createMVIndexLine);
        c.voidEval(createMVIndexLine);

        // reflection block: start ------------------------------------------>

        String requestTypeToken = sro.getRequestType();// (Download|EDA|Xtab|Zelig)
        dbgLog.fine("requestTypeToken=" + requestTypeToken);
        historyEntry.add("#### The Request is " + requestTypeToken + " ####");
        // get a test method
        Method mthd = runMethods.get(requestTypeToken);
        dbgLog.fine("method=" + mthd);

        try {
            // invoke this method
            result = (Map<String, String>) mthd.invoke(this, sro, c);
        } catch (InvocationTargetException e) {
            //Throwable cause = e.getCause();
            //err.format(cause.getMessage());
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        // add the variable list
        result.put("variableList", joinNelementsPerLine(jvnamesRaw, 5, null, false, null, null));

        //result.put("variableList",StringUtils.join(jvnamesRaw, ", "));

        // replication: var-level unf
        String repDVN_varUNF = "attr(dvnData, 'variableUNF') <- paste(unf(dvnData,version=3))";
        c.voidEval(repDVN_varUNF);

        // calculate the file-leve UNF

        String fileUNFline = "fileUNF <- paste(summary(unf(dvnData, version=3)))";
        c.voidEval(fileUNFline);
        String fileUNF = c.eval("fileUNF").asString();
        if (fileUNF == null) {
            fileUNF = "NA";
        }

        // replication: file-level unf
        String repDVN_fileUNF = "attr(dvnData, 'fileUNF') <- fileUNF";
        c.voidEval(repDVN_fileUNF);

        String RversionLine = "R.Version()$version.string";
        String Rversion = c.eval(RversionLine).asString();

        dbgLog.fine(String.format("R-version String = %s", Rversion));

        // replication: R version
        String repDVN_Rversion = "attr(dvnData, 'R.version') <- R.Version()$version.string";
        c.voidEval(repDVN_Rversion);

        String zeligVersionLine = "packageDescription('Zelig')$Version";
        String zeligVersion = c.eval(zeligVersionLine).asString();

        //String RexecDate = c.eval("date()").asString();
        String RexecDate = c.eval("as.character(as.POSIXct(Sys.time()))").asString();
        // replication: date
        String repDVN_date = "attr(dvnData, 'date') <- as.character(as.POSIXct(Sys.time()))";
        c.voidEval(repDVN_date);

        String repDVN_dfOrigin = "attr(dvnData, 'data.frame.origin')<- "
                + "list('provider'='Dataverse Network Project',"
                + "'format' = list('name' = 'DVN data.frame', 'version'='1.3'))";
        c.voidEval(repDVN_dfOrigin);
        /*
        if (result.containsKey("option")){
        result.put("R_run_status", "T");
        } else {
        result.put("option", requestTypeToken.toLowerCase()); //download  zelig eda xtab
        result.put("R_run_status", "F");
        }
        */
        if (sro.hasRecodedVariables()) {
            if (sro.getSubsetConditions() != null) {
                result.put("subsettingCriteria", StringUtils.join(sro.getSubsetConditions(), "\n"));
                // replication: subset lines
                String[] sbst = null;
                sbst = (String[]) sro.getSubsetConditions()
                        .toArray(new String[sro.getSubsetConditions().size()]);

                c.assign("sbstLines", new REXPString(sbst));

                String repDVN_sbst = "attr(dvnData, 'subsetLines') <- sbstLines";
                c.voidEval(repDVN_sbst);
            }
            /* to be used in the future? */
            if (sro.getRecodeConditions() != null) {
                result.put("recodingCriteria", StringUtils.join(sro.getRecodeConditions(), "\n"));
                String[] rcd = null;
                rcd = (String[]) sro.getRecodeConditions()
                        .toArray(new String[sro.getRecodeConditions().size()]);

                c.assign("rcdtLines", new REXPString(rcd));

                String repDVN_rcd = "attr(dvnData, 'recodeLines') <- rcdtLines";
                c.voidEval(repDVN_rcd);
            }

        }

        // save workspace as a replication data set

        String RdataFileName = "DVNdataFrame." + PID + ".RData";
        result.put("Rdata", "/" + requestdir + "/" + RdataFileName);

        String saveWS = "save('dvnData', file='" + wrkdir + "/" + RdataFileName + "')";
        dbgLog.fine("save the workspace=" + saveWS);
        c.voidEval(saveWS);

        // write back the R workspace to the dvn 

        String wrkspFileName = wrkdir + "/" + RdataFileName;
        dbgLog.fine("wrkspFileName=" + wrkspFileName);

        int wrkspflSize = getFileSize(c, wrkspFileName);

        File wsfl = writeBackFileToDvn(c, wrkspFileName, RWRKSP_FILE_PREFIX, "RData", wrkspflSize);

        result.put("dvn_RData_FileName", wsfl.getName());

        if (wsfl != null) {
            result.put("wrkspFileName", wsfl.getAbsolutePath());
            dbgLog.fine("wrkspFileName=" + wsfl.getAbsolutePath());
        } else {
            dbgLog.fine("wrkspFileName is null");
        }

        result.put("library_1", "VDCutil");

        result.put("fileUNF", fileUNF);
        result.put("dsbHost", RSERVE_HOST);
        result.put("dsbPort", DSB_HOST_PORT);
        result.put("dsbContextRootDir", DSB_CTXT_DIR);
        result.put("PID", PID);
        result.put("Rversion", Rversion);
        result.put("zeligVersion", zeligVersion);
        result.put("RexecDate", RexecDate);
        result.put("RCommandHistory", StringUtils.join(historyEntry, "\n"));

        result.putAll(tmpResult);
        dbgLog.fine("result object (before closing the Rserve):\n" + result);

        // reflection block: end

        // create a zip file of the directory created: 

        //String zipTmpDir = "system(\"(cd "+DSB_TMP_DIR+"; zip -r /tmp/"+requestdir+".zip "+requestdir+")\")";
        //c.voidEval(zipTmpDir);        

        // transfer the zip file to the application side: 

        //RFileInputStream ris = null;
        //OutputStream outbr   = null;

        //int zipSize = getFileSize(c,"/tmp/"+requestdir+".zip");

        String listAnalysisFiles = "list.files('" + DSB_TMP_DIR + "/" + requestdir + "', recursive=TRUE)";
        dbgLog.fine("looking up the analysis result files on the DSB/Rserve side: " + listAnalysisFiles);
        String[] analysisReportFiles = c.eval(listAnalysisFiles).asStrings();
        RFileInputStream ris = null;
        OutputStream outbr = null;

        try {
            File localReportDir = new File(TEMP_DIR + "/DVN", requestdir);
            if (!localReportDir.exists()) {
                localReportDir.mkdir();
            }

            for (int i = 0; i < analysisReportFiles.length; i++) {
                String reportFile = analysisReportFiles[i];
                int reportFileSize = getFileSize(c, DSB_TMP_DIR + "/" + requestdir + "/" + reportFile);
                dbgLog.fine("DvnRData: transferring file " + reportFile);
                dbgLog.fine("DvnRData: file size: " + reportFileSize);

                if (reportFile.lastIndexOf("/") > 0) {
                    File localReportSubDir = new File(TEMP_DIR + "/DVN/" + requestdir,
                            reportFile.substring(0, reportFile.lastIndexOf("/")));
                    if (!localReportSubDir.exists()) {
                        localReportSubDir.mkdirs();
                    }
                }

                ris = c.openFile(DSB_TMP_DIR + "/" + requestdir + "/" + reportFile);
                outbr = new BufferedOutputStream(
                        new FileOutputStream(new File(TEMP_DIR + "/DVN/" + requestdir, reportFile)));

                byte[] obuf = new byte[reportFileSize];
                int obufsize = 0;

                while ((obufsize = ris.read(obuf)) != -1) {
                    outbr.write(obuf, 0, reportFileSize);
                }

                ris.close();
                outbr.close();
            }

            //String unZipCmd = "/usr/bin/unzip "+TEMP_DIR+"/DVN/"+requestdir+".zip -d "+TEMP_DIR+"/DVN";
            //int exitValue = 1;

            //dbgLog.fine("attempting to execute "+unZipCmd);

            //try {
            //Runtime runtime = Runtime.getRuntime();
            //Process process = runtime.exec(unZipCmd);
            //exitValue = process.waitFor();
            //} catch (Exception e) {
            //e.printStackTrace();
            //exitValue = 1;
            //}

            //if (exitValue == 0) {
            //result.put("webFolderArchived",TEMP_DIR+"/DVN/"+requestdir+".zip");
            //}

        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        } finally {
            if (ris != null) {
                ris.close();
            }
            if (outbr != null) {
                outbr.close();
            }
        }

        // // move the temp dir to the web-temp root dir
        //String mvTmpDir = "file.rename('"+wrkdir+"','"+webwrkdir+"')";
        //dbgLog.fine("web-temp_dir="+mvTmpDir);
        //c.voidEval(mvTmpDir);        

        // close the Rserve connection
        c.close();

    } catch (RserveException rse) {
        // RserveException (Rserve is not running)
        rse.printStackTrace();

        result.put("dsbContextRootDir", DSB_CTXT_DIR);
        result.put("PID", PID);
        result.put("RCommandHistory", StringUtils.join(historyEntry, "\n"));
        result.put("option", sro.getRequestType().toLowerCase());

        result.put("RexecError", "true");
        return result;

    } catch (REXPMismatchException mme) {

        // REXP mismatch exception (what we got differs from what we expected)
        mme.printStackTrace();

        result.put("dsbContextRootDir", DSB_CTXT_DIR);
        result.put("PID", PID);
        result.put("RCommandHistory", StringUtils.join(historyEntry, "\n"));
        result.put("option", sro.getRequestType().toLowerCase());

        result.put("RexecError", "true");
        return result;

    } catch (IOException ie) {
        ie.printStackTrace();

        result.put("dsbContextRootDir", DSB_CTXT_DIR);
        result.put("PID", PID);
        result.put("RCommandHistory", StringUtils.join(historyEntry, "\n"));
        result.put("option", sro.getRequestType().toLowerCase());

        result.put("RexecError", "true");
        return result;

    } catch (Exception ex) {
        ex.printStackTrace();

        result.put("dsbContextRootDir", DSB_CTXT_DIR);
        result.put("PID", PID);

        result.put("RCommandHistory", StringUtils.join(historyEntry, "\n"));
        result.put("option", sro.getRequestType().toLowerCase());

        result.put("RexecError", "true");
        return result;
    }

    return result;

}

From source file:org.lwes.ArrayEvent.java

/**
 * This method shows detailed information about the internal state of the
 * event, and was designed as a "Detail Formatter" for tracing execution
 * under Eclipse.  It may be useful for other IDEs or other uses.
 *//*from   w  ww  . j av a  2 s  . c o  m*/
public String toStringDetailed() {
    final StringBuilder buf = new StringBuilder();
    try {
        buf.append(String.format("Event name:        \"%s\"\n", getEventName()));
        buf.append(String.format("Serialized length: %d\n", length));
        buf.append(String.format("tempState index:   %d\n", tempState.currentIndex()));
        buf.append(
                String.format("Encoding:          %s\n", Event.ENCODING_STRINGS[encoding].getEncodingString()));
        buf.append(String.format("Number of fields:  %d\n", getNumEventAttributes()));
        final DeserializerState ds = new DeserializerState();
        ds.set(getValueListIndex());
        while (ds.currentIndex() < length) {
            String field;
            FieldType type;
            Object value;
            try {
                field = Deserializer.deserializeATTRIBUTEWORD(ds, bytes);
            } catch (Exception e) {
                throw new Exception("Error when reading field name: " + e.getMessage());
            }
            try {
                type = FieldType.byToken(Deserializer.deserializeBYTE(ds, bytes));
            } catch (Exception e) {
                throw new Exception("Error when reading field name: " + e.getMessage());
            }
            try {
                value = Deserializer.deserializeValue(ds, bytes, type, encoding);
            } catch (Exception e) {
                throw new Exception("Error when reading field name: " + e.getMessage());
            }
            if (value.getClass().isArray()) {
                value = Arrays.deepToString(new Object[] { value });
            }
            buf.append(String.format("  field \"%s\" (%s): %s\n", field, type, value));
        }
    } catch (Exception e) {
        buf.append("\nEXCEPTION: ").append(e.getMessage());
    }
    return buf.toString();
}