Example usage for java.util Collections synchronizedList

List of usage examples for java.util Collections synchronizedList

Introduction

In this page you can find the example usage for java.util Collections synchronizedList.

Prototype

public static <T> List<T> synchronizedList(List<T> list) 

Source Link

Document

Returns a synchronized (thread-safe) list backed by the specified list.

Usage

From source file:com.gemini.provision.network.openstack.NetworkProviderOpenStackImpl.java

@Override
public List<GeminiNetworkRouter> getEnvRouters(GeminiTenant tenant, GeminiEnvironment env) {
    //authenticate the session with the OpenStack installation
    OSClient os = OSFactory.builder().endpoint(env.getEndPoint())
            .credentials(env.getAdminUserName(), env.getAdminPassword()).tenantName(tenant.getName())
            .authenticate();//from w  w w  .  j  a v  a 2 s.  c  o  m
    if (os == null) {
        Logger.error("Failed to authenticate Tenant: {}",
                ToStringBuilder.reflectionToString(tenant, ToStringStyle.MULTI_LINE_STYLE));
        return null;
    }

    //get the list of routers from the cloud
    List<? extends Router> osRouters = os.networking().router().list();
    if (osRouters.isEmpty()) {
        Logger.debug("No routers found for Tenant: {}",
                ToStringBuilder.reflectionToString(tenant, ToStringStyle.MULTI_LINE_STYLE));
        return null;
    }

    //convert the Router to GeminiNetworkRouter 
    //TODO: change code to use Dozer Mapper
    List<GeminiNetworkRouter> routers = Collections.synchronizedList(new ArrayList());
    osRouters.stream().forEach(osRouter -> {
        GeminiNetworkRouter nRouter = new GeminiNetworkRouter();
        nRouter.setCloudID(osRouter.getId());
        nRouter.setName(osRouter.getName());
        String gID = osRouter.getExternalGatewayInfo().getNetworkId();
        if (!gID.isEmpty()) {
            //stream through envs, map to stream of GeminiNetwork objects, filter on 
            //the ID and then get the first object... note there will only be one
            //so we can use findOne or findAny
            GeminiNetwork gemGateway = tenant.getEnvironments().stream().map(GeminiEnvironment::getApplications)
                    .flatMap(List::stream).map(GeminiApplication::getNetworks).flatMap(List::stream)
                    .filter(n -> n.getCloudID().equals(gID)).findFirst().get();
            nRouter.setGateway(gemGateway);
        }

        //get the host routes
        List<? extends HostRoute> osHostRoutes = osRouter.getRoutes();
        osHostRoutes.stream().forEach(
                osHostRoute -> nRouter.addRoute(osHostRoute.getNexthop(), osHostRoute.getDestination()));

        //TODO: get the interfaces attached to the router
        //OPEN STACK DOES NOT HAVE THIS FUNCTIONALITY - THIS IS RIDICULOUS!!!!! WE HAVE TO CREATE IT EACH TIME

        //add it to the tenant
        routers.add(nRouter);
    });
    return routers;
}

From source file:aarddict.android.ArticleViewActivity.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override//from  www . j  av a  2  s.co  m
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    backItems = Collections.synchronizedList((List) savedInstanceState.getSerializable("backItems"));
    scrollPositionsH = Collections
            .synchronizedMap((Map) savedInstanceState.getSerializable("scrollPositionsH"));
    scrollPositionsV = Collections
            .synchronizedMap((Map) savedInstanceState.getSerializable("scrollPositionsV"));
}

From source file:org.apache.solr.handler.IndexFetcher.java

private void downloadConfFiles(List<Map<String, Object>> confFilesToDownload, long latestGeneration)
        throws Exception {
    LOG.info("Starting download of configuration files from master: " + confFilesToDownload);
    confFilesDownloaded = Collections.synchronizedList(new ArrayList<>());
    File tmpconfDir = new File(solrCore.getResourceLoader().getConfigDir(), "conf." + getDateAsStr(new Date()));
    try {/*  www  .j  av a 2s.  c  om*/
        boolean status = tmpconfDir.mkdirs();
        if (!status) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                    "Failed to create temporary config folder: " + tmpconfDir.getName());
        }
        for (Map<String, Object> file : confFilesToDownload) {
            String saveAs = (String) (file.get(ALIAS) == null ? file.get(NAME) : file.get(ALIAS));
            localFileFetcher = new LocalFsFileFetcher(tmpconfDir, file, saveAs, CONF_FILE_SHORT,
                    latestGeneration);
            currentFile = file;
            localFileFetcher.fetchFile();
            confFilesDownloaded.add(new HashMap<>(file));
        }
        // this is called before copying the files to the original conf dir
        // so that if there is an exception avoid corrupting the original files.
        terminateAndWaitFsyncService();
        copyTmpConfFiles2Conf(tmpconfDir);
    } finally {
        delTree(tmpconfDir);
    }
}

From source file:com.streamsets.pipeline.stage.origin.spooldir.TestSpoolDirSource.java

@Test
public void testWithMultipleThreadsInitialOffsets() throws Exception {
    // set up multiple test files
    File f = new File("target", UUID.randomUUID().toString());
    Assert.assertTrue(f.mkdirs());//from   w w  w  . ja  va2  s . co  m

    final int numFiles = 10;
    for (int i = 0; i < numFiles; i++) {
        FileOutputStream outputStream = new FileOutputStream(
                new File(f.getAbsolutePath(), "file-" + i + ".log"));
        // each file has 5 lines
        IOUtils.writeLines(ImmutableList.of("1", "2", "3", "4", "5"), "\n", outputStream);
        outputStream.close();
    }

    // let the first 2 files, file-0.log and file-3.log, were processed and
    // file-2.log was processed 1 line/record
    // file-1.log will be skipped since is less then file-3.log
    Map<String, String> lastSourceOffsetMap = ImmutableMap.of(SpoolDirSource.OFFSET_VERSION, OFFSET_VERSION_ONE,
            "file-0.log", "{\"POS\":\"-1\"}", "file-2.log", "{\"POS\":\"2\"}", "file-3.log",
            "{\"POS\":\"-1\"}");

    SpoolDirConfigBean conf = new SpoolDirConfigBean();
    conf.dataFormat = DataFormat.TEXT;
    conf.spoolDir = f.getAbsolutePath();
    conf.batchSize = 10;
    conf.overrunLimit = 100;
    conf.poolingTimeoutSecs = 1;
    conf.filePattern = "file-[0-9].log";
    conf.pathMatcherMode = PathMatcherMode.GLOB;
    conf.maxSpoolFiles = 10;
    conf.initialFileToProcess = null;
    conf.dataFormatConfig.compression = Compression.NONE;
    conf.dataFormatConfig.filePatternInArchive = "*";
    conf.errorArchiveDir = null;
    conf.postProcessing = PostProcessingOptions.NONE;
    conf.retentionTimeMins = 10;
    conf.dataFormatConfig.textMaxLineLen = 10;
    conf.dataFormatConfig.onParseError = OnParseError.ERROR;
    conf.dataFormatConfig.maxStackTraceLines = 0;
    conf.allowLateDirectory = false;
    conf.numberOfThreads = 10;

    SpoolDirSource source = new SpoolDirSource(conf);
    PushSourceRunner runner = new PushSourceRunner.Builder(SpoolDirDSource.class, source).addOutputLane("lane")
            .build();

    AtomicInteger batchCount = new AtomicInteger(0);
    final List<Record> records = Collections.synchronizedList(new ArrayList<>(10));
    runner.runInit();

    final int maxBatchSize = 10;

    try {
        runner.runProduce(lastSourceOffsetMap, maxBatchSize, output -> {
            batchCount.incrementAndGet();

            synchronized (records) {
                records.addAll(output.getRecords().get("lane"));
            }

            if (records.size() == 34 || batchCount.get() > 10) {
                runner.setStop();
            }
        });

        runner.waitOnProduce();
        Assert.assertTrue(batchCount.get() > 1);
        TestOffsetUtil.compare("file-9.log::-1", runner.getOffsets());
        Assert.assertEquals(34, records.size());
    } finally {
        runner.runDestroy();
    }

}

From source file:org.apache.solr.handler.IndexFetcher.java

/**
 * Download all the tlog files to the temp tlog directory.
 *//*from  w w  w  .  ja v  a  2  s .c o m*/
private long downloadTlogFiles(File tmpTlogDir, long latestGeneration) throws Exception {
    LOG.info("Starting download of tlog files from master: " + tlogFilesToDownload);
    tlogFilesDownloaded = Collections.synchronizedList(new ArrayList<>());
    long bytesDownloaded = 0;

    boolean status = tmpTlogDir.mkdirs();
    if (!status) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
                "Failed to create temporary tlog folder: " + tmpTlogDir.getName());
    }
    for (Map<String, Object> file : tlogFilesToDownload) {
        String saveAs = (String) (file.get(ALIAS) == null ? file.get(NAME) : file.get(ALIAS));
        localFileFetcher = new LocalFsFileFetcher(tmpTlogDir, file, saveAs, TLOG_FILE, latestGeneration);
        currentFile = file;
        localFileFetcher.fetchFile();
        bytesDownloaded += localFileFetcher.getBytesDownloaded();
        tlogFilesDownloaded.add(new HashMap<>(file));
    }
    return bytesDownloaded;
}

From source file:cn.openwatch.internal.http.loopj.AsyncHttpClient.java

/**
 * Puts a new request in queue as a new thread in pool to be executed
 *
 * @param client          HttpClient to be used for request, can differ in single
 *                        requests//ww w .j  a va 2  s .com
 * @param contentType     MIME body type, for POST and PUT requests, may be null
 * @param context         Context of Android application, to hold the reference of
 *                        request
 * @param httpContext     HttpContext in which the request will be executed
 * @param responseHandler ResponseHandler or its subclass to put the response into
 * @param uriRequest      instance of HttpUriRequest, which means it must be of
 *                        HttpDelete, HttpPost, HttpGet, HttpPut, etc.
 * @return RequestHandle of future request process
 */
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext,
        HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler,
        Context context) {
    if (uriRequest == null) {
        throw new IllegalArgumentException("HttpUriRequest must not be null");
    }

    if (responseHandler == null) {
        throw new IllegalArgumentException("ResponseHandler must not be null");
    }

    if (responseHandler.getUseSynchronousMode() && !responseHandler.getUsePoolThread()) {
        throw new IllegalArgumentException(
                "Synchronous ResponseHandler used in AsyncHttpClient. You should create your response handler in a looper thread or use SyncHttpClient instead.");
    }

    if (contentType != null) {
        if (uriRequest instanceof HttpEntityEnclosingRequestBase
                && ((HttpEntityEnclosingRequestBase) uriRequest).getEntity() != null
                && uriRequest.containsHeader(HEADER_CONTENT_TYPE)) {
        } else {
            uriRequest.setHeader(HEADER_CONTENT_TYPE, contentType);
        }
    }

    responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
    responseHandler.setRequestURI(uriRequest.getURI());

    AsyncHttpRequest request = newAsyncHttpRequest(client, httpContext, uriRequest, contentType,
            responseHandler, context);
    threadPool.submit(request);
    RequestHandle requestHandle = new RequestHandle(request);

    if (context != null) {
        List<RequestHandle> requestList;
        // Add request to request map
        synchronized (requestMap) {
            requestList = requestMap.get(context);
            if (requestList == null) {
                requestList = Collections.synchronizedList(new LinkedList<RequestHandle>());
                requestMap.put(context, requestList);
            }
        }

        requestList.add(requestHandle);

        Iterator<RequestHandle> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            if (iterator.next().shouldBeGarbageCollected()) {
                iterator.remove();
            }
        }
    }

    return requestHandle;
}

From source file:com.streamsets.pipeline.stage.origin.spooldir.TestSpoolDirSource.java

public void errorFile(boolean preview) throws Exception {
    File spoolDir = new File("target", UUID.randomUUID().toString());
    spoolDir.mkdir();//  w  w w .ja v a 2 s .c  o m
    File errorDir = new File("target", UUID.randomUUID().toString());
    errorDir.mkdir();

    SpoolDirConfigBean conf = new SpoolDirConfigBean();
    conf.dataFormat = DataFormat.JSON;
    conf.spoolDir = spoolDir.getAbsolutePath();
    conf.batchSize = 10;
    conf.overrunLimit = 100;
    conf.poolingTimeoutSecs = 1;
    conf.filePattern = "file-[0-9].log";
    conf.pathMatcherMode = PathMatcherMode.GLOB;
    conf.maxSpoolFiles = 10;
    conf.initialFileToProcess = "file-0.log";
    conf.dataFormatConfig.compression = Compression.NONE;
    conf.dataFormatConfig.filePatternInArchive = "*";
    conf.dataFormatConfig.csvHeader = CsvHeader.WITH_HEADER;
    conf.errorArchiveDir = errorDir.getAbsolutePath();
    conf.postProcessing = PostProcessingOptions.NONE;
    conf.retentionTimeMins = 10;
    conf.allowLateDirectory = false;
    conf.dataFormatConfig.jsonContent = JsonMode.MULTIPLE_OBJECTS;
    conf.dataFormatConfig.onParseError = OnParseError.ERROR;

    FileOutputStream outputStream = new FileOutputStream(new File(conf.spoolDir, "file-0.log"));
    // Incorrect JSON
    IOUtils.writeLines(ImmutableList.of("{a"), "\n", outputStream);
    outputStream.close();

    SpoolDirSource source = new SpoolDirSource(conf);
    PushSourceRunner runner = new PushSourceRunner.Builder(SpoolDirDSource.class, source).setPreview(preview)
            .setOnRecordError(OnRecordError.TO_ERROR).addOutputLane("lane").build();
    final List<Record> records = Collections.synchronizedList(new ArrayList<>(10));

    runner.runInit();
    try {
        runner.runProduce(new HashMap<>(), 10, output -> {
            synchronized (records) {
                records.addAll(output.getRecords().get("lane"));
            }
            runner.setStop();
        });

        runner.waitOnProduce();

        // Verify proper record
        Assert.assertNotNull(records);
        Assert.assertEquals(0, records.size());

        // Depending on the preview flag, we should see the file in one directory or the other
        if (preview) {
            Assert.assertEquals(0, errorDir.list().length);
            Assert.assertEquals(1, spoolDir.list().length);
        } else {
            Assert.assertEquals(1, errorDir.list().length);
            Assert.assertEquals(0, spoolDir.list().length);
        }

    } finally {
        runner.runDestroy();
    }
}

From source file:com.gemini.provision.network.openstack.NetworkProviderOpenStackImpl.java

@Override
public List<ProvisioningProviderResponseType> bulkCreateRouter(GeminiTenant tenant, GeminiEnvironment env,
        List<GeminiNetworkRouter> routes) {
    List<ProvisioningProviderResponseType> retValues = Collections.synchronizedList(new ArrayList());
    //TODO: Only the first element is set ... NEED to research whether it is possible to get the current position from the stream
    routes.stream().forEach(r -> retValues.set(0, createRouter(tenant, env, r)));
    return retValues;
}

From source file:org.jdesktop.swingworker.AccumulativeRunnable.java

public final void testPublishAndProcess() throws Exception {
    final Exchanger<List<Integer>> listExchanger = 
        new Exchanger<List<Integer>>();
    final Exchanger<Boolean> boolExchanger = 
        new Exchanger<Boolean>();
    SwingWorker<List<Integer>,Integer> test = 
        new SwingWorker<List<Integer>, Integer>() {
            List<Integer> receivedArgs = 
                Collections.synchronizedList(new ArrayList<Integer>());
            Boolean isOnEDT = Boolean.TRUE;
            final int NUMBERS = 100;
            @Override//from  w w  w . ja va2  s .c om
            protected List<Integer> doInBackground() throws Exception {
                List<Integer> ret = 
                    Collections.synchronizedList(
                        new ArrayList<Integer>(NUMBERS));
                for (int i = 0; i < NUMBERS; i++) {
                    publish(i);
                    ret.add(i);
                }
                return ret;
            }
            @Override
            protected void process(List<Integer> args) {
                for(Integer i : args) {
                    receivedArgs.add(i);
                }
                isOnEDT = isOnEDT && SwingUtilities.isEventDispatchThread();
                if (receivedArgs.size() == NUMBERS) {
                    try {
                        boolExchanger.exchange(isOnEDT);
                        listExchanger.exchange(receivedArgs);
                    } catch (InterruptedException ignore) {
                        ignore.printStackTrace();
                    }
                }
            }
    };
    test.execute();
    assertTrue(boolExchanger.exchange(null, TIME_OUT, TIME_OUT_UNIT));
    assertEquals(test.get(TIME_OUT, TIME_OUT_UNIT), 
        listExchanger.exchange(null, TIME_OUT, TIME_OUT_UNIT));
}

From source file:voldemort.client.rebalance.AbstractNonZonedRebalanceTest.java

@Test(timeout = 600000)
public void testServerSideRouting() throws Exception {
    logger.info("Starting testServerSideRouting");
    try {//from  w ww. j  av  a 2  s  .  c  o  m
        final Cluster currentCluster = ServerTestUtils.getLocalCluster(2,
                new int[][] { { 0, 1, 2, 3, 4, 5, 6 }, { 7, 8 } });

        final Cluster finalCluster = UpdateClusterUtils.createUpdatedCluster(currentCluster, 1,
                Lists.newArrayList(2, 3));

        final List<Integer> serverList = Arrays.asList(0, 1);
        Map<String, String> configProps = new HashMap<String, String>();
        configProps.put("admin.max.threads", "50");
        configProps.put("enable.server.routing", "true");
        final Cluster updatedCurrentCluster = startServers(currentCluster, storeDefFileWithReplication,
                serverList, configProps);

        ExecutorService executors = Executors.newFixedThreadPool(2);
        final AtomicBoolean rebalancingToken = new AtomicBoolean(false);
        final List<Exception> exceptions = Collections.synchronizedList(new ArrayList<Exception>());

        String bootstrapUrl = getBootstrapUrl(currentCluster, 0);
        int maxParallel = 2;
        final ClusterTestUtils.RebalanceKit rebalanceKit = ClusterTestUtils.getRebalanceKit(bootstrapUrl,
                maxParallel, finalCluster);

        // Populate the two stores
        populateData(updatedCurrentCluster, roStoreDefWithReplication, rebalanceKit.controller.getAdminClient(),
                true);

        populateData(updatedCurrentCluster, rwStoreDefWithReplication, rebalanceKit.controller.getAdminClient(),
                false);

        Node node = updatedCurrentCluster.getNodeById(1);
        final Store<ByteArray, byte[], byte[]> serverSideRoutingStoreRW = getSocketStore(testStoreNameRW,
                node.getHost(), node.getSocketPort(), true);
        final Store<ByteArray, byte[], byte[]> serverSideRoutingStoreRO = getSocketStore(testStoreNameRO,
                node.getHost(), node.getSocketPort(), true);

        final CountDownLatch latch = new CountDownLatch(1);

        // start get operation.
        executors.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    List<String> keys = new ArrayList<String>(testEntries.keySet());

                    while (!rebalancingToken.get()) {
                        // should always able to get values.
                        int index = (int) (Math.random() * keys.size());

                        // should get a valid value
                        try {
                            List<Versioned<byte[]>> values = serverSideRoutingStoreRW
                                    .get(new ByteArray(ByteUtils.getBytes(keys.get(index), "UTF-8")), null);

                            assertEquals("serverSideRoutingStore should return value.", 1, values.size());
                            assertEquals("Value returned should be good",
                                    new Versioned<String>(testEntries.get(keys.get(index))),
                                    new Versioned<String>(
                                            ByteUtils.getString(values.get(0).getValue(), "UTF-8"),
                                            values.get(0).getVersion()));
                            values = serverSideRoutingStoreRO
                                    .get(new ByteArray(ByteUtils.getBytes(keys.get(index), "UTF-8")), null);

                            assertEquals("serverSideRoutingStore should return value.", 1, values.size());
                            assertEquals("Value returned should be good",
                                    new Versioned<String>(testEntries.get(keys.get(index))),
                                    new Versioned<String>(
                                            ByteUtils.getString(values.get(0).getValue(), "UTF-8"),
                                            values.get(0).getVersion()));

                        } catch (UnreachableStoreException e) {
                            // ignore
                        } catch (Exception e) {
                            exceptions.add(e);
                        }
                    }

                    latch.countDown();
                } catch (Exception e) {
                    exceptions.add(e);
                }
            }

        });

        executors.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                    rebalanceAndCheck(rebalanceKit.plan, rebalanceKit.controller, Arrays.asList(0, 1));

                    Thread.sleep(500);
                    rebalancingToken.set(true);
                    checkConsistentMetadata(finalCluster, serverList);
                } catch (Exception e) {
                    exceptions.add(e);
                } finally {
                    // stop servers as soon as the client thread has exited
                    // its
                    // loop.
                    try {
                        latch.await(300, TimeUnit.SECONDS);
                        stopServer(serverList);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        executors.shutdown();
        executors.awaitTermination(300, TimeUnit.SECONDS);

        // check No Exception
        if (exceptions.size() > 0) {
            for (Exception e : exceptions) {
                e.printStackTrace();
            }
            fail("Should not see any exceptions !!");
        }
    } catch (AssertionError ae) {
        logger.error("Assertion broken in testServerSideRouting ", ae);
        throw ae;
    }
}