Example usage for java.util.concurrent Semaphore release

List of usage examples for java.util.concurrent Semaphore release

Introduction

In this page you can find the example usage for java.util.concurrent Semaphore release.

Prototype

public void release() 

Source Link

Document

Releases a permit, returning it to the semaphore.

Usage

From source file:org.commoncrawl.service.listcrawler.CrawlListsUI.java

private static void getListsForCustomer(final String customerId, final HttpServletResponse resp,
        final HttpResult result) throws IOException {

    final CommonCrawlServer server = CommonCrawlServer.getServerSingleton();

    server.dispatchAsyncWebRequest(new AsyncWebServerRequest("", resp.getWriter()) {

        @Override/*from w  w w. j  a va 2  s . c o m*/
        public boolean handleRequest(final Semaphore completionSemaphore) throws IOException {

            final ProxyServer proxyServer = (ProxyServer) server;

            LOG.info("Getting List for Customer:" + customerId);
            final Collection<CrawlListDatabaseRecord> recordSet = proxyServer
                    .getListInfoForCustomerId(customerId).values();

            final ArrayList<CrawlListDatabaseRecord> sortedSet = new ArrayList<CrawlListDatabaseRecord>();

            sortedSet.addAll(recordSet);

            // sort by timestamp
            Collections.sort(sortedSet, new Comparator<CrawlListDatabaseRecord>() {

                @Override
                public int compare(CrawlListDatabaseRecord o1, CrawlListDatabaseRecord o2) {
                    return (o1.getListId() > o2.getListId()) ? -1 : 1;
                }

            });

            LOG.info("Found:" + sortedSet.size() + " Lists for Customer:" + customerId);

            if (sortedSet.size() != 0) {

                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        LOG.info("Running Worker Thread");
                        try {
                            PrintWriter writer = resp.getWriter();

                            JsonWriter jsonWriter = new JsonWriter(writer);

                            jsonWriter.beginObject();
                            jsonWriter.name("items");
                            jsonWriter.beginArray();

                            for (CrawlListDatabaseRecord listRecord : sortedSet) {

                                // get the list 
                                CrawlList list = proxyServer.getCrawlHistoryManager()
                                        .getList(listRecord.getListId());
                                if (list == null) {
                                    LOG.error("DID NOT Find List Object for List:" + listRecord.getListId()
                                            + " Name:" + listRecord.getListName() + " FileName:"
                                            + listRecord.getSourceFileName() + " TempFile:"
                                            + listRecord.getTempFileName());
                                }
                                if (list != null) {
                                    String queueState = "W";
                                    if (list.isListLoaded()) {

                                        if (list.getQueuedState() == QueueState.QUEUEING)
                                            queueState = "Q";
                                        else if (list.getQueuedState() == QueueState.QUEUED)
                                            queueState = "L";
                                        else if (list.getQueuedState() == QueueState.ERROR)
                                            queueState = "L";
                                        else
                                            queueState = "?";
                                        CrawlListMetadata metadata = list.getMetadata();
                                        CrawlListDomainItem summary = buildListSummary(metadata);
                                        // populate identification info 
                                        summary.setListId(list.getListId());
                                        summary.setListName(listRecord.getListName());

                                        jsonWriter.beginArray();
                                        jsonWriter.value(summary.getListId());
                                        jsonWriter.value(queueState);
                                        jsonWriter.value(summary.getListName());
                                        jsonWriter.value(list.getSubDomainItemCount());
                                        jsonWriter.value(summary.getUrlCount());
                                        jsonWriter.value(summary.getUrlsCrawled());
                                        jsonWriter.value(summary.getHttp200Count());
                                        jsonWriter.value(summary.getRobotsExcludedCount());
                                        jsonWriter.value(summary.getErrorCount());
                                        jsonWriter.value(summary.getQueuedCount());

                                        jsonWriter.endArray();
                                    } else if (list.getLoadState() == CrawlList.LoadState.QUEUED_FOR_LOADING) {
                                        jsonWriter.beginArray();
                                        jsonWriter.value(list.getListId());
                                        jsonWriter.value(queueState);
                                        jsonWriter.value("<B>Queued:</B>" + listRecord.getListName());
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);

                                        jsonWriter.endArray();
                                    } else if (list.getLoadState() == CrawlList.LoadState.REALLY_LOADING) {
                                        jsonWriter.beginArray();
                                        jsonWriter.value(list.getListId());
                                        jsonWriter.value("<B>Loading:</B>" + listRecord.getListName());
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);

                                        jsonWriter.endArray();
                                    } else if (list.getLoadState() == CrawlList.LoadState.ERROR) {
                                        jsonWriter.beginArray();
                                        jsonWriter.value(list.getListId());
                                        jsonWriter.value("ERR");
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);
                                        jsonWriter.value(0);

                                        jsonWriter.endArray();
                                    }
                                }
                            }

                            jsonWriter.endArray();
                            jsonWriter.endObject();

                            LOG.info("Done");

                            result._resultCode = HttpServletResponse.SC_OK;

                        } catch (IOException e) {
                            LOG.error(CCStringUtils.stringifyException(e));
                        } catch (Exception e) {
                            LOG.error(CCStringUtils.stringifyException(e));
                        } finally {
                            LOG.error("DONE");
                            completionSemaphore.release();
                        }
                    }

                });

                LOG.info("Spawning Worker Thread");
                thread.start();

                return true;
            }
            return false;
        }

    });

}

From source file:org.apache.brooklyn.location.jclouds.JcloudsLocation.java

protected MachineLocation obtainOnce(ConfigBag setup) throws NoMachinesAvailableException {
    AccessController.Response access = getManagementContext().getAccessController().canProvisionLocation(this);
    if (!access.isAllowed()) {
        throw new IllegalStateException(
                "Access controller forbids provisioning in " + this + ": " + access.getMsg());
    }/*from  w w  w. j  av  a2  s. c om*/

    setCreationString(setup);
    boolean waitForSshable = !"false".equalsIgnoreCase(setup.get(WAIT_FOR_SSHABLE));
    boolean waitForWinRmable = !"false".equalsIgnoreCase(setup.get(WAIT_FOR_WINRM_AVAILABLE));
    boolean usePortForwarding = setup.get(USE_PORT_FORWARDING);
    boolean skipJcloudsSshing = Boolean.FALSE.equals(setup.get(USE_JCLOUDS_SSH_INIT)) || usePortForwarding;
    JcloudsPortForwarderExtension portForwarder = setup.get(PORT_FORWARDER);
    if (usePortForwarding)
        checkNotNull(portForwarder, "portForwarder, when use-port-forwarding enabled");

    final ComputeService computeService = getConfig(COMPUTE_SERVICE_REGISTRY).findComputeService(setup, true);
    CloudMachineNamer cloudMachineNamer = getCloudMachineNamer(setup);
    String groupId = elvis(setup.get(GROUP_ID), cloudMachineNamer.generateNewGroupId(setup));
    NodeMetadata node = null;
    JcloudsMachineLocation machineLocation = null;
    Duration semaphoreTimestamp = null;
    Duration templateTimestamp = null;
    Duration provisionTimestamp = null;
    Duration usableTimestamp = null;
    Duration customizedTimestamp = null;
    Stopwatch provisioningStopwatch = Stopwatch.createStarted();

    try {
        LOG.info("Creating VM " + setup.getDescription() + " in " + this);

        Semaphore machineCreationSemaphore = getMachineCreationSemaphore();
        boolean acquired = machineCreationSemaphore.tryAcquire(0, TimeUnit.SECONDS);
        if (!acquired) {
            LOG.info("Waiting in {} for machine-creation permit ({} other queuing requests already)",
                    new Object[] { this, machineCreationSemaphore.getQueueLength() });
            Stopwatch blockStopwatch = Stopwatch.createStarted();
            machineCreationSemaphore.acquire();
            LOG.info("Acquired in {} machine-creation permit, after waiting {}", this,
                    Time.makeTimeStringRounded(blockStopwatch));
        } else {
            LOG.debug("Acquired in {} machine-creation permit immediately", this);
        }
        semaphoreTimestamp = Duration.of(provisioningStopwatch);

        LoginCredentials userCredentials = null;
        Set<? extends NodeMetadata> nodes;
        Template template;
        try {
            // Setup the template
            template = buildTemplate(computeService, setup);
            boolean expectWindows = isWindows(template, setup);
            if (!skipJcloudsSshing) {
                if (expectWindows) {
                    // TODO Was this too early to look at template.getImage? e.g. customizeTemplate could subsequently modify it.
                    LOG.warn("Ignoring invalid configuration for Windows provisioning of " + template.getImage()
                            + ": " + USE_JCLOUDS_SSH_INIT.getName() + " should be false");
                    skipJcloudsSshing = true;
                } else if (waitForSshable) {
                    userCredentials = initTemplateForCreateUser(template, setup);
                }
            }

            templateTimestamp = Duration.of(provisioningStopwatch);
            // "Name" metadata seems to set the display name; at least in AWS
            // TODO it would be nice if this salt comes from the location's ID (but we don't know that yet as the ssh machine location isn't created yet)
            // TODO in softlayer we want to control the suffix of the hostname which is 3 random hex digits
            template.getOptions().getUserMetadata().put("Name",
                    cloudMachineNamer.generateNewMachineUniqueNameFromGroupId(setup, groupId));

            if (setup.get(JcloudsLocationConfig.INCLUDE_BROOKLYN_USER_METADATA)) {
                template.getOptions().getUserMetadata().put("brooklyn-user", System.getProperty("user.name"));

                Object context = setup.get(CALLER_CONTEXT);
                if (context instanceof Entity) {
                    Entity entity = (Entity) context;
                    template.getOptions().getUserMetadata().put("brooklyn-app-id", entity.getApplicationId());
                    template.getOptions().getUserMetadata().put("brooklyn-app-name",
                            entity.getApplication().getDisplayName());
                    template.getOptions().getUserMetadata().put("brooklyn-entity-id", entity.getId());
                    template.getOptions().getUserMetadata().put("brooklyn-entity-name",
                            entity.getDisplayName());
                    template.getOptions().getUserMetadata().put("brooklyn-server-creation-date",
                            Time.makeDateSimpleStampString());
                }
            }

            customizeTemplate(setup, computeService, template);

            LOG.debug("jclouds using template {} / options {} to provision machine in {}",
                    new Object[] { template, template.getOptions(), setup.getDescription() });

            if (!setup.getUnusedConfig().isEmpty())
                if (LOG.isDebugEnabled())
                    LOG.debug("NOTE: unused flags passed to obtain VM in " + setup.getDescription() + ": "
                            + Sanitizer.sanitize(setup.getUnusedConfig()));

            nodes = computeService.createNodesInGroup(groupId, 1, template);
            provisionTimestamp = Duration.of(provisioningStopwatch);
        } finally {
            machineCreationSemaphore.release();
        }

        node = Iterables.getOnlyElement(nodes, null);
        LOG.debug("jclouds created {} for {}", node, setup.getDescription());
        if (node == null)
            throw new IllegalStateException(
                    "No nodes returned by jclouds create-nodes in " + setup.getDescription());

        boolean windows = isWindows(node, setup);
        if (windows) {
            int newLoginPort = node.getLoginPort() == 22 ? 5985 : node.getLoginPort();
            String newLoginUser = "root".equals(node.getCredentials().getUser()) ? "Administrator"
                    : node.getCredentials().getUser();
            LOG.debug(
                    "jclouds created Windows VM {}; transforming connection details: loginPort from {} to {}; loginUser from {} to {}",
                    new Object[] { node, node.getLoginPort(), newLoginPort, node.getCredentials().getUser(),
                            newLoginUser });

            node = NodeMetadataBuilder.fromNodeMetadata(node).loginPort(newLoginPort)
                    .credentials(LoginCredentials.builder(node.getCredentials()).user(newLoginUser).build())
                    .build();
        }
        // FIXME How do we influence the node.getLoginPort, so it is set correctly for Windows?
        // Setup port-forwarding, if required
        Optional<HostAndPort> sshHostAndPortOverride;
        if (usePortForwarding) {
            sshHostAndPortOverride = Optional.of(portForwarder.openPortForwarding(node, node.getLoginPort(),
                    Optional.<Integer>absent(), Protocol.TCP, Cidr.UNIVERSAL));
        } else {
            sshHostAndPortOverride = Optional.absent();
        }

        LoginCredentials initialCredentials = node.getCredentials();
        if (skipJcloudsSshing) {
            boolean waitForConnectable = (windows) ? waitForWinRmable : waitForSshable;
            if (waitForConnectable) {
                if (windows) {
                    // TODO Does jclouds support any windows user setup?
                    initialCredentials = waitForWinRmAvailable(computeService, node, sshHostAndPortOverride,
                            setup);
                } else {
                    initialCredentials = waitForSshable(computeService, node, sshHostAndPortOverride, setup);
                }
                userCredentials = createUser(computeService, node, sshHostAndPortOverride, initialCredentials,
                        setup);
            }
        }

        // Figure out which login-credentials to use
        LoginCredentials customCredentials = setup.get(CUSTOM_CREDENTIALS);
        if (customCredentials != null) {
            userCredentials = customCredentials;
            //set userName and other data, from these credentials
            Object oldUsername = setup.put(USER, customCredentials.getUser());
            LOG.debug("node {} username {} / {} (customCredentials)",
                    new Object[] { node, customCredentials.getUser(), oldUsername });
            if (customCredentials.getOptionalPassword().isPresent())
                setup.put(PASSWORD, customCredentials.getOptionalPassword().get());
            if (customCredentials.getOptionalPrivateKey().isPresent())
                setup.put(PRIVATE_KEY_DATA, customCredentials.getOptionalPrivateKey().get());
        }
        if (userCredentials == null || (!userCredentials.getOptionalPassword().isPresent()
                && !userCredentials.getOptionalPrivateKey().isPresent())) {
            // We either don't have any userCredentials, or it is missing both a password/key.
            // TODO See waitForSshable, which now handles if the node.getLoginCredentials has both a password+key
            userCredentials = extractVmCredentials(setup, node, initialCredentials);
        }
        if (userCredentials == null) {
            // TODO See waitForSshable, which now handles if the node.getLoginCredentials has both a password+key
            userCredentials = extractVmCredentials(setup, node, initialCredentials);
        }
        if (userCredentials != null) {
            node = NodeMetadataBuilder.fromNodeMetadata(node).credentials(userCredentials).build();
        } else {
            // only happens if something broke above...
            userCredentials = LoginCredentials.fromCredentials(node.getCredentials());
        }
        // store the credentials, in case they have changed
        setup.putIfNotNull(JcloudsLocationConfig.PASSWORD, userCredentials.getOptionalPassword().orNull());
        setup.putIfNotNull(JcloudsLocationConfig.PRIVATE_KEY_DATA,
                userCredentials.getOptionalPrivateKey().orNull());

        // Wait for the VM to be reachable over SSH
        if (waitForSshable && !windows) {
            waitForSshable(computeService, node, sshHostAndPortOverride, ImmutableList.of(userCredentials),
                    setup);
        } else {
            LOG.debug("Skipping ssh check for {} ({}) due to config waitForSshable=false", node,
                    setup.getDescription());
        }
        usableTimestamp = Duration.of(provisioningStopwatch);

        //            JcloudsSshMachineLocation jcloudsSshMachineLocation = null;
        //            WinRmMachineLocation winRmMachineLocation = null;
        // Create a JcloudsSshMachineLocation, and register it
        if (windows) {
            machineLocation = registerWinRmMachineLocation(computeService, node, userCredentials,
                    sshHostAndPortOverride, setup);
        } else {
            machineLocation = registerJcloudsSshMachineLocation(computeService, node,
                    Optional.fromNullable(template), userCredentials, sshHostAndPortOverride, setup);
        }

        if (usePortForwarding && sshHostAndPortOverride.isPresent()) {
            // Now that we have the sshMachineLocation, we can associate the port-forwarding address with it.
            PortForwardManager portForwardManager = setup.get(PORT_FORWARDING_MANAGER);
            if (portForwardManager != null) {
                portForwardManager.associate(node.getId(), sshHostAndPortOverride.get(), machineLocation,
                        node.getLoginPort());
            } else {
                LOG.warn("No port-forward manager for {} so could not associate {} -> {} for {}",
                        new Object[] { this, node.getLoginPort(), sshHostAndPortOverride, machineLocation });
            }
        }

        if ("docker".equals(this.getProvider())) {
            if (windows) {
                throw new UnsupportedOperationException("Docker not supported on Windows");
            }
            Map<Integer, Integer> portMappings = JcloudsUtil.dockerPortMappingsFor(this, node.getId());
            PortForwardManager portForwardManager = setup.get(PORT_FORWARDING_MANAGER);
            if (portForwardManager != null) {
                for (Integer containerPort : portMappings.keySet()) {
                    Integer hostPort = portMappings.get(containerPort);
                    String dockerHost = ((JcloudsSshMachineLocation) machineLocation).getSshHostAndPort()
                            .getHostText();
                    portForwardManager.associate(node.getId(), HostAndPort.fromParts(dockerHost, hostPort),
                            machineLocation, containerPort);
                }
            } else {
                LOG.warn("No port-forward manager for {} so could not associate docker port-mappings for {}",
                        this, machineLocation);
            }
        }

        List<String> customisationForLogging = new ArrayList<String>();
        // Apply same securityGroups rules to iptables, if iptables is running on the node
        if (waitForSshable) {

            String setupScript = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_URL);
            List<String> setupScripts = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_URL_LIST);
            Collection<String> allScripts = new MutableList<String>().appendIfNotNull(setupScript)
                    .appendAll(setupScripts);
            for (String setupScriptItem : allScripts) {
                if (Strings.isNonBlank(setupScriptItem)) {
                    customisationForLogging.add("custom setup script " + setupScriptItem);

                    String setupVarsString = setup.get(JcloudsLocationConfig.CUSTOM_MACHINE_SETUP_SCRIPT_VARS);
                    Map<String, String> substitutions = (setupVarsString != null)
                            ? Splitter.on(",").withKeyValueSeparator(":").split(setupVarsString)
                            : ImmutableMap.<String, String>of();
                    String scriptContent = ResourceUtils.create(this).getResourceAsString(setupScriptItem);
                    String script = TemplateProcessor.processTemplateContents(scriptContent,
                            getManagementContext(), substitutions);
                    if (windows) {
                        ((WinRmMachineLocation) machineLocation)
                                .executeCommand(ImmutableList.copyOf((script.replace("\r", "").split("\n"))));
                    } else {
                        ((SshMachineLocation) machineLocation).execCommands("Customizing node " + this,
                                ImmutableList.of(script));
                    }
                }
            }

            if (setup.get(JcloudsLocationConfig.MAP_DEV_RANDOM_TO_DEV_URANDOM)) {
                if (windows) {
                    LOG.warn("Ignoring flag MAP_DEV_RANDOM_TO_DEV_URANDOM on Windows location {}",
                            machineLocation);
                } else {
                    customisationForLogging.add("point /dev/random to urandom");

                    ((SshMachineLocation) machineLocation).execCommands("using urandom instead of random",
                            Arrays.asList("sudo mv /dev/random /dev/random-real",
                                    "sudo ln -s /dev/urandom /dev/random"));
                }
            }

            if (setup.get(GENERATE_HOSTNAME)) {
                if (windows) {
                    // TODO: Generate Windows Hostname
                    LOG.warn("Ignoring flag GENERATE_HOSTNAME on Windows location {}", machineLocation);
                } else {
                    customisationForLogging.add("configure hostname");

                    ((SshMachineLocation) machineLocation).execCommands("Generate hostname " + node.getName(),
                            Arrays.asList("sudo hostname " + node.getName(),
                                    "sudo sed -i \"s/HOSTNAME=.*/HOSTNAME=" + node.getName()
                                            + "/g\" /etc/sysconfig/network",
                                    "sudo bash -c \"echo 127.0.0.1   `hostname` >> /etc/hosts\""));
                }
            }

            if (setup.get(OPEN_IPTABLES)) {
                if (windows) {
                    LOG.warn("Ignoring DEPRECATED flag OPEN_IPTABLES on Windows location {}", machineLocation);
                } else {
                    LOG.warn(
                            "Using DEPRECATED flag OPEN_IPTABLES (will not be supported in future versions) for {} at {}",
                            machineLocation, this);

                    @SuppressWarnings("unchecked")
                    Iterable<Integer> inboundPorts = (Iterable<Integer>) setup.get(INBOUND_PORTS);

                    if (inboundPorts == null || Iterables.isEmpty(inboundPorts)) {
                        LOG.info("No ports to open in iptables (no inbound ports) for {} at {}",
                                machineLocation, this);
                    } else {
                        customisationForLogging.add("open iptables");

                        List<String> iptablesRules = Lists.newArrayList();

                        if (isLocationFirewalldEnabled((SshMachineLocation) machineLocation)) {
                            for (Integer port : inboundPorts) {
                                iptablesRules.add(IptablesCommands.addFirewalldRule(Chain.INPUT, Protocol.TCP,
                                        port, Policy.ACCEPT));
                            }
                        } else {
                            iptablesRules = createIptablesRulesForNetworkInterface(inboundPorts);
                            iptablesRules.add(IptablesCommands.saveIptablesRules());
                        }
                        List<String> batch = Lists.newArrayList();
                        // Some entities, such as Riak (erlang based) have a huge range of ports, which leads to a script that
                        // is too large to run (fails with a broken pipe). Batch the rules into batches of 50
                        for (String rule : iptablesRules) {
                            batch.add(rule);
                            if (batch.size() == 50) {
                                ((SshMachineLocation) machineLocation)
                                        .execCommands("Inserting iptables rules, 50 command batch", batch);
                                batch.clear();
                            }
                        }
                        if (batch.size() > 0) {
                            ((SshMachineLocation) machineLocation).execCommands("Inserting iptables rules",
                                    batch);
                        }
                        ((SshMachineLocation) machineLocation).execCommands("List iptables rules",
                                ImmutableList.of(IptablesCommands.listIptablesRule()));
                    }
                }
            }

            if (setup.get(STOP_IPTABLES)) {
                if (windows) {
                    LOG.warn("Ignoring DEPRECATED flag OPEN_IPTABLES on Windows location {}", machineLocation);
                } else {
                    LOG.warn(
                            "Using DEPRECATED flag STOP_IPTABLES (will not be supported in future versions) for {} at {}",
                            machineLocation, this);

                    customisationForLogging.add("stop iptables");

                    List<String> cmds = ImmutableList.<String>of();
                    if (isLocationFirewalldEnabled((SshMachineLocation) machineLocation)) {
                        cmds = ImmutableList.of(IptablesCommands.firewalldServiceStop(),
                                IptablesCommands.firewalldServiceStatus());
                    } else {
                        cmds = ImmutableList.of(IptablesCommands.iptablesServiceStop(),
                                IptablesCommands.iptablesServiceStatus());
                    }
                    ((SshMachineLocation) machineLocation).execCommands("Stopping iptables", cmds);
                }
            }

            List<String> extraKeyUrlsToAuth = setup.get(EXTRA_PUBLIC_KEY_URLS_TO_AUTH);
            if (extraKeyUrlsToAuth != null && !extraKeyUrlsToAuth.isEmpty()) {
                if (windows) {
                    LOG.warn("Ignoring flag EXTRA_PUBLIC_KEY_URLS_TO_AUTH on Windows location",
                            machineLocation);
                } else {
                    List<String> extraKeyDataToAuth = MutableList.of();
                    for (String keyUrl : extraKeyUrlsToAuth) {
                        extraKeyDataToAuth.add(ResourceUtils.create().getResourceAsString(keyUrl));
                    }
                    ((SshMachineLocation) machineLocation).execCommands("Authorizing ssh keys",
                            ImmutableList.of(new AuthorizeRSAPublicKeys(extraKeyDataToAuth)
                                    .render(org.jclouds.scriptbuilder.domain.OsFamily.UNIX)));
                }
            }

        } else {
            // Otherwise we have deliberately not waited to be ssh'able, so don't try now to
            // ssh to exec these commands!
        }

        // Apply any optional app-specific customization.
        for (JcloudsLocationCustomizer customizer : getCustomizers(setup)) {
            LOG.debug("Customizing machine {}, using customizer {}", machineLocation, customizer);
            customizer.customize(this, computeService, machineLocation);
        }
        for (MachineLocationCustomizer customizer : getMachineCustomizers(setup)) {
            LOG.debug("Customizing machine {}, using customizer {}", machineLocation, customizer);
            customizer.customize(machineLocation);
        }

        customizedTimestamp = Duration.of(provisioningStopwatch);

        try {
            String logMessage = "Finished VM " + setup.getDescription() + " creation:" + " "
                    + machineLocation.getUser() + "@" + machineLocation.getAddress() + ":"
                    + machineLocation.getPort()
                    + (Boolean.TRUE.equals(setup.get(LOG_CREDENTIALS))
                            ? "password=" + userCredentials.getOptionalPassword().or("<absent>") + " && key="
                                    + userCredentials.getOptionalPrivateKey().or("<absent>")
                            : "")
                    + " ready after " + Duration.of(provisioningStopwatch).toStringRounded() + " ("
                    + "semaphore obtained in " + Duration.of(semaphoreTimestamp).toStringRounded() + ";"
                    + template + " template built in "
                    + Duration.of(templateTimestamp).subtract(semaphoreTimestamp).toStringRounded() + ";" + " "
                    + node + " provisioned in "
                    + Duration.of(provisionTimestamp).subtract(templateTimestamp).toStringRounded() + ";" + " "
                    + machineLocation + " connection usable in "
                    + Duration.of(usableTimestamp).subtract(provisionTimestamp).toStringRounded() + ";"
                    + " and os customized in "
                    + Duration.of(customizedTimestamp).subtract(usableTimestamp).toStringRounded() + " - "
                    + Joiner.on(", ").join(customisationForLogging) + ")";
            LOG.info(logMessage);
        } catch (Exception e) {
            // TODO Remove try-catch! @Nakomis: why did you add it? What exception happened during logging?
            Exceptions.propagateIfFatal(e);
            LOG.warn("Problem generating log message summarising completion of jclouds machine provisioning "
                    + machineLocation + " by " + this, e);
        }

        return machineLocation;

    } catch (Exception e) {
        if (e instanceof RunNodesException && ((RunNodesException) e).getNodeErrors().size() > 0) {
            node = Iterables.get(((RunNodesException) e).getNodeErrors().keySet(), 0);
        }
        // sometimes AWS nodes come up busted (eg ssh not allowed); just throw it back (and maybe try for another one)
        boolean destroyNode = (node != null) && Boolean.TRUE.equals(setup.get(DESTROY_ON_FAILURE));

        if (e.toString().contains("VPCResourceNotSpecified")) {
            LOG.error(
                    "Detected that your EC2 account is a legacy 'classic' account, but the recommended instance type requires VPC. "
                            + "You can specify the 'eu-central-1' region to avoid this problem, or you can specify a classic-compatible instance type, "
                            + "or you can specify a subnet to use with 'networkName' "
                            + "(taking care that the subnet auto-assigns public IP's and allows ingress on all ports, "
                            + "as Brooklyn does not currently configure security groups for non-default VPC's; "
                            + "or setting up Brooklyn to be in the subnet or have a jump host or other subnet access configuration). "
                            + "For more information on VPC vs classic see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-vpc.html.");
        }

        LOG.error(
                "Failed to start VM for " + setup.getDescription() + (destroyNode ? " (destroying)" : "")
                        + (node != null ? "; node " + node : "") + " after "
                        + Duration.of(provisioningStopwatch).toStringRounded()
                        + (semaphoreTimestamp != null
                                ? " (" + "semaphore obtained in "
                                        + Duration.of(semaphoreTimestamp).toStringRounded() + ";"
                                        + (templateTimestamp != null && semaphoreTimestamp != null
                                                ? " template built in " + Duration.of(templateTimestamp)
                                                        .subtract(semaphoreTimestamp).toStringRounded() + ";"
                                                : "")
                                        + (provisionTimestamp != null && templateTimestamp != null
                                                ? " node provisioned in " + Duration.of(provisionTimestamp)
                                                        .subtract(templateTimestamp).toStringRounded() + ";"
                                                : "")
                                        + (usableTimestamp != null && provisioningStopwatch != null
                                                ? " connection usable in "
                                                        + Duration.of(usableTimestamp)
                                                                .subtract(provisionTimestamp).toStringRounded()
                                                        + ";"
                                                : "")
                                        + (customizedTimestamp != null && usableTimestamp != null
                                                ? " and OS customized in " + Duration.of(customizedTimestamp)
                                                        .subtract(usableTimestamp).toStringRounded()
                                                : "")
                                        + ")"
                                : "")
                        + ": " + e.getMessage());
        LOG.debug(Throwables.getStackTraceAsString(e));

        if (destroyNode) {
            Stopwatch destroyingStopwatch = Stopwatch.createStarted();
            if (machineLocation != null) {
                releaseSafely(machineLocation);
            } else {
                releaseNodeSafely(node);
            }
            LOG.info("Destroyed " + (machineLocation != null ? "machine " + machineLocation : "node " + node)
                    + " in " + Duration.of(destroyingStopwatch).toStringRounded());
        }

        throw Exceptions.propagate(e);
    }
}

From source file:com.clustercontrol.monitor.run.factory.RunMonitor.java

/**
 * FacilityId????????/*from w ww  .j  a va2 s  .c o m*/
 * 
 * @return ??????</code> true </code>
 * @throws FacilityNotFound
 * @throws MonitorNotFound
 * @throws InvalidRole
 * @throws EntityExistsException
 * @throws HinemosUnknown
 *
 */
private boolean runMonitorInfoAggregateByNode()
        throws FacilityNotFound, MonitorNotFound, InvalidRole, EntityExistsException, HinemosUnknown {

    if (!m_isMonitorJob) {
        // ?????????????????
        final RepositoryControllerBean repository = new RepositoryControllerBean();
        final NodeInfo targetNode = repository.getNode(m_facilityId);
        final Set<Integer> currentTask = NodeMonitorPollerController
                .calcCurrentExecMonitorIntervals(targetNode);

        final Set<Integer> plannedTask = getPlannedTasksForNodeAggregateMonitor(m_monitorTypeId, m_facilityId);
        synchronized (plannedTask) {
            plannedTask.addAll(currentTask);
        }

        final NodeToMonitorCache node2monitor = NodeToMonitorCache.getInstance(m_monitorTypeId);
        final Semaphore execSingleThreadSemaphore = getSemaphoreForNodeAggregateMonitor(m_monitorTypeId,
                m_facilityId);

        // ?try???????????1??????????
        // Semaphore??????????????????????
        // ?????????
        if (execSingleThreadSemaphore.tryAcquire() == false) {
            for (Map.Entry<Integer, Set<MonitorInfo>> entry : node2monitor
                    .getMonitorsWithCalendar(m_facilityId, currentTask).entrySet()) {

                // ??????????
                final Set<MonitorInfo> monitors = entry.getValue();
                for (MonitorInfo monitor : monitors) {
                    String notifyGroupId = monitor.getNotifyGroupId();

                    // 
                    OutputBasicInfo notifyInfo = new OutputBasicInfo();
                    notifyInfo.setPluginId(m_monitorTypeId);
                    notifyInfo.setMonitorId(monitor.getMonitorId());
                    notifyInfo.setApplication(monitor.getApplication());

                    String facilityPath = new RepositoryControllerBean().getFacilityPath(m_facilityId, null);
                    notifyInfo.setFacilityId(m_facilityId);
                    notifyInfo.setScopeText(facilityPath);

                    // ????
                    int priority = PriorityConstant.TYPE_UNKNOWN;

                    String message = MessageConstant.MESSAGE_MONITOR_UNCOMPLETED.getMessage();
                    String messageOrg = MessageConstant.MESSAGE_MONITOR_UNCOMPLETED.getMessage();
                    notifyInfo.setPriority(priority);
                    notifyInfo.setMessage(message);
                    notifyInfo.setMessageOrg(messageOrg);
                    notifyInfo.setGenerationDate(m_now.getTime());

                    // for debug
                    if (m_log.isDebugEnabled()) {
                        m_log.debug("notify() priority = " + priority + " , message = " + message
                                + " , messageOrg = " + messageOrg + ", generationDate = " + m_now);
                    }

                    // ?
                    if (m_log.isDebugEnabled()) {
                        m_log.debug("sending message" + " : priority=" + notifyInfo.getPriority()
                                + " generationDate=" + notifyInfo.getGenerationDate() + " pluginId="
                                + notifyInfo.getPluginId() + " monitorId=" + notifyInfo.getMonitorId()
                                + " facilityId=" + notifyInfo.getFacilityId() + " subKey="
                                + notifyInfo.getSubKey() + ")");
                    }

                    new NotifyControllerBean().notify(notifyInfo, notifyGroupId);
                }
            }

            // ?????????????????????true?
            return true;
        }

        // ?????1??????Semaphore???
        try {
            // ???????????????
            Set<Integer> execTargetInterval = null;
            synchronized (plannedTask) {
                execTargetInterval = new HashSet<>(plannedTask);
                plannedTask.clear();
            }

            // ??????preCollect???????????????preCollect????????
            final Object preCollectData = preCollect(execTargetInterval);

            // ??????RunMonitor???runMonitorList???????
            final List<RunMonitor> runMonitorList = new ArrayList<>();
            Set<MonitorInfo> monitorInfos = new HashSet<>();
            for (Set<MonitorInfo> addMonitors : node2monitor
                    .getMonitorsWithCalendar(m_facilityId, execTargetInterval).values()) {
                monitorInfos.addAll(addMonitors);
            }
            // ????????FacilityId????Map???????????1?????
            final Map<String, NodeInfo> nodeinfoMap = new HashMap<>();
            nodeinfoMap.put(m_facilityId, targetNode);
            for (final MonitorInfo monitorInfo : monitorInfos) {
                // ID?runMonitor?????
                RunMonitor runMonitor = this.createMonitorInstance();
                runMonitor.m_monitorTypeId = m_monitorTypeId;
                runMonitor.m_monitorId = monitorInfo.getMonitorId();
                runMonitor.m_now = m_now;
                if (runMonitor.setMonitorInfo(m_monitorTypeId, monitorInfo.getMonitorId())) {
                    runMonitorList.add(runMonitor);

                    // setMonitorInfo?????FacilityId???
                    // ??????????????????????
                    runMonitor.m_facilityId = m_facilityId;

                    // ????runMonitor?????runMonitorInfo??????
                    // TODO ?? m_priorityMap ???????????????????
                    runMonitor.setJudgementInfo();
                    runMonitor.nodeInfo = nodeinfoMap;
                    runMonitor.setCheckInfo();
                    runMonitor.m_isNode = true;
                }
            }

            // ??????
            checkMultiMonitorInfoData(preCollectData, runMonitorList);
        } finally {
            execSingleThreadSemaphore.release();
        }
    } else {
        // ?????
        final Object preCollectData = preCollect(null);

        // ID?runMonitor?????
        setJudgementInfo();
        final RepositoryControllerBean repository = new RepositoryControllerBean();
        final NodeInfo targetNode = repository.getNode(m_facilityId);
        final Map<String, NodeInfo> nodeinfoMap = new HashMap<>();
        nodeinfoMap.put(m_facilityId, targetNode);
        nodeInfo = nodeinfoMap;
        setCheckInfo();
        m_isNode = true;

        // ??????
        checkMultiMonitorInfoData(preCollectData, Arrays.asList(this));
    }
    return true;
}

From source file:net.bluehack.ui.ChatActivity.java

@Override
public boolean onFragmentCreate() {
    final int chatId = arguments.getInt("chat_id", 0);
    final int userId = arguments.getInt("user_id", 0);
    final int encId = arguments.getInt("enc_id", 0);
    inlineReturn = arguments.getLong("inline_return", 0);
    String inlineQuery = arguments.getString("inline_query");
    startLoadFromMessageId = arguments.getInt("message_id", 0);
    int migrated_to = arguments.getInt("migrated_to", 0);
    scrollToTopOnResume = arguments.getBoolean("scrollToTopOnResume", false);

    if (chatId != 0) {
        currentChat = MessagesController.getInstance().getChat(chatId);
        if (currentChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override//from   w  w  w.j  a  va  2s.  com
                public void run() {
                    currentChat = MessagesStorage.getInstance().getChat(chatId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentChat != null) {
                MessagesController.getInstance().putChat(currentChat, true);
            } else {
                return false;
            }
        }
        if (chatId > 0) {
            dialog_id = -chatId;
        } else {
            isBroadcast = true;
            dialog_id = AndroidUtilities.makeBroadcastId(chatId);
        }
        if (ChatObject.isChannel(currentChat)) {
            MessagesController.getInstance().startShortPoll(chatId, false);
        }
    } else if (userId != 0) {
        currentUser = MessagesController.getInstance().getUser(userId);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(userId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = userId;
        botUser = arguments.getString("botUser");
        if (inlineQuery != null) {
            MessagesController.getInstance().sendBotStart(currentUser, inlineQuery);
        }
    } else if (encId != 0) {
        currentEncryptedChat = MessagesController.getInstance().getEncryptedChat(encId);
        if (currentEncryptedChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentEncryptedChat = MessagesStorage.getInstance().getEncryptedChat(encId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentEncryptedChat != null) {
                MessagesController.getInstance().putEncryptedChat(currentEncryptedChat, true);
            } else {
                return false;
            }
        }
        currentUser = MessagesController.getInstance().getUser(currentEncryptedChat.user_id);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(currentEncryptedChat.user_id);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = ((long) encId) << 32;
        maxMessageId[0] = maxMessageId[1] = Integer.MIN_VALUE;
        minMessageId[0] = minMessageId[1] = Integer.MAX_VALUE;
        MediaController.getInstance().startMediaObserver();
    } else {
        return false;
    }

    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedNewMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesRead);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDeleted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByServer);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByAck);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageSendError);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.contactsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.encryptedChatUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadEncrypted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.removeAllMessagesFromDialog);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidReset);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.screenshotTook);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.blockedUsersDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileNewChunkAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didCreatedNewDeleteTask);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidStarted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateMessageMedia);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.replaceMessagesObjects);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.notificationsSettingsUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedReplyMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpagesInUpdates);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadContent);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botKeyboardDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatSearchResultsAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didUpdatedMessagesViews);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoCantLoad);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedPinnedMessage);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.peerSettingsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.newDraftReceived);

    super.onFragmentCreate();

    if (currentEncryptedChat == null && !isBroadcast) {
        BotQuery.loadBotKeyboard(dialog_id);
    }

    loading = true;
    MessagesController.getInstance().loadPeerSettings(dialog_id, currentUser, currentChat);
    MessagesController.getInstance().setLastCreatedDialogId(dialog_id, true);
    if (startLoadFromMessageId != 0) {
        needSelectFromMessageId = true;
        waitingForLoad.add(lastLoadIndex);
        if (migrated_to != 0) {
            mergeDialogId = migrated_to;
            MessagesController.getInstance().loadMessages(mergeDialogId, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        } else {
            MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        }
    } else {
        waitingForLoad.add(lastLoadIndex);
        MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20, 0, true,
                0, classGuid, 2, 0, ChatObject.isChannel(currentChat), lastLoadIndex++);
    }

    if (currentChat != null) {
        Semaphore semaphore = null;
        if (isBroadcast) {
            semaphore = new Semaphore(0);
        }
        MessagesController.getInstance().loadChatInfo(currentChat.id, semaphore,
                ChatObject.isChannel(currentChat));
        if (isBroadcast && semaphore != null) {
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }

    if (userId != 0 && currentUser.bot) {
        BotQuery.loadBotInfo(userId, true, classGuid);
    } else if (info instanceof TLRPC.TL_chatFull) {
        for (int a = 0; a < info.participants.participants.size(); a++) {
            TLRPC.ChatParticipant participant = info.participants.participants.get(a);
            TLRPC.User user = MessagesController.getInstance().getUser(participant.user_id);
            if (user != null && user.bot) {
                BotQuery.loadBotInfo(user.id, true, classGuid);
            }
        }
    }

    if (currentUser != null) {
        userBlocked = MessagesController.getInstance().blockedUsers.contains(currentUser.id);
    }

    if (AndroidUtilities.isTablet()) {
        NotificationCenter.getInstance().postNotificationName(NotificationCenter.openedChatChanged, dialog_id,
                false);
    }

    if (currentEncryptedChat != null && AndroidUtilities
            .getMyLayerVersion(currentEncryptedChat.layer) != SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
        SecretChatHelper.getInstance().sendNotifyLayerMessage(currentEncryptedChat, null);
    }

    return true;
}

From source file:kr.wdream.ui.ChatActivity.java

@Override
public boolean onFragmentCreate() {
    final int chatId = arguments.getInt("chat_id", 0);
    final int userId = arguments.getInt("user_id", 0);
    final int encId = arguments.getInt("enc_id", 0);
    inlineReturn = arguments.getLong("inline_return", 0);
    String inlineQuery = arguments.getString("inline_query");
    startLoadFromMessageId = arguments.getInt("message_id", 0);
    int migrated_to = arguments.getInt("migrated_to", 0);
    scrollToTopOnResume = arguments.getBoolean("scrollToTopOnResume", false);

    if (chatId != 0) {
        currentChat = MessagesController.getInstance().getChat(chatId);
        if (currentChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override//from  w w w  .j av  a 2 s .  c o  m
                public void run() {
                    currentChat = MessagesStorage.getInstance().getChat(chatId);
                    semaphore.release();

                    Log.d(LOG_TAG, "currentChat : " + currentChat);
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentChat != null) {
                MessagesController.getInstance().putChat(currentChat, true);

                Log.d(LOG_TAG, "currentChat2 : " + currentChat);
            } else {
                return false;
            }
        }
        if (chatId > 0) {
            dialog_id = -chatId;
        } else {
            isBroadcast = true;
            dialog_id = AndroidUtilities.makeBroadcastId(chatId);
        }
        if (ChatObject.isChannel(currentChat)) {
            MessagesController.getInstance().startShortPoll(chatId, false);
        }
    } else if (userId != 0) {
        currentUser = MessagesController.getInstance().getUser(userId);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(userId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = userId;
        botUser = arguments.getString("botUser");
        if (inlineQuery != null) {
            MessagesController.getInstance().sendBotStart(currentUser, inlineQuery);
        }
    } else if (encId != 0) {
        currentEncryptedChat = MessagesController.getInstance().getEncryptedChat(encId);
        if (currentEncryptedChat == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentEncryptedChat = MessagesStorage.getInstance().getEncryptedChat(encId);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentEncryptedChat != null) {
                MessagesController.getInstance().putEncryptedChat(currentEncryptedChat, true);
            } else {
                return false;
            }
        }
        currentUser = MessagesController.getInstance().getUser(currentEncryptedChat.user_id);
        if (currentUser == null) {
            final Semaphore semaphore = new Semaphore(0);
            MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
                @Override
                public void run() {
                    currentUser = MessagesStorage.getInstance().getUser(currentEncryptedChat.user_id);
                    semaphore.release();
                }
            });
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
            if (currentUser != null) {
                MessagesController.getInstance().putUser(currentUser, true);
            } else {
                return false;
            }
        }
        dialog_id = ((long) encId) << 32;
        maxMessageId[0] = maxMessageId[1] = Integer.MIN_VALUE;
        minMessageId[0] = minMessageId[1] = Integer.MAX_VALUE;
        MediaController.getInstance().startMediaObserver();
    } else {
        return false;
    }

    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.emojiDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateInterfaces);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedNewMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesRead);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesDeleted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByServer);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByAck);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageSendError);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.contactsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.encryptedChatUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadEncrypted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.removeAllMessagesFromDialog);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioProgressDidChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidReset);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioPlayStateChanged);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.screenshotTook);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.blockedUsersDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileNewChunkAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didCreatedNewDeleteTask);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.audioDidStarted);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.updateMessageMedia);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.replaceMessagesObjects);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.notificationsSettingsUpdated);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedReplyMessages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpages);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didReceivedWebpagesInUpdates);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.messagesReadContent);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botInfoDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.botKeyboardDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatSearchResultsAvailable);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didUpdatedMessagesViews);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.chatInfoCantLoad);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.didLoadedPinnedMessage);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.peerSettingsDidLoaded);
    NotificationCenter.getInstance().addObserver(this, NotificationCenter.newDraftReceived);

    super.onFragmentCreate();

    if (currentEncryptedChat == null && !isBroadcast) {
        BotQuery.loadBotKeyboard(dialog_id);
    }

    loading = true;
    MessagesController.getInstance().loadPeerSettings(dialog_id, currentUser, currentChat);
    MessagesController.getInstance().setLastCreatedDialogId(dialog_id, true);
    if (startLoadFromMessageId != 0) {
        needSelectFromMessageId = true;
        waitingForLoad.add(lastLoadIndex);
        if (migrated_to != 0) {
            mergeDialogId = migrated_to;
            MessagesController.getInstance().loadMessages(mergeDialogId, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        } else {
            MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20,
                    startLoadFromMessageId, true, 0, classGuid, 3, 0, ChatObject.isChannel(currentChat),
                    lastLoadIndex++);
        }
    } else {
        waitingForLoad.add(lastLoadIndex);
        MessagesController.getInstance().loadMessages(dialog_id, AndroidUtilities.isTablet() ? 30 : 20, 0, true,
                0, classGuid, 2, 0, ChatObject.isChannel(currentChat), lastLoadIndex++);
    }

    if (currentChat != null) {
        Semaphore semaphore = null;
        if (isBroadcast) {
            semaphore = new Semaphore(0);
        }
        MessagesController.getInstance().loadChatInfo(currentChat.id, semaphore,
                ChatObject.isChannel(currentChat));
        if (isBroadcast && semaphore != null) {
            try {
                semaphore.acquire();
            } catch (Exception e) {
                FileLog.e("tmessages", e);
            }
        }
    }

    if (userId != 0 && currentUser.bot) {
        BotQuery.loadBotInfo(userId, true, classGuid);
    } else if (info instanceof TLRPC.TL_chatFull) {
        for (int a = 0; a < info.participants.participants.size(); a++) {
            TLRPC.ChatParticipant participant = info.participants.participants.get(a);
            TLRPC.User user = MessagesController.getInstance().getUser(participant.user_id);
            if (user != null && user.bot) {
                BotQuery.loadBotInfo(user.id, true, classGuid);
            }
        }
    }

    if (currentUser != null) {
        userBlocked = MessagesController.getInstance().blockedUsers.contains(currentUser.id);
    }

    if (AndroidUtilities.isTablet()) {
        NotificationCenter.getInstance().postNotificationName(NotificationCenter.openedChatChanged, dialog_id,
                false);
    }

    if (currentEncryptedChat != null && AndroidUtilities
            .getMyLayerVersion(currentEncryptedChat.layer) != SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
        SecretChatHelper.getInstance().sendNotifyLayerMessage(currentEncryptedChat, null);
    }

    return true;
}