Example usage for java.util.concurrent.atomic AtomicReference notify

List of usage examples for java.util.concurrent.atomic AtomicReference notify

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicReference notify.

Prototype

@HotSpotIntrinsicCandidate
public final native void notify();

Source Link

Document

Wakes up a single thread that is waiting on this object's monitor.

Usage

From source file:com.sybase365.mobiliser.custom.project.handlers.authentication.GtalkAuthenticationHandler.java

@Override
public void authenticate(final SubTransaction transaction, final String authToken, final boolean payer)
        throws AuthenticationException {

    lazyInit();//from w  w w.j ava  2s .c o  m

    final Customer customer;
    final String otherName;
    if (payer) {
        customer = transaction.getTransaction().getPayer();
        otherName = transaction.getTransaction().getPayee().getDisplayName();
    } else {
        customer = transaction.getTransaction().getPayee();
        otherName = transaction.getTransaction().getPayer().getDisplayName();
    }

    final String email = this.notificationLogic.getCustomersEmail(customer, 0).get(0);

    final AtomicReference<String> userResponse = new AtomicReference<String>();

    final ChatManager chatmanager = this.connection.getChatManager();

    final MessageListener messageListener = new MessageListener() {

        @Override
        public void processMessage(Chat chat, Message message) {
            synchronized (userResponse) {
                LOG.info("got user response: " + message.getBody());
                userResponse.set(message.getBody());
                userResponse.notify();
            }
        }

    };

    final Chat newChat = chatmanager.createChat(email, messageListener);

    try {
        try {
            newChat.sendMessage("Please confirm payment to " + otherName + " by entering yes.");
        } catch (final XMPPException e) {
            LOG.info("can not send message to user " + email + ": " + e.getMessage(), e);

            throw new AuthenticationFailedPermanentlyException(StatusCodes.ERROR_IVR_NO_PICKUP);
        }

        synchronized (userResponse) {
            try {
                userResponse.wait(10000);
            } catch (final InterruptedException e) {
                LOG.info("Interrupted while waiting", e);
                Thread.currentThread().interrupt();
            }
        }

        if (userResponse.get() == null) {
            LOG.info("did not receive a response in time.");

            throw new AuthenticationFailedPermanentlyException("did not receive a response in time.",
                    StatusCodes.ERROR_IVR_NO_USER_INPUT, new HashMap<String, String>());
        }

        if ("yes".equalsIgnoreCase(userResponse.get())) {

            LOG.debug("received confirmation.");

            try {
                newChat.sendMessage("Thank You.");
            } catch (final XMPPException e1) {
                LOG.debug("Thank you Response was not sent. Ignored", e1);
            }

            return;
        }

        LOG.debug("received unknown response: " + userResponse.get());

        try {
            newChat.sendMessage("Transaction will be cancelled.");
        } catch (final XMPPException e2) {
            LOG.debug("Response was not sent. Ignored", e2);
        }

        throw new AuthenticationFailedPermanentlyException(StatusCodes.ERROR_IVR_HANGUP);

    } finally {
        newChat.removeMessageListener(messageListener);
    }

}

From source file:com.heliosdecompiler.helios.tasks.DecompileAndSaveTask.java

@Override
public void run() {
    File file = FileChooserUtil.chooseSaveLocation(Settings.LAST_DIRECTORY.get().asString(),
            Collections.singletonList("zip"));
    if (file == null)
        return;//from   w  w  w  . ja  va 2 s .  c  om
    if (file.exists()) {
        boolean delete = SWTUtil.promptForYesNo(Constants.REPO_NAME + " - Overwrite existing file",
                "The selected file already exists. Overwrite?");
        if (!delete) {
            return;
        }
    }

    AtomicReference<Transformer> transformer = new AtomicReference<>();

    Display display = Display.getDefault();
    display.asyncExec(() -> {
        Shell shell = new Shell(Display.getDefault());
        FillLayout layout = new FillLayout();
        layout.type = SWT.VERTICAL;
        shell.setLayout(layout);
        Transformer.getAllTransformers(t -> {
            return t instanceof Decompiler || t instanceof Disassembler;
        }).forEach(t -> {
            Button button = new Button(shell, SWT.RADIO);
            button.setText(t.getName());
            button.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    transformer.set(t);
                }
            });
        });
        Button ok = new Button(shell, SWT.NONE);
        ok.setText("OK");
        ok.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                shell.close();
                shell.dispose();
                synchronized (transformer) {
                    transformer.notify();
                }
            }
        });
        shell.pack();
        SWTUtil.center(shell);
        shell.open();
    });

    synchronized (transformer) {
        try {
            transformer.wait();
        } catch (InterruptedException e) {
            ExceptionHandler.handle(e);
        }
    }

    FileOutputStream fileOutputStream = null;
    ZipOutputStream zipOutputStream = null;

    try {
        file.createNewFile();
        fileOutputStream = new FileOutputStream(file);
        zipOutputStream = new ZipOutputStream(fileOutputStream);
        Set<String> written = new HashSet<>();
        for (Pair<String, String> pair : data) {
            LoadedFile loadedFile = Helios.getLoadedFile(pair.getValue0());
            if (loadedFile != null) {
                String innerName = pair.getValue1();
                byte[] bytes = loadedFile.getAllData().get(innerName);
                if (bytes != null) {
                    if (loadedFile.getClassNode(pair.getValue1()) != null) {
                        StringBuilder buffer = new StringBuilder();
                        transformer.get().transform(loadedFile.getClassNode(pair.getValue1()), bytes, buffer);
                        String name = innerName.substring(0, innerName.length() - 6) + ".java";
                        if (written.add(name)) {
                            zipOutputStream.putNextEntry(new ZipEntry(name));
                            zipOutputStream.write(buffer.toString().getBytes(StandardCharsets.UTF_8));
                            zipOutputStream.closeEntry();
                        } else {
                            SWTUtil.showMessage("Duplicate entry occured: " + name);
                        }
                    } else {
                        if (written.add(pair.getValue1())) {
                            zipOutputStream.putNextEntry(new ZipEntry(pair.getValue1()));
                            zipOutputStream.write(loadedFile.getAllData().get(pair.getValue1()));
                            zipOutputStream.closeEntry();
                        } else {
                            SWTUtil.showMessage("Duplicate entry occured: " + pair.getValue1());
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        ExceptionHandler.handle(e);
    } finally {
        IOUtils.closeQuietly(zipOutputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }
}