Example usage for java.util Stack Stack

List of usage examples for java.util Stack Stack

Introduction

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

Prototype

public Stack() 

Source Link

Document

Creates an empty Stack.

Usage

From source file:hudson.plugins.active_directory.ActiveDirectoryUnixAuthenticationProvider.java

/**
 * Performs recursive group membership lookup.
 *
 * This was how we did the lookup traditionally until we discovered 1.2.840.113556.1.4.1941.
 * But various people reported that it slows down the execution tremendously to the point that it is unusable,
 * while others seem to report that it runs faster than recursive search (http://social.technet.microsoft.com/Forums/fr-FR/f238d2b0-a1d7-48e8-8a60-542e7ccfa2e8/recursive-retrieval-of-all-ad-group-memberships-of-a-user?forum=ITCG)
 *
 * This implementation is kept for Windows 2003 that doesn't support 1.2.840.113556.1.4.1941, but it can be also
 * enabled for those who are seeing the performance problem.
 *
 * See JENKINS-22830//w w w  .  j a v  a2  s. c o  m
 */
private void recursiveGroupLookup(DirContext context, Attributes id, Set<GrantedAuthority> groups)
        throws NamingException {
    Stack<Attributes> q = new Stack<Attributes>();
    q.push(id);
    while (!q.isEmpty()) {
        Attributes identity = q.pop();
        LOGGER.finer("Looking up group of " + identity);

        Attribute memberOf = identity.get("memberOf");
        if (memberOf == null)
            continue;

        for (int i = 0; i < memberOf.size(); i++) {
            try {
                LOGGER.log(Level.FINE, "Trying to get the CN of {0}", memberOf.get(i));
                Attributes group = context.getAttributes(new LdapName(memberOf.get(i).toString()),
                        new String[] { "CN", "memberOf" });
                Attribute cn = group.get("CN");
                if (cn == null) {
                    LOGGER.fine("Failed to obtain CN of " + memberOf.get(i));
                    continue;
                }
                if (LOGGER.isLoggable(Level.FINE))
                    LOGGER.fine(cn.get() + " is a member of " + memberOf.get(i));

                if (groups.add(new GrantedAuthorityImpl(cn.get().toString()))) {
                    q.add(group); // recursively look for groups that this group is a member of.
                }
            } catch (NameNotFoundException e) {
                LOGGER.fine("Failed to obtain CN of " + memberOf.get(i));
            }
        }
    }
}

From source file:com.udojava.evalex.Expression.java

/**
 * Evaluates the expression./* w w  w  .j av a  2 s . com*/
 *
 * @return The result of the expression.
 */
public MyComplex eval() {
    Stack<LazyNumber> stack = new Stack<>();

    for (final String token : getRPN()) {
        if (operators.containsKey(token)) {
            final LazyNumber v1 = stack.pop();
            final LazyNumber v2 = stack.pop();
            LazyNumber number = () -> operators.get(token).eval(v2.eval(), v1.eval());
            stack.push(number);
        } else if (mainVars.getMap().containsKey(token)) {
            MyComplex v = mainVars.get(token);
            if (v.type == ValueType.ARRAY) {
                stack.push(() -> v);
            } else {
                PitDecimal bd = new PitDecimal(v.real, v.imaginary);
                bd.type = v.type;
                bd.setVarToken(token);
                stack.push(() -> bd);
            }
        } else if (functions.containsKey(token.toUpperCase(Locale.ROOT))) {
            LazyFunction f = functions.get(token.toUpperCase(Locale.ROOT));
            ArrayList<LazyNumber> p = new ArrayList<>(!f.numParamsVaries() ? f.getNumParams() : 0);
            // pop parameters off the stack until we hit the start of
            // this function's parameter list
            while (!stack.isEmpty() && stack.peek() != PARAMS_START) {
                p.add(0, stack.pop());
            }
            if (stack.peek() == PARAMS_START) {
                stack.pop();
            }
            LazyNumber fResult = f.lazyEval(p);
            stack.push(fResult);
        } else if ("(".equals(token)) {
            stack.push(PARAMS_START);
        } else {
            MyComplex bd;
            if (token.endsWith("i")) {
                String str = token.substring(0, token.length() - 1);
                if (str.isEmpty())
                    str = "1";
                bd = new MyComplex("0", str);
            } else {
                bd = new MyComplex(token);
            }
            MyComplex finalBd = bd;
            stack.push(() -> finalBd); // blank constant
        }
    }
    return stack.pop().eval();
}

From source file:com.espertech.esper.rowregex.EventRowRegexNFAView.java

private String print(RegexNFAState[] states) {
    StringWriter writer = new StringWriter();
    PrintWriter buf = new PrintWriter(writer);
    Stack<RegexNFAState> currentStack = new Stack<RegexNFAState>();
    print(Arrays.asList(states), buf, 0, currentStack);
    return writer.toString();
}

From source file:fsi_admin.JFsiTareas.java

public synchronized void respaldarServidor(Calendar fecha, PrintWriter out) {
    try {//from  w w w  . j a v a2s . co m
        String path = "/usr/local/forseti/log/RESP-FORSETI_ADMIN-"
                + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + ".log";
        FileWriter filewri = new FileWriter(path, true);
        PrintWriter pw = new PrintWriter(filewri);

        if (out != null) {
            out.println("----------------------------------------------------------------------------<br>");
            out.println("RESPALDANDO LA BASE DE DATOS PRINCIPAL: FORSETI_ADMIN Y ARCHIVOS "
                    + JUtil.obtFechaTxt(new Date(), "HH:mm:ss") + "<br>");
            out.println("----------------------------------------------------------------------------<br>");
            out.flush();
        }
        pw.println("----------------------------------------------------------------------------");
        pw.println("RESPALDANDO LA BASE DE DATOS PRINCIPAL: FORSETI_ADMIN Y ARCHIVOS "
                + JUtil.obtFechaTxt(new Date(), "HH:mm:ss"));
        pw.println("----------------------------------------------------------------------------");
        pw.flush();

        if (respaldos.equals("NC")) {
            if (out != null) {
                out.println(
                        "PRECAUCION: La variable RESPALDOS (ruta para los archivos de respaldo) no est definida... No se puede generar<br>");
                out.flush();
            }
            pw.println(
                    "PRECAUCION: La variable RESPALDOS (ruta para los archivos de respaldo) no est definida... No se puede generar");
            pw.flush();
            return;
        }

        if (tomcat.equals("NC")) {
            if (out != null) {
                out.println(
                        "PRECAUCION: La variable TOMCAT (ruta de instalacin de tomcat) no est definida... No se puede generar");
                out.flush();
            }
            pw.println(
                    "PRECAUCION: La variable TOMCAT (ruta de instalacin de tomcat) no est definida... No se puede generar");
            pw.flush();
            return;
        }

        JFsiScript sc = new JFsiScript();
        sc.setVerbose(true);

        String ERROR = "", RES = "";

        try {
            JAdmVariablesSet var = new JAdmVariablesSet(null);
            var.ConCat(true);
            var.m_Where = "ID_Variable = 'VERSION'";
            var.Open();
            String vers = var.getAbsRow(0).getVAlfanumerico();

            File dir = new File(respaldos, "FORSETI_ADMIN-" + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm"));
            dir.mkdir();
            // Primero Agrega el archivo de version para este servidor
            File version = new File(respaldos + "/" + "FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "/forseti.version");
            FileWriter fw = new FileWriter(version);
            fw.write(vers);
            fw.close();
            File diremp = new File(respaldos + "/FORSETI_ADMIN-" + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm"),
                    "emp");
            diremp.mkdir();

            //System.out.println(dir.getAbsolutePath());
            String CONTENT = "";
            CONTENT += "rsync -av --stats /usr/local/forseti/act " + respaldos + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "\n";
            CONTENT += "rsync -av --stats /usr/local/forseti/bin " + respaldos + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "\n";
            CONTENT += "rsync -av --stats /usr/local/forseti/log " + respaldos + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "\n";
            CONTENT += "rsync -av --stats /usr/local/forseti/pac " + respaldos + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "\n";
            CONTENT += "rsync -av --stats /usr/local/forseti/rec " + respaldos + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "\n";
            CONTENT += "rsync -av --stats " + tomcat + "/webapps/ROOT.war " + respaldos + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "\n";

            sc.setContent(CONTENT);
            System.out.println(CONTENT);
            RES = sc.executeCommand();
            ERROR += sc.getError();
            if (!ERROR.equals("")) {
                if (out != null) {
                    pw.println("ERROR al respaldar en RSYNC: " + ERROR + "<br>");
                    pw.flush();
                }
                pw.println("ERROR al respaldar en RSYNC: " + ERROR);
                pw.flush();
            } else {
                if (out != null) {
                    out.println("El respaldo de los archivos se genero con xito en: " + respaldos
                            + "/FORSETI_ADMIN-" + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "<br>");
                    out.flush();
                }
                pw.println("El respaldo de los archivos se genero con xito en: " + respaldos
                        + "/FORSETI_ADMIN-" + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm"));
                pw.flush();
            }

            if (out != null) {
                out.println("FINALIZANDO RESPALDO DE ARCHIVOS FORSETI_ADMIN: "
                        + JUtil.obtFechaTxt(new Date(), "HH:mm:ss") + "<br>");
                out.flush();
            }
            pw.println("FINALIZANDO RESPALDO DE ARCHIVOS FORSETI_ADMIN: "
                    + JUtil.obtFechaTxt(new Date(), "HH:mm:ss"));
            pw.flush();

            RES = "";
            ERROR = "";
            CONTENT = "PGUSER=forseti PGPASSWORD=" + JUtil.getPASS() + " pg_dump --host=" + JUtil.getADDR()
                    + " --port=" + JUtil.getPORT() + " --file=" + respaldos + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + "/FORSETI_ADMIN-"
                    + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + ".dump FORSETI_ADMIN";
            sc.setContent(CONTENT);
            //System.out.println(CONTENT);
            RES = sc.executeCommand();
            ERROR += sc.getError();
            //Ahora genera el Archivo Base64
            if (!ERROR.equals("")) {
                //System.out.println(ERROR);
                if (out != null) {
                    out.println("ERROR al crear el respaldo de la base de datos principal: " + ERROR + "<br>");
                    out.flush();
                }
                pw.println("ERROR al crear el respaldo de la base de datos principal: " + ERROR);
                pw.flush();
            } else {
                if (RES.equals("")) {
                    if (out != null) {
                        out.println(
                                "El respaldo de la base de datos principal se gener con xito como archivo .dump dentro de este directurio");
                        out.flush();
                    }
                    pw.println(
                            "El respaldo de la base de datos principal se gener con xito como archivo .dump dentro de este directurio");
                    pw.flush();
                } else {
                    if (out != null) {
                        out.println("RESPUESTA PG_DUMP: " + RES + "<br>");
                        out.flush();
                    }
                    pw.println("RESPUESTA PG_DUMP: " + RES);
                    pw.flush();
                }
            }
            //////////////////////////////////////////
            if (out != null) {
                out.println("FINALIZANDO RESPALDO DE LA BASE DE DATOS: FORSETI_ADMIN "
                        + JUtil.obtFechaTxt(new Date(), "HH:mm:ss") + "<br>");
                out.flush();
            }
            pw.println("FINALIZANDO RESPALDO DE LA BASE DE DATOS: FORSETI_ADMIN "
                    + JUtil.obtFechaTxt(new Date(), "HH:mm:ss"));
            pw.flush();
        } catch (Throwable e) {
            if (out != null) {
                out.println("ERROR Throwable:<br>");
                e.printStackTrace(out);
                out.flush();
            }
            pw.println("ERROR Throwable:");
            e.printStackTrace(pw);
            pw.flush();
        }

        JBDSSet set = new JBDSSet(null);
        set.ConCat(true);
        set.m_OrderBy = "ID_BD ASC";
        set.Open();

        for (int i = 0; i < set.getNumRows(); i++) {
            if (!set.getAbsRow(i).getSU().equals("3")) // La base de datos esta corrupta, se debe eliminar
            {
                out.println("La siguiente base de datos esta corrupta y se debe eliminar: "
                        + set.getAbsRow(i).getNombre() + "<br>");
                out.flush();
                pw.println("La siguiente base de datos esta corrupta y se debe eliminar: "
                        + set.getAbsRow(i).getNombre());
                pw.flush();
                continue;
            } else
                respaldarEmpresa(set, i, fecha, out, pw);
        }
        if (out != null) {
            out.println(
                    "Generando el archivo zip... Esto puede tardar demasiado tiempo, hay que ser pacientes<br>");
            out.flush();
        }
        pw.println("Generando el archivo zip...");
        pw.flush();
        JZipUnZipUtil azip = new JZipUnZipUtil();
        azip.zipFolder(respaldos + "/FORSETI_ADMIN-" + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm"),
                respaldos + "/FORSETI_ADMIN-" + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm") + ".zip");
        if (out != null) {
            out.println("Eliminando carpeta de respaldo...<br>");
            out.flush();
        }
        pw.println("Eliminando carpeta de respaldo...");
        pw.flush();
        //Borra los archivos del respaldo
        File dirbd = new File(respaldos + "/FORSETI_ADMIN-" + JUtil.obtFechaTxt(fecha, "yyyy-MM-dd-HH-mm"));
        File[] currList;
        Stack<File> stack = new Stack<File>();
        stack.push(dirbd);
        while (!stack.isEmpty()) {
            if (stack.lastElement().isDirectory()) {
                currList = stack.lastElement().listFiles();
                if (currList.length > 0) {
                    for (File curr : currList) {
                        stack.push(curr);
                    }
                } else {
                    stack.pop().delete();
                }
            } else {
                stack.pop().delete();
            }
        }
        if (out != null) {
            out.println("--------------- FIN DEL RESPALDO " + JUtil.obtFechaTxt(new Date(), "HH:mm:ss")
                    + " ---------------<br>");
            out.flush();
        }
        pw.println("--------------- FIN DEL RESPALDO " + JUtil.obtFechaTxt(new Date(), "HH:mm:ss")
                + " ---------------");
        pw.flush();
        pw.close();

    } catch (IOException e) {
        if (out != null) {
            out.println("OCURRIERON ERRORES DE IOException<br>");
            e.printStackTrace(out);
            out.flush();
        }
        System.out.println("OCURRIERON ERRORES DE IOException<br>");
        e.printStackTrace(System.out);

    } catch (Exception e) {
        if (out != null) {
            out.println("OCURRIERON ERRORES DE Exception<br>");
            e.printStackTrace(out);
            out.flush();
        }
        System.out.println("OCURRIERON ERRORES DE Exception<br>");
        e.printStackTrace(System.out);

    }

}

From source file:org.eclipse.b3.p2.maven.loader.Maven2RepositoryLoader.java

@Override
public void open(URI location, IProvisioningAgent agent, MetadataRepositoryImpl mdr) throws CoreException {
    this.location = location;
    this.agent = agent;
    indexer = IndexerUtils.getIndexer("nexus");

    // get nexus index timestamp without using nexus tools for now
    long remoteIndexTimestamp = getRemoteIndexTimestamp();

    // if no indexer is available or no index is available, check if the repository is allowed to be crawled
    if (indexer == null || remoteIndexTimestamp == 0L)
        if (!robotSafe(location.toString(), "/")) {
            StringBuilder message = new StringBuilder("Crawling of %1$s is discouraged (see %1$s/robots.txt)");
            if (remoteIndexTimestamp != 0L)
                message.append(// w w w . j a v a2 s. c  o  m
                        ". Hint: The repository is indexed. Install an index reader to map this repository.");

            throw ExceptionUtils.fromMessage(message.toString(), location.toString());
        }

    repository = mdr;

    iteratorStack = new Stack<UriIterator>();
    versionEntryItor = Collections.<VersionEntry>emptyList().iterator();
}

From source file:hudson.model.Hudson.java

/**
 * Gets all the {@link Item}s recursively in the {@link ItemGroup} tree
 * and filter them by the given type./*from w  ww. j av a2s. com*/
 */
public <T extends Item> List<T> getAllItems(Class<T> type) {
    List<T> r = new ArrayList<T>();

    Stack<ItemGroup> q = new Stack<ItemGroup>();
    q.push(this);

    while (!q.isEmpty()) {
        ItemGroup<?> parent = q.pop();
        for (Item i : parent.getItems()) {
            if (type.isInstance(i)) {
                if (i.hasPermission(Item.READ))
                    r.add(type.cast(i));
            }
            if (i instanceof ItemGroup)
                q.push((ItemGroup) i);
        }
    }

    return r;
}

From source file:com.udojava.evalex.Expression.java

/**
 * Implementation of the <i>Shunting Yard</i> algorithm to transform an
 * infix expression to a RPN expression.
 *
 * @param expression The input expression in infx.
 * @return A RPN representation of the expression, with each token as a list
 * member.//from   w  w w  .ja  va  2 s.com
 */
private List<String> shuntingYard(String expression) {
    List<String> outputQueue = new ArrayList<>();
    Stack<String> stack = new Stack<>();

    Tokenizer tokenizer = new Tokenizer(expression);

    String lastFunction = null;
    String previousToken = null;
    while (tokenizer.hasNext()) {
        String token = tokenizer.next();
        if (isNumber(token)) {
            if (token.startsWith("x")) {

                BigInteger bd = new BigInteger(token.substring(1), 16);
                outputQueue.add(bd.toString(10));
            } else if (token.startsWith("b")) {
                BigInteger bd = new BigInteger(token.substring(1), 2);
                outputQueue.add(bd.toString(10));
            } else if (token.startsWith("o")) {
                BigInteger bd = new BigInteger(token.substring(1), 8);
                outputQueue.add(bd.toString(10));
            } else {
                outputQueue.add(token);
            }
        } else if (mainVars.containsKey(token)) {
            outputQueue.add(token);
        } else if (functions.containsKey(token.toUpperCase(Locale.ROOT))) {
            stack.push(token);
            lastFunction = token;
        } else if ((Character.isLetter(token.charAt(0)) || token.charAt(0) == '_')
                && !operators.containsKey(token)) {
            mainVars.put(token, new MyComplex(0, 0)); // create variable
            outputQueue.add(token);
            //stack.push(token);
        } else if (",".equals(token)) {
            if (operators.containsKey(previousToken)) {
                throw new ExpressionException("Missing parameter(s) for operator " + previousToken
                        + " at character position " + (tokenizer.getPos() - 1 - previousToken.length()));
            }
            while (!stack.isEmpty() && !"(".equals(stack.peek())) {
                outputQueue.add(stack.pop());
            }
            if (stack.isEmpty()) {
                throw new ExpressionException("Parse error for function '" + lastFunction + "'");
            }
        } else if (operators.containsKey(token)) {
            if (",".equals(previousToken) || "(".equals(previousToken)) {
                throw new ExpressionException("Missing parameter(s) for operator " + token
                        + " at character position " + (tokenizer.getPos() - token.length()));
            }
            Operator o1 = operators.get(token);
            String token2 = stack.isEmpty() ? null : stack.peek();
            while (token2 != null && operators.containsKey(token2)
                    && ((o1.isLeftAssoc() && o1.getPrecedence() <= operators.get(token2).getPrecedence())
                            || (o1.getPrecedence() < operators.get(token2).getPrecedence()))) {
                outputQueue.add(stack.pop());
                token2 = stack.isEmpty() ? null : stack.peek();
            }
            stack.push(token);
        } else if ("(".equals(token)) {
            if (previousToken != null) {
                if (isNumber(previousToken)) {
                    throw new ExpressionException(
                            "Missing operator at character position " + tokenizer.getPos());
                }
                // if the ( is preceded by a valid function, then it
                // denotes the start of a parameter list
                if (functions.containsKey(previousToken.toUpperCase(Locale.ROOT))) {
                    outputQueue.add(token);
                }
            }
            stack.push(token);
        } else if (")".equals(token)) {
            if (operators.containsKey(previousToken)) {
                throw new ExpressionException("Missing parameter(s) for operator " + previousToken
                        + " at character position " + (tokenizer.getPos() - 1 - previousToken.length()));
            }
            while (!stack.isEmpty() && !"(".equals(stack.peek())) {
                outputQueue.add(stack.pop());
            }
            if (stack.isEmpty()) {
                throw new ExpressionException("Mismatched parentheses");
            }
            stack.pop();
            if (!stack.isEmpty() && functions.containsKey(stack.peek().toUpperCase(Locale.ROOT))) {
                outputQueue.add(stack.pop());
            }
        }
        previousToken = token;
    }
    while (!stack.isEmpty()) {
        String element = stack.pop();
        if ("(".equals(element) || ")".equals(element)) {
            throw new ExpressionException("Mismatched parentheses");
        }

        if (!operators.containsKey(element)) {
            throw new ExpressionException("Unknown operator or function: " + element);
        }
        outputQueue.add(element);
    }
    return outputQueue;
}

From source file:com.joliciel.jochre.graphics.ShapeImpl.java

@Override
public TreeSet<VerticalLineSegment> getVerticalLineSegments() {
    if (lines == null) {
        WritableImageGrid mirror = this.graphicsService.getEmptyMirror(this);

        int[] startingPoint = this.getStartingPoint();
        int startX = startingPoint[0];
        int startY = startingPoint[1];

        // let's imagine
        // 0 X 0 0 x x
        // x x x 0 0 x
        // 0 0 x x x x

        //as we build the shape, we keep track in memory of all of the vertical line segments that we find
        //and which vertical line segments touch them to the right and left
        //a segment can have more than one left segment (if they're broken by a white space)
        Stack<VerticalLineSegment> lineStack = new Stack<VerticalLineSegment>();
        lines = new TreeSet<VerticalLineSegment>();
        VerticalLineSegment firstLine = new VerticalLineSegment(startX, startY);

        lineStack.push(firstLine);//from  w w w  .java  2 s .c o m

        while (!lineStack.isEmpty()) {
            VerticalLineSegment line = lineStack.pop();
            // Add this line's pixels to the mirror so that we don't touch it again.
            for (int rely = line.yTop; rely <= line.yBottom; rely++)
                mirror.setPixel(line.x, rely, 1);

            // extend the vertical line segment up and down from this point
            for (int rely = line.yTop - 1; rely >= 0; rely--) {
                if (this.isPixelBlack(line.x, rely, this.getJochreImage().getSeparationThreshold())) {
                    mirror.setPixel(line.x, rely, 1);
                    line.yTop = rely;
                } else {
                    break;
                }
            }
            for (int rely = line.yBottom + 1; rely < this.getHeight(); rely++) {
                if (this.isPixelBlack(line.x, rely, this.getJochreImage().getSeparationThreshold())) {
                    mirror.setPixel(line.x, rely, 1);
                    line.yBottom = rely;
                } else {
                    break;
                }
            }

            //LOG.debug("Adding line x = " + line.x + ", top = " + line.yTop + ", bottom = " + line.yBottom);
            lines.add(line);

            // find any points to the left of this segment
            int relx = line.x - 1;
            VerticalLineSegment leftLine = null;
            for (int rely = line.yTop - 1; rely <= line.yBottom + 1; rely++) {
                if (this.isPixelBlack(relx, rely, this.getJochreImage().getSeparationThreshold())) {
                    if (leftLine == null) {
                        leftLine = new VerticalLineSegment(relx, rely);
                    } else {
                        leftLine.yBottom = rely;
                    }
                } else {
                    if (leftLine != null) {
                        if (mirror.getPixel(relx, leftLine.yTop) > 0) {
                            // if we already found this line before - let's find it again.
                            for (VerticalLineSegment lineSegment : lines) {
                                if (lineSegment.x == relx) {
                                    if (lineSegment.yTop <= leftLine.yTop
                                            && leftLine.yTop <= lineSegment.yBottom) {
                                        leftLine = lineSegment;
                                        break;
                                    }
                                }
                            }
                        } else if (lineStack.contains(leftLine)) {
                            leftLine = lineStack.get(lineStack.indexOf(leftLine));
                        } else {
                            lineStack.push(leftLine);
                        }
                        line.leftSegments.add(leftLine);
                        leftLine = null;
                    }
                }
            } // next rely

            // add the last line
            if (leftLine != null) {
                if (mirror.getPixel(relx, leftLine.yTop) > 0) {
                    // if we already found this line before - let's find it again.
                    for (VerticalLineSegment lineSegment : lines) {
                        if (lineSegment.x == relx) {
                            if (lineSegment.yTop <= leftLine.yTop && leftLine.yTop <= lineSegment.yBottom) {
                                leftLine = lineSegment;
                                break;
                            }
                        }
                    }
                } else if (lineStack.contains(leftLine)) {
                    leftLine = lineStack.get(lineStack.indexOf(leftLine));
                } else {
                    lineStack.push(leftLine);
                }
                line.leftSegments.add(leftLine);
            }

            // find any points to the right of this segment
            relx = line.x + 1;
            VerticalLineSegment rightLine = null;
            for (int rely = line.yTop - 1; rely <= line.yBottom + 1; rely++) {
                if (this.isPixelBlack(relx, rely, this.getJochreImage().getSeparationThreshold())) {
                    if (rightLine == null) {
                        rightLine = new VerticalLineSegment(relx, rely);
                    } else {
                        rightLine.yBottom = rely;
                    }
                } else {
                    if (rightLine != null) {
                        if (mirror.getPixel(relx, rightLine.yTop) > 0) {
                            // if we already found this line before - let's find it again.
                            for (VerticalLineSegment lineSegment : lines) {
                                if (lineSegment.x == relx) {
                                    if (lineSegment.yTop <= rightLine.yTop
                                            && rightLine.yTop <= lineSegment.yBottom) {
                                        rightLine = lineSegment;
                                        break;
                                    }
                                }
                            }
                        } else if (lineStack.contains(rightLine)) {
                            rightLine = lineStack.get(lineStack.indexOf(rightLine));
                        } else {
                            lineStack.push(rightLine);
                        }
                        line.rightSegments.add(rightLine);
                        rightLine = null;
                    }
                }
            } // next rely

            // add the last line
            if (rightLine != null) {
                if (mirror.getPixel(relx, rightLine.yTop) > 0) {
                    // if we already found this line before - let's find it again.
                    for (VerticalLineSegment lineSegment : lines) {
                        if (lineSegment.x == relx) {
                            if (lineSegment.yTop <= rightLine.yTop && rightLine.yTop <= lineSegment.yBottom) {
                                rightLine = lineSegment;
                                break;
                            }
                        }
                    }
                } else if (lineStack.contains(rightLine)) {
                    rightLine = lineStack.get(lineStack.indexOf(rightLine));
                } else {
                    lineStack.push(rightLine);
                }
                line.rightSegments.add(rightLine);
            }
        } // next line on stack
        LOG.debug("Found " + lines.size() + " lines");
    }
    return lines;
}

From source file:com.pinterest.hdfsbackup.distcp.DistCp.java

/**
 * Initialize DFSCopyFileMapper specific job-configuration.
 * @param conf : The dfs/mapred configuration.
 * @param jobConf : The handle to the jobConf object to be initialized.
 * @param args Arguments/* w  w w  .j a  v a2 s  . c o  m*/
 */
private static void setup(Configuration conf, JobConf jobConf, final Arguments args) throws IOException {
    jobConf.set(DST_DIR_LABEL, args.dst.toUri().toString());

    //set boolean values
    final boolean update = args.flags.contains(Options.UPDATE);
    final boolean overwrite = !update && args.flags.contains(Options.OVERWRITE);
    jobConf.setBoolean(Options.UPDATE.propertyname, update);
    jobConf.setBoolean(Options.OVERWRITE.propertyname, overwrite);
    jobConf.setBoolean(Options.IGNORE_READ_FAILURES.propertyname,
            args.flags.contains(Options.IGNORE_READ_FAILURES));
    jobConf.setBoolean(Options.PRESERVE_STATUS.propertyname, args.flags.contains(Options.PRESERVE_STATUS));

    final String randomId = getRandomId();
    JobClient jClient = new JobClient(jobConf);
    Path jobDirectory = new Path(jClient.getSystemDir(), NAME + "_" + randomId);
    jobConf.set(JOB_DIR_LABEL, jobDirectory.toString());

    FileSystem dstfs = args.dst.getFileSystem(conf);
    boolean dstExists = dstfs.exists(args.dst);
    boolean dstIsDir = false;
    if (dstExists) {
        dstIsDir = dstfs.getFileStatus(args.dst).isDir();
    }

    // default logPath
    Path logPath = args.log;
    if (logPath == null) {
        String filename = "_distcp_logs_" + randomId;
        if (!dstExists || !dstIsDir) {
            Path parent = args.dst.getParent();
            if (!dstfs.exists(parent)) {
                dstfs.mkdirs(parent);
            }
            logPath = new Path(parent, filename);
        } else {
            logPath = new Path(args.dst, filename);
        }
    }
    FileOutputFormat.setOutputPath(jobConf, logPath);

    // create src list, dst list
    FileSystem jobfs = jobDirectory.getFileSystem(jobConf);

    Path srcfilelist = new Path(jobDirectory, "_distcp_src_files");
    jobConf.set(SRC_LIST_LABEL, srcfilelist.toString());
    SequenceFile.Writer src_writer = SequenceFile.createWriter(jobfs, jobConf, srcfilelist, LongWritable.class,
            FilePair.class, SequenceFile.CompressionType.NONE);

    Path dstfilelist = new Path(jobDirectory, "_distcp_dst_files");
    SequenceFile.Writer dst_writer = SequenceFile.createWriter(jobfs, jobConf, dstfilelist, Text.class,
            Text.class, SequenceFile.CompressionType.NONE);

    Path dstdirlist = new Path(jobDirectory, "_distcp_dst_dirs");
    jobConf.set(DST_DIR_LIST_LABEL, dstdirlist.toString());
    SequenceFile.Writer dir_writer = SequenceFile.createWriter(jobfs, jobConf, dstdirlist, Text.class,
            FilePair.class, SequenceFile.CompressionType.NONE);

    // handle the case where the destination directory doesn't exist
    // and we've only a single src directory OR we're updating/overwriting
    // the contents of the destination directory.
    final boolean special = (args.srcs.size() == 1 && !dstExists) || update || overwrite;
    int srcCount = 0, cnsyncf = 0, dirsyn = 0;
    long fileCount = 0L, byteCount = 0L, cbsyncs = 0L;
    try {
        for (Iterator<Path> srcItr = args.srcs.iterator(); srcItr.hasNext();) {
            final Path src = srcItr.next();
            FileSystem srcfs = src.getFileSystem(conf);
            FileStatus srcfilestat = srcfs.getFileStatus(src);
            Path root = special && srcfilestat.isDir() ? src : src.getParent();
            if (srcfilestat.isDir()) {
                ++srcCount;
            }

            Stack<FileStatus> pathstack = new Stack<FileStatus>();
            for (pathstack.push(srcfilestat); !pathstack.empty();) {
                FileStatus cur = pathstack.pop();
                FileStatus[] children = srcfs.listStatus(cur.getPath());
                for (int i = 0; i < children.length; i++) {
                    boolean skipfile = false;
                    final FileStatus child = children[i];
                    final String dst = makeRelative(root, child.getPath());
                    ++srcCount;

                    if (child.isDir()) {
                        pathstack.push(child);
                    } else {
                        //skip file if the src and the dst files are the same.
                        skipfile = update && sameFile(srcfs, child, dstfs, new Path(args.dst, dst));
                        //skip file if it exceed file limit or size limit
                        skipfile |= fileCount == args.filelimit || byteCount + child.getLen() > args.sizelimit;

                        if (!skipfile) {
                            ++fileCount;
                            byteCount += child.getLen();

                            if (LOG.isTraceEnabled()) {
                                LOG.trace("adding file " + child.getPath());
                            }

                            ++cnsyncf;
                            cbsyncs += child.getLen();
                            if (cnsyncf > SYNC_FILE_MAX || cbsyncs > BYTES_PER_MAP) {
                                src_writer.sync();
                                dst_writer.sync();
                                cnsyncf = 0;
                                cbsyncs = 0L;
                            }
                        }
                    }

                    if (!skipfile) {
                        src_writer.append(new LongWritable(child.isDir() ? 0 : child.getLen()),
                                new FilePair(child, dst));
                    }

                    dst_writer.append(new Text(dst), new Text(child.getPath().toString()));
                }

                if (cur.isDir()) {
                    String dst = makeRelative(root, cur.getPath());
                    dir_writer.append(new Text(dst), new FilePair(cur, dst));
                    if (++dirsyn > SYNC_FILE_MAX) {
                        dirsyn = 0;
                        dir_writer.sync();
                    }
                }
            }
        }
    } finally {
        checkAndClose(src_writer);
        checkAndClose(dst_writer);
        checkAndClose(dir_writer);
    }

    FileStatus dststatus = null;
    try {
        dststatus = dstfs.getFileStatus(args.dst);
    } catch (FileNotFoundException fnfe) {
        LOG.info(args.dst + " does not exist.");
    }

    // create dest path dir if copying > 1 file
    if (dststatus == null) {
        if (srcCount > 1 && !dstfs.mkdirs(args.dst)) {
            throw new IOException("Failed to create" + args.dst);
        }
    }

    final Path sorted = new Path(jobDirectory, "_distcp_sorted");
    checkDuplication(jobfs, dstfilelist, sorted, conf);

    if (dststatus != null && args.flags.contains(Options.DELETE)) {
        deleteNonexisting(dstfs, dststatus, sorted, jobfs, jobDirectory, jobConf, conf);
    }

    Path tmpDir = new Path(
            (dstExists && !dstIsDir) || (!dstExists && srcCount == 1) ? args.dst.getParent() : args.dst,
            "_distcp_tmp_" + randomId);
    jobConf.set(TMP_DIR_LABEL, tmpDir.toUri().toString());
    LOG.info("srcCount=" + srcCount);
    jobConf.setInt(SRC_COUNT_LABEL, srcCount);
    jobConf.setLong(TOTAL_SIZE_LABEL, byteCount);
    setMapCount(byteCount, jobConf);
}