Example usage for java.util Queue poll

List of usage examples for java.util Queue poll

Introduction

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

Prototype

E poll();

Source Link

Document

Retrieves and removes the head of this queue, or returns null if this queue is empty.

Usage

From source file:eu.stratosphere.nephele.jobmanager.splitassigner.DefaultInputSplitAssigner.java

@Override
public InputSplit getNextInputSplit(final ExecutionVertex vertex) {

    final Queue<InputSplit> queue = this.splitMap.get(vertex.getGroupVertex());
    if (queue == null) {
        final JobID jobID = vertex.getExecutionGraph().getJobID();
        LOG.error("Cannot find split queue for vertex " + vertex.getGroupVertex() + " (job " + jobID + ")");
        return null;
    }//ww  w .j  av a2  s  .c  o  m

    InputSplit nextSplit = queue.poll();

    if (LOG.isDebugEnabled() && nextSplit != null) {
        LOG.debug("Assigning split " + nextSplit.getSplitNumber() + " to " + vertex);
    }

    return nextSplit;
}

From source file:gobblin.util.HadoopUtils.java

/**
 * This method is an additive implementation of the {@link FileSystem#rename(Path, Path)} method. It moves all the
 * files/directories under 'from' path to the 'to' path without overwriting existing directories in the 'to' path.
 *
 * <p>//from w ww.  j  av a 2s .co m
 * The rename operation happens at the first non-existent sub-directory. If a directory at destination path already
 * exists, it recursively tries to move sub-directories. If all the sub-directories also exist at the destination,
 * a file level move is done
 * </p>
 *
 * @param fileSystem on which the data needs to be moved
 * @param from path of the data to be moved
 * @param to path of the data to be moved
 */
public static void renameRecursively(FileSystem fileSystem, Path from, Path to) throws IOException {

    log.info(String.format("Recursively renaming %s in %s to %s.", from, fileSystem.getUri(), to));

    FileSystem throttledFS = getOptionallyThrottledFileSystem(fileSystem, 10000);

    ExecutorService executorService = ScalingThreadPoolExecutor.newScalingThreadPool(1, 100, 100,
            ExecutorsUtils.newThreadFactory(Optional.of(log), Optional.of("rename-thread-%d")));
    Queue<Future<?>> futures = Queues.newConcurrentLinkedQueue();

    try {
        if (!fileSystem.exists(from)) {
            throw new IOException("Trying to rename a path that does not exist! " + from);
        }

        futures.add(executorService.submit(new RenameRecursively(throttledFS, fileSystem.getFileStatus(from),
                to, executorService, futures)));
        int futuresUsed = 0;
        while (!futures.isEmpty()) {
            try {
                futures.poll().get();
                futuresUsed++;
            } catch (ExecutionException | InterruptedException ee) {
                throw new IOException(ee.getCause());
            }
        }

        log.info(String.format("Recursive renaming of %s to %s. (details: used %d futures)", from, to,
                futuresUsed));

    } finally {
        ExecutorsUtils.shutdownExecutorService(executorService, Optional.of(log), 1, TimeUnit.SECONDS);
    }
}

From source file:org.kuali.rice.krad.uif.util.ObjectPropertyUtils.java

/**
 * Locate the generic type declaration for a given target class in the generic type hierarchy of
 * the source class.//from w w w . j av a  2  s .  co  m
 * 
 * @param sourceClass The class representing the generic type hierarchy to scan.
 * @param targetClass The class representing the generic type declaration to locate within the
 *        source class' hierarchy.
 * @return The generic type representing the target class within the source class' generic
 *         hierarchy.
 */
public static Type findGenericType(Class<?> sourceClass, Class<?> targetClass) {
    if (!targetClass.isAssignableFrom(sourceClass)) {
        throw new IllegalArgumentException(targetClass + " is not assignable from " + sourceClass);
    }

    if (sourceClass.equals(targetClass)) {
        return sourceClass;
    }

    @SuppressWarnings("unchecked")
    Queue<Type> typeQueue = RecycleUtils.getInstance(LinkedList.class);
    typeQueue.offer(sourceClass);
    while (!typeQueue.isEmpty()) {
        Type type = typeQueue.poll();

        Class<?> upperBound = getUpperBound(type);
        if (targetClass.equals(upperBound)) {
            return type;
        }

        Type genericSuper = upperBound.getGenericSuperclass();
        if (genericSuper != null) {
            typeQueue.offer(genericSuper);
        }

        Type[] genericInterfaces = upperBound.getGenericInterfaces();
        for (int i = 0; i < genericInterfaces.length; i++) {
            if (genericInterfaces[i] != null) {
                typeQueue.offer(genericInterfaces[i]);
            }
        }
    }

    throw new IllegalStateException(targetClass + " is assignable from " + sourceClass
            + " but could not be found in the generic type hierarchy");
}

From source file:org.rhq.modules.plugins.wildfly10.itest.standalone.TemplatedResourcesTest.java

@Test(priority = 11)
public void loadUpdateTemplatedResourceConfiguration() throws Exception {
    ConfigurationManager configurationManager = this.pluginContainer.getConfigurationManager();

    for (ResourceData resourceData : testResourceData) {
        ResourceType resourceType = new ResourceType(resourceData.resourceTypeName, Constants.PLUGIN_NAME,
                ResourceCategory.SERVICE, null);
        Resource subsystem = waitForResourceByTypeAndKey(platform, server, resourceType,
                resourceData.resourceKey);

        Assert.assertNotNull(subsystem);

        Queue<Resource> unparsedResources = new LinkedList<Resource>();
        unparsedResources.add(subsystem);

        List<Resource> foundResources = new ArrayList<Resource>();
        while (!unparsedResources.isEmpty()) {
            Resource currentResource = unparsedResources.poll();

            for (Resource childResource : currentResource.getChildResources()) {
                unparsedResources.add(childResource);
                if (childResource.getResourceType().getName().equals(resourceData.subResourceType)) {
                    foundResources.add(childResource);
                }/*from  w ww .  j  a  v  a 2s . c o m*/
            }
        }

        for (Resource resourceUnderTest : foundResources) {
            log.info(foundResources);

            assert resourceUnderTest
                    .getId() != 0 : "Resource not properly initialized. Id = 0. Try extending sleep after discovery.";
            Configuration resourceUnderTestConfig = configurationManager
                    .loadResourceConfiguration(resourceUnderTest.getId());

            Assert.assertNotNull(resourceUnderTestConfig);
            Assert.assertFalse(resourceUnderTestConfig.getProperties().isEmpty());

            boolean specialPropertyFound = false;
            for (Property property : resourceUnderTestConfig.getProperties()) {
                if (property.getName().equals(resourceData.specialConfigurationProperty)) {
                    Assert.assertNotNull(((PropertySimple) property).getStringValue());
                    specialPropertyFound = true;
                    break;
                }
            }

            Assert.assertTrue(specialPropertyFound,
                    resourceData.specialConfigurationProperty + "property not found in the list of properties");

            ConfigurationUpdateRequest testUpdateRequest = new ConfigurationUpdateRequest(1,
                    resourceUnderTestConfig, resourceUnderTest.getId());
            ConfigurationUpdateResponse response = configurationManager
                    .executeUpdateResourceConfigurationImmediately(testUpdateRequest);

            Assert.assertNotNull(response);
        }
    }
}

From source file:org.rhq.modules.plugins.jbossas7.itest.standalone.TemplatedResourcesTest.java

@Test(priority = 11)
public void loadUpdateTemplatedResourceConfiguration() throws Exception {
    ConfigurationManager configurationManager = this.pluginContainer.getConfigurationManager();

    for (ResourceData resourceData : testResourceData) {
        ResourceType resourceType = new ResourceType(resourceData.resourceTypeName, PLUGIN_NAME,
                ResourceCategory.SERVICE, null);
        Resource subsystem = waitForResourceByTypeAndKey(platform, server, resourceType,
                resourceData.resourceKey);

        Assert.assertNotNull(subsystem);

        Queue<Resource> unparsedResources = new LinkedList<Resource>();
        unparsedResources.add(subsystem);

        List<Resource> foundResources = new ArrayList<Resource>();
        while (!unparsedResources.isEmpty()) {
            Resource currentResource = unparsedResources.poll();

            for (Resource childResource : currentResource.getChildResources()) {
                unparsedResources.add(childResource);
                if (childResource.getResourceType().getName().equals(resourceData.subResourceType)) {
                    foundResources.add(childResource);
                }// ww w  .j  ava  2  s .c  o m
            }
        }

        for (Resource resourceUnderTest : foundResources) {
            log.info(foundResources);

            assert resourceUnderTest
                    .getId() != 0 : "Resource not properly initialized. Id = 0. Try extending sleep after discovery.";
            Configuration resourceUnderTestConfig = configurationManager
                    .loadResourceConfiguration(resourceUnderTest.getId());

            Assert.assertNotNull(resourceUnderTestConfig);
            Assert.assertFalse(resourceUnderTestConfig.getProperties().isEmpty());

            boolean specialPropertyFound = false;
            for (Property property : resourceUnderTestConfig.getProperties()) {
                if (property.getName().equals(resourceData.specialConfigurationProperty)) {
                    Assert.assertNotNull(((PropertySimple) property).getStringValue());
                    specialPropertyFound = true;
                    break;
                }
            }

            Assert.assertTrue(specialPropertyFound,
                    resourceData.specialConfigurationProperty + "property not found in the list of properties");

            ConfigurationUpdateRequest testUpdateRequest = new ConfigurationUpdateRequest(1,
                    resourceUnderTestConfig, resourceUnderTest.getId());
            ConfigurationUpdateResponse response = configurationManager
                    .executeUpdateResourceConfigurationImmediately(testUpdateRequest);

            Assert.assertNotNull(response);
        }
    }
}

From source file:it.geosolutions.geobatch.destination.action.datamigration.MigrationAction.java

/**
 *
 *//*from w w  w  .ja v  a2 s.c o m*/
public Queue<EventObject> execute(Queue<EventObject> events) throws ActionException {

    listenerForwarder.setTask("Check config");
    checkInit();
    final LinkedList<EventObject> ret = new LinkedList<EventObject>();
    try {
        listenerForwarder.started();
        while (!events.isEmpty()) {
            EventObject event = events.poll();
            if (event instanceof FileSystemEvent) {
                FileSystemEvent fse = (FileSystemEvent) event;
                File file = fse.getSource();
                doProcess(configuration, FilenameUtils.getBaseName(file.getName()));
            }

            // pass the feature config to the next action
            ret.add(new FileSystemEvent(((FileSystemEvent) event).getSource(), FileSystemEventType.FILE_ADDED));
        }
        listenerForwarder.completed();
        return ret;
    } catch (Exception t) {
        listenerForwarder.failed(t);
        throw new ActionException(this, t.getMessage(), t);
    }
}

From source file:org.rhq.plugins.cassandra.itest.DiscoveryAndConfigurationTest.java

private Set<Resource> findResourcesForTest(Resource parent, Set<String> ignoredResourceTypes,
        Set<String> ignoredResourceNames) {
    Set<Resource> foundResources = new HashSet<Resource>();

    Queue<Resource> discoveryQueue = new LinkedList<Resource>();
    discoveryQueue.add(parent);//from   w  w  w .  j  a v  a 2  s. c  o m

    while (!discoveryQueue.isEmpty()) {
        Resource currentResource = discoveryQueue.poll();

        if (ignoredResourceTypes.contains(currentResource.getResourceType().getName())
                || ignoredResourceNames.contains(currentResource.getName())) {
            continue;
        }

        log.info("Discovered resource of type: " + currentResource.getResourceType().getName());
        if (currentResource.getResourceType().getPlugin().equals(PLUGIN_NAME)) {
            foundResources.add(currentResource);
        }

        if (currentResource.getChildResources() != null) {
            for (Resource child : currentResource.getChildResources()) {
                discoveryQueue.add(child);
            }
        }
    }

    return foundResources;
}

From source file:com.liferay.server.manager.internal.executor.PluginExecutor.java

@Override
public void executeUpdate(HttpServletRequest request, JSONObject responseJSONObject, Queue<String> arguments)
        throws Exception {

    String context = arguments.poll();

    List<File> installedDirs = getInstalledDirectories(context);

    for (File installedDir : installedDirs) {
        if (!installedDir.exists()) {
            responseJSONObject.put(JSONKeys.ERROR,
                    "Context directory " + installedDir.getAbsolutePath() + " does not exist");
            responseJSONObject.put(JSONKeys.STATUS, 1);

            return;
        }/*w ww. ja v  a 2  s  .c om*/
    }

    File tempFile = getTempFile(request, responseJSONObject);

    if (tempFile == null) {
        return;
    }

    for (File deployDirectory : installedDirs) {
        FileUtil.unzip(tempFile, deployDirectory);
    }

    File partialAppDeletePropsFile = new File(installedDirs.get(0), "META-INF/liferay-partialapp-delete.props");

    if (!partialAppDeletePropsFile.exists()) {
        return;
    }

    BufferedReader bufferedReader = new BufferedReader(new FileReader(partialAppDeletePropsFile));

    String line = null;

    while ((line = bufferedReader.readLine()) != null) {
        for (File deployDirectory : installedDirs) {
            File staleFile = new File(deployDirectory, line.trim());

            if (!staleFile.exists()) {
                continue;
            }

            boolean success = FileUtils.deleteQuietly(staleFile);

            if (success) {
                continue;
            }

            String message = "Unable to delete file " + staleFile.getAbsolutePath();

            _log.error(message);

            responseJSONObject.put(JSONKeys.ERROR, message);
        }
    }

    FileUtils.deleteQuietly(partialAppDeletePropsFile);

    if (_log.isInfoEnabled()) {
        _log.info("Successfully updated " + context);
    }
}

From source file:graph.inference.module.TransitiveWorker.java

protected void transitiveSearch(QueryObject queryObj) {
    // Find the index
    int atomicIndex = queryObj.getAtomicIndex();
    int varIndex = (atomicIndex == 1) ? 2 : 1;
    DAGNode atomic = queryObj.getAtomic();
    if (atomic == null)
        return;// w  w  w.j  a v  a 2  s  .c  o  m

    Queue<DAGNode> toCheck = new LinkedList<>();
    toCheck.add(atomic);
    Collection<Edge> genlEdges = relatedModule_.findEdgeByNodes((DAGNode) queryObj.getNode(0));

    while (!toCheck.isEmpty()) {
        DAGNode n = querier_.getExpanded(toCheck.poll());

        if (queryObj.isCompleted(n))
            continue;
        queryObj.addCompleted(n);

        // Function checking
        if (atomicIndex == 1 && n instanceof OntologyFunction) {
            Collection<DAGNode> functionEdges = querier_.functionResults((OntologyFunction) n,
                    CommonConcepts.RESULT_GENL);
            for (DAGNode resultNode : functionEdges) {
                if (queryObj.isProof() && resultNode.equals(queryObj.getNode(2))) {
                    queryObj.addResult(new Substitution(), CommonConcepts.GENLS.getNode(dag_), n, resultNode);
                    return;
                }
                if (queryObj.addResult(CommonConcepts.GENLS.getNode(dag_), n, resultNode))
                    return;
                toCheck.add(resultNode);
            }
        }

        // Intersect the collections
        Collection<Edge> nodeEdges = relatedModule_.execute(n, atomicIndex + 1);
        nodeEdges = CollectionUtils.retainAll(nodeEdges, genlEdges);

        // Self genls check
        if (n == atomic) {
            Collection<Edge> selfEdges = nodeEdges;
            if (selfEdges.isEmpty()) {
                selfEdges = relatedModule_.execute(atomic, varIndex + 1);
                selfEdges = CollectionUtils.retainAll(selfEdges, genlEdges);
            }
            if (!selfEdges.isEmpty()) {
                if (queryObj.addResult(queryObj.getNode(0), atomic, n))
                    return;
            }
        }

        // Create the subs
        for (Edge e : nodeEdges) {
            if (!(e.getNodes()[varIndex] instanceof DAGNode))
                continue;
            DAGNode edgeNode = (DAGNode) e.getNodes()[varIndex];

            if (queryObj.isProof() && e.getNodes()[varIndex].equals(queryObj.getNode(2))) {
                queryObj.addResult(new Substitution(), e.getNodes());
                return;
            }
            if (queryObj.addResult(e.getNodes()))
                return;
            toCheck.add(edgeNode);
        }
    }
}

From source file:com.github.rvesse.airline.builder.CliBuilder.java

@Override
public Cli<C> build() {
    CommandMetadata defaultCommandMetadata = null;
    List<CommandMetadata> allCommands = new ArrayList<CommandMetadata>();
    if (defaultCommand != null) {
        defaultCommandMetadata = MetadataLoader.loadCommand(defaultCommand);
    }//  w w  w. j a  va2s. c  om

    List<CommandMetadata> defaultCommandGroup = defaultCommandGroupCommands != null
            ? MetadataLoader.loadCommands(defaultCommandGroupCommands)
            : new ArrayList<CommandMetadata>();

    allCommands.addAll(defaultCommandGroup);
    if (defaultCommandMetadata != null)
        allCommands.add(defaultCommandMetadata);

    // Build groups
    List<CommandGroupMetadata> commandGroups;
    if (groups != null) {
        commandGroups = new ArrayList<CommandGroupMetadata>();
        for (GroupBuilder<C> groupBuilder : groups.values()) {
            commandGroups.add(groupBuilder.build());
        }
    } else {
        commandGroups = new ArrayList<>();
    }

    // Find all commands registered in groups and sub-groups, we use this to
    // check this is a valid CLI with at least 1 command
    for (CommandGroupMetadata group : commandGroups) {
        allCommands.addAll(group.getCommands());
        if (group.getDefaultCommand() != null)
            allCommands.add(group.getDefaultCommand());

        // Make sure to scan sub-groups
        Queue<CommandGroupMetadata> subGroups = new LinkedList<CommandGroupMetadata>();
        subGroups.addAll(group.getSubGroups());
        while (!subGroups.isEmpty()) {
            CommandGroupMetadata subGroup = subGroups.poll();
            allCommands.addAll(subGroup.getCommands());
            if (subGroup.getDefaultCommand() != null)
                allCommands.add(subGroup.getDefaultCommand());
            subGroups.addAll(subGroup.getSubGroups());
        }
    }

    // add commands to groups based on the value of groups in the @Command
    // annotations
    // rather than change the entire way metadata is loaded, I figured just
    // post-processing was an easier, yet uglier, way to go
    MetadataLoader.loadCommandsIntoGroupsByAnnotation(allCommands, commandGroups, defaultCommandGroup);

    // Build restrictions
    // Use defaults if none specified
    if (restrictions.size() == 0)
        withDefaultRestrictions();

    if (allCommands.size() == 0)
        throw new IllegalArgumentException("Must specify at least one command to create a CLI");

    // Build metadata objects
    GlobalMetadata<C> metadata = MetadataLoader.<C>loadGlobal(name, description, defaultCommandMetadata,
            ListUtils.unmodifiableList(defaultCommandGroup), ListUtils.unmodifiableList(commandGroups),
            ListUtils.unmodifiableList(restrictions), this.parserBuilder.build());

    return new Cli<C>(metadata);
}