Example usage for java.util.concurrent ExecutorService execute

List of usage examples for java.util.concurrent ExecutorService execute

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService execute.

Prototype

void execute(Runnable command);

Source Link

Document

Executes the given command at some time in the future.

Usage

From source file:com.tage.calcite.adapter.druid.DruidConnectionImpl.java

/** Executes a request and returns the resulting rows as an
 * {@link Enumerable}, running the parser in a thread provided by
 * {@code service}. *//*  w ww  .j  av a2 s  . co  m*/
public Enumerable<Row> enumerable(final com.tage.calcite.adapter.druid.QueryType queryType,
        final String request, final List<String> fieldNames, final ExecutorService service) throws IOException {
    return new AbstractEnumerable<Row>() {
        public Enumerator<Row> enumerator() {
            final BlockingQueueEnumerator<Row> enumerator = new BlockingQueueEnumerator<>();
            final RunnableQueueSink sink = new RunnableQueueSink() {
                public void send(Row row) throws InterruptedException {
                    enumerator.queue.put(row);
                }

                public void end() {
                    enumerator.done.set(true);
                }

                public void setSourceEnumerable(Enumerable<Row> enumerable) throws InterruptedException {
                    for (Row row : enumerable) {
                        send(row);
                    }
                    end();
                }

                public void run() {
                    try {
                        final Page page = new Page();
                        final List<Primitive> fieldTypes = Collections.nCopies(fieldNames.size(), null);
                        request(queryType, request, this, fieldNames, fieldTypes, page);
                        enumerator.done.set(true);
                    } catch (Throwable e) {
                        enumerator.throwableHolder.set(e);
                        enumerator.done.set(true);
                    }
                }
            };
            service.execute(sink);
            return enumerator;
        }
    };
}

From source file:com.github.cukedoctor.jenkins.CukedoctorPublisher.java

@Override
public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener)
        throws IOException, InterruptedException {

    FilePath workspaceJsonSourceDir;//most of the time on slave
    FilePath workspaceJsonTargetDir;//always on master
    if (!hasText(featuresDir)) {
        workspaceJsonSourceDir = workspace;
        workspaceJsonTargetDir = getMasterWorkspaceDir(build);
    } else {/*from  www .j  av a  2s.  com*/
        workspaceJsonSourceDir = new FilePath(workspace, featuresDir);
        workspaceJsonTargetDir = new FilePath(getMasterWorkspaceDir(build), featuresDir);
    }

    logger = listener.getLogger();
    workspaceJsonSourceDir.copyRecursiveTo("**/*.json,**/cukedoctor-intro.adoc,**/cukedoctor.properties",
            workspaceJsonTargetDir);

    System.setProperty("INTRO_CHAPTER_DIR", workspaceJsonTargetDir.getRemote());

    logger.println("");
    logger.println("Generating living documentation for " + build.getFullDisplayName()
            + " with the following arguments: ");
    logger.println("Features dir: " + workspaceJsonSourceDir.getRemote());
    logger.println("Format: " + format.getFormat());
    logger.println("Toc: " + toc.getToc());
    logger.println("Title: " + title);
    logger.println("Numbered: " + Boolean.toString(numbered));
    logger.println("Section anchors: " + Boolean.toString(sectAnchors));
    logger.println("Hide features section: " + Boolean.toString(hideFeaturesSection));
    logger.println("Hide summary: " + Boolean.toString(hideSummary));
    logger.println("Hide scenario keyword: " + Boolean.toString(hideScenarioKeyword));
    logger.println("Hide step time: " + Boolean.toString(hideStepTime));
    logger.println("Hide tags: " + Boolean.toString(hideTags));
    logger.println("");

    Result result = Result.SUCCESS;
    List<Feature> features = FeatureParser.findAndParse(workspaceJsonTargetDir.getRemote());
    if (!features.isEmpty()) {
        if (!hasText(title)) {
            title = "Living Documentation";
        }

        logger.println("Found " + features.size() + " feature(s)...");

        File targetBuildDirectory = new File(build.getRootDir(), CukedoctorBaseAction.BASE_URL);
        if (!targetBuildDirectory.exists()) {
            boolean created = targetBuildDirectory.mkdirs();
            if (!created) {
                listener.error("Could not create file at location: " + targetBuildDirectory.getAbsolutePath());
                result = Result.UNSTABLE;
            }
        }

        GlobalConfig globalConfig = GlobalConfig.getInstance();
        DocumentAttributes documentAttributes = globalConfig.getDocumentAttributes().backend(format.getFormat())
                .toc(toc.getToc()).numbered(numbered).sectAnchors(sectAnchors).docTitle(title);

        globalConfig.getLayoutConfig().setHideFeaturesSection(hideFeaturesSection);

        globalConfig.getLayoutConfig().setHideSummarySection(hideSummary);

        globalConfig.getLayoutConfig().setHideScenarioKeyword(hideScenarioKeyword);

        globalConfig.getLayoutConfig().setHideStepTime(hideStepTime);

        globalConfig.getLayoutConfig().setHideTags(hideTags);

        String outputPath = targetBuildDirectory.getAbsolutePath();
        CukedoctorBuildAction action = new CukedoctorBuildAction(build);
        final ExecutorService pool = Executors.newFixedThreadPool(4);
        if ("all".equals(format.getFormat())) {
            File allHtml = new File(
                    outputPath + System.getProperty("file.separator") + CukedoctorBaseAction.ALL_DOCUMENTATION);
            if (!allHtml.exists()) {
                boolean created = allHtml.createNewFile();
                if (!created) {
                    listener.error("Could not create file at location: " + allHtml.getAbsolutePath());
                    result = Result.UNSTABLE;
                }
            }
            InputStream is = null;
            OutputStream os = null;
            try {
                is = getClass().getResourceAsStream("/" + CukedoctorBaseAction.ALL_DOCUMENTATION);
                os = new FileOutputStream(allHtml);

                int copyResult = IOUtils.copy(is, os);
                if (copyResult == -1) {
                    listener.error("File is too big.");//will never reach here but findbugs forced it...
                    result = Result.UNSTABLE;
                }
            } finally {
                if (is != null) {
                    is.close();
                }
                if (os != null) {
                    os.close();
                }
            }

            action.setDocumentationPage(CukedoctorBaseAction.ALL_DOCUMENTATION);
            pool.execute(runAll(features, documentAttributes, outputPath));
        } else {
            action.setDocumentationPage("documentation." + format.getFormat());
            pool.execute(run(features, documentAttributes, outputPath));
        }

        build.addAction(action);
        pool.shutdown();
        try {
            if (format.equals(FormatType.HTML)) {
                pool.awaitTermination(5, TimeUnit.MINUTES);
            } else {
                pool.awaitTermination(15, TimeUnit.MINUTES);
            }
        } catch (final InterruptedException e) {
            Thread.interrupted();
            listener.error(
                    "Your documentation is taking too long to be generated. Halting the generation now to not throttle Jenkins.");
            result = Result.FAILURE;
        }

        if (result.equals(Result.SUCCESS)) {
            listener.hyperlink("../" + CukedoctorBaseAction.BASE_URL, "Documentation generated successfully!");
            logger.println("");
        }

    } else {
        logger.println(String.format("No features Found in %s. %sLiving documentation will not be generated.",
                workspaceJsonTargetDir.getRemote(), "\n"));

    }

    build.setResult(result);
}

From source file:com.clust4j.algo.KMeansTests.java

@Test
public void testSynchronization() {
    /*//from w w  w. ja  v  a  2s  .c o m
     * This is hard to test!
     */
    KMeans km = new KMeans(data_, new KMeansParameters(3).setVerbose(true));

    // create a 2 thread pool with a small buffer for the runnable jobs
    ExecutorService threadPool = Executors.newCachedThreadPool();

    // this job executes the fit after a brief nap
    KMRunnable first = new KMRunnable(km) {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
                this.model.fit();
                this.hasRun = true;
            } catch (InterruptedException e) {
                System.out.println("First failed!");
            } finally {
                assertNotNull(this.model.getLabels());
            }
        }
    };

    // this tries to get the volatile objs too early...
    KMRunnable second = new KMRunnable(first) {
        @Override
        public void run() {
            boolean was_fit = true;

            try {
                this.labels = model.getLabels();
                this.hasRun = true;
            } catch (ModelNotFitException m) {
                this.labels = null;
                was_fit = false;
            } finally {
                System.out.println("Second");
                assertFalse(was_fit);
                assertNull(this.labels);
            }
        }
    };

    // submit 2 jobs that take a while to run
    threadPool.execute(first);
    threadPool.execute(second);
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.createJobs.CreateJobsMainHandler.java

/**
 * Creates a job//from  ww w .j  a v  a  2 s .co  m
 */
private int createJobs(Map<Object, Object> dataMap) {
    try {
        User user = (User) dataMap.get("user");
        String currentCompanyId = (String) dataMap.get("currentCompanyId");
        String jobName = (String) dataMap.get("jobName");
        String comment = (String) dataMap.get("comment");
        String tmpFolderName = (String) dataMap.get("tmpFolderName");
        String[] filePaths = (String[]) dataMap.get("jobFilePath");
        String[] l10nAndfileProfiles = (String[]) dataMap.get("fileProfile");
        String[] targetLocales = (String[]) dataMap.get("targetLocale");
        String priority = (String) dataMap.get("priority");
        String baseFolder = (String) dataMap.get("baseFolder");
        if (StringUtils.isNotEmpty(baseFolder)) {
            this.saveBaseFolder(user.getUserId(), SELECTED_FOLDER, baseFolder);
        }
        String[] isSwitched = (String[]) dataMap.get("isSwitched");
        String attachmentName = (String) dataMap.get("attachment");
        String baseStorageFolder = (String) dataMap.get("baseStorageFolder");
        String attribute = (String) dataMap.get("attributeString");

        BasicL10nProfile l10Profile = getBasicL10Profile(l10nAndfileProfiles);
        // init files and file profiles infomation
        List<File> sourceFilesList = new ArrayList<File>();
        List<String> descList = new ArrayList<String>();
        List<String> fpIdList = new ArrayList<String>();
        List<FileProfile> fileProfileList = new ArrayList<FileProfile>();
        // init target locale infomation
        String locs = this.initTargetLocale(targetLocales);
        // for GBS-2137, initialize the job with "IN_QUEUE" state
        SessionManager sessionMgr = (SessionManager) dataMap.get(SESSION_MANAGER);
        String uuid = sessionMgr.getAttribute("uuid") == null ? null : (String) sessionMgr.getAttribute("uuid");
        sessionMgr.removeElement("uuid");

        Job job = JobCreationMonitor.initializeJob(jobName, uuid, user.getUserId(), l10Profile.getId(),
                priority, Job.IN_QUEUE);
        this.initDescAndFileProfile(descList, fpIdList, filePaths, l10nAndfileProfiles, l10Profile,
                tmpFolderName, job, isSwitched, sourceFilesList, fileProfileList);
        Map<String, long[]> filesToFpId = FileProfileUtil.excuteScriptOfFileProfile(descList, fileProfileList,
                job);
        Set<String> fileNames = filesToFpId.keySet();
        Integer pageCount = new Integer(fileNames.size());
        String jobUuid = uuid == null ? ((JobImpl) job).getUuid() : uuid;
        List<JobAttribute> jobAttribtues = getJobAttributes(attribute, l10Profile);
        // cache job attributes if there are any
        if (jobAttribtues != null && jobAttribtues.size() != 0) {
            RuntimeCache.addJobAtttibutesCache(jobUuid, jobAttribtues);
        }

        int count = 0;
        List<CxeMessage> cxeMsgs = new ArrayList<CxeMessage>();
        for (Iterator<String> i = fileNames.iterator(); i.hasNext();) {
            String fileName = i.next();
            long[] tmp = filesToFpId.get(fileName);
            String fileProfileId = String.valueOf(tmp[0]);
            int exitValue = (int) tmp[1];

            String key = jobName + fileName + ++count;
            CxeProxy.setTargetLocales(key, locs);
            // If use JMS
            if (FileImportUtil.USE_JMS) {
                CxeProxy.importFromFileSystem(fileName, String.valueOf(job.getId()), jobName, fileProfileId,
                        pageCount, count, 1, 1, Boolean.TRUE, Boolean.FALSE, CxeProxy.IMPORT_TYPE_L10N,
                        exitValue, priority);
            }
            // If not use JMS, we control the concurrent threads number
            else {
                CxeMessage cxeMessage = CxeProxy.formCxeMessageType(fileName, String.valueOf(job.getId()),
                        jobName, fileProfileId, pageCount, count, 1, 1, Boolean.TRUE, Boolean.FALSE,
                        CxeProxy.IMPORT_TYPE_L10N, exitValue, priority, String.valueOf(job.getCompanyId()));
                cxeMsgs.add(cxeMessage);
            }
        }

        // If not use JMS
        if (cxeMsgs.size() > 0) {
            ExecutorService pool = Executors.newFixedThreadPool(100);
            for (CxeMessage msg : cxeMsgs) {
                FileImportRunnable runnable = new FileImportRunnable(msg);
                Thread t = new MultiCompanySupportedThread(runnable);
                pool.execute(t);
            }
            pool.shutdown();
        }

        // save job attributes if there are any
        if (jobAttribtues != null) {
            saveAttributes(jobAttribtues, currentCompanyId, job);
        }

        // save job comment
        if (!StringUtils.isEmpty(comment) || !StringUtils.isEmpty(attachmentName)) {
            String dir = convertFilePath(AmbFileStoragePathUtils.getFileStorageDirPath(job.getCompanyId()))
                    + File.separator + "GlobalSight" + File.separator + "CommentReference" + File.separator
                    + "tmp" + File.separator + baseStorageFolder.split(",")[0];
            SaveCommentThread sct = new SaveCommentThread(jobName, comment, attachmentName, user.getUserId(),
                    dir);
            sct.start();
        }
        // Send email at the end.
        Project project = l10Profile.getProject();
        if (comment == null || comment.equals("null")) {
            comment = "";
        }
        sendUploadCompletedEmail(filePaths, fpIdList, jobName, comment, new Date(), user, currentCompanyId,
                project);

        // after all steps, delete files that are used to create job
        for (File source : sourceFilesList) {
            FileUtil.deleteFile(source);
        }
        // delete the upload directory
        File uploads = new File(AmbFileStoragePathUtils.getCxeDocDir() + File.separator + TMP_FOLDER_NAME
                + File.separator + tmpFolderName);
        if (uploads != null && uploads.exists()) {
            FileUtil.deleteFile(uploads);
        }
        return SUCCEED;
    } catch (FileNotFoundException ex) {
        logger.error("Cannot find the tmp uploaded files.", ex);
        return FAIL;
    } catch (Exception e) {
        logger.error("Create job failed.", e);
        return FAIL;
    }
}

From source file:ubic.BAMSandAllen.optimize.GreedyMultiThreaded.java

/**
 * Iteratively remove rows from the B data matrix of the matrix pair, each
 * time increasing the correlation the maximum possible
 * //from   w w  w.j a va2  s.c o  m
 * @param iterations
 *            number of iterations to perform
 * @param slow
 *            indicates whether to re-compute regressions for every gene
 *            removal test
 * @throws Exception
 */
public void run(int iterations, boolean slow, boolean keepSign) throws Exception {
    StopWatch watch = new StopWatch();
    watch.start();
    StopWatch smallWatch = new StopWatch();
    long startTime = System.currentTimeMillis();

    double firstBaseLine = pair.getCorrelation(true);
    // make it more negative if it starts below zero
    boolean increase = firstBaseLine > 0;
    // force the increase on random runs with low correlation
    if (Math.abs(firstBaseLine) < 0.1)
        increase = true;

    // if we are going against the current correlation sign - eg go from
    // positive correlation to negative
    if (!keepSign) {
        increase = !increase;
    }

    List<GreedyThreadRunner> runners = new LinkedList<GreedyThreadRunner>();

    // create the runners
    for (int threadInd = 0; threadInd < threads; threadInd++) {
        MatrixPair pairCopy = (MatrixPair) deepCopy(pair);
        runners.add(new GreedyThreadRunner(pairCopy, slow, increase));
    }

    for (int i = 0; i < iterations; i++) {
        smallWatch.reset();
        smallWatch.start();

        ExecutorService pool;
        pool = Executors.newFixedThreadPool(threads);
        // divide up the rows
        List<String> rows = pair.getMatrixBDataRows();
        List<Collection<String>> splits = split(rows, threads);

        double baseLine = pair.getCorrelation(true);

        log.info("Base correlation:" + baseLine + " size:" + pair.getMatrixBDataRows().size());

        // set the baseline and call the runners
        for (int threadInd = 0; threadInd < threads; threadInd++) {
            GreedyThreadRunner runner = runners.get(threadInd);
            runner.setBaseline(baseLine);

            // set residual calculation triangles to match accross all
            // threads, if we are doing partial regression
            if (pair instanceof ConnectivityAndAllenPartialExpressionMatrixPair) {
                ConnectivityAndAllenPartialExpressionMatrixPair partialPair = ((ConnectivityAndAllenPartialExpressionMatrixPair) pair);
                // only do it if we are regressing on expression
                if (partialPair
                        .getRegressType() == ConnectivityAndAllenPartialExpressionMatrixPair.RegressMatrix.BOTH
                        && partialPair
                                .getRegressType() == ConnectivityAndAllenPartialExpressionMatrixPair.RegressMatrix.EXPRESSION) {
                    RegressionVector triangles = partialPair.getTrianglesMatrixB();
                    runner.setTrianglesMatrixB(triangles);
                }
            }

            runner.setRowsToTest(splits.get(threadInd));
            // log.info( "Split size:" + splits.get( threadInd ).size() );
            pool.execute(runner);
        }

        // log.info( "Waiting for threads to finish" );
        pool.shutdown();
        pool.awaitTermination(15, TimeUnit.MINUTES);

        // go through all the runners and get results
        double bestIncrease = Double.MAX_VALUE * -1;
        String bestRow = null;
        for (GreedyThreadRunner runner : runners) {
            double diff = runner.getBestIncrease();
            if (!increase) {
                diff *= -1;
            }
            if (diff > bestIncrease) {
                bestRow = runner.getBestRow();
                bestIncrease = diff;
            }
        }

        if (bestRow == null) {
            log.info("No best row found " + pair.getCorrelation(true));
            log.info("Putting remaining " + rows.size() + " genes at end of file");
            for (String row : rows) {
                outputRowToFile(startTime, row);
                FileTools.stringToFile(i + "," + baseLine + "," + row + "\n",
                        new File(SetupParameters.getDataFolder() + "LOOResults." + startTime + ".txt"), true);
            }
            break;
        }

        pair.removeMatrixBDataRowFast(bestRow);
        for (GreedyThreadRunner runner : runners) {
            runner.removeRow(bestRow);
        }

        outputRowToFile(startTime, bestRow);
        // write correlation

        FileTools.stringToFile(i + "," + baseLine + "," + bestRow + "\n",
                new File(SetupParameters.getDataFolder() + "LOOResults." + startTime + ".txt"), true);

        int eta = (int) (smallWatch.getTime() / 1000) / 2 * pair.getMatrixBDataRows().size() / 3600;
        log.info(bestRow + " changes correlation by " + bestIncrease + " time:" + (smallWatch.getTime() / 1000)
                + "s total:" + (watch.getTime() / 1000) + "s estimated hours remaining:" + eta);
    }
    log.info("Start time:" + startTime);
    FileTools.stringToFile(startTime + "\n",
            new File(SetupParameters.getDataFolder() + "Link." + startTime + ".txt"), true);

    // make a link between it's output files and starttime - akward hack
    File jarFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
    FileTools.stringToFile(jarFile.toString() + "\n",
            new File(SetupParameters.getDataFolder() + "Link." + startTime + ".txt"), true);

}

From source file:org.apache.maven.plugin.invoker.AbstractInvokerMojo.java

/**
 * Runs the specified build jobs.//from   w  w w .  j ava 2  s .c o  m
 *
 * @param projectsDir The base directory of all projects, must not be <code>null</code>.
 * @param buildJobs   The build jobs to run must not be <code>null</code> nor contain <code>null</code> elements.
 * @throws org.apache.maven.plugin.MojoExecutionException
 *          If any build could not be launched.
 */
private void runBuilds(final File projectsDir, BuildJob[] buildJobs) throws MojoExecutionException {
    if (!localRepositoryPath.exists()) {
        localRepositoryPath.mkdirs();
    }

    //-----------------------------------------------
    // interpolate settings file
    //-----------------------------------------------

    File interpolatedSettingsFile = null;
    if (settingsFile != null) {
        if (cloneProjectsTo != null) {
            interpolatedSettingsFile = new File(cloneProjectsTo, "interpolated-" + settingsFile.getName());
        } else {
            interpolatedSettingsFile = new File(settingsFile.getParentFile(),
                    "interpolated-" + settingsFile.getName());
        }
        buildInterpolatedFile(settingsFile, interpolatedSettingsFile);
    }

    //-----------------------------------------------
    // merge settings file
    //-----------------------------------------------

    SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();

    File mergedSettingsFile;
    Settings mergedSettings = this.settings;
    if (mergeUserSettings) {
        if (interpolatedSettingsFile != null) {
            // Have to merge the specified settings file (dominant) and the one of the invoking Maven process
            Reader reader = null;
            try {
                reader = new XmlStreamReader(interpolatedSettingsFile);
                SettingsXpp3Reader settingsReader = new SettingsXpp3Reader();
                Settings dominantSettings = settingsReader.read(reader);

                // MINVOKER-137: NPE on dominantSettings.getRuntimeInfo()
                // DefaultMavenSettingsBuilder does the same trick
                if (dominantSettings.getRuntimeInfo() == null) {
                    RuntimeInfo rtInfo = new RuntimeInfo(dominantSettings);
                    rtInfo.setFile(interpolatedSettingsFile);
                    dominantSettings.setRuntimeInfo(rtInfo);
                }

                Settings recessiveSettings = cloneSettings();

                SettingsUtils.merge(dominantSettings, recessiveSettings, TrackableBase.USER_LEVEL);

                mergedSettings = dominantSettings;
                getLog().debug("Merged specified settings file with settings of invoking process");
            } catch (XmlPullParserException e) {
                throw new MojoExecutionException("Could not read specified settings file", e);
            } catch (IOException e) {
                throw new MojoExecutionException("Could not read specified settings file", e);
            } finally {
                IOUtil.close(reader);
            }
        }
    }
    if (this.settingsFile != null && !mergeUserSettings) {
        mergedSettingsFile = interpolatedSettingsFile;
    } else {
        try {
            mergedSettingsFile = File.createTempFile("invoker-settings", ".xml");

            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(mergedSettingsFile);
                settingsWriter.write(fileWriter, mergedSettings);
            } finally {
                IOUtil.close(fileWriter);
            }

            if (getLog().isDebugEnabled()) {
                getLog().debug("Created temporary file for invoker settings.xml: "
                        + mergedSettingsFile.getAbsolutePath());
            }
        } catch (IOException e) {
            throw new MojoExecutionException("Could not create temporary file for invoker settings.xml", e);
        }
    }
    final File finalSettingsFile = mergedSettingsFile;

    if (mavenHome != null) {
        actualMavenVersion = SelectorUtils.getMavenVersion(mavenHome);
    } else {
        actualMavenVersion = SelectorUtils.getMavenVersion();
    }
    scriptRunner.setGlobalVariable("mavenVersion", actualMavenVersion);

    if (javaHome != null) {
        resolveExternalJreVersion();
    } else {
        actualJreVersion = SelectorUtils.getJreVersion();
    }

    try {
        if (isParallelRun()) {
            getLog().info("use parallelThreads " + parallelThreads);

            ExecutorService executorService = Executors.newFixedThreadPool(parallelThreads);
            for (final BuildJob job : buildJobs) {
                executorService.execute(new Runnable() {
                    public void run() {
                        try {
                            runBuild(projectsDir, job, finalSettingsFile);
                        } catch (MojoExecutionException e) {
                            throw new RuntimeException(e.getMessage(), e);
                        }
                    }
                });
            }

            try {
                executorService.shutdown();
                // TODO add a configurable time out
                executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                throw new MojoExecutionException(e.getMessage(), e);
            }
        } else {
            for (BuildJob job : buildJobs) {
                runBuild(projectsDir, job, finalSettingsFile);
            }
        }
    } finally {
        if (interpolatedSettingsFile != null && cloneProjectsTo == null) {
            interpolatedSettingsFile.delete();
        }
        if (mergedSettingsFile != null && mergedSettingsFile.exists()) {
            mergedSettingsFile.delete();
        }
    }
}

From source file:MSUmpire.DIA.DIAPack.java

public void AssignQuant(boolean export) throws IOException, SQLException {
    Logger.getRootLogger().info("Assign peak cluster to identified peptides");
    GenerateClusterScanNomapping();//from www. j a v  a  2  s .  c  o  m

    ExecutorService executorPool = null;
    for (PeakCluster cluster : MS1FeatureMap.PeakClusters) {
        cluster.Identified = false;
    }

    for (PepIonID pepIonID : IDsummary.GetPepIonList().values()) {
        pepIonID.MS1PeakClusters = new ArrayList<>();
        pepIonID.MS2UnfragPeakClusters = new ArrayList<>();
    }

    //Assign precursor features and grouped fragments for all identified peptide ions for a isolation window
    for (LCMSPeakDIAMS2 DIAWindow : DIAWindows) {
        DIA_window_Quant dia_w = new DIA_window_Quant(GetQ1Name(), GetQ2Name(), GetQ3Name(), ScanClusterMap_Q1,
                ScanClusterMap_Q2, ScanClusterMap_Q3, MS1FeatureMap, DIAWindow, IDsummary, NoCPUs);
        dia_w.run();
    }

    executorPool = Executors.newFixedThreadPool(NoCPUs);

    //Match fragments and calculate quantification for each peptide ion
    for (PepIonID pepIonID : IDsummary.GetPepIonList().values()) {
        DIAAssignQuantUnit quantunit = new DIAAssignQuantUnit(pepIonID, MS1FeatureMap, parameter);
        executorPool.execute(quantunit);
    }
    executorPool.shutdown();

    try {
        executorPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
        Logger.getRootLogger().info("interrupted..");
    }

    if (export) {
        ExportID();
    }
}

From source file:MSUmpire.DIA.DIAPack.java

public void GenerateRawMGF() throws IOException, Exception {

    if (RawMGFExist()) {
        return;//from ww  w .  j a va  2s .co m
    }
    Logger.getRootLogger().info("Extracting pseudo MS/MS spectra with raw intensity");
    HashMap<Integer, ArrayList<PseudoMSMSProcessing>> ScanList = new HashMap<>();
    HashMap<String, PseudoMSMSProcessing> UnfragScanList = new HashMap<>();
    parameter.BoostComplementaryIon = false;
    ExecutorService executorPool = Executors.newFixedThreadPool(NoCPUs);
    for (LCMSPeakDIAMS2 DIAwindow : DIAWindows) {
        DIAwindow.ReadPeakCluster();
        DIAwindow.ReadPrecursorFragmentClu2Cur();
        DIAwindow.BuildFragmentMS1ranking();
        DIAwindow.FilterByCriteria();
        DIAwindow.BuildFragmentUnfragranking();
        DIAwindow.FilterByCriteriaUnfrag();
        for (PeakCluster ms1cluster : MS1FeatureMap.PeakClusters) {
            if (DIAwindow.DIA_MZ_Range.getX() <= ms1cluster.GetMaxMz()
                    && DIAwindow.DIA_MZ_Range.getY() >= ms1cluster.TargetMz()
                    && DIAwindow.FragmentsClu2Cur.containsKey(ms1cluster.Index)) {
                DIAwindow.ExtractFragmentForPeakCluser(ms1cluster);
                if (DIAwindow.Last_MZ_Range == null || DIAwindow.Last_MZ_Range.getY() < ms1cluster.TargetMz()) {
                    PseudoMSMSProcessing mSMSProcessing = new PseudoMSMSProcessing(ms1cluster, parameter);
                    executorPool.execute(mSMSProcessing);
                    if (!ScanList.containsKey(ms1cluster.Index)) {
                        ScanList.put(ms1cluster.Index, new ArrayList<PseudoMSMSProcessing>());
                    }
                    ScanList.get(ms1cluster.Index).add(mSMSProcessing);
                }
            }
        }

        for (PeakCluster ms2cluster : DIAwindow.PeakClusters) {
            if (DIAwindow.DIA_MZ_Range.getX() <= ms2cluster.TargetMz()
                    && DIAwindow.DIA_MZ_Range.getY() >= ms2cluster.TargetMz()
                    && DIAwindow.UnFragIonClu2Cur.containsKey(ms2cluster.Index)) {
                DIAwindow.ExtractFragmentForUnfragPeakCluser(ms2cluster);
                PseudoMSMSProcessing mSMSProcessing = new PseudoMSMSProcessing(ms2cluster, parameter);
                executorPool.execute(mSMSProcessing);
                UnfragScanList.put(DIAwindow.WindowID + ";" + ms2cluster.Index, mSMSProcessing);
            }
        }
        DIAwindow.ClearAllPeaks();
        System.gc();
        Logger.getRootLogger()
                .info("(Memory usage:" + Math.round(
                        (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576)
                        + "MB)");
    }
    executorPool.shutdown();

    try {
        executorPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    } catch (InterruptedException e) {
        Logger.getRootLogger().info("interrupted..");
    }
    ReadScanNoMapping();
    String mgffile = GetSkylineFolder() + GetForLibQ1Name() + ".mgf";
    FileWriter mgfWriter = new FileWriter(mgffile, false);

    for (final int ScanNo : new java.util.TreeSet<>(ScanClusterMap_Q1.keySet())) {
        int ClusterIndex = ScanClusterMap_Q1.get(ScanNo);
        XYPointCollection Scan = new XYPointCollection();
        PseudoMSMSProcessing mSMSProcessing = null;
        for (PseudoMSMSProcessing MS2Processing : ScanList.get(ClusterIndex)) {
            mSMSProcessing = MS2Processing;
            for (PrecursorFragmentPairEdge fragmentClusterUnit : MS2Processing.fragments) {
                Scan.AddPointKeepMaxIfValueExisted(fragmentClusterUnit.FragmentMz,
                        fragmentClusterUnit.Intensity);
            }
        }
        StringBuilder mgfString = new StringBuilder();
        mgfString.append("BEGIN IONS\n");
        mgfString.append("PEPMASS=" + mSMSProcessing.Precursorcluster.TargetMz() + "\n");
        mgfString.append("CHARGE=" + mSMSProcessing.Precursorcluster.Charge + "+\n");
        mgfString.append("RTINSECONDS=" + mSMSProcessing.Precursorcluster.PeakHeightRT[0] * 60f + "\n");
        mgfString.append("TITLE=ClusterIndex:" + mSMSProcessing.Precursorcluster.Index + "\n");
        for (int i = 0; i < Scan.PointCount(); i++) {
            mgfString.append(Scan.Data.get(i).getX()).append(" ").append(Scan.Data.get(i).getY()).append("\n");
        }
        mgfString.append("END IONS\n\n");
        mgfWriter.write(mgfString.toString());
    }
    mgfWriter.close();

    ////////////////////////////////////////////////////////////////////////////////
    String mgffile2 = GetSkylineFolder() + GetForLibQ2Name() + ".mgf";
    FileWriter mgfWriter2 = new FileWriter(mgffile2, false);

    for (final int ScanNo : new java.util.TreeSet<>(ScanClusterMap_Q2.keySet())) {
        int ClusterIndex = ScanClusterMap_Q2.get(ScanNo);
        XYPointCollection Scan = new XYPointCollection();
        PseudoMSMSProcessing mSMSProcessing = null;
        for (PseudoMSMSProcessing MS2Processing : ScanList.get(ClusterIndex)) {
            mSMSProcessing = MS2Processing;
            for (PrecursorFragmentPairEdge fragmentClusterUnit : MS2Processing.fragments) {
                Scan.AddPointKeepMaxIfValueExisted(fragmentClusterUnit.FragmentMz,
                        fragmentClusterUnit.Intensity);
            }
        }
        StringBuilder mgfString = new StringBuilder();
        mgfString.append("BEGIN IONS\n");
        mgfString.append("PEPMASS=" + mSMSProcessing.Precursorcluster.TargetMz() + "\n");
        mgfString.append("CHARGE=" + mSMSProcessing.Precursorcluster.Charge + "+\n");
        mgfString.append("RTINSECONDS=" + mSMSProcessing.Precursorcluster.PeakHeightRT[0] * 60f + "\n");
        mgfString.append("TITLE=ClusterIndex:" + mSMSProcessing.Precursorcluster.Index + "\n");
        for (int i = 0; i < Scan.PointCount(); i++) {
            mgfString.append(Scan.Data.get(i).getX()).append(" ").append(Scan.Data.get(i).getY()).append("\n");
        }
        mgfString.append("END IONS\n\n");
        mgfWriter2.write(mgfString.toString());
    }

    mgfWriter2.close();

    ////////////////////////////////
    String mgffile3 = GetSkylineFolder() + GetForLibQ3Name() + ".mgf";
    FileWriter mgfWriter3 = new FileWriter(mgffile3, false);
    mzXMLParser Q3mzxml = new mzXMLParser(FilenameUtils.getFullPath(Filename) + GetQ3Name() + ".mzXML",
            parameter, SpectralDataType.DataType.DDA, null, NoCPUs);
    Q3mzxml.GetAllScanCollectionByMSLabel(false, true, false, false);
    for (final int ScanNo : new java.util.TreeSet<>(ScanClusterMap_Q3.keySet())) {
        String key = ScanClusterMap_Q3.get(ScanNo);
        XYPointCollection Scan = new XYPointCollection();
        PseudoMSMSProcessing mSMSProcessing = UnfragScanList.get(key);

        for (PrecursorFragmentPairEdge fragmentClusterUnit : mSMSProcessing.fragments) {
            Scan.AddPointKeepMaxIfValueExisted(fragmentClusterUnit.FragmentMz, fragmentClusterUnit.Intensity);
        }

        StringBuilder mgfString = new StringBuilder();
        mgfString.append("BEGIN IONS\n");
        mgfString.append("PEPMASS=" + mSMSProcessing.Precursorcluster.TargetMz() + "\n");
        mgfString.append("CHARGE=" + mSMSProcessing.Precursorcluster.Charge + "+\n");
        mgfString.append("RTINSECONDS=" + mSMSProcessing.Precursorcluster.PeakHeightRT[0] * 60f + "\n");
        mgfString.append("TITLE=ClusterIndex:" + mSMSProcessing.Precursorcluster.Index + "\n");
        for (int i = 0; i < Scan.PointCount(); i++) {
            mgfString.append(Scan.Data.get(i).getX()).append(" ").append(Scan.Data.get(i).getY()).append("\n");
        }
        mgfString.append("END IONS\n\n");
        mgfWriter3.write(mgfString.toString());
    }
    mgfWriter3.close();
}

From source file:com.paniclauncher.data.Settings.java

/**
 * This checks the servers hashes.xml file and downloads and new/updated files that differ from what the user has
 *///from   w  ww.j  av a2 s .co m
private void checkForUpdatedFiles() {
    String hashes = null;
    while (hashes == null) {
        hashes = Utils.urlToString(getFileURL("launcher/hashes.xml"));
        if (hashes == null) {
            boolean changed = disableServerGetNext(); // Disable the server and get the next one
            if (!changed) {
                this.offlineMode = true;
                return;
            }
        }
    }
    ArrayList<PanicLauncherDownloadable> downloads = new ArrayList<PanicLauncherDownloadable>();
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(hashes)));
        document.getDocumentElement().normalize();
        NodeList nodeList = document.getElementsByTagName("hash");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                String name = element.getAttribute("name");
                String type = element.getAttribute("type");
                String md5 = element.getAttribute("md5");
                File file = null;
                if (type.equalsIgnoreCase("Root")) {
                    file = new File(configsDir, name);
                } else if (type.equalsIgnoreCase("Images")) {
                    file = new File(imagesDir, name);
                    name = "images/" + name;
                } else if (type.equalsIgnoreCase("Skins")) {
                    file = new File(skinsDir, name);
                    name = "skins/" + name;
                } else if (type.equalsIgnoreCase("Languages")) {
                    file = new File(languagesDir, name);
                    name = "languages/" + name;
                } else if (type.equalsIgnoreCase("Libraries")) {
                    file = new File(librariesDir, name);
                    name = "libraries/" + name;
                } else if (type.equalsIgnoreCase("Launcher")) {
                    String version = element.getAttribute("version");
                    if (!getVersion().equalsIgnoreCase(version)) {
                        if (getVersion().equalsIgnoreCase("%VERSION%")) {
                            continue;
                        } else {
                            log("Update to Launcher found. Current version: " + this.version + ", New version: "
                                    + version);
                            downloadUpdate();
                        }
                    } else {
                        continue;
                    }
                } else {
                    continue; // Don't know what to do with this file so ignore it
                }
                boolean download = false; // If we have to download the file or not
                if (!file.exists()) {
                    download = true; // File doesn't exist so download it
                } else {
                    if (!Utils.getMD5(file).equalsIgnoreCase(md5)) {
                        download = true; // MD5 hashes don't match so download it
                    }
                }

                if (download) {
                    if (!file.canWrite()) {
                        file.delete();
                    }
                    downloads.add(new PanicLauncherDownloadable("launcher/" + name, file, md5));
                    log("Downloading: " + name);
                }
            }
        }
    } catch (SAXException e) {
        this.console.logStackTrace(e);
    } catch (ParserConfigurationException e) {
        this.console.logStackTrace(e);
    } catch (IOException e) {
        this.console.logStackTrace(e);
    }
    ExecutorService executor = Executors.newFixedThreadPool(8);
    for (PanicLauncherDownloadable download : downloads) {
        executor.execute(download);
    }
    executor.shutdown();
    while (!executor.isTerminated()) {
    }
}

From source file:org.deegree.tools.coverage.converter.RasterConverter.java

/**
 * @param numThreads/*  w  w  w.  j  a v  a2  s  .com*/
 * @param outLoc
 * @throws InterruptedException
 * @throws IOException
 * 
 */
private int convert(AbstractCoverage source, String outLoc, int numThreads, String outputFormat,
        final boolean verbose) throws InterruptedException, IOException {
    List<SimpleRaster> tiles = new LinkedList<SimpleRaster>();
    getTiles(source, tiles);
    if (tiles.isEmpty()) {
        System.err.println("Found no raster tiles in source: " + source + ", hence nothing to convert.");
        return 1;
    }
    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    SimpleRaster simpleRaster = tiles.get(0);
    RasterReader rasterReader = ((ByteBufferRasterData) simpleRaster.getRasterData()).getReader();
    File file = rasterReader.file();
    File outputLocation = getOutputLocation(file, outLoc, outputFormat, tiles.size() == 1);

    for (final SimpleRaster tile : tiles) {
        File outFile = null;
        ByteBufferRasterData data = (ByteBufferRasterData) tile.getRasterData();
        RasterReader origReader = data.getReader();
        final String tileName = origReader.getDataLocationId();
        if (tiles.size() == 1) {
            if (outputLocation.isFile()) {
                outFile = outputLocation.getAbsoluteFile();
            } else {
                outFile = createNewFile(outputLocation, tileName, outputFormat);
            }
        } else {
            outFile = createNewFile(outputLocation, tileName, outputFormat);
        }
        final File absTileFilename = outFile;
        System.out.println("Converting: " + tileName + " -> file: " + absTileFilename);

        Runnable command = new Runnable() {
            public void run() {
                try {
                    String thread = Thread.currentThread().getName();
                    System.out.println(thread + " saving... " + tileName);
                    RasterFactory.saveRasterToFile(tile, absTileFilename);
                    tile.dispose();
                    System.out.println(thread + " done... " + tileName);
                } catch (Exception e) {
                    System.err.println("Ar error occurred while processing tile: " + tileName + ": "
                            + e.getLocalizedMessage());
                    if (verbose) {
                        e.printStackTrace();
                    }
                }
            }
        };
        executor.execute(command);

    }
    shutdownExecutorAndWaitForFinish(executor);
    return 0;
}