Example usage for java.util ArrayList clear

List of usage examples for java.util ArrayList clear

Introduction

In this page you can find the example usage for java.util ArrayList clear.

Prototype

public void clear() 

Source Link

Document

Removes all of the elements from this list.

Usage

From source file:net.tourbook.tour.photo.TourPhotoManager.java

/**
 * History tours can occure multiple times in sequence, when tours between history tours do not
 * contain photos. This will merge multiple history tours into one.
 * /*from www  . ja v a 2 s  . c  om*/
 * @param allTourPhotoLinks
 */
private void createTourPhotoLinks_60_MergeHistoryTours(final ArrayList<TourPhotoLink> allTourPhotoLinks) {

    if (allTourPhotoLinks.size() == 0) {
        return;
    }

    boolean isSubsequentHistory = false;
    boolean isHistory = false;

    for (final TourPhotoLink tourPhotoLink : allTourPhotoLinks) {

        if (isHistory && tourPhotoLink.isHistoryTour == isHistory) {

            // 2 subsequent tours contains the same tour type
            isSubsequentHistory = true;
            break;
        }

        isHistory = tourPhotoLink.isHistoryTour;
    }

    if (isSubsequentHistory == false) {
        // there is nothing to merge
        return;
    }

    final ArrayList<TourPhotoLink> mergedLinks = new ArrayList<TourPhotoLink>();
    TourPhotoLink prevHistoryTour = null;

    for (final TourPhotoLink tourPhotoLink : allTourPhotoLinks) {

        final boolean isHistoryTour = tourPhotoLink.isHistoryTour;

        if (isHistoryTour && prevHistoryTour == null) {

            // first history tour

            prevHistoryTour = tourPhotoLink;

            continue;
        }

        if (isHistoryTour && prevHistoryTour != null) {

            // this is a subsequent history tour, it is merged into previous history tour

            prevHistoryTour.linkPhotos.addAll(tourPhotoLink.linkPhotos);
            prevHistoryTour.numberOfGPSPhotos += tourPhotoLink.numberOfGPSPhotos;
            prevHistoryTour.numberOfNoGPSPhotos += tourPhotoLink.numberOfNoGPSPhotos;

            continue;
        }

        if (isHistoryTour == false && prevHistoryTour != null) {

            // this is a real tour, finalize previous history tour

            prevHistoryTour.setTourEndTime(Long.MAX_VALUE);
            mergedLinks.add(prevHistoryTour);
        }

        prevHistoryTour = null;

        // this is a real tour

        mergedLinks.add(tourPhotoLink);
    }

    if (prevHistoryTour != null) {

        // finalize previous history tour
        prevHistoryTour.setTourEndTime(Long.MAX_VALUE);
        mergedLinks.add(prevHistoryTour);
    }

    allTourPhotoLinks.clear();
    allTourPhotoLinks.addAll(mergedLinks);
}

From source file:axiom.framework.core.Application.java

protected String[][] setupRewriteRules() {
    String[][] rules = null;//from  w  ww  . j  a  v  a  2  s .c om
    for (Repository repository : this.getRepositories()) {
        BufferedReader br = null;
        boolean found_resource = false;
        try {
            //properties aren't ordered, must read lines
            Resource res = repository.getResource("rewrite.properties");
            if (res != null && res.exists()) {
                br = new BufferedReader(new InputStreamReader(res.getInputStream()));
                String line;
                ArrayList<String> lines = new ArrayList<String>();
                while ((line = br.readLine()) != null) {
                    lines.add(line);
                }

                final int size = lines.size();
                rules = new String[size][2];
                for (int i = 0; i < size; i++) {
                    String[] split = lines.get(i).toString().split("=");
                    if (split.length > 1) {
                        split[0] = split[0].trim();
                        split[1] = split[1].trim();
                        if (!split[0].equals("/") && !split[0].endsWith("/")) {
                            split[0] += "/";
                        }
                        if (!split[1].equals("/") && !split[1].endsWith("/")) {
                            split[1] += "/";
                        }
                        rules[i][0] = split[0].trim();
                        rules[i][1] = split[1].trim();
                    } else {
                        rules[i][0] = "";
                        rules[i][1] = "";
                    }
                }

                lines.clear();
                lines = null;

                found_resource = true;
            }
        } catch (Exception ignore) {
            logEvent("Error applying rewrite rules: " + ignore.getMessage());
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception ignoreagain) {
                    logEvent("Error applying rewrite rules: " + ignoreagain.getMessage());
                }
                br = null;
            }
        }

        if (found_resource) {
            break;
        }
    }

    if (rules == null) {
        rules = new String[0][0];
    }

    return rules;
}

From source file:frameworks.Masken.java

public boolean maskenInstall(JTable table, JTextArea jTLog, EDPSession session, String LinuxUser,
        String LinuxPass, String Host) {
    try {/*from   www .j a v  a 2s.c o  m*/
        ByteArrayOutputStream error = new ByteArrayOutputStream();
        StringBuilder fromServer = new StringBuilder();
        StringBuilder screenls = new StringBuilder();
        StringBuilder screenlsstring = new StringBuilder();
        String maskennummer;
        String fromServerString = "";
        String[] fromServerArray;
        String resourcesServerString = "";
        String[] resourcesServerArray;
        ArrayList datei = new ArrayList();
        String prio = "";
        String xmlziel = "";
        String resourcesziel = "";
        int masksearch = 0;
        int sshexitstatus = 0;
        File file = null;
        int zielzeile = 0;
        String tabellekopf = "";
        boolean failure = false;
        int firstcell = 0;
        int lastcell = 0;
        boolean newpanenotebook = true;

        //SSh Verbindung zum Mandanten ffnen
        SshClient sshclient = new SshClient();
        sshclient.connect(LinuxUser, LinuxPass, Host, 22);

        jTLog.append("----------Maskenimport----------\n");

        jTLog.append("Vorhandene Masken aus Mandanten ermittelt\n");
        jTLog.paint(jTLog.getGraphics());
        //Dateinamen erzeugen
        for (int i = 0; i < table.getRowCount(); i++) {
            //Fehlerflag auf true setzen
            failure = true;
            xmlziel = table.getValueAt(i, 2).toString().replace(table.getValueAt(i, 0).toString(),
                    table.getValueAt(i, 1).toString());
            xmlziel = table.getValueAt(i, 2).toString();
            resourcesziel = table.getValueAt(i, 3).toString().replace(table.getValueAt(i, 0).toString(),
                    table.getValueAt(i, 1).toString());
            maskennummer = table.getValueAt(i, 1).toString();

            // hab ich eine ZD und ein Masken TGZ?      
            if (table.getValueAt(i, 3).equals("TGZ")) {
                xmlziel = table.getValueAt(i, 2).toString();
                prio = xmlziel.substring(xmlziel.indexOf(".") + 1, xmlziel.lastIndexOf("."));
                prio = prio.substring(prio.indexOf(".") + 1, prio.length());
                // Dann kann ich die Maske einfach auf den Serverv kopieren
                file = new File(GlobalVars.dir + "\\Masken\\" + table.getValueAt(i, 2).toString());
                sshexitstatus = sshclient.sendfile(file.toString(), xmlziel);
                jTLog.append("Neue Maske " + maskennummer + prio + " wird hochgeladen\n");
                jTLog.paint(jTLog.getGraphics());
                // und importieren
                jTLog.append("Neue Maske " + maskennummer + prio + " wird im Mandanten importiert\n");
                jTLog.paint(jTLog.getGraphics());
                sshexitstatus = sshclient.sendcommand(
                        "eval `sh denv.sh`;screen_import.sh -n " + maskennummer + " -p " + prio + " " + xmlziel,
                        error, fromServer);

                // und generieren
                jTLog.append("Neue Maske " + maskennummer + prio + " wird generiert\n");
                jTLog.paint(jTLog.getGraphics());
                // Maske generieren
                maskegenerieren(session, maskennummer, prio);

                jTLog.append(error.toString());
                jTLog.paint(jTLog.getGraphics());
                // Kein ZD und kein TGZ File, also muss die Maske integriert werden
            } else {
                datei.clear();
                prio = xmlziel.substring(xmlziel.indexOf(".") + 1, xmlziel.lastIndexOf("."));
                prio = prio.substring(prio.indexOf(".") + 1, prio.indexOf("-"));
                //Tabelle oder Kopf
                tabellekopf = xmlziel.substring(xmlziel.lastIndexOf(".") - 1, xmlziel.lastIndexOf("."));
                if (tabellekopf.equals("Z")) {
                    tabellekopf = "_T.xml";
                } else {
                    tabellekopf = "_M.xml";
                }
                // Maske individualisieren falls notwendig
                individuelleMaskePruefen(session, maskennummer, prio, jTLog);
                // Maske sichern mit screen_export
                jTLog.append(
                        "Maske " + maskennummer + " " + prio + " wird gesichert und kann mit screen_import -n "
                                + maskennummer + " -p " + prio + " screen_restauriert werden\n");
                jTLog.paint(jTLog.getGraphics());
                sshexitstatus = sshclient.sendcommand(
                        "eval `sh denv.sh`;screen_export.sh -n " + maskennummer + " -p " + prio, error,
                        fromServer);

                //Tmp leeren
                File tmpdir = new File("tmp");
                if (tmpdir.exists() && (tmpdir.isDirectory())) {
                    deleteDir(tmpdir);

                }
                new File("tmp").mkdir();

                // Masken.tgz abholen
                jTLog.append("Maske " + maskennummer + " " + prio + " wird geladen\n");
                jTLog.paint(jTLog.getGraphics());
                String servermaske = "./screen." + maskennummer + "." + prio + ".tgz";

                sshexitstatus = sshclient.getfile(servermaske, "tmp\\");
                if (sshexitstatus == -1) {// Maske wurde erfolgreich geladen

                    //Maske einlesen
                    jTLog.append("Maske " + maskennummer + " " + prio + " wird ausgepackt\n");
                    jTLog.paint(jTLog.getGraphics());
                    //tgz lokal auspacken
                    uncompressTarGZ(new File("tmp\\screen." + maskennummer + "." + prio + ".tgz"),
                            new File("tmp\\"));
                    // tgz lschen
                    new File("tmp\\screen." + maskennummer + "." + prio + ".tgz").delete();
                    String changemaske = "screen_" + maskennummer + "_" + prio + tabellekopf;
                    //Maske einlesen
                    jTLog.append("Maske " + maskennummer + " " + prio + " wird erweitert\n");
                    jTLog.paint(jTLog.getGraphics());
                    BufferedReader inservermaske = null;
                    inservermaske = new BufferedReader(new FileReader("tmp\\screens\\screen_" + maskennummer
                            + "\\" + prio + "\\screen_" + maskennummer + "_" + prio + tabellekopf));
                    BufferedReader inmaske = null;
                    inmaske = new BufferedReader(
                            new FileReader(GlobalVars.dir + "\\Masken\\" + table.getValueAt(i, 2).toString()));
                    PrintWriter outservermaske = new PrintWriter(
                            new BufferedWriter(new FileWriter("tmp\\screens\\screen_" + maskennummer + "\\"
                                    + prio + "\\Xscreen_" + maskennummer + "_" + prio + tabellekopf)));
                    int lineNr = 0;
                    zielzeile = 0;
                    firstcell = 0;
                    lastcell = 0;
                    String line = inservermaske.readLine();
                    while (line != null) {

                        datei.add(line);
                        if (line.contains("<cell") && (firstcell == 0))
                            firstcell = lineNr;
                        if (line.contains("</layout.grid") && (firstcell != 0))
                            lastcell = lineNr;
                        if (line.contains("</pane.notebook>")) {
                            zielzeile = lineNr;
                            newpanenotebook = false;
                        }
                        lineNr++;

                        line = inservermaske.readLine();
                    }
                    inservermaske.close();

                    // Raus schreiben der Datei
                    for (int izeilen = 0; izeilen < lineNr - 1; izeilen++) {
                        outservermaske.print(datei.get(izeilen) + "\n");
                        if ((zielzeile == 0) && (firstcell == izeilen)) {
                            outservermaske.print(
                                    "<layout.notebook>\n<pane.notebook msgId=\"b825893d-f00a-4500-ae24-c2fe5364ae0e\">\n <layout.grid>\n"
                                            + "      <cell gridX=\"0\" gridY=\"0\">\n");
                        }
                        if ((zielzeile == 0) && (lastcell == izeilen)) {
                            outservermaske.print("</pane.notebook>\n");
                            zielzeile = izeilen;
                        }

                        if ((izeilen == zielzeile) && (zielzeile != 0)) {
                            line = inmaske.readLine();
                            while (line != null) {
                                // Zuerst die Datei einlesen und jede Zeile raus schreiben
                                outservermaske.print(line + "\n");
                                line = inmaske.readLine();

                            }
                            if (newpanenotebook)
                                outservermaske.print("</layout.notebook>\n </cell>\n </layout.grid> \n");
                        }
                    }
                    outservermaske.close();

                    // Neue Datei nun auf die alte kopieren
                    Files.move(
                            Paths.get("tmp\\screens\\screen_" + maskennummer + "\\" + prio + "\\Xscreen_"
                                    + maskennummer + "_" + prio + tabellekopf),
                            Paths.get("tmp\\screens\\screen_" + maskennummer + "\\" + prio + "\\screen_"
                                    + maskennummer + "_" + prio + tabellekopf),
                            REPLACE_EXISTING);

                    // Resources Dateien anschauen
                    // Append an die Resources.language
                    // sshexitstatus = sshclient.sendcommand("cat  ./screens/screen_" + maskennummer + "/" + prio + "/ResourcesFW  >> ./screens/screen_" + maskennummer + "/" + prio + "/Resources.language", error, fromServer);
                    String resourcesstring = FileUtils.readFileToString(
                            new File(GlobalVars.dir + "\\Masken\\" + table.getValueAt(i, 3).toString()),
                            "utf-8");
                    FileWriter fw = new FileWriter(new File(
                            "tmp\\screens\\screen_" + maskennummer + "\\" + prio + "\\Resources.language"),
                            true);
                    fw.write(resourcesstring);//appends the string to the file
                    if (newpanenotebook)
                        fw.write("b825893d-f00a-4500-ae24-c2fe5364ae0e=Allgemein\n");
                    fw.close();
                    //An Resources_*  den String auch noch anhngen
                    // dazu erst mal die Dateien suchen
                    Path dir = Paths.get("tmp\\screens\\screen_" + maskennummer + "\\" + prio);
                    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "Resources_*")) {
                        for (Path resourcesfile : stream) {
                            fw = new FileWriter(resourcesfile.toFile(), true);
                            fw.write(resourcesstring);//appends the string to the file
                            if (newpanenotebook)
                                fw.write("b825893d-f00a-4500-ae24-c2fe5364ae0e=Allgemein\n");
                            fw.close();
                        }
                    }

                    // Tgz wieder erzeugen
                    createTarGzip("tmp\\", "tmp\\screen." + maskennummer + "." + prio + ".tgz");
                    // Maske wieder auf Server schieben

                    sshexitstatus = sshclient.sendfile("\\tmp\\screen." + maskennummer + "." + prio + ".tgz",
                            "screen." + maskennummer + "." + prio + ".tgz");
                    jTLog.append("Neue Maske " + maskennummer + prio + " wird hochgeladen\n");
                    jTLog.paint(jTLog.getGraphics());
                    // und importieren
                    jTLog.append("Neue Maske " + maskennummer + prio + " wird im Mandanten importiert\n");
                    jTLog.paint(jTLog.getGraphics());
                    sshexitstatus = sshclient
                            .sendcommand(
                                    "eval `sh denv.sh`;screen_import.sh -n " + maskennummer + " -p " + prio
                                            + " " + "screen." + maskennummer + "." + prio + ".tgz",
                                    error, fromServer);

                    // und generieren
                    jTLog.append("Neue Maske " + maskennummer + prio + " wird generiert\n");
                    jTLog.paint(jTLog.getGraphics());
                    // Maske generieren
                    maskegenerieren(session, maskennummer, prio);

                    jTLog.append(error.toString());
                    jTLog.paint(jTLog.getGraphics());

                }
                /*
                //Alter Weg
                // Maske abholen
                jTLog.append("Maske " + maskennummer + " " + prio + " wird geladen\n");
                jTLog.paint(jTLog.getGraphics());
                //String servermaske = "./screens/screen_" + maskennummer + "/" + prio + "/screen_" + maskennummer + "_" + prio + "_M.xml";
                servermaske = "./screens/screen_" + maskennummer + "/" + prio + "/screen_" + maskennummer + "_" + prio + tabellekopf;
                        
                sshexitstatus = sshclient.getfile(servermaske, "tmp\\" + maskennummer + prio + ".xml");
                if (sshexitstatus == -1) {// Maske wurde erfolgreich geladen
                //Maske einlesen
                jTLog.append("Maske " + maskennummer + " " + prio + " wird erweitert\n");
                jTLog.paint(jTLog.getGraphics());
                        
                BufferedReader inservermaske = null;
                inservermaske = new BufferedReader(new FileReader("tmp\\" + maskennummer + prio + ".xml"));
                BufferedReader inmaske = null;
                inmaske = new BufferedReader(new FileReader(GlobalVars.dir + "\\Masken\\" + table.getValueAt(i, 2).toString()));
                PrintWriter outservermaske = new PrintWriter(new BufferedWriter(new FileWriter("tmp\\" + maskennummer + prio + "X.xml")));
                int lineNr = 0;
                String line = inservermaske.readLine();
                while (line != null) {
                        
                    datei.add(line);
                        
                    if (line.contains("</pane.notebook>")) {
                        zielzeile = lineNr;
                    }
                    lineNr++;
                        
                    line = inservermaske.readLine();
                }
                inservermaske.close();
                        
                // Raus schreiben der Datei
                for (int izeilen = 0; izeilen < lineNr - 1; izeilen++) {
                    outservermaske.print(datei.get(izeilen) + "\n");
                    if (izeilen == zielzeile) {
                        line = inmaske.readLine();
                        while (line != null) {
                            // Zuerst die Datei einlesen und jede Zeile raus schreiben
                            outservermaske.print(line + "\n");
                            line = inmaske.readLine();
                        
                        }
                    }
                }
                outservermaske.close();
                        
                //Datei zurck zum server schicken
                jTLog.append("Maske " + maskennummer + " " + prio + " wird gesendet\n");
                jTLog.paint(jTLog.getGraphics());
                sshexitstatus = sshclient.sendfile("tmp\\" + maskennummer + prio + "X.xml", servermaske);
                if (sshexitstatus == -1) {// Maske wurde auf Server bertragen
                    // Jetzt noch die Resources Datei an die Resources auf dem server anhngen
                    sshexitstatus = sshclient.sendfile(GlobalVars.dir + "\\Masken\\" + table.getValueAt(i, 3).toString(), "./screens/screen_" + maskennummer + "/" + prio + "/ResourcesFW");
                    if (sshexitstatus == -1) {
                        sshexitstatus = sshclient.sendcommand("cat  ./screens/screen_" + maskennummer + "/" + prio + "/ResourcesFW  >> ./screens/screen_" + maskennummer + "/" + prio + "/Resources.language", error, fromServer);
                        //Resources wurde auf Server bertragen  
                         jTLog.append(error.toString());
                        jTLog.paint(jTLog.getGraphics());
                        if (sshexitstatus==0)
                        {
                           sshexitstatus = sshclient.sendcommand("ls screens/screen_" + maskennummer + "/" + prio + "/Resources_* -l", error, fromServer);
                           jTLog.append(error.toString());
                            jTLog.paint(jTLog.getGraphics());
                        if (sshexitstatus == 0) {
                                    
                            resourcesServerString = fromServer.toString();
                            resourcesServerArray = resourcesServerString.split("\n");
                            ///Bisher alles gut gelaufen, deswegen failure auf false setzen
                            failure = false;
                            for (int iresources = 0; iresources < resourcesServerArray.length; iresources++) {
                                if (resourcesServerArray[iresources].toString().contains("Resources")) {
                                    sshexitstatus = sshclient.sendcommand("cat  ./screens/screen_" + maskennummer + "/" + prio + "/ResourcesFW  >>" + resourcesServerArray[iresources].toString().substring(resourcesServerArray[iresources].lastIndexOf(" "), resourcesServerArray[iresources].length()), error, fromServer);
                                    if (sshexitstatus != 0) {
                                        failure = true;
                                    }
                                    jTLog.append(error.toString());
                                    jTLog.paint(jTLog.getGraphics());
                                }
                        
                            }
                        
                            jTLog.append("Maske " + maskennummer + " " + prio + " wird generiert\n");
                        
                            // Maske nun noch generieren
                            maskegenerieren(session, maskennummer, prio); 
                        }
                               
                                
                        }
                        
                    }
                        
                }
                        
                }*/
                if (failure) {

                    //Alte Maske wieder importieren 
                    //  jTLog.append(error.toString());
                    //  jTLog.append("Gesicherte Maske wird wieder installiert !!! manuelle Nacharbeit notwendig!!!\n");
                    //  jTLog.paint(jTLog.getGraphics());
                    //  sshexitstatus = sshclient.sendcommand("eval `sh denv.sh`;screen_import.sh -n " + maskennummer + " -p " + prio+" screen."+maskennummer+"."+prio+".tgz", error, fromServer);
                }

            }

        }

    } catch (JSchException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SftpException ex) {
        Logger.getLogger(Masken.class.getName()).log(Level.SEVERE, null, ex);
    }
    return true;
}

From source file:org.apache.manifoldcf.crawler.connectors.sharepoint.SPSProxyHelper.java

/**
*
* @param site/*from   ww w. ja  va 2  s.c o m*/
* @param docLibrary
* @return an XML document
* @throws ManifoldCFException
* @throws ServiceInterruption
*/
public boolean getChildren(IFileStream fileStream, String site, String guid, boolean dspStsWorks)
        throws ManifoldCFException, ServiceInterruption {
    long currentTime;
    try {
        if (site.equals("/"))
            site = ""; // root case
        if (dspStsWorks) {
            StsAdapterWS listService = new StsAdapterWS(baseUrl + site, userName, password, configuration,
                    httpClient);
            StsAdapterSoapStub stub = (StsAdapterSoapStub) listService.getStsAdapterSoapHandler();

            String[] vArray = new String[1];
            vArray[0] = "1.0";
            VersionsHeader myVersion = new VersionsHeader();
            myVersion.setVersion(vArray);

            stub.setHeader("http://schemas.microsoft.com/sharepoint/dsp", "versions", myVersion);

            RequestHeader reqHeader = new RequestHeader();
            reqHeader.setDocument(DocumentType.content);
            reqHeader.setMethod(MethodType.query);

            stub.setHeader("http://schemas.microsoft.com/sharepoint/dsp", "request", reqHeader);

            QueryRequest myRequest = new QueryRequest();

            DSQuery sQuery = new DSQuery();
            sQuery.setSelect("/list[@id='" + guid + "']");
            myRequest.setDsQuery(sQuery);

            StsAdapterSoap call = stub;
            ArrayList nodeList = new ArrayList();

            QueryResponse resp = call.query(myRequest);
            org.apache.axis.message.MessageElement[] list = resp.get_any();
            if (Logging.connectors.isDebugEnabled()) {
                Logging.connectors.debug("SharePoint: list xml: '" + list[0].toString() + "'");
            }

            XMLDoc doc = new XMLDoc(list[0].toString());

            doc.processPath(nodeList, "*", null);
            if (nodeList.size() != 1) {
                throw new ManifoldCFException("Bad xml - missing outer 'ns1:dsQueryResponse' node - there are "
                        + Integer.toString(nodeList.size()) + " nodes");
            }

            Object parent = nodeList.get(0);
            //System.out.println( "Outer NodeName = " + doc.getNodeName(parent) );
            if (!doc.getNodeName(parent).equals("ns1:dsQueryResponse"))
                throw new ManifoldCFException("Bad xml - outer node is not 'ns1:dsQueryResponse'");

            nodeList.clear();
            doc.processPath(nodeList, "*", parent);

            if (nodeList.size() != 2) {
                throw new ManifoldCFException(" No results found.");
            }

            // Now, extract the files from the response document
            XMLDoc docs = doc;
            ArrayList nodeDocs = new ArrayList();

            docs.processPath(nodeDocs, "*", null);
            parent = nodeDocs.get(0); // ns1:dsQueryResponse
            nodeDocs.clear();
            docs.processPath(nodeDocs, "*", parent);
            Object documents = nodeDocs.get(1);
            nodeDocs.clear();
            docs.processPath(nodeDocs, "*", documents);

            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < nodeDocs.size(); j++) {
                Object node = nodeDocs.get(j);
                Logging.connectors.debug(node.toString());
                String relPath = docs.getData(docs.getElement(node, "FileRef"));
                fileStream.addFile(relPath, null);
            }
        } else {
            // New code

            MCPermissionsWS itemService = new MCPermissionsWS(baseUrl + site, userName, password, configuration,
                    httpClient);
            com.microsoft.sharepoint.webpartpages.PermissionsSoap itemCall = itemService
                    .getPermissionsSoapHandler();

            int startingIndex = 0;
            int amtToRequest = 10000;
            while (true) {
                com.microsoft.sharepoint.webpartpages.GetListItemsResponseGetListItemsResult itemsResult = itemCall
                        .getListItems(guid, Integer.toString(startingIndex), Integer.toString(amtToRequest));

                MessageElement[] itemsList = itemsResult.get_any();

                if (Logging.connectors.isDebugEnabled()) {
                    Logging.connectors
                            .debug("SharePoint: getListItems xml response: '" + itemsList[0].toString() + "'");
                }

                if (itemsList.length != 1)
                    throw new ManifoldCFException("Bad response - expecting one outer 'GetListItems' node, saw "
                            + Integer.toString(itemsList.length));

                MessageElement items = itemsList[0];
                if (!items.getElementName().getLocalName().equals("GetListItems"))
                    throw new ManifoldCFException(
                            "Bad response - outer node should have been 'GetListItems' node");

                int resultCount = 0;
                Iterator iter = items.getChildElements();
                while (iter.hasNext()) {
                    MessageElement child = (MessageElement) iter.next();
                    if (child.getElementName().getLocalName().equals("GetListItemsResponse")) {
                        Iterator resultIter = child.getChildElements();
                        while (resultIter.hasNext()) {
                            MessageElement result = (MessageElement) resultIter.next();
                            if (result.getElementName().getLocalName().equals("GetListItemsResult")) {
                                resultCount++;
                                String relPath = result.getAttribute("FileRef");
                                String displayURL = result.getAttribute("ListItemURL");
                                fileStream.addFile(relPath, displayURL);
                            }
                        }

                    }
                }

                if (resultCount < amtToRequest)
                    break;

                startingIndex += resultCount;
            }
        }

        return true;
    } catch (java.net.MalformedURLException e) {
        throw new ManifoldCFException("Bad SharePoint url: " + e.getMessage(), e);
    } catch (javax.xml.rpc.ServiceException e) {
        if (Logging.connectors.isDebugEnabled())
            Logging.connectors.debug("SharePoint: Got a service exception getting documents for site " + site
                    + " guid " + guid + " - retrying", e);
        currentTime = System.currentTimeMillis();
        throw new ServiceInterruption("Service exception: " + e.getMessage(), e, currentTime + 300000L,
                currentTime + 12 * 60 * 60000L, -1, true);
    } catch (org.apache.axis.AxisFault e) {
        currentTime = System.currentTimeMillis();
        if (e.getFaultCode().equals(new javax.xml.namespace.QName("http://xml.apache.org/axis/", "HTTP"))) {
            org.w3c.dom.Element elem = e.lookupFaultDetail(
                    new javax.xml.namespace.QName("http://xml.apache.org/axis/", "HttpErrorCode"));
            if (elem != null) {
                elem.normalize();
                String httpErrorCode = elem.getFirstChild().getNodeValue().trim();
                if (httpErrorCode.equals("404")) {
                    // Page did not exist
                    if (Logging.connectors.isDebugEnabled())
                        Logging.connectors.debug("SharePoint: The page at " + baseUrl + site
                                + " did not exist; assuming library deleted");
                    return false;
                } else if (httpErrorCode.equals("401")) {
                    // User did not have permissions for this library to get the acls
                    if (Logging.connectors.isDebugEnabled())
                        Logging.connectors
                                .debug("SharePoint: The crawl user did not have access to list documents for "
                                        + baseUrl + site + "; skipping documents within");
                    return false;
                } else if (httpErrorCode.equals("403"))
                    throw new ManifoldCFException(
                            "Http error " + httpErrorCode + " while reading from " + baseUrl + site
                                    + " - check IIS and SharePoint security settings! " + e.getMessage(),
                            e);
                else
                    throw new ManifoldCFException("Unexpected http error code " + httpErrorCode
                            + " accessing SharePoint at " + baseUrl + site + ": " + e.getMessage(), e);
            }
            throw new ManifoldCFException("Unknown http error occurred: " + e.getMessage(), e);
        } else if (e.getFaultCode()
                .equals(new javax.xml.namespace.QName("http://schemas.xmlsoap.org/soap/envelope/", "Server"))) {
            org.w3c.dom.Element elem = e.lookupFaultDetail(new javax.xml.namespace.QName(
                    "http://schemas.microsoft.com/sharepoint/soap/", "errorcode"));
            if (elem != null) {
                elem.normalize();
                String sharepointErrorCode = elem.getFirstChild().getNodeValue().trim();
                if (sharepointErrorCode.equals("0x82000006")) {
                    // List did not exist
                    if (Logging.connectors.isDebugEnabled())
                        Logging.connectors.debug("SharePoint: The list " + guid + " in site " + site
                                + " did not exist; assuming library deleted");
                    return false;
                } else {
                    if (Logging.connectors.isDebugEnabled()) {
                        org.w3c.dom.Element elem2 = e.lookupFaultDetail(new javax.xml.namespace.QName(
                                "http://schemas.microsoft.com/sharepoint/soap/", "errorstring"));
                        String errorString = "";
                        if (elem != null)
                            errorString = elem2.getFirstChild().getNodeValue().trim();

                        Logging.connectors.debug("SharePoint: Getting child documents for the list " + guid
                                + " in site " + site + " failed with unexpected SharePoint error code "
                                + sharepointErrorCode + ": " + errorString + " - Skipping", e);
                    }
                    return false;
                }
            }
            if (Logging.connectors.isDebugEnabled())
                Logging.connectors
                        .debug("SharePoint: Unknown SharePoint server error getting child documents for site "
                                + site + " guid " + guid + " - axis fault = " + e.getFaultCode().getLocalPart()
                                + ", detail = " + e.getFaultString() + " - retrying", e);

            throw new ServiceInterruption("Unknown SharePoint server error: " + e.getMessage() + " - retrying",
                    e, currentTime + 300000L, currentTime + 3 * 60 * 60000L, -1, false);
        }

        if (e.getFaultCode().equals(new javax.xml.namespace.QName("http://schemas.xmlsoap.org/soap/envelope/",
                "Server.userException"))) {
            String exceptionName = e.getFaultString();
            if (exceptionName.equals("java.lang.InterruptedException"))
                throw new ManifoldCFException("Interrupted", ManifoldCFException.INTERRUPTED);
        }

        if (Logging.connectors.isDebugEnabled())
            Logging.connectors
                    .debug("SharePoint: Got an unknown remote exception getting child documents for site "
                            + site + " guid " + guid + " - axis fault = " + e.getFaultCode().getLocalPart()
                            + ", detail = " + e.getFaultString() + " - retrying", e);
        throw new ServiceInterruption("Remote procedure exception: " + e.getMessage(), e, currentTime + 300000L,
                currentTime + 3 * 60 * 60000L, -1, false);
    } catch (java.rmi.RemoteException e) {
        // We expect the axis exception to be thrown, not this generic one!
        // So, fail hard if we see it.
        if (Logging.connectors.isDebugEnabled())
            Logging.connectors
                    .debug("SharePoint: Got an unexpected remote exception getting child documents for site "
                            + site + " guid " + guid, e);
        throw new ManifoldCFException("Unexpected remote procedure exception: " + e.getMessage(), e);
    }
}

From source file:com.krawler.spring.crm.emailMarketing.crmEmailMarketingController.java

public ModelAndView targetListForImport(HttpServletRequest request, HttpServletResponse response)
        throws ServletException {
    JSONObject jobj = new JSONObject();
    JSONArray jarr = new JSONArray();
    JSONObject jobjTemp = new JSONObject();
    JSONArray jarrColumns = new JSONArray();
    JSONArray jarrRecords = new JSONArray();
    JSONObject jMeta = new JSONObject();
    try {/*  ww w. j ava  2 s  .c  o m*/
        HashMap<String, Object> requestParams = new HashMap<String, Object>();
        HashMap<String, Object> innerrequestParams = new HashMap<String, Object>();
        String companyid = sessionHandlerImpl.getCompanyid(request);
        ArrayList filter_names = new ArrayList();
        ArrayList filter_params = new ArrayList();
        filter_names.add("deleted");
        filter_params.add(0);
        filter_names.add("creator.company.companyID");
        filter_params.add(companyid);
        filter_names.add("saveflag");
        filter_params.add(1);
        filter_names.add("!id");
        filter_params.add(request.getParameter("tlid"));
        if (request.getParameter("ss") != null && !StringUtil.isNullOrEmpty(request.getParameter("ss"))) {
            requestParams.put("ss", request.getParameter("ss"));
        }
        requestParams.put("start", StringUtil.checkForNull(request.getParameter("start")));
        requestParams.put("limit", StringUtil.checkForNull(request.getParameter("limit")));
        requestParams.put("filter_names", filter_names);
        requestParams.put("filter_params", filter_params);

        KwlReturnObject kmsg = crmEmailMarketingDAOObj.getTargetList(requestParams);
        Iterator ite = kmsg.getEntityList().iterator();
        while (ite.hasNext()) {
            TargetList obj = (TargetList) ite.next();
            JSONObject jtemp = new JSONObject();
            jtemp.put("relatedid", obj.getId());
            jtemp.put("name", obj.getName());
            jtemp.put("targetlistDescription", obj.getDescription());
            innerrequestParams.clear();
            innerrequestParams.put("listID", obj.getId());
            filter_names.clear();
            filter_params.clear();

            filter_names.add("targetlisttargets.targetlistid.id");
            filter_params.add(obj.getId());
            innerrequestParams.put("filter_names", filter_names);
            innerrequestParams.put("filter_params", filter_params);
            innerrequestParams.put("companyid", companyid);
            KwlReturnObject innerkmsg = crmEmailMarketingDAOObj.getTargetListTargets(innerrequestParams);
            jtemp.put("targetscount", innerkmsg.getRecordTotalCount());
            jarr.put(jtemp);
        }

        jobjTemp = new JSONObject();
        jobjTemp.put("header", mSource.getMessage("crm.targetlists.targetlistname", null,
                RequestContextUtils.getLocale(request)));//"Target List Name");
        jobjTemp.put("tip", mSource.getMessage("crm.targetlists.targetlistname", null,
                RequestContextUtils.getLocale(request)));//"Target List Name");
        jobjTemp.put("pdfwidth", 60);
        jobjTemp.put("dataIndex", "name");
        jarrColumns.put(jobjTemp);

        jobjTemp = new JSONObject();
        jobjTemp.put("header", mSource.getMessage("crm.targetlists.targetlistname", null,
                RequestContextUtils.getLocale(request)));//"No of Targets");
        jobjTemp.put("tip", mSource.getMessage("crm.targetlists.targetlistname", null,
                RequestContextUtils.getLocale(request)));//"No of Targets");
        jobjTemp.put("pdfwidth", 60);
        jobjTemp.put("dataIndex", "targetscount");
        jarrColumns.put(jobjTemp);

        jobjTemp = new JSONObject();
        jobjTemp.put("header", mSource.getMessage("crm.case.defaultheader.desc", null,
                RequestContextUtils.getLocale(request)));//"Description");
        jobjTemp.put("tip", mSource.getMessage("crm.case.defaultheader.desc", null,
                RequestContextUtils.getLocale(request)));//"Description");
        jobjTemp.put("pdfwidth", 60);
        jobjTemp.put("dataIndex", "targetlistDescription");
        jarrColumns.put(jobjTemp);

        jobjTemp = new JSONObject();
        jobjTemp.put("name", "targetscount");
        jarrRecords.put(jobjTemp);
        jobjTemp = new JSONObject();
        jobjTemp.put("name", "name");
        jarrRecords.put(jobjTemp);
        jobjTemp = new JSONObject();
        jobjTemp.put("name", "emailid");
        jarrRecords.put(jobjTemp);
        jobjTemp = new JSONObject();
        jobjTemp.put("name", "relatedto");
        jarrRecords.put(jobjTemp);
        jobjTemp = new JSONObject();
        jobjTemp.put("name", "company");
        jarrRecords.put(jobjTemp);
        jobjTemp = new JSONObject();
        jobjTemp.put("name", "targetlistDescription");
        jarrRecords.put(jobjTemp);
        jobjTemp = new JSONObject();
        jobjTemp.put("name", "relatedid");
        jarrRecords.put(jobjTemp);
        jobjTemp = new JSONObject();
        jobjTemp.put("name", "fname");
        jarrRecords.put(jobjTemp);

        jobj.put("columns", jarrColumns);
        jMeta.put("totalProperty", "totalCount");
        jMeta.put("root", "data");
        jMeta.put("fields", jarrRecords);
        jobj.put("metaData", jMeta);
        jobj.put("totalCount", kmsg.getRecordTotalCount());
        jobj.put("data", jarr);
    } catch (ServiceException e) {
        logger.warn(e.getMessage(), e);
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
    }
    return new ModelAndView("jsonView", "model", jobj.toString());
}

From source file:org.apache.catalina.realm.RealmBase.java

/**
 * Return the SecurityConstraints configured to guard the request URI for
 * this request, or <code>null</code> if there is no such constraint.
 *
 * @param request Request we are processing
 * @param context Context the Request is mapped to
 *///from  w ww. java  2s  . c  o m
public SecurityConstraint[] findSecurityConstraints(HttpRequest request, Context context) {

    ArrayList results = null;
    // Are there any defined security constraints?
    SecurityConstraint constraints[] = context.findConstraints();
    if ((constraints == null) || (constraints.length == 0)) {
        if (log.isDebugEnabled())
            log.debug("  No applicable constraints defined");
        return (null);
    }

    // Check each defined security constraint
    HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
    String uri = request.getRequestPathMB().toString();

    String method = hreq.getMethod();
    int i;
    boolean found = false;
    for (i = 0; i < constraints.length; i++) {
        SecurityCollection[] collection = constraints[i].findCollections();

        if (log.isDebugEnabled())
            log.debug("  Checking constraint '" + constraints[i] + "' against " + method + " " + uri + " --> "
                    + constraints[i].included(uri, method));
        for (int j = 0; j < collection.length; j++) {
            String[] patterns = collection[j].findPatterns();
            for (int k = 0; k < patterns.length; k++) {
                if (uri.equals(patterns[k])) {
                    found = true;
                    if (collection[j].findMethod(method)) {
                        if (results == null) {
                            results = new ArrayList();
                        }
                        results.add(constraints[i]);
                    }
                }
            }
        }
    }

    if (found) {
        return resultsToArray(results);
    }

    int longest = -1;

    for (i = 0; i < constraints.length; i++) {
        SecurityCollection[] collection = constraints[i].findCollections();

        if (log.isDebugEnabled())
            log.debug("  Checking constraint '" + constraints[i] + "' against " + method + " " + uri + " --> "
                    + constraints[i].included(uri, method));
        for (int j = 0; j < collection.length; j++) {
            String[] patterns = collection[j].findPatterns();
            boolean matched = false;
            int length = -1;
            for (int k = 0; k < patterns.length; k++) {
                String pattern = patterns[k];
                if (pattern.startsWith("/") && pattern.endsWith("/*") && pattern.length() >= longest) {

                    if (pattern.length() == 2) {
                        matched = true;
                        length = pattern.length();
                    } else if (pattern.regionMatches(0, uri, 0, pattern.length() - 2)) {
                        matched = true;
                        length = pattern.length();
                    }
                }
            }
            if (matched) {
                found = true;
                if (length > longest) {
                    if (results != null) {
                        results.clear();
                    }
                    longest = length;
                }
                if (collection[j].findMethod(method)) {
                    if (results == null) {
                        results = new ArrayList();
                    }
                    results.add(constraints[i]);
                }
            }
        }
    }

    if (found) {
        return resultsToArray(results);
    }

    for (i = 0; i < constraints.length; i++) {
        SecurityCollection[] collection = constraints[i].findCollections();

        if (log.isDebugEnabled())
            log.debug("  Checking constraint '" + constraints[i] + "' against " + method + " " + uri + " --> "
                    + constraints[i].included(uri, method));
        boolean matched = false;
        int pos = -1;
        for (int j = 0; j < collection.length; j++) {
            String[] patterns = collection[j].findPatterns();
            for (int k = 0; k < patterns.length && !matched; k++) {
                String pattern = patterns[k];
                if (pattern.startsWith("*.")) {
                    int slash = uri.lastIndexOf("/");
                    int dot = uri.lastIndexOf(".");
                    if (slash >= 0 && dot > slash && dot != uri.length() - 1
                            && uri.length() - dot == pattern.length() - 1) {
                        if (pattern.regionMatches(1, uri, dot, uri.length() - dot)) {
                            matched = true;
                            pos = j;
                        }
                    }
                }
            }
        }
        if (matched) {
            found = true;
            if (collection[pos].findMethod(method)) {
                if (results == null) {
                    results = new ArrayList();
                }
                results.add(constraints[i]);
            }
        }
    }

    if (found) {
        return resultsToArray(results);
    }

    for (i = 0; i < constraints.length; i++) {
        SecurityCollection[] collection = constraints[i].findCollections();

        if (log.isDebugEnabled())
            log.debug("  Checking constraint '" + constraints[i] + "' against " + method + " " + uri + " --> "
                    + constraints[i].included(uri, method));
        for (int j = 0; j < collection.length; j++) {
            String[] patterns = collection[j].findPatterns();
            boolean matched = false;
            for (int k = 0; k < patterns.length && !matched; k++) {
                String pattern = patterns[k];
                if (pattern.equals("/")) {
                    matched = true;
                }
            }
            if (matched) {
                if (results == null) {
                    results = new ArrayList();
                }
                results.add(constraints[i]);
            }
        }
    }

    if (results == null) {
        // No applicable security constraint was found
        if (log.isDebugEnabled())
            log.debug("  No applicable constraint located");
    }
    return resultsToArray(results);
}

From source file:com.ferdi2005.secondgram.NotificationsController.java

public void processReadMessages(final SparseArray<Long> inbox, final long dialog_id, final int max_date,
        final int max_id, final boolean isPopup) {
    final ArrayList<MessageObject> popupArray = popupMessages.isEmpty() ? null : new ArrayList<>(popupMessages);
    notificationsQueue.postRunnable(new Runnable() {
        @Override//from ww w . j a  v  a2  s  .c om
        public void run() {
            int oldCount = popupArray != null ? popupArray.size() : 0;
            if (inbox != null) {
                for (int b = 0; b < inbox.size(); b++) {
                    int key = inbox.keyAt(b);
                    long messageId = inbox.get(key);
                    for (int a = 0; a < pushMessages.size(); a++) {
                        MessageObject messageObject = pushMessages.get(a);
                        if (messageObject.getDialogId() == key && messageObject.getId() <= (int) messageId) {
                            if (isPersonalMessage(messageObject)) {
                                personal_count--;
                            }
                            if (popupArray != null) {
                                popupArray.remove(messageObject);
                            }
                            long mid = messageObject.messageOwner.id;
                            if (messageObject.messageOwner.to_id.channel_id != 0) {
                                mid |= ((long) messageObject.messageOwner.to_id.channel_id) << 32;
                            }
                            pushMessagesDict.remove(mid);
                            delayedPushMessages.remove(messageObject);
                            pushMessages.remove(a);
                            a--;
                        }
                    }
                }
                if (popupArray != null && pushMessages.isEmpty() && !popupArray.isEmpty()) {
                    popupArray.clear();
                }
            }
            if (dialog_id != 0 && (max_id != 0 || max_date != 0)) {
                for (int a = 0; a < pushMessages.size(); a++) {
                    MessageObject messageObject = pushMessages.get(a);
                    if (messageObject.getDialogId() == dialog_id) {
                        boolean remove = false;
                        if (max_date != 0) {
                            if (messageObject.messageOwner.date <= max_date) {
                                remove = true;
                            }
                        } else {
                            if (!isPopup) {
                                if (messageObject.getId() <= max_id || max_id < 0) {
                                    remove = true;
                                }
                            } else {
                                if (messageObject.getId() == max_id || max_id < 0) {
                                    remove = true;
                                }
                            }
                        }
                        if (remove) {
                            if (isPersonalMessage(messageObject)) {
                                personal_count--;
                            }
                            pushMessages.remove(a);
                            delayedPushMessages.remove(messageObject);
                            if (popupArray != null) {
                                popupArray.remove(messageObject);
                            }
                            long mid = messageObject.messageOwner.id;
                            if (messageObject.messageOwner.to_id.channel_id != 0) {
                                mid |= ((long) messageObject.messageOwner.to_id.channel_id) << 32;
                            }
                            pushMessagesDict.remove(mid);
                            a--;
                        }
                    }
                }
                if (popupArray != null && pushMessages.isEmpty() && !popupArray.isEmpty()) {
                    popupArray.clear();
                }
            }
            if (popupArray != null && oldCount != popupArray.size()) {
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        popupMessages = popupArray;
                        NotificationCenter.getInstance()
                                .postNotificationName(NotificationCenter.pushMessagesUpdated);
                    }
                });
            }
        }
    });
}

From source file:cs.man.ac.uk.stats.ComputeANOVAStats.java

/**
 * Processes a file containing test results. Outputs a file containing the averages of the data read in.
 * @param path the file to write the averages to.
 * @param spacer the number of lines after which a newline should be inserted in the output file (for formatting only).
 *///www .ja v a 2s . c  o m
private static void process(String path, int spacer) {
    // Path to write out summary statistics to.
    String outputPath = path.replace(".csv", ".Statistics.csv");
    Common.fileDelete(outputPath);// Simply clean up any previous files.

    //Firstly try to read the file
    File file = new File(path);

    //if the file exists
    if (file.exists()) {
        /*
         * FILE FORMAT:
         * 
         * We expect the first line to contain labels followed by 1 or more lines of data.
         * 
         * There will then be a two line break (no data in lines), signaling the start of a new data set. For example:
         * 
         * TEST_<+>_<->_<balance>_<labelling>,...,<Label n>
         * TEST_10_20_1.0_0.5,1.0,...,100
         * TEST_10_20_1.0_0.5,2.0,...,200
         * ...
         * TEST_10_20_1.0_0.5,5.0,...,500
         * ,,
         * ,,
         * TEST_50_10_0.5_0.1,1.0,...,100
         * TEST_50_10_0.5_0.1,2.0,...,200
         * ...
         * TEST_50_10_0.5_0.1,5.0,...,500
         */

        // Variables used to store the line of the being read
        // using the input stream, and an array list to store the input
        // patterns into.
        String line = "";

        // Read the file and display it line by line. 
        BufferedReader in = null;

        try {
            //open stream to file
            in = new BufferedReader(new FileReader(file));

            // Read in first line separately.
            line = in.readLine();

            Writer.append(outputPath, line + "\n");

            // Obtain column headers.
            header = line;

            try {
                // Stores the data found in each block
                ArrayList<ArrayList<String>> content = new ArrayList<ArrayList<String>>();
                boolean haveMeta = false; // Flag that when true says we have meta information.
                boolean printed = false; // Flag that when true says we have printed summary statistics.

                // Variables that describe the result in each test block.
                String positives = "";
                String negatives = "";
                String balance = "";
                String labelling = "";

                // Rows of data in a data set
                int rows = 0;
                int columns = 0;
                int writeCount = 0; // Counts number of writes to statistics file, used to improve file formatting.

                while ((line = in.readLine()) != null) {
                    if (line.startsWith(",")) // Empty line, no data.
                    {
                        // If we encounter an empty line, then we must have already processed some data
                        // given the input file structure. So we print the summary statisitics of this data
                        // before proceeding.

                        if (!printed) // If we haven't printed previous data collected.
                        {
                            String key = "TEST_" + positives + "_" + negatives + "_" + balance + "_"
                                    + labelling;
                            //System.out.println(key);

                            // Obtain the summary statisitics.
                            StatsDataSet dataSet = new StatsDataSet(content, rows, columns);

                            ANOVA anova = new ANOVA(dataSet.getRows(), dataSet.getColumns(), path);
                            for (int i = 0; i < dataSet.getRows(); i++)
                                anova.addRow(dataSet.getRowAsArray(i), i);

                            anova.centreData();

                            if (anovaObjects.containsKey(key))
                                anovaObjects.get(key).add(anova);
                            else {
                                Vector<ANOVA> value = new Vector<ANOVA>();
                                value.add(anova);
                                anovaObjects.put(key, value);
                            }

                            // Print out: Labelling, Balance, <Average 1>,...<Average n>
                            // Firstly get averages.
                            String[] averages = dataSet.avgColumns_CreateNewRow();

                            // Concatenate the averages in to a single string for writing out.
                            String avgString = averages[1];
                            for (int i = 2; i < averages.length; i++)
                                avgString = avgString + "," + averages[i];

                            Writer.append(outputPath,
                                    "Balance_" + balance + "_Labelling_" + labelling + "," + avgString + "\n");

                            printed = true;
                            writeCount++;

                            // Add new line to improve formatting in output file.
                            if (writeCount == spacer) {
                                Writer.append(outputPath, line + "\n");
                                writeCount = 0;
                            }
                        }

                        // Now Reset these variables.
                        haveMeta = false;
                        positives = "";
                        negatives = "";
                        balance = "";
                        labelling = "";
                        rows = 0;
                        columns = 0;
                        content.clear();
                        continue;
                    } else // Collect more data.
                    {
                        String[] components = line.split(",");
                        rows++;
                        columns = components.length;

                        // An array list will represent a row of data from the file.
                        ArrayList<String> row = new ArrayList<String>();

                        //For each comma separated String we retrieve from the file
                        for (String c : components) {
                            //add it to the row array list
                            row.add(c.replace("\"", ""));
                        }

                        //add the row to the over all contents array list
                        content.add(row);

                        // Finally,
                        if (haveMeta == false) {
                            String[] metaComponents = components[0].split("_");
                            // TEST_<+>_<->_<balance>_<labelling>
                            //   ^   ^   ^     ^            ^
                            //   |   |   |     |            |
                            //   0   1   2     3            4
                            positives = metaComponents[1];
                            negatives = metaComponents[2];
                            balance = metaComponents[3];
                            labelling = metaComponents[4];
                            printed = false;
                        }

                    }
                }

                if (!printed) // If we haven't printed previous data but have reached the end of the file.
                {
                    String key = "TEST_" + positives + "_" + negatives + "_" + balance + "_" + labelling;
                    //System.out.println(key);

                    // Obtain the summary statisitics.
                    StatsDataSet dataSet = new StatsDataSet(content, rows, columns);

                    ANOVA anova = new ANOVA(dataSet.getRows(), dataSet.getColumns(), path);
                    for (int i = 0; i < dataSet.getRows(); i++)
                        anova.addRow(dataSet.getRowAsArray(i), i);

                    anova.centreData();

                    if (anovaObjects.containsKey(key))
                        anovaObjects.get(key).add(anova);
                    else {
                        Vector<ANOVA> value = new Vector<ANOVA>();
                        value.add(anova);
                        anovaObjects.put(key, value);
                    }

                    // Print out: Labelling, Balance, <Average 1>,...<Average n>
                    // Firstly get averages.
                    String[] averages = dataSet.avgColumns_CreateNewRow();

                    // Concatenate the averages in to a single string for writing out.
                    String avgString = averages[1];
                    for (int i = 2; i < averages.length; i++)
                        avgString = avgString + "," + averages[i];

                    Writer.append(outputPath,
                            "Balance_" + balance + "_Labelling_" + labelling + "," + avgString + "\n");

                    printed = true;
                    writeCount++;

                    // Add new line to improve formatting in output file.
                    if (writeCount == spacer) {
                        Writer.append(outputPath, line + "\n");
                        writeCount = 0;
                    }
                }
            } catch (IOException e) {
                System.out.println("Exception reading data file: " + path);
                e.printStackTrace();
            } finally {
                in.close();
            }
        } catch (Exception e) {
            System.out.println("Exception reading data file: " + path);
            e.printStackTrace();
        }
    } else {
        System.out.println("Could not read data file: " + path);
    }
}

From source file:bot.UniversalBot.java

@Override
public void run() {

    ArrayList<String> choices = new ArrayList<String>();
    int betAbility = 0;

    System.out.println("[DEBUG] Nick: " + nick);

    JSONArray toSend = new JSONArray();
    JSONObject object;//w w w  . j a v a  2s.  com

    try {
        object = new JSONObject().put("action", "first_connection");
        toSend.put(object);
        object = new JSONObject().put("nick", nick);
        toSend.put(object);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

    sendData(toSend.toString());

    String wynik;
    JSONArray jsonMainArr;
    JSONObject childJSONObject;

    while (isPlaying && connection) {

        try {
            wynik = in.readLine();
            System.out.println("[DEBUG] Get: " + wynik);

            jsonMainArr = new JSONArray(wynik);

            switch (jsonMainArr.getJSONObject(0).getString("action")) {

            /**
            * Getting cards for the first time from Server. Receiving cards from server.
            */
            case "cards":
                hand.clear();

                for (int i = 1; i < jsonMainArr.length(); i++) {
                    childJSONObject = jsonMainArr.getJSONObject(i);
                    final String rank = childJSONObject.getString("rank");
                    final String suit = childJSONObject.getString("suit");
                    hand.add(new Card(suit, rank));
                }

                betAbility = betAbility(hand) - (pot) / 2;

                System.out.println("[DEBUG] Rka bota " + hand);

                System.out.println("[DEBUG] OTO " + betAbility);

                break;

            /**
             * Overwriting:
             * bet
             * maxBet
             * chips 
             */
            case "bet_change":

                int bet = jsonMainArr.getJSONObject(1).getInt("bet");
                int chips = jsonMainArr.getJSONObject(1).getInt("chips");

                if (bet > maxBet)
                    maxBet = bet;

                if (jsonMainArr.getJSONObject(1).getString("player").equals(nick)) {
                    actualChips = chips;
                    actualBet = bet;
                }
                System.out.println("[DEBUG] Actual maxbet: " + maxBet + " Actual bet: " + actualBet
                        + " Actual chips: " + chips);
                //System.out.println("OTO "+jsonMainArr.getJSONObject(1).getString("player"));

                break;

            case "first_connection":

                chips(jsonMainArr.getJSONObject(1).getInt("chips"),
                        jsonMainArr.getJSONObject(2).getInt("chips"),
                        jsonMainArr.getJSONObject(3).getInt("chips"));

                break;

            /**
             * Sending cards to server.
             */
            case "exchange_cards":

                hand.clear();

                for (int i = 1; i < jsonMainArr.length(); i++) {
                    childJSONObject = jsonMainArr.getJSONObject(i);
                    final String rank = childJSONObject.getString("rank");
                    final String suit = childJSONObject.getString("suit");
                    hand.add(new Card(suit, rank));
                }

                betAbility = betAbility(hand) - (pot) / 2;
                System.out.println("[DEBUG] OTO " + hand);
                break;

            /**
             * Main bot's AI (actually it is ordinary behavior). Order has got the meaning.
             */
            case "available_choices":

                choices.clear(); // czyszcz list z poprzednich moliwoci

                for (int i = 1; i < jsonMainArr.length(); i++) {
                    childJSONObject = jsonMainArr.getJSONObject(i);
                    choices.add(childJSONObject.getString("choice"));
                }

                JSONArray doWyslania = new JSONArray();

                System.out.println("[DEBUG] OTO " + choices.toString()); // ok wszystko dziaa

                if (choices.contains("Bet") && betAbility > (pot) / 2
                        && !(betAbility + (maxBet - actualBet) >= actualChips) && betAbility >= maxBet) {

                    betAbility = betAbility(hand) - (pot) / 2;

                    doWyslania.put(new JSONObject().put("action", "bet"));
                    doWyslania.put(new JSONObject().put("bet", betAbility));
                    sendData(doWyslania.toString());

                } else if (choices.contains("Small Blind")) {
                    doWyslania.put(new JSONObject().put("action", "small_blind"));
                    sendData(doWyslania.toString());
                } else if (choices.contains("Big Blind")) {

                    doWyslania.put(new JSONObject().put("action", "big_blind"));
                    sendData(doWyslania.toString());
                } else if (choices.contains("Raise") && betAbility > (pot) / 2
                        && !(betAbility + (maxBet - actualBet) >= actualChips) && betAbility >= maxBet) {

                    betAbility = betAbility(hand) - (pot) / 2;

                    doWyslania.put(new JSONObject().put("action", "raise"));
                    doWyslania.put(new JSONObject().put("bet", betAbility));
                    sendData(doWyslania.toString());

                } else if (choices.contains("Exchange cards")) {

                    doWyslania.put(new JSONObject().put("action", "exchange_cards"));
                    System.out.println("[DEBUG] OTO " + hand);

                    for (int i = 0; i < cardsToExchange(hand).size(); i++) {
                        doWyslania.put(new JSONObject().put("suit", cardsToExchange(hand).get(i).getSuit())
                                .put("rank", cardsToExchange(hand).get(i).getRank()));
                    }
                    sendData(doWyslania.toString());
                    System.out.println("[DEBUG] OTO " + doWyslania);

                } else if (choices.contains("All-In")) {
                    doWyslania.put(new JSONObject().put("action", "all-in"));
                    sendData(doWyslania.toString());

                } else if (choices.contains("Call") && (maxBet - actualBet) <= actualChips
                        && (Math.random() < 0.6)) {
                    doWyslania.put(new JSONObject().put("action", "call"));
                    sendData(doWyslania.toString());
                } else if (choices.contains("Check")) {
                    doWyslania.put(new JSONObject().put("action", "check"));
                    sendData(doWyslania.toString());
                } else if (choices.contains("Fold")) {
                    doWyslania.put(new JSONObject().put("action", "fold"));
                    sendData(doWyslania.toString());
                }

                break;

            case "pot":
                pot = jsonMainArr.getJSONObject(1).getInt("pot_size");
                maxBet = 0;
                break;

            case "exit":
                connection = false;
                break;

            case "the_same_nick":
                System.out.println("[FATAL] Another player has the same nick");
                connection = false;
                break;

            case "you_lost":
                System.out.println("I lost ;-(");
                connection = false;
                break;

            case "you_won":
                System.out.println("I won :-)");
                connection = false;
                break;

            case "message":
                final String message = jsonMainArr.getJSONObject(1).getString("message");
                System.out.println("[DEBUG] Message from server" + message);
                break;

            case "connection_successful":
                break;

            default:
                System.out
                        .println("[ERROR] Unknown command " + jsonMainArr.getJSONObject(0).getString("action"));

            }
        } catch (NullPointerException | IOException | JSONException e) {
            e.printStackTrace();
            break;
        }
    }

    try {
        socket.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    out.close();

}

From source file:com.att.nsa.cambria.service.impl.MMServiceImpl.java

private void pushEventsWithTransaction(DMaaPContext ctx, InputStream inputStream, final String topic,
        final String partitionKey, final String requestTime, final boolean chunked, final String mediaType)
        throws ConfigDbException, AccessDeniedException, TopicExistsException, IOException,
        CambriaApiException {/* w  w w  .j a  v a  2  s . c o m*/

    final MetricsSet metricsSet = ctx.getConfigReader().getfMetrics();

    // setup the event set
    final CambriaEventSet events = new CambriaEventSet(mediaType, inputStream, chunked, partitionKey);

    // start processing, building a batch to push to the backend
    final long startMs = System.currentTimeMillis();
    long count = 0;
    long maxEventBatch = 1024 * 16;
    String evenlen = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, BATCH_LENGTH);
    if (null != evenlen)
        maxEventBatch = Long.parseLong(evenlen);

    final LinkedList<Publisher.message> batch = new LinkedList<Publisher.message>();
    final ArrayList<KeyedMessage<String, String>> kms = new ArrayList<KeyedMessage<String, String>>();

    Publisher.message m = null;
    int messageSequence = 1;
    Long batchId = 1L;
    final boolean transactionEnabled = true;
    int publishBatchCount = 0;
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");

    // LOG.warn("Batch Start Id: " +
    // Utils.getFromattedBatchSequenceId(batchId));
    try {
        // for each message...
        batchId = DMaaPContext.getBatchID();

        String responseTransactionId = null;

        while ((m = events.next()) != null) {

            // LOG.warn("Batch Start Id: " +
            // Utils.getFromattedBatchSequenceId(batchId));

            addTransactionDetailsToMessage(m, topic, ctx.getRequest(), requestTime, messageSequence, batchId,
                    transactionEnabled);
            messageSequence++;

            // add the message to the batch
            batch.add(m);

            responseTransactionId = m.getLogDetails().getTransactionId();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("message", m.getMessage());
            jsonObject.put("transactionId", responseTransactionId);
            final KeyedMessage<String, String> data = new KeyedMessage<String, String>(topic, m.getKey(),
                    jsonObject.toString());
            kms.add(data);

            // check if the batch is full
            final int sizeNow = batch.size();
            if (sizeNow >= maxEventBatch) {
                String startTime = sdf.format(new Date());
                LOG.info("Batch Start Details:[serverIp=" + ctx.getRequest().getLocalAddr() + ",Batch Start Id="
                        + batchId + "]");
                try {
                    ctx.getConfigReader().getfPublisher().sendBatchMessage(topic, kms);
                    // transactionLogs(batch);
                    for (message msg : batch) {
                        LogDetails logDetails = msg.getLogDetails();
                        LOG.info("Publisher Log Details : " + logDetails.getPublisherLogDetails());
                    }
                } catch (Exception excp) {

                    int status = HttpStatus.SC_NOT_FOUND;
                    String errorMsg = null;
                    if (excp instanceof CambriaApiException) {
                        status = ((CambriaApiException) excp).getStatus();
                        JSONTokener jsonTokener = new JSONTokener(((CambriaApiException) excp).getBody());
                        JSONObject errObject = new JSONObject(jsonTokener);
                        errorMsg = (String) errObject.get("message");
                    }
                    ErrorResponse errRes = new ErrorResponse(status,
                            DMaaPResponseCode.PARTIAL_PUBLISH_MSGS.getResponseCode(),
                            "Transaction-" + errorMessages.getPublishMsgError() + ":" + topic + "."
                                    + errorMessages.getPublishMsgCount() + count + "." + errorMsg,
                            null, Utils.getFormattedDate(new Date()), topic,
                            Utils.getUserApiKey(ctx.getRequest()), ctx.getRequest().getRemoteHost(), null,
                            null);
                    LOG.info(errRes.toString());
                    throw new CambriaApiException(errRes);
                }
                kms.clear();
                batch.clear();
                metricsSet.publishTick(sizeNow);
                publishBatchCount = sizeNow;
                count += sizeNow;
                // batchId++;
                String endTime = sdf.format(new Date());
                LOG.info("Batch End Details:[serverIp=" + ctx.getRequest().getLocalAddr() + ",Batch End Id="
                        + batchId + ",Batch Total=" + publishBatchCount + ",Batch Start Time=" + startTime
                        + ",Batch End Time=" + endTime + "]");
                batchId = DMaaPContext.getBatchID();
            }
        }

        // send the pending batch
        final int sizeNow = batch.size();
        if (sizeNow > 0) {
            String startTime = sdf.format(new Date());
            LOG.info("Batch Start Details:[serverIp=" + ctx.getRequest().getLocalAddr() + ",Batch Start Id="
                    + batchId + "]");
            try {
                ctx.getConfigReader().getfPublisher().sendBatchMessage(topic, kms);
                // transactionLogs(batch);
                for (message msg : batch) {
                    LogDetails logDetails = msg.getLogDetails();
                    LOG.info("Publisher Log Details : " + logDetails.getPublisherLogDetails());
                }
            } catch (Exception excp) {
                int status = HttpStatus.SC_NOT_FOUND;
                String errorMsg = null;
                if (excp instanceof CambriaApiException) {
                    status = ((CambriaApiException) excp).getStatus();
                    JSONTokener jsonTokener = new JSONTokener(((CambriaApiException) excp).getBody());
                    JSONObject errObject = new JSONObject(jsonTokener);
                    errorMsg = (String) errObject.get("message");
                }

                ErrorResponse errRes = new ErrorResponse(status,
                        DMaaPResponseCode.PARTIAL_PUBLISH_MSGS.getResponseCode(),
                        "Transaction-" + errorMessages.getPublishMsgError() + ":" + topic + "."
                                + errorMessages.getPublishMsgCount() + count + "." + errorMsg,
                        null, Utils.getFormattedDate(new Date()), topic, Utils.getUserApiKey(ctx.getRequest()),
                        ctx.getRequest().getRemoteHost(), null, null);
                LOG.info(errRes.toString());
                throw new CambriaApiException(errRes);
            }
            kms.clear();
            metricsSet.publishTick(sizeNow);
            count += sizeNow;
            // batchId++;
            String endTime = sdf.format(new Date());
            publishBatchCount = sizeNow;
            LOG.info("Batch End Details:[serverIp=" + ctx.getRequest().getLocalAddr() + ",Batch End Id="
                    + batchId + ",Batch Total=" + publishBatchCount + ",Batch Start Time=" + startTime
                    + ",Batch End Time=" + endTime + "]");
        }

        final long endMs = System.currentTimeMillis();
        final long totalMs = endMs - startMs;

        LOG.info("Published " + count + " msgs in " + totalMs + "ms for topic " + topic);

        // build a response
        final JSONObject response = new JSONObject();
        response.put("count", count);
        response.put("serverTimeMs", totalMs);

    } catch (Exception excp) {
        int status = HttpStatus.SC_NOT_FOUND;
        String errorMsg = null;
        if (excp instanceof CambriaApiException) {
            status = ((CambriaApiException) excp).getStatus();
            JSONTokener jsonTokener = new JSONTokener(((CambriaApiException) excp).getBody());
            JSONObject errObject = new JSONObject(jsonTokener);
            errorMsg = (String) errObject.get("message");
        }

        ErrorResponse errRes = new ErrorResponse(status,
                DMaaPResponseCode.PARTIAL_PUBLISH_MSGS.getResponseCode(),
                "Transaction-" + errorMessages.getPublishMsgError() + ":" + topic + "."
                        + errorMessages.getPublishMsgCount() + count + "." + errorMsg,
                null, Utils.getFormattedDate(new Date()), topic, Utils.getUserApiKey(ctx.getRequest()),
                ctx.getRequest().getRemoteHost(), null, null);
        LOG.info(errRes.toString());
        throw new CambriaApiException(errRes);
    }
}