Example usage for java.util.concurrent.atomic AtomicBoolean set

List of usage examples for java.util.concurrent.atomic AtomicBoolean set

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicBoolean set.

Prototype

public final void set(boolean newValue) 

Source Link

Document

Sets the value to newValue , with memory effects as specified by VarHandle#setVolatile .

Usage

From source file:org.eclipse.hono.adapter.http.AbstractVertxBasedHttpProtocolAdapter.java

private void doUploadMessage(final RoutingContext ctx, final String tenant, final String deviceId,
        final Buffer payload, final String contentType, final Future<MessageSender> senderTracker,
        final String endpointName) {

    if (!isPayloadOfIndicatedType(payload, contentType)) {
        HttpUtils.badRequest(ctx,//  w w w . java  2 s  .com
                String.format("Content-Type %s does not match with the payload", contentType));
    } else {
        final Integer qosHeader = getQoSLevel(ctx.request().getHeader(Constants.HEADER_QOS_LEVEL));
        if (contentType == null) {
            HttpUtils.badRequest(ctx, String.format("%s header is missing", HttpHeaders.CONTENT_TYPE));
        } else if (qosHeader != null && qosHeader == HEADER_QOS_INVALID) {
            HttpUtils.badRequest(ctx, "Bad QoS Header Value");
        } else {

            final Device authenticatedDevice = getAuthenticatedDevice(ctx);
            final Future<JsonObject> tokenTracker = getRegistrationAssertion(tenant, deviceId,
                    authenticatedDevice);
            final Future<TenantObject> tenantConfigTracker = getTenantConfiguration(tenant);

            // AtomicBoolean to control if the downstream message was sent successfully
            final AtomicBoolean downstreamMessageSent = new AtomicBoolean(false);
            // AtomicReference to a Handler to be called to close an open command receiver link.
            final AtomicReference<Handler<Void>> closeLinkAndTimerHandlerRef = new AtomicReference<>();

            // Handler to be called with a received command. If the timer expired, null is provided as command.
            final Handler<Message> commandReceivedHandler = commandMessage -> {
                // reset the closeHandler reference, since it is not valid anymore at this time.
                closeLinkAndTimerHandlerRef.set(null);
                if (downstreamMessageSent.get()) {
                    // finish the request, since the response is now complete (command was added)
                    if (!ctx.response().closed()) {
                        ctx.response().end();
                    }
                }
            };

            CompositeFuture.all(tokenTracker, tenantConfigTracker, senderTracker).compose(ok -> {

                if (tenantConfigTracker.result().isAdapterEnabled(getTypeName())) {
                    final MessageSender sender = senderTracker.result();
                    final Message downstreamMessage = newMessage(
                            ResourceIdentifier.from(endpointName, tenant, deviceId),
                            sender.isRegistrationAssertionRequired(), ctx.request().uri(), contentType, payload,
                            tokenTracker.result(), HttpUtils.getTimeTilDisconnect(ctx));
                    customizeDownstreamMessage(downstreamMessage, ctx);

                    // first open the command receiver link (if needed)
                    return openCommandReceiverLink(ctx, tenant, deviceId, commandReceivedHandler)
                            .compose(closeLinkAndTimerHandler -> {
                                closeLinkAndTimerHandlerRef.set(closeLinkAndTimerHandler);

                                if (qosHeader == null) {
                                    return sender.send(downstreamMessage);
                                } else {
                                    return sender.sendAndWaitForOutcome(downstreamMessage);
                                }
                            });
                } else {
                    // this adapter is not enabled for the tenant
                    return Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_FORBIDDEN));
                }
            }).compose(delivery -> {
                LOG.trace(
                        "successfully processed message for device [tenantId: {}, deviceId: {}, endpoint: {}]",
                        tenant, deviceId, endpointName);
                metrics.incrementProcessedHttpMessages(endpointName, tenant);
                ctx.response().setStatusCode(HttpURLConnection.HTTP_ACCEPTED);
                downstreamMessageSent.set(true);

                // if no command timer was created, the request now can be responded
                if (closeLinkAndTimerHandlerRef.get() == null) {
                    ctx.response().end();
                }

                return Future.succeededFuture();

            }).recover(t -> {

                LOG.debug("cannot process message for device [tenantId: {}, deviceId: {}, endpoint: {}]",
                        tenant, deviceId, endpointName, t);

                cancelResponseTimer(closeLinkAndTimerHandlerRef);

                if (ClientErrorException.class.isInstance(t)) {
                    final ClientErrorException e = (ClientErrorException) t;
                    ctx.fail(e.getErrorCode());
                } else {
                    metrics.incrementUndeliverableHttpMessages(endpointName, tenant);
                    HttpUtils.serviceUnavailable(ctx, 2);
                }
                return Future.failedFuture(t);
            });
        }
    }
}

From source file:org.apache.tinkerpop.gremlin.structure.IoTest.java

@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_DOUBLE_VALUES)
public void shouldReadWriteDetachedEdgeAsReferenceToGraphSON() throws Exception {
    final Vertex v1 = g.addVertex(T.label, "person");
    final Vertex v2 = g.addVertex(T.label, "person");
    final Edge e = DetachedFactory.detach(v1.addEdge("friend", v2, "weight", 0.5f, "acl", "rw"), false);

    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        final GraphSONWriter writer = g.io().graphSONWriter().create();
        writer.writeEdge(os, e);/* w ww. j a  va  2s.c om*/

        final AtomicBoolean called = new AtomicBoolean(false);
        final GraphSONReader reader = g.io().graphSONReader().create();
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            reader.readEdge(bais, detachedEdge -> {
                assertEquals(e.id().toString(), detachedEdge.id().toString()); // lossy
                assertEquals(v1.id().toString(),
                        detachedEdge.iterators().vertexIterator(Direction.OUT).next().id().toString()); // lossy
                assertEquals(v2.id().toString(),
                        detachedEdge.iterators().vertexIterator(Direction.IN).next().id().toString()); // lossy
                assertEquals(v1.label(), detachedEdge.iterators().vertexIterator(Direction.OUT).next().label());
                assertEquals(v2.label(), detachedEdge.iterators().vertexIterator(Direction.IN).next().label());
                assertEquals(e.label(), detachedEdge.label());
                assertEquals(e.keys().size(),
                        StreamFactory.stream(detachedEdge.iterators().propertyIterator()).count());
                called.set(true);
                return null;
            });
        }

        assertTrue(called.get());
    }
}

From source file:divconq.tool.release.Main.java

@Override
public void run(Scanner scan, ApiSession api) {
    Path relpath = null;//from  w w w  .j  a  v a  2 s. c o m
    Path gitpath = null;
    Path wikigitpath = null;

    XElement fldset = Hub.instance.getConfig().selectFirst("CommandLine/Settings");

    if (fldset != null) {
        relpath = Paths.get(fldset.getAttribute("ReleasePath"));
        gitpath = Paths.get(fldset.getAttribute("GitPath"));
        wikigitpath = Paths.get(fldset.getAttribute("WikiGitPath"));
    }

    boolean running = true;

    while (running) {
        try {
            System.out.println();
            System.out.println("-----------------------------------------------");
            System.out.println("   Release Builder Menu");
            System.out.println("-----------------------------------------------");
            System.out.println("0)  Exit");

            if (relpath != null)
                System.out.println("1)  Build release package from Settings File");

            System.out.println("2)  Build custom release package [under construction]");

            System.out.println("4)  Pack the .jar files");

            if (gitpath != null)
                System.out.println("5)  Copy Source to GitHub folder");

            System.out.println("6)  Update AWWW");

            String opt = scan.nextLine();

            Long mopt = StringUtil.parseInt(opt);

            if (mopt == null)
                continue;

            switch (mopt.intValue()) {
            case 0:
                running = false;
                break;

            case 1: {
                ReleasesHelper releases = new ReleasesHelper();

                if (!releases.init(relpath))
                    break;

                System.out.println("Select a release to build");
                System.out.println("0) None");

                List<String> rnames = releases.names();

                for (int i = 0; i < rnames.size(); i++)
                    System.out.println((i + 1) + ") " + rnames.get(i));

                System.out.println("Option #: ");
                opt = scan.nextLine();

                mopt = StringUtil.parseInt(opt);

                if ((mopt == null) || (mopt == 0))
                    break;

                XElement relchoice = releases.get(mopt.intValue() - 1);

                if (relchoice == null) {
                    System.out.println("Invalid option");
                    break;
                }

                PackagesHelper availpackages = new PackagesHelper();
                availpackages.init();

                InstallHelper inst = new InstallHelper();
                if (!inst.init(availpackages, relchoice))
                    break;

                XElement prindesc = availpackages.get(inst.prinpackage);

                XElement prininst = prindesc.find("Install");

                if (prininst == null) {
                    System.out.println("Principle package: " + inst.prinpackagenm
                            + " cannot be released directly, it must be part of another package.");
                    break;
                }

                String relvers = prindesc.getAttribute("Version");

                System.out.println("Building release version " + relvers);

                if (prindesc.hasAttribute("LastVersion"))
                    System.out.println("Previous release version " + prindesc.getAttribute("LastVersion"));

                String rname = relchoice.getAttribute("Name");
                Path destpath = relpath.resolve(rname + "/" + rname + "-" + relvers + "-bin.zip");

                if (Files.exists(destpath)) {
                    System.out.println("Version " + relvers + " already exists, overwrite? (y/n): ");
                    if (!scan.nextLine().toLowerCase().startsWith("y"))
                        break;

                    Files.delete(destpath);
                }

                System.out.println("Preparing zip files");

                AtomicBoolean errored = new AtomicBoolean();
                Path tempfolder = FileUtil.allocateTempFolder2();

                ListStruct ignorepaths = new ListStruct();
                Set<String> nolongerdepends = new HashSet<>();
                Set<String> dependson = new HashSet<>();

                // put all the release files into a temp folder
                inst.instpkgs.forEach(pname -> {
                    availpackages.get(pname).selectAll("DependsOn").stream()
                            .filter(doel -> !doel.hasAttribute("Option")
                                    || inst.relopts.contains(doel.getAttribute("Option")))
                            .forEach(doel -> {
                                // copy all libraries we rely on
                                doel.selectAll("Library").forEach(libel -> {
                                    dependson.add(libel.getAttribute("File"));

                                    Path src = Paths.get("./lib/" + libel.getAttribute("File"));
                                    Path dest = tempfolder.resolve("lib/" + libel.getAttribute("File"));

                                    try {
                                        Files.createDirectories(dest.getParent());

                                        if (Files.notExists(dest))
                                            Files.copy(src, dest, StandardCopyOption.COPY_ATTRIBUTES);
                                    } catch (Exception x) {
                                        errored.set(true);
                                        System.out.println("Unable to copy file: " + src);
                                    }
                                });

                                // copy all files we rely on
                                doel.selectAll("File").forEach(libel -> {
                                    Path src = Paths.get("./" + libel.getAttribute("Path"));
                                    Path dest = tempfolder.resolve(libel.getAttribute("Path"));

                                    try {
                                        Files.createDirectories(dest.getParent());

                                        if (Files.notExists(dest))
                                            Files.copy(src, dest, StandardCopyOption.COPY_ATTRIBUTES);
                                    } catch (Exception x) {
                                        errored.set(true);
                                        System.out.println("Unable to copy file: " + src);
                                    }
                                });

                                // copy all folders we rely on
                                doel.selectAll("Folder").forEach(libel -> {
                                    Path src = Paths.get("./" + libel.getAttribute("Path"));
                                    Path dest = tempfolder.resolve(libel.getAttribute("Path"));

                                    try {
                                        Files.createDirectories(dest.getParent());
                                    } catch (Exception x) {
                                        errored.set(true);
                                        System.out.println("Unable to copy file: " + src);
                                    }

                                    OperationResult cres = FileUtil.copyFileTree(src, dest);

                                    if (cres.hasErrors())
                                        errored.set(true);
                                });
                            });

                    availpackages.get(pname).selectAll("IgnorePaths/Ignore")
                            .forEach(doel -> ignorepaths.addItem(doel.getAttribute("Path")));

                    // NoLongerDependsOn functionally currently only applies to libraries
                    availpackages.get(pname).selectAll("NoLongerDependsOn/Library")
                            .forEach(doel -> nolongerdepends.add(doel.getAttribute("File")));

                    // copy the released packages folders
                    Path src = Paths.get("./packages/" + pname);
                    Path dest = tempfolder.resolve("packages/" + pname);

                    try {
                        Files.createDirectories(dest.getParent());
                    } catch (Exception x) {
                        errored.set(true);
                        System.out.println("Unable to copy file: " + src);
                    }

                    // we may wish to enhance filter to allow .JAR sometimes, but this is meant to prevent copying of packages/pname/lib/abc.lib.jar files 
                    OperationResult cres = FileUtil.copyFileTree(src, dest,
                            path -> !path.toString().endsWith(".jar"));

                    if (cres.hasErrors())
                        errored.set(true);

                    // copy the released packages libraries
                    Path libsrc = Paths.get("./packages/" + pname + "/lib");
                    Path libdest = tempfolder.resolve("lib");

                    if (Files.exists(libsrc)) {
                        cres = FileUtil.copyFileTree(libsrc, libdest);

                        if (cres.hasErrors())
                            errored.set(true);
                    }
                });

                if (errored.get()) {
                    System.out.println("Error with assembling package");
                    break;
                }

                // copy the principle config
                Path csrc = Paths.get("./packages/" + inst.prinpackage + "/config");
                Path cdest = tempfolder.resolve("config/" + inst.prinpackagenm);

                if (Files.exists(csrc)) {
                    Files.createDirectories(cdest);

                    OperationResult cres = FileUtil.copyFileTree(csrc, cdest);

                    if (cres.hasErrors()) {
                        System.out.println("Error with prepping config");
                        break;
                    }
                }

                boolean configpassed = true;

                // copy packages with config = true
                for (XElement pkg : relchoice.selectAll("Package")) {
                    if (!"true".equals(pkg.getAttribute("Config")))
                        break;

                    String pname = pkg.getAttribute("Name");

                    int pspos = pname.lastIndexOf('/');
                    String pnm = (pspos != -1) ? pname.substring(pspos + 1) : pname;

                    csrc = Paths.get("./packages/" + pname + "/config");
                    cdest = tempfolder.resolve("config/" + pnm);

                    if (Files.exists(csrc)) {
                        Files.createDirectories(cdest);

                        OperationResult cres = FileUtil.copyFileTree(csrc, cdest);

                        if (cres.hasErrors()) {
                            System.out.println("Error with prepping extra config");
                            configpassed = false;
                            break;
                        }
                    }
                }

                if (!configpassed)
                    break;

                // also copy installer config if being used
                if (inst.includeinstaller) {
                    csrc = Paths.get("./packages/dc/dcInstall/config");
                    cdest = tempfolder.resolve("config/dcInstall");

                    if (Files.exists(csrc)) {
                        Files.createDirectories(cdest);

                        OperationResult cres = FileUtil.copyFileTree(csrc, cdest);

                        if (cres.hasErrors()) {
                            System.out.println("Error with prepping install config");
                            break;
                        }
                    }
                }

                // write out the deployed file
                RecordStruct deployed = new RecordStruct();

                deployed.setField("Version", relvers);
                deployed.setField("PackageFolder", relpath.resolve(rname));
                deployed.setField("PackagePrefix", rname);

                OperationResult d1res = IOUtil.saveEntireFile(tempfolder.resolve("config/deployed.json"),
                        deployed.toPrettyString());

                if (d1res.hasErrors()) {
                    System.out.println("Error with prepping deployed");
                    break;
                }

                RecordStruct deployment = new RecordStruct();

                deployment.setField("Version", relvers);

                if (prindesc.hasAttribute("LastVersion"))
                    deployment.setField("DependsOn", prindesc.getAttribute("LastVersion"));

                deployment.setField("UpdateMessage",
                        "This update is complete, you may accept this update as runnable.");

                nolongerdepends.removeAll(dependson);

                ListStruct deletefiles = new ListStruct();

                nolongerdepends.forEach(fname -> deletefiles.addItem("lib/" + fname));

                deployment.setField("DeleteFiles", deletefiles);
                deployment.setField("IgnorePaths", ignorepaths);

                d1res = IOUtil.saveEntireFile(tempfolder.resolve("deployment.json"),
                        deployment.toPrettyString());

                if (d1res.hasErrors()) {
                    System.out.println("Error with prepping deployment");
                    break;
                }

                // write env file
                d1res = IOUtil.saveEntireFile(tempfolder.resolve("env.bat"), "set mem="
                        + relchoice.getAttribute("Memory", "2048") + "\r\n" + "SET project="
                        + inst.prinpackagenm + "\r\n" + "SET service="
                        + relchoice.getAttribute("Service", inst.prinpackagenm) + "\r\n" + "SET servicename="
                        + relchoice.getAttribute("ServiceName", inst.prinpackagenm + " Service") + "\r\n");

                if (d1res.hasErrors()) {
                    System.out.println("Error with prepping env");
                    break;
                }

                System.out.println("Packing Release file.");

                Path relbin = relpath.resolve(rname + "/" + rname + "-" + relvers + "-bin.zip");

                if (Files.notExists(relbin.getParent()))
                    Files.createDirectories(relbin.getParent());

                ZipArchiveOutputStream zipout = new ZipArchiveOutputStream(relbin.toFile());

                try {
                    Files.walkFileTree(tempfolder, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                            new SimpleFileVisitor<Path>() {
                                @Override
                                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                                        throws IOException {
                                    ZipArchiveEntry entry = new ZipArchiveEntry(
                                            tempfolder.relativize(file).toString());
                                    entry.setSize(Files.size(file));
                                    zipout.putArchiveEntry(entry);
                                    zipout.write(Files.readAllBytes(file));
                                    zipout.closeArchiveEntry();

                                    return FileVisitResult.CONTINUE;
                                }
                            });
                } catch (IOException x) {
                    System.out.println("Error building zip: " + x);
                }

                zipout.close();

                System.out.println("Release file written");

                FileUtil.deleteDirectory(tempfolder);

                break;
            } // end case 1

            case 3: {
                System.out.println("Note these utilities are only good from the main console,");
                System.out.println("if you are using a remote connection then the encryption will");
                System.out.println("not work as expected.  [we do not have access the master keys]");
                System.out.println();

                Foreground.utilityMenu(scan);

                break;
            }

            case 4: {
                System.out.println("Packing jar library files.");

                String[] packlist = new String[] { "divconq.core", "divconq.interchange", "divconq.web",
                        "divconq.tasks", "divconq.tasks.api", "ncc.uploader.api", "ncc.uploader.core",
                        "ncc.workflow", "sd.core" };

                String[] packnames = new String[] { "dcCore", "dcInterchange", "dcWeb", "dcTasks", "dcTasksApi",
                        "nccUploaderApi", "nccUploader", "nccWorkflow", "sd/sdBackend" };

                for (int i = 0; i < packlist.length; i++) {
                    String lib = packlist[i];
                    String pname = packnames[i];

                    Path relbin = Paths.get("./ext/" + lib + ".jar");
                    Path srcbin = Paths.get("./" + lib + "/bin");
                    Path packbin = Paths.get("./packages/" + pname + "/lib/" + lib + ".jar");

                    if (Files.notExists(relbin.getParent()))
                        Files.createDirectories(relbin.getParent());

                    Files.deleteIfExists(relbin);

                    ZipArchiveOutputStream zipout = new ZipArchiveOutputStream(relbin.toFile());

                    try {
                        Files.walkFileTree(srcbin, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                                new SimpleFileVisitor<Path>() {
                                    @Override
                                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                                            throws IOException {
                                        ZipArchiveEntry entry = new ZipArchiveEntry(
                                                srcbin.relativize(file).toString());
                                        entry.setSize(Files.size(file));
                                        zipout.putArchiveEntry(entry);
                                        zipout.write(Files.readAllBytes(file));
                                        zipout.closeArchiveEntry();

                                        return FileVisitResult.CONTINUE;
                                    }
                                });
                    } catch (IOException x) {
                        System.out.println("Error building zip: " + x);
                    }

                    zipout.close();

                    Files.copy(relbin, packbin, StandardCopyOption.REPLACE_EXISTING);
                }

                System.out.println("Done");

                break;
            }

            case 5: {
                System.out.println("Copying Source Files");

                System.out.println("Cleaning folders");

                OperationResult or = FileUtil.deleteDirectory(gitpath.resolve("divconq.core/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error deleting files");
                    break;
                }

                or = FileUtil.deleteDirectory(gitpath.resolve("divconq.core/src/main/resources"));

                if (or.hasErrors()) {
                    System.out.println("Error deleting files");
                    break;
                }

                or = FileUtil.deleteDirectory(gitpath.resolve("divconq.interchange/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error deleting files");
                    break;
                }

                or = FileUtil.deleteDirectory(gitpath.resolve("divconq.tasks/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error deleting files");
                    break;
                }

                or = FileUtil.deleteDirectory(gitpath.resolve("divconq.tasks.api/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error deleting files");
                    break;
                }

                or = FileUtil.deleteDirectory(gitpath.resolve("divconq.web/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error deleting files");
                    break;
                }

                or = FileUtil.deleteDirectory(gitpath.resolve("packages"));

                if (or.hasErrors()) {
                    System.out.println("Error deleting files");
                    break;
                }

                or = FileUtil.deleteDirectoryContent(wikigitpath, ".git");

                if (or.hasErrors()) {
                    System.out.println("Error deleting wiki files");
                    break;
                }

                System.out.println("Copying folders");

                System.out.println("Copy tree ./divconq.core/src");

                or = FileUtil.copyFileTree(Paths.get("./divconq.core/src/divconq"),
                        gitpath.resolve("divconq.core/src/main/java/divconq"), new Predicate<Path>() {
                            @Override
                            public boolean test(Path file) {
                                return file.getFileName().toString().endsWith(".java");
                            }
                        });

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                or = FileUtil.copyFileTree(Paths.get("./divconq.core/src/org"),
                        gitpath.resolve("divconq.core/src/main/java/org"), new Predicate<Path>() {
                            @Override
                            public boolean test(Path file) {
                                return file.getFileName().toString().endsWith(".java");
                            }
                        });

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                or = FileUtil.copyFileTree(Paths.get("./divconq.core/src/localize"),
                        gitpath.resolve("divconq.core/src/main/resources/localize"), new Predicate<Path>() {
                            @Override
                            public boolean test(Path file) {
                                return file.getFileName().toString().endsWith(".xml");
                            }
                        });

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./divconq.interchange/src");

                or = FileUtil.copyFileTree(Paths.get("./divconq.interchange/src"),
                        gitpath.resolve("divconq.interchange/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./divconq.tasks/src");

                or = FileUtil.copyFileTree(Paths.get("./divconq.tasks/src"),
                        gitpath.resolve("divconq.tasks/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./divconq.tasks.api/src");

                or = FileUtil.copyFileTree(Paths.get("./divconq.tasks.api/src"),
                        gitpath.resolve("divconq.tasks.api/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./divconq.web/src");

                or = FileUtil.copyFileTree(Paths.get("./divconq.web/src"),
                        gitpath.resolve("divconq.web/src/main/java"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcCore");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcCore"), gitpath.resolve("packages/dcCore"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcCorePublic");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcCorePublic"),
                        gitpath.resolve("packages/dcCorePublic"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcInterchange");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcInterchange"),
                        gitpath.resolve("packages/dcInterchange"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcTasks");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcTasks"),
                        gitpath.resolve("packages/dcTasks"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcTasksApi");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcTasksApi"),
                        gitpath.resolve("packages/dcTasksApi"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcTasksWeb");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcTasksWeb"),
                        gitpath.resolve("packages/dcTasksWeb"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcTest");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcTest"), gitpath.resolve("packages/dcTest"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./packages/dcWeb");

                or = FileUtil.copyFileTree(Paths.get("./packages/dcWeb"), gitpath.resolve("packages/dcWeb"));

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copy tree ./divconq.wiki/public");

                or = FileUtil.copyFileTree(Paths.get("./divconq.wiki/public"), wikigitpath);

                if (or.hasErrors()) {
                    System.out.println("Error copying files");
                    break;
                }

                System.out.println("Copying files");

                Files.copy(Paths.get("./README.md"), gitpath.resolve("README.md"),
                        StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
                Files.copy(Paths.get("./RELEASE_NOTES.md"), gitpath.resolve("RELEASE_NOTES.md"),
                        StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
                Files.copy(Paths.get("./NOTICE.txt"), gitpath.resolve("NOTICE.txt"),
                        StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
                Files.copy(Paths.get("./LICENSE.txt"), gitpath.resolve("LICENSE.txt"),
                        StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);

                System.out.println("Done");

                break;
            }
            case 6: {
                System.out.println("Are you sure you want to update AWWW Server? (y/n): ");
                if (!scan.nextLine().toLowerCase().startsWith("y"))
                    break;

                ReleasesHelper releases = new ReleasesHelper();
                if (!releases.init(relpath))
                    break;

                XElement relchoice = releases.get("AWWWServer");

                if (relchoice == null) {
                    System.out.println("Invalid option");
                    break;
                }

                PackagesHelper availpackages = new PackagesHelper();
                availpackages.init();

                InstallHelper inst = new InstallHelper();
                if (!inst.init(availpackages, relchoice))
                    break;

                ServerHelper ssh = new ServerHelper();
                if (!ssh.init(relchoice.find("SSH")))
                    break;

                ChannelSftp sftp = null;

                try {
                    Channel channel = ssh.session().openChannel("sftp");
                    channel.connect();
                    sftp = (ChannelSftp) channel;

                    // go to routines folder
                    sftp.cd("/usr/local/bin/dc/AWWWServer");

                    FileRepositoryBuilder builder = new FileRepositoryBuilder();

                    Repository repository = builder.setGitDir(new File(".git")).findGitDir() // scan up the file system tree
                            .build();

                    String lastsync = releases.getData("AWWWServer").getFieldAsString("LastCommitSync");

                    RevWalk rw = new RevWalk(repository);
                    ObjectId head1 = repository.resolve(Constants.HEAD);
                    RevCommit commit1 = rw.parseCommit(head1);

                    releases.getData("AWWWServer").setField("LastCommitSync", head1.name());

                    ObjectId rev2 = repository.resolve(lastsync);
                    RevCommit parent = rw.parseCommit(rev2);
                    //RevCommit parent2 = rw.parseCommit(parent.getParent(0).getId());

                    DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
                    df.setRepository(repository);
                    df.setDiffComparator(RawTextComparator.DEFAULT);
                    df.setDetectRenames(true);

                    // list oldest first or change types are all wrong!!
                    List<DiffEntry> diffs = df.scan(parent.getTree(), commit1.getTree());

                    for (DiffEntry diff : diffs) {
                        String gnpath = diff.getNewPath();
                        String gopath = diff.getOldPath();

                        Path npath = Paths.get("./" + gnpath);
                        Path opath = Paths.get("./" + gopath);

                        if (diff.getChangeType() == ChangeType.DELETE) {
                            if (inst.containsPathExtended(opath)) {
                                System.out.println("- " + diff.getChangeType().name() + " - " + opath);

                                try {
                                    sftp.rm(opath.toString());
                                    System.out.println("deleted!!");
                                } catch (SftpException x) {
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                    System.out.println("Sftp Error: " + x);
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                }
                            } else {
                                System.out.println("/ " + diff.getChangeType().name() + " - " + gopath
                                        + " !!!!!!!!!!!!!!!!!!!!!!!!!");
                            }
                        } else if ((diff.getChangeType() == ChangeType.ADD)
                                || (diff.getChangeType() == ChangeType.MODIFY)
                                || (diff.getChangeType() == ChangeType.COPY)) {
                            if (inst.containsPathExtended(npath)) {
                                System.out.println("+ " + diff.getChangeType().name() + " - " + npath);

                                try {
                                    ssh.makeDirSftp(sftp, npath.getParent());

                                    sftp.put(npath.toString(), npath.toString(), ChannelSftp.OVERWRITE);
                                    sftp.chmod(npath.endsWith(".sh") ? 484 : 420, npath.toString()); // 644 octal = 420 dec, 744 octal = 484 dec
                                    System.out.println("uploaded!!");
                                } catch (SftpException x) {
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                    System.out.println("Sftp Error: " + x);
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                }
                            } else {
                                System.out.println("> " + diff.getChangeType().name() + " - " + gnpath
                                        + " !!!!!!!!!!!!!!!!!!!!!!!!!");
                            }
                        } else if (diff.getChangeType() == ChangeType.RENAME) {
                            // remove the old
                            if (inst.containsPathExtended(opath)) {
                                System.out.println("- " + diff.getChangeType().name() + " - " + opath);

                                try {
                                    sftp.rm(opath.toString());
                                    System.out.println("deleted!!");
                                } catch (SftpException x) {
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                    System.out.println("Sftp Error: " + x);
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                }
                            } else {
                                System.out.println("/ " + diff.getChangeType().name() + " - " + gopath
                                        + " !!!!!!!!!!!!!!!!!!!!!!!!!");
                            }

                            // add the new path
                            if (inst.containsPathExtended(npath)) {
                                System.out.println("+ " + diff.getChangeType().name() + " - " + npath);

                                try {
                                    ssh.makeDirSftp(sftp, npath.getParent());

                                    sftp.put(npath.toString(), npath.toString(), ChannelSftp.OVERWRITE);
                                    sftp.chmod(npath.endsWith(".sh") ? 484 : 420, npath.toString()); // 644 octal = 420 dec, 744 octal = 484 dec
                                    System.out.println("uploaded!!");
                                } catch (SftpException x) {
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                    System.out.println("Sftp Error: " + x);
                                    System.out.println(
                                            " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                                }
                            } else {
                                System.out.println("> " + diff.getChangeType().name() + " - " + gnpath
                                        + " !!!!!!!!!!!!!!!!!!!!!!!!!");
                            }
                        } else {
                            System.out.println("??????????????????????????????????????????????????????????");
                            System.out.println(": " + diff.getChangeType().name() + " - " + gnpath
                                    + " ?????????????????????????");
                            System.out.println("??????????????????????????????????????????????????????????");
                        }
                    }

                    rw.dispose();

                    repository.close();

                    releases.saveData();
                } catch (JSchException x) {
                    System.out.println("Sftp Error: " + x);
                } finally {
                    if (sftp.isConnected())
                        sftp.exit();

                    ssh.close();
                }

                break;
            }

            case 7: {
                Path sfolder = Paths.get("/Work/Projects/awww-current/dairy-graze/poly");
                Path dfolder = Paths.get("/Work/Projects/awww-current/dairy-graze/poly-js");

                Files.list(sfolder).forEach(file -> {
                    String fname = file.getFileName().toString();

                    if (!fname.endsWith(".xml"))
                        return;

                    FuncResult<XElement> lres = XmlReader.loadFile(file, false);

                    if (lres.isEmptyResult()) {
                        System.out.println("Unable to parse: " + file);
                        return;
                    }

                    String zc = fname.substring(5, 8);
                    String code = "zipsData['" + zc + "'] = ";
                    XElement root = lres.getResult();

                    /*
                    <polyline1 lng="-90.620897" lat="45.377447"/>
                    <polyline1 lng="-90.619327" lat="45.3805"/>
                            
                                   [-71.196845,41.67757],[-71.120168,41.496831],[-71.317338,41.474923],[-71.196845,41.67757]
                     */
                    ListStruct center = new ListStruct();
                    ListStruct cords = new ListStruct();
                    ListStruct currentPoly = null;
                    //String currentName = null;

                    for (XElement child : root.selectAll("*")) {
                        String cname = child.getName();

                        if (cname.startsWith("marker")) {
                            // not always accurate
                            if (center.isEmpty())
                                center.addItem(Struct.objectToDecimal(child.getAttribute("lng")),
                                        Struct.objectToDecimal(child.getAttribute("lat")));

                            currentPoly = new ListStruct();
                            cords.addItem(new ListStruct(currentPoly));

                            continue;
                        }

                        /*
                        if (cname.startsWith("info")) {
                           System.out.println("areas: " + child.getAttribute("areas"));
                           continue;
                        }
                        */

                        if (!cname.startsWith("polyline"))
                            continue;

                        if (currentPoly == null) {
                            //if (!cname.equals(currentName)) {
                            //if (currentName == null) {
                            //   currentName = cname;

                            //   System.out.println("new poly: " + cname);

                            currentPoly = new ListStruct();
                            cords.addItem(new ListStruct(currentPoly));
                        }

                        currentPoly.addItem(new ListStruct(Struct.objectToDecimal(child.getAttribute("lng")),
                                Struct.objectToDecimal(child.getAttribute("lat"))));
                    }

                    RecordStruct feat = new RecordStruct().withField("type", "Feature")
                            .withField("id", "zip" + zc)
                            .withField("properties",
                                    new RecordStruct().withField("name", "Prefix " + zc).withField("alias", zc))
                            .withField("geometry", new RecordStruct().withField("type", "MultiPolygon")
                                    .withField("coordinates", cords));

                    RecordStruct entry = new RecordStruct().withField("code", zc).withField("geo", feat)
                            .withField("center", center);

                    IOUtil.saveEntireFile2(dfolder.resolve("us-zips-" + zc + ".js"),
                            code + entry.toPrettyString() + ";");
                });

                break;
            }

            }
        } catch (Exception x) {
            System.out.println("CLI error: " + x);
        }
    }
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.internal.localworkspace.LocalDataAccessLayer.java

private static boolean reconcileLocalWorkspaceHelper(final Workspace workspace,
        final WebServiceLayer webServiceLayer, final boolean unscannedReconcile,
        final boolean reconcileMissingFromDisk, final AtomicReference<Failure[]> failures,
        final AtomicBoolean pendingChangesUpdatedByServer) {
    Check.notNull(workspace, "workspace"); //$NON-NLS-1$
    pendingChangesUpdatedByServer.set(false);
    final List<PendingChange> convertedAdds = new ArrayList<PendingChange>();

    final boolean throwOnProjectRenamed;
    if (EnvironmentVariables.isDefined(EnvironmentVariables.DD_SUITES_PROJECT_RENAME_UNPATCHED_CLIENT)) {
        throwOnProjectRenamed = false;//from  w w w.  ja  v  a 2 s .  c o m
    } else {
        throwOnProjectRenamed = true;
    }

    final AtomicReference<GUID> serverPendingChangeSignature = new AtomicReference<GUID>(GUID.EMPTY);

    // No optimization away of reconciles when sending up MissingFromDisk
    // rows, since the bit in the header (lvHeader.PendingReconcile) may be
    // false when we actually have work to do (there are rows in the table
    // marked MissingFromDisk).
    if ((unscannedReconcile || !workspace.getWorkspaceWatcher().isScanNecessary())
            && !reconcileMissingFromDisk) {
        // Pre-reconcile
        final AtomicBoolean hasPendingLocalVersionRows = new AtomicBoolean(true);
        LocalWorkspaceTransaction transaction = new LocalWorkspaceTransaction(workspace);

        try {
            transaction.execute(new LocalVersionHeaderTransaction() {
                @Override
                public void invoke(final WorkspaceVersionTableHeader lvh) {
                    hasPendingLocalVersionRows.set(lvh.getPendingReconcile());
                }
            });
        } finally {
            try {
                transaction.close();
            } catch (final IOException e) {
                throw new VersionControlException(e);
            }
        }

        final AtomicReference<GUID> clientPendingChangeSignature = new AtomicReference<GUID>(GUID.EMPTY);

        if (!hasPendingLocalVersionRows.get()) {
            transaction = new LocalWorkspaceTransaction(workspace);
            try {
                transaction.execute(new PendingChangesHeaderTransaction() {
                    @Override
                    public void invoke(final LocalPendingChangesTableHeader pch) {
                        clientPendingChangeSignature.set(pch.getClientSignature());

                    }
                });
            } finally {
                try {
                    transaction.close();
                } catch (final IOException e) {
                    throw new VersionControlException(e);
                }
            }

            final GUID lastServerPendingChangeGuid = workspace.getOfflineCacheData()
                    .getLastServerPendingChangeSignature();
            final Calendar lastReconcileTime = workspace.getOfflineCacheData().getLastReconcileTime();
            lastReconcileTime.add(Calendar.SECOND, 8);

            if (lastServerPendingChangeGuid != GUID.EMPTY
                    && clientPendingChangeSignature.get().equals(lastServerPendingChangeGuid)
                    && lastReconcileTime.after(Calendar.getInstance())) {
                // This reconcile was optimized away with no server call.

                failures.set(new Failure[0]);
                return false;
            }

            serverPendingChangeSignature.set(
                    webServiceLayer.queryPendingChangeSignature(workspace.getName(), workspace.getOwnerName()));

            if (serverPendingChangeSignature.get() != GUID.EMPTY
                    && clientPendingChangeSignature.get().equals(serverPendingChangeSignature)) {
                // This reconcile was optimized away.

                workspace.getOfflineCacheData()
                        .setLastServerPendingChangeSignature(serverPendingChangeSignature.get());
                workspace.getOfflineCacheData().setLastReconcileTime(Calendar.getInstance());

                failures.set(new Failure[0]);
                return false;
            }
        }
    }

    final AtomicBoolean toReturn = new AtomicBoolean(true);

    final LocalWorkspaceTransaction transaction = new LocalWorkspaceTransaction(workspace);
    try {
        final AtomicReference<Failure[]> delegateFailures = new AtomicReference<Failure[]>(new Failure[0]);
        final AtomicBoolean delegatePCUpdated = new AtomicBoolean(false);

        transaction.execute(new AllTablesTransaction() {
            @Override
            public void invoke(final LocalWorkspaceProperties wp, final WorkspaceVersionTable lv,
                    final LocalPendingChangesTable pc) {
                if (!unscannedReconcile) {
                    // The line below has been commented out because we
                    // decided not to force a full scan here, because it
                    // causes significant degradation in UI performance.
                    //
                    // workspace.getWorkspaceWatcher().markPathChanged(null);
                    //
                    // It was an attempt to fix the bug:
                    // Bug 6191: When using local workspaces, get latest
                    // does not get a file that has been deleted from disk.
                    //
                    // The customer has to explicitly invoke
                    // Pending Changes>Actions>Detect local changes
                    // in Team Explorer.
                    //
                    // Note that none of customers reported that as an
                    // issue.
                    // It was detected on our tests only.
                    workspace.getWorkspaceWatcher().scan(wp, lv, pc);
                }

                // Pre-reconcile
                if (!lv.getPendingReconcile() && !reconcileMissingFromDisk
                        && GUID.EMPTY == serverPendingChangeSignature.get()) {
                    serverPendingChangeSignature.set(webServiceLayer
                            .queryPendingChangeSignature(workspace.getName(), workspace.getOwnerName()));

                    if (serverPendingChangeSignature.get() != GUID.EMPTY
                            && pc.getClientSignature().equals(serverPendingChangeSignature.get())) {
                        // This reconcile was optimized away.
                        delegateFailures.set(new Failure[0]);

                        workspace.getOfflineCacheData()
                                .setLastServerPendingChangeSignature(serverPendingChangeSignature.get());
                        workspace.getOfflineCacheData().setLastReconcileTime(Calendar.getInstance());

                        toReturn.set(true);

                        return;
                    }
                }

                // Acknowledgment of team project renames, if any have been
                // completed
                if (wp.getNewProjectRevisionId() > 0) {
                    webServiceLayer.promotePendingWorkspaceMappings(workspace.getName(),
                            workspace.getOwnerName(), wp.getNewProjectRevisionId());

                    wp.setNewProjectRevisionId(0);
                }

                final LocalPendingChange[] pendingChanges = pc.queryByTargetServerItem(ServerPath.ROOT,
                        RecursionType.FULL, null);

                /*
                 * TEE-specific Code
                 *
                 * In order to support offline property changes, which
                 * cannot be reconciled with
                 * WebServiceLayer.reconcileLocalWorkspace (the property
                 * values can't be sent), we have to pull out the pended
                 * property changes and send them to the server before
                 * reconciling.
                 */
                final List<ChangeRequest> propertyRequests = new ArrayList<ChangeRequest>();

                for (final LocalPendingChange lpc : pendingChanges) {
                    if (lpc.getChangeType().contains(ChangeType.PROPERTY)) {
                        final PropertyValue[] pv = lpc.getPropertyValues();
                        final String serverItem = lpc.getTargetServerItem();

                        if (pv != null && pv.length > 0 && serverItem != null) {
                            final ChangeRequest request = new ChangeRequest(
                                    new ItemSpec(serverItem, RecursionType.NONE),
                                    new WorkspaceVersionSpec(workspace), RequestType.PROPERTY, ItemType.ANY,
                                    VersionControlConstants.ENCODING_UNCHANGED, LockLevel.UNCHANGED, 0, null,
                                    false);

                            request.setProperties(pv);

                            propertyRequests.add(request);
                        }
                    }
                }

                if (propertyRequests.size() > 0) {
                    ((WebServiceLayerLocalWorkspaces) webServiceLayer).pendChangesInLocalWorkspace(
                            workspace.getName(), workspace.getOwnerName(),
                            propertyRequests.toArray(new ChangeRequest[propertyRequests.size()]),
                            PendChangesOptions.NONE, SupportedFeatures.ALL, new AtomicReference<Failure[]>(),
                            null, null, new AtomicBoolean(), new AtomicReference<ChangePendedFlags>());

                    // TODO handle failures?
                }

                // Back to normal, non-TEE behavior

                final AtomicBoolean outClearLocalVersionTable = new AtomicBoolean();
                final ServerItemLocalVersionUpdate[] lvUpdates = lv.getUpdatesForReconcile(pendingChanges,
                        reconcileMissingFromDisk, outClearLocalVersionTable);

                ReconcileResult result = webServiceLayer.reconcileLocalWorkspace(workspace.getName(),
                        workspace.getOwnerName(), pc.getClientSignature(), pendingChanges, lvUpdates,
                        outClearLocalVersionTable.get(), throwOnProjectRenamed);

                // report any failures
                Failure[] reconcileFailures = result.getFailures();
                workspace.getClient().reportFailures(workspace, reconcileFailures);

                if (reconcileFailures.length > 0) {
                    throw new ReconcileFailedException(reconcileFailures);
                }

                GUID newSignature = new GUID(result.getNewSignature());
                PendingChange[] newPendingChanges = result.getNewPendingChanges();

                // If the local version rows for this local workspace have
                // been purged from the server, then the server will set
                // this flag on the result of the next reconcile.
                if (result.isReplayLocalVersionsRequired()) {
                    // Reconcile a second time. This time, set the
                    // clearLocalVersionTable flag. This way, we know
                    // we have cleared out any lingering local version rows
                    // for this workspace.
                    if (!outClearLocalVersionTable.get()) {
                        result = webServiceLayer.reconcileLocalWorkspace(workspace.getName(),
                                workspace.getOwnerName(), pc.getClientSignature(), pendingChanges, lvUpdates,
                                true /* clearLocalVersionTable */, throwOnProjectRenamed);

                        // Report any failures
                        reconcileFailures = result.getFailures();
                        workspace.getClient().reportFailures(workspace, reconcileFailures);

                        if (reconcileFailures.length > 0) {
                            throw new ReconcileFailedException(reconcileFailures);
                        }

                        // Grab the new signature and new pending changes
                        newSignature = new GUID(result.getNewSignature());
                        newPendingChanges = result.getNewPendingChanges();
                    }

                    // Now, go through the local version table and replay
                    // every row that we have.
                    final List<ServerItemLocalVersionUpdate> replayUpdates = new ArrayList<ServerItemLocalVersionUpdate>(
                            Math.min(lv.getLocalItemsCount(), 1000));

                    for (final WorkspaceLocalItem lvEntry : lv.queryByServerItem(ServerPath.ROOT,
                            RecursionType.FULL, null, true /* includeDeleted */)) {
                        final ServerItemLocalVersionUpdate replayUpdate = lvEntry
                                .getLocalVersionUpdate(reconcileMissingFromDisk, true /* force */);

                        if (replayUpdate != null) {
                            replayUpdates.add(replayUpdate);

                            // Batch these updates in groups of 1000 items.
                            if (replayUpdates.size() >= 1000) {
                                webServiceLayer.updateLocalVersion(workspace.getName(),
                                        workspace.getOwnerName(), replayUpdates.toArray(
                                                new ServerItemLocalVersionUpdate[replayUpdates.size()]));

                                replayUpdates.clear();
                            }
                        }
                    }

                    if (replayUpdates.size() > 0) {
                        webServiceLayer.updateLocalVersion(workspace.getName(), workspace.getOwnerName(),
                                replayUpdates.toArray(new ServerItemLocalVersionUpdate[replayUpdates.size()]));
                    }
                }

                if (result.isPendingChangesUpdated()) {
                    delegatePCUpdated.set(true);

                    final Map<String, ItemType> newPendingDeletes = new TreeMap<String, ItemType>(
                            String.CASE_INSENSITIVE_ORDER);

                    for (final PendingChange pendingChange : newPendingChanges) {
                        if (pendingChange.isAdd()) {
                            final LocalPendingChange oldPendingChange = pc
                                    .getByTargetServerItem(pendingChange.getServerItem());

                            if (null == oldPendingChange || !oldPendingChange.isAdd()) {
                                // Before calling ReconcileLocalWorkspace,
                                // we did not have a pending add at this
                                // target server item.
                                convertedAdds.add(pendingChange);
                            }
                        } else if (pendingChange.isDelete()) {
                            // If the server removed any of our presented
                            // pending deletes, we want to know about it so
                            // we can get rid of the local version rows that
                            // we have in the deleted state. The server will
                            // remove our pending deletes when the item has
                            // been destroyed on the server.
                            newPendingDeletes.put(
                                    pendingChange.getSourceServerItem() == null ? pendingChange.getServerItem()
                                            : pendingChange.getSourceServerItem(),
                                    pendingChange.getItemType());
                        }
                    }

                    for (final LocalPendingChange oldPendingChange : pc
                            .queryByCommittedServerItem(ServerPath.ROOT, RecursionType.FULL, null)) {
                        if (oldPendingChange.isDelete()
                                && !newPendingDeletes.containsKey(oldPendingChange.getCommittedServerItem())) {
                            // We presented this delete to the server for
                            // Reconcile, but the server removed it from the
                            // pending changes manifest. We need to get rid
                            // of the LV rows for
                            // oldPendingChange.CommittedServerItem since
                            // this item is now destroyed.
                            final List<ServerItemIsCommittedTuple> lvRowsToRemove = new ArrayList<ServerItemIsCommittedTuple>();

                            final RecursionType recursion = oldPendingChange.isRecursiveChange()
                                    ? RecursionType.FULL
                                    : RecursionType.NONE;

                            // Aggregate up the deleted local version
                            // entries at this committed server item
                            // (or below if it's a folder), and we'll remove
                            // them.
                            for (final WorkspaceLocalItem lvEntry : lv.queryByServerItem(
                                    oldPendingChange.getCommittedServerItem(), recursion, null,
                                    true /* includeDeleted */)) {
                                if (lvEntry.isDeleted()) {
                                    lvRowsToRemove.add(new ServerItemIsCommittedTuple(lvEntry.getServerItem(),
                                            lvEntry.isCommitted()));
                                }
                            }

                            for (final ServerItemIsCommittedTuple tuple : lvRowsToRemove) {
                                // We don't need to reconcile the removal of
                                // LV entries marked IsDeleted since they
                                // don't exist on the server anyway.
                                lv.removeByServerItem(tuple.getCommittedServerItem(), tuple.isCommitted(),
                                        false);
                            }
                        }
                    }

                    pc.replacePendingChanges(newPendingChanges);
                }

                // If all we're doing to LV is marking it reconciled, then
                // don't use TxF to commit
                // both tables atomically as this is slower
                if (!lv.isDirty()) {
                    transaction.setAllowTxF(false);
                }

                if (lvUpdates.length > 0) {
                    lv.markAsReconciled(wp, reconcileMissingFromDisk);

                    // If we removed all missing-from-disk items from the
                    // local version table, then we need to remove
                    // the corresponding candidate delete rows for those
                    // items as well.
                    if (reconcileMissingFromDisk) {
                        List<String> candidatesToRemove = null;

                        for (final LocalPendingChange candidateChange : pc
                                .queryCandidatesByTargetServerItem(ServerPath.ROOT, RecursionType.FULL, null)) {
                            if (candidateChange.isDelete()) {
                                if (null == candidatesToRemove) {
                                    candidatesToRemove = new ArrayList<String>();
                                }
                                candidatesToRemove.add(candidateChange.getTargetServerItem());
                            }
                        }

                        if (null != candidatesToRemove) {
                            for (final String candidateDeleteTargetServerItem : candidatesToRemove) {
                                pc.removeCandidateByTargetServerItem(candidateDeleteTargetServerItem);
                            }
                            // Set the candidates changed to true so that it
                            // refreshes the UI
                            LocalWorkspaceTransaction.getCurrent().setRaisePendingChangeCandidatesChanged(true);
                        }
                    }
                }

                newSignature = webServiceLayer.queryPendingChangeSignature(workspace.getName(),
                        workspace.getOwnerName());

                if (!newSignature.equals(pc.getClientSignature())) {
                    pc.setClientSignature(newSignature);
                    workspace.getOfflineCacheData().setLastServerPendingChangeSignature(newSignature);
                }

                if (!newSignature.equals(pc.getClientSignature())) {
                    pc.setClientSignature(newSignature);
                    workspace.getOfflineCacheData().setLastServerPendingChangeSignature(newSignature);
                }

                workspace.getOfflineCacheData().setLastReconcileTime(Calendar.getInstance());
            }
        });

        failures.set(delegateFailures.get());
        pendingChangesUpdatedByServer.set(delegatePCUpdated.get());
    } finally {
        try {
            transaction.close();
        } catch (final IOException e) {
            throw new VersionControlException(e);
        }
    }

    if (convertedAdds.size() > 0) {
        final UpdateLocalVersionQueueOptions options = UpdateLocalVersionQueueOptions.UPDATE_BOTH;
        final UpdateLocalVersionQueue ulvq = new UpdateLocalVersionQueue(workspace, options);

        try {
            for (final PendingChange pc : convertedAdds) {
                // Every item in this list has a ChangeType of Add. As a
                // result they are uncommitted items with no committed hash
                // value, no committed length, and no baseline file GUID.
                ulvq.queueUpdate(new ClientLocalVersionUpdate(pc.getServerItem(), pc.getItemID(),
                        pc.getLocalItem(), 0 /* localVersion */, DotNETDate.MIN_CALENDAR, pc.getEncoding(),
                        null /* committedHashValue */, 0 /* committedLength */, null /* baselineFileGuid */,
                        null /* pendingChangeTargetServerItem */, null /* properties */));
            }
        } finally {
            ulvq.close();
        }
    }

    return toReturn.get();
}

From source file:org.apache.hadoop.yarn.client.api.async.impl.TestAMRMClientAsync.java

@SuppressWarnings("unchecked")
@Test(timeout = 10000)//from  www .  j  a  va 2  s  .c  om
public void testAMRMClientAsync() throws Exception {
    Configuration conf = new Configuration();
    final AtomicBoolean heartbeatBlock = new AtomicBoolean(true);
    List<ContainerStatus> completed1 = Arrays
            .asList(ContainerStatus.newInstance(newContainerId(0, 0, 0, 0), ContainerState.COMPLETE, "", 0));
    List<Container> containers = Arrays.asList(Container.newInstance(null, null, null, null, null, null));
    final AllocateResponse response1 = createAllocateResponse(new ArrayList<ContainerStatus>(), containers,
            null);
    final AllocateResponse response2 = createAllocateResponse(completed1, new ArrayList<Container>(), null);
    final AllocateResponse response3 = createAllocateResponse(new ArrayList<ContainerStatus>(),
            new ArrayList<Container>(), containers, containers, null);
    final AllocateResponse emptyResponse = createAllocateResponse(new ArrayList<ContainerStatus>(),
            new ArrayList<Container>(), null);

    TestCallbackHandler callbackHandler = new TestCallbackHandler();
    final AMRMClient<ContainerRequest> client = mock(AMRMClientImpl.class);
    final AtomicInteger secondHeartbeatSync = new AtomicInteger(0);
    when(client.allocate(anyFloat())).thenReturn(response1).thenAnswer(new Answer<AllocateResponse>() {
        @Override
        public AllocateResponse answer(InvocationOnMock invocation) throws Throwable {
            secondHeartbeatSync.incrementAndGet();
            while (heartbeatBlock.get()) {
                synchronized (heartbeatBlock) {
                    heartbeatBlock.wait();
                }
            }
            secondHeartbeatSync.incrementAndGet();
            return response2;
        }
    }).thenReturn(response3).thenReturn(emptyResponse);
    when(client.registerApplicationMaster(anyString(), anyInt(), anyString())).thenReturn(null);
    when(client.getAvailableResources()).thenAnswer(new Answer<Resource>() {
        @Override
        public Resource answer(InvocationOnMock invocation) throws Throwable {
            // take client lock to simulate behavior of real impl
            synchronized (client) {
                Thread.sleep(10);
            }
            return null;
        }
    });

    AMRMClientAsync<ContainerRequest> asyncClient = AMRMClientAsync.createAMRMClientAsync(client, 20,
            callbackHandler);
    asyncClient.init(conf);
    asyncClient.start();
    asyncClient.registerApplicationMaster("localhost", 1234, null);

    // while the CallbackHandler will still only be processing the first response,
    // heartbeater thread should still be sending heartbeats.
    // To test this, wait for the second heartbeat to be received. 
    while (secondHeartbeatSync.get() < 1) {
        Thread.sleep(10);
    }

    // heartbeat will be blocked. make sure we can call client methods at this
    // time. Checks that heartbeat is not holding onto client lock
    assert (secondHeartbeatSync.get() < 2);
    asyncClient.getAvailableResources();
    // method returned. now unblock heartbeat
    assert (secondHeartbeatSync.get() < 2);
    synchronized (heartbeatBlock) {
        heartbeatBlock.set(false);
        heartbeatBlock.notifyAll();
    }

    // allocated containers should come before completed containers
    Assert.assertEquals(null, callbackHandler.takeCompletedContainers());

    // wait for the allocated containers from the first heartbeat's response
    while (callbackHandler.takeAllocatedContainers() == null) {
        Assert.assertEquals(null, callbackHandler.takeCompletedContainers());
        Thread.sleep(10);
    }

    // wait for the completed containers from the second heartbeat's response
    while (callbackHandler.takeCompletedContainers() == null) {
        Thread.sleep(10);
    }

    // wait for the changed containers from the thrid heartbeat's response
    while (callbackHandler.takeChangedContainers() == null) {
        Thread.sleep(10);
    }

    asyncClient.stop();

    Assert.assertEquals(null, callbackHandler.takeAllocatedContainers());
    Assert.assertEquals(null, callbackHandler.takeCompletedContainers());
    Assert.assertEquals(null, callbackHandler.takeChangedContainers());
}

From source file:org.apache.tinkerpop.gremlin.structure.IoTest.java

@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_DOUBLE_VALUES)
public void shouldReadWriteEdgeToGraphSON() throws Exception {
    final Vertex v1 = g.addVertex(T.label, "person");
    final Vertex v2 = g.addVertex(T.label, "person");
    final Edge e = v1.addEdge("friend", v2, "weight", 0.5f, "acl", "rw");

    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        final GraphSONWriter writer = g.io().graphSONWriter().create();
        writer.writeEdge(os, e);//from  w ww .j av  a  2s . com

        final AtomicBoolean called = new AtomicBoolean(false);
        final GraphSONReader reader = g.io().graphSONReader().create();
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            reader.readEdge(bais, detachedEdge -> {
                assertEquals(e.id().toString(), detachedEdge.id().toString()); // lossy
                assertEquals(v1.id().toString(),
                        detachedEdge.iterators().vertexIterator(Direction.OUT).next().id().toString()); // lossy
                assertEquals(v2.id().toString(),
                        detachedEdge.iterators().vertexIterator(Direction.IN).next().id().toString()); // lossy
                assertEquals(v1.label(), detachedEdge.iterators().vertexIterator(Direction.OUT).next().label());
                assertEquals(v2.label(), detachedEdge.iterators().vertexIterator(Direction.IN).next().label());
                assertEquals(e.label(), detachedEdge.label());
                assertEquals(0.5d, detachedEdge.iterators().propertyIterator("weight").next().value());
                assertEquals("rw", detachedEdge.iterators().propertyIterator("acl").next().value());
                called.set(true);
                return null;
            });
        }

        assertTrue(called.get());
    }
}

From source file:org.apache.tinkerpop.gremlin.structure.IoTest.java

@Test
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES)
@FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_DOUBLE_VALUES)
public void shouldReadWriteDetachedEdgeToGraphSON() throws Exception {
    final Vertex v1 = g.addVertex(T.label, "person");
    final Vertex v2 = g.addVertex(T.label, "person");
    final Edge e = DetachedFactory.detach(v1.addEdge("friend", v2, "weight", 0.5f, "acl", "rw"), true);

    try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
        final GraphSONWriter writer = g.io().graphSONWriter().create();
        writer.writeEdge(os, e);/*  ww  w  . java 2 s . c  om*/

        final AtomicBoolean called = new AtomicBoolean(false);
        final GraphSONReader reader = g.io().graphSONReader().create();
        try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) {
            reader.readEdge(bais, detachedEdge -> {
                assertEquals(e.id().toString(), detachedEdge.id().toString()); // lossy
                assertEquals(v1.id().toString(),
                        detachedEdge.iterators().vertexIterator(Direction.OUT).next().id().toString()); // lossy
                assertEquals(v2.id().toString(),
                        detachedEdge.iterators().vertexIterator(Direction.IN).next().id().toString()); // lossy
                assertEquals(v1.label(), detachedEdge.iterators().vertexIterator(Direction.OUT).next().label());
                assertEquals(v2.label(), detachedEdge.iterators().vertexIterator(Direction.IN).next().label());
                assertEquals(e.label(), detachedEdge.label());
                assertEquals(0.5d, detachedEdge.iterators().propertyIterator("weight").next().value());
                assertEquals("rw", detachedEdge.iterators().propertyIterator("acl").next().value());
                called.set(true);
                return null;
            });
        }

        assertTrue(called.get());
    }
}

From source file:org.apache.activemq.leveldb.test.ReplicatedLevelDBBrokerTest.java

@Test
@Ignore//from  ww  w .  java 2 s  . c  om
public void testReplicationQuorumLoss() throws Throwable {

    System.out.println("======================================");
    System.out.println(" Start 2 ActiveMQ nodes.");
    System.out.println("======================================");
    startBrokerAsync(createBrokerNode("node-1", port));
    startBrokerAsync(createBrokerNode("node-2", port));
    BrokerService master = waitForNextMaster();
    System.out.println("======================================");
    System.out.println(" Start the producer and consumer");
    System.out.println("======================================");

    final AtomicBoolean stopClients = new AtomicBoolean(false);
    final ArrayBlockingQueue<String> errors = new ArrayBlockingQueue<String>(100);
    final AtomicLong receivedCounter = new AtomicLong();
    final AtomicLong sentCounter = new AtomicLong();
    Thread producer = startFailoverClient("producer", new Client() {
        @Override
        public void execute(Connection connection) throws Exception {
            Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            MessageProducer producer = session.createProducer(session.createQueue("test"));
            long actual = 0;
            while (!stopClients.get()) {
                TextMessage msg = session.createTextMessage("Hello World");
                msg.setLongProperty("id", actual++);
                producer.send(msg);
                sentCounter.incrementAndGet();
            }
        }
    });

    Thread consumer = startFailoverClient("consumer", new Client() {
        @Override
        public void execute(Connection connection) throws Exception {
            connection.start();
            Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
            MessageConsumer consumer = session.createConsumer(session.createQueue("test"));
            long expected = 0;
            while (!stopClients.get()) {
                Message msg = consumer.receive(200);
                if (msg != null) {
                    long actual = msg.getLongProperty("id");
                    if (actual != expected) {
                        errors.offer("Received got unexpected msg id: " + actual + ", expected: " + expected);
                    }
                    msg.acknowledge();
                    expected = actual + 1;
                    receivedCounter.incrementAndGet();
                }
            }
        }
    });

    try {
        assertCounterMakesProgress(sentCounter, 10, TimeUnit.SECONDS);
        assertCounterMakesProgress(receivedCounter, 5, TimeUnit.SECONDS);
        assertNull(errors.poll());

        System.out.println("======================================");
        System.out.println(" Master should stop once the quorum is lost.");
        System.out.println("======================================");
        ArrayList<BrokerService> stopped = stopSlaves();// stopping the slaves should kill the quorum.
        assertStopsWithin(master, 10, TimeUnit.SECONDS);
        assertNull(errors.poll()); // clients should not see an error since they are failover clients.
        stopped.add(master);

        System.out.println("======================================");
        System.out.println(" Restart the slave. Clients should make progress again..");
        System.out.println("======================================");
        startBrokersAsync(createBrokerNodes(stopped));
        assertCounterMakesProgress(sentCounter, 10, TimeUnit.SECONDS);
        assertCounterMakesProgress(receivedCounter, 5, TimeUnit.SECONDS);
        assertNull(errors.poll());
    } catch (Throwable e) {
        e.printStackTrace();
        throw e;
    } finally {
        // Wait for the clients to stop..
        stopClients.set(true);
        producer.join();
        consumer.join();
    }
}

From source file:org.apache.hadoop.hdfs.TestBlockReaderFactory.java

/**
 * Test the case where we have a failure to complete a short circuit read
 * that occurs, and then later on, we have a success.
 * Any thread waiting on a cache load should receive the failure (if it
 * occurs);  however, the failure result should not be cached.  We want 
 * to be able to retry later and succeed.
 *//* w  w w  .  ja  v a  2 s.  c om*/
@Test(timeout = 60000)
public void testShortCircuitCacheTemporaryFailure() throws Exception {
    BlockReaderTestUtil.enableBlockReaderFactoryTracing();
    final AtomicBoolean replicaCreationShouldFail = new AtomicBoolean(true);
    final AtomicBoolean testFailed = new AtomicBoolean(false);
    DFSInputStream.tcpReadsDisabledForTesting = true;
    BlockReaderFactory.createShortCircuitReplicaInfoCallback = new ShortCircuitCache.ShortCircuitReplicaCreator() {
        @Override
        public ShortCircuitReplicaInfo createShortCircuitReplicaInfo() {
            if (replicaCreationShouldFail.get()) {
                // Insert a short delay to increase the chance that one client
                // thread waits for the other client thread's failure via
                // a condition variable.
                Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS);
                return new ShortCircuitReplicaInfo();
            }
            return null;
        }
    };
    TemporarySocketDirectory sockDir = new TemporarySocketDirectory();
    Configuration conf = createShortCircuitConf("testShortCircuitCacheTemporaryFailure", sockDir);
    final MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
    cluster.waitActive();
    final DistributedFileSystem dfs = cluster.getFileSystem();
    final String TEST_FILE = "/test_file";
    final int TEST_FILE_LEN = 4000;
    final int NUM_THREADS = 2;
    final int SEED = 0xFADED;
    final CountDownLatch gotFailureLatch = new CountDownLatch(NUM_THREADS);
    final CountDownLatch shouldRetryLatch = new CountDownLatch(1);
    DFSTestUtil.createFile(dfs, new Path(TEST_FILE), TEST_FILE_LEN, (short) 1, SEED);
    Runnable readerRunnable = new Runnable() {
        @Override
        public void run() {
            try {
                // First time should fail.
                List<LocatedBlock> locatedBlocks = cluster.getNameNode().getRpcServer()
                        .getBlockLocations(TEST_FILE, 0, TEST_FILE_LEN).getLocatedBlocks();
                LocatedBlock lblock = locatedBlocks.get(0); // first block
                BlockReader blockReader = null;
                try {
                    blockReader = BlockReaderTestUtil.getBlockReader(cluster, lblock, 0, TEST_FILE_LEN);
                    Assert.fail("expected getBlockReader to fail the first time.");
                } catch (Throwable t) {
                    Assert.assertTrue(
                            "expected to see 'TCP reads were disabled " + "for testing' in exception " + t,
                            t.getMessage().contains("TCP reads were disabled for testing"));
                } finally {
                    if (blockReader != null)
                        blockReader.close(); // keep findbugs happy
                }
                gotFailureLatch.countDown();
                shouldRetryLatch.await();

                // Second time should succeed.
                try {
                    blockReader = BlockReaderTestUtil.getBlockReader(cluster, lblock, 0, TEST_FILE_LEN);
                } catch (Throwable t) {
                    LOG.error("error trying to retrieve a block reader " + "the second time.", t);
                    throw t;
                } finally {
                    if (blockReader != null)
                        blockReader.close();
                }
            } catch (Throwable t) {
                LOG.error("getBlockReader failure", t);
                testFailed.set(true);
            }
        }
    };
    Thread threads[] = new Thread[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(readerRunnable);
        threads[i].start();
    }
    gotFailureLatch.await();
    replicaCreationShouldFail.set(false);
    shouldRetryLatch.countDown();
    for (int i = 0; i < NUM_THREADS; i++) {
        Uninterruptibles.joinUninterruptibly(threads[i]);
    }
    cluster.shutdown();
    sockDir.close();
    Assert.assertFalse(testFailed.get());
}