Search in sources :

Example 1 with ProgressPlugin

use of aQute.bnd.service.progress.ProgressPlugin in project bnd by bndtools.

the class MavenBndRepoTest method config.

void config(Map<String, String> override) throws Exception {
    Map<String, String> config = new HashMap<>();
    config.put("local", tmpName + "/local");
    config.put("index", tmpName + "/index");
    config.put("releaseUrl", fnx.getBaseURI() + "/repo/");
    if (override != null)
        config.putAll(override);
    reporter = new Processor();
    reporter.addBasicPlugin(new ProgressPlugin() {

        @Override
        public Task startTask(final String name, int size) {
            System.out.println("Starting " + name);
            return new Task() {

                @Override
                public void worked(int units) {
                    System.out.println("Worked " + name + " " + units);
                }

                @Override
                public void done(String message, Throwable e) {
                    System.out.println("Done " + name + " " + message + " " + e);
                }

                @Override
                public boolean isCanceled() {
                    return false;
                }
            };
        }
    });
    HttpClient client = new HttpClient();
    client.setRegistry(reporter);
    Executor executor = Executors.newCachedThreadPool();
    reporter.addBasicPlugin(client);
    reporter.setTrace(true);
    repo = new MavenBndRepository();
    repo.setReporter(reporter);
    repo.setRegistry(reporter);
    repo.setProperties(config);
}
Also used : Processor(aQute.bnd.osgi.Processor) Executor(java.util.concurrent.Executor) HashMap(java.util.HashMap) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) HttpClient(aQute.bnd.http.HttpClient)

Example 2 with ProgressPlugin

use of aQute.bnd.service.progress.ProgressPlugin in project bnd by bndtools.

the class HttpClientTest method testCancel.

public void testCancel() throws Exception {
    final long deadline = System.currentTimeMillis() + 1000L;
    try (HttpClient hc = new HttpClient()) {
        Processor p = new Processor();
        p.addBasicPlugin(new ProgressPlugin() {

            @Override
            public Task startTask(String name, int size) {
                return new Task() {

                    @Override
                    public void worked(int units) {
                        System.out.println("Worked " + units);
                    }

                    @Override
                    public void done(String message, Throwable e) {
                        System.out.println("Done " + message + " " + e);
                    }

                    @Override
                    public boolean isCanceled() {
                        System.out.println("Cancel check ");
                        return System.currentTimeMillis() > deadline;
                    }
                };
            }
        });
        hc.setRegistry(p);
        TaggedData tag = hc.build().asTag().go(httpServer.getBaseURI("timeout/50"));
        assertNotNull(tag);
        assertEquals(200, tag.getResponseCode());
        try {
            String s = IO.collect(tag.getInputStream());
            fail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : Processor(aQute.bnd.osgi.Processor) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) HttpClient(aQute.bnd.http.HttpClient) TaggedData(aQute.bnd.service.url.TaggedData) URISyntaxException(java.net.URISyntaxException)

Example 3 with ProgressPlugin

use of aQute.bnd.service.progress.ProgressPlugin in project bnd by bndtools.

the class HttpClientTest method testMultipleProgressPlugins.

public void testMultipleProgressPlugins() throws Exception {
    final long deadline = System.currentTimeMillis() + 1000L;
    try (HttpClient hc = new HttpClient()) {
        Processor p = new Processor();
        final int[] counts = new int[2];
        counts[0] = counts[1] = 0;
        p.addBasicPlugin(new ProgressPlugin() {

            @Override
            public Task startTask(String name, int size) {
                return new Task() {

                    @Override
                    public void worked(int units) {
                        counts[0]++;
                    }

                    @Override
                    public void done(String message, Throwable e) {
                        counts[0]++;
                    }

                    @Override
                    public boolean isCanceled() {
                        return false;
                    }
                };
            }
        });
        p.addBasicPlugin(new ProgressPlugin() {

            @Override
            public Task startTask(String name, int size) {
                return new Task() {

                    @Override
                    public void worked(int units) {
                        counts[1]++;
                    }

                    @Override
                    public void done(String message, Throwable e) {
                        counts[1]++;
                    }

                    @Override
                    public boolean isCanceled() {
                        return false;
                    }
                };
            }
        });
        hc.setRegistry(p);
        String text = hc.build().get(String.class).go(httpServer.getBaseURI("get"));
        assertNotNull(text);
        assertTrue(counts[0] > 0);
        assertEquals(counts[0], counts[1]);
    }
}
Also used : Processor(aQute.bnd.osgi.Processor) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) HttpClient(aQute.bnd.http.HttpClient)

Example 4 with ProgressPlugin

use of aQute.bnd.service.progress.ProgressPlugin in project bnd by bndtools.

the class HttpClient method getTask.

ProgressPlugin.Task getTask(final HttpRequest<?> request) {
    final String name = (request.upload == null ? "Download " : "Upload ") + request.url;
    final int size = 100;
    final ProgressPlugin.Task task;
    final List<ProgressPlugin> progressPlugins = registry != null ? registry.getPlugins(ProgressPlugin.class) : null;
    if (progressPlugins != null && progressPlugins.size() > 1) {
        final List<ProgressPlugin.Task> multiplexedTasks = new ArrayList<>();
        for (ProgressPlugin progressPlugin : progressPlugins) {
            multiplexedTasks.add(progressPlugin.startTask(name, size));
        }
        task = new ProgressPlugin.Task() {

            @Override
            public void worked(int units) {
                for (ProgressPlugin.Task task : multiplexedTasks) {
                    task.worked(units);
                }
            }

            @Override
            public void done(String message, Throwable e) {
                for (ProgressPlugin.Task task : multiplexedTasks) {
                    task.done(message, e);
                }
            }

            @Override
            public boolean isCanceled() {
                for (ProgressPlugin.Task task : multiplexedTasks) {
                    if (task.isCanceled()) {
                        return true;
                    }
                }
                return false;
            }
        };
    } else if (progressPlugins != null && progressPlugins.size() == 1) {
        task = progressPlugins.get(0).startTask(name, size);
    } else {
        task = new ProgressPlugin.Task() {

            @Override
            public void worked(int units) {
            }

            @Override
            public void done(String message, Throwable e) {
            }

            @Override
            public boolean isCanceled() {
                return Thread.currentThread().isInterrupted();
            }
        };
    }
    return task;
}
Also used : Task(aQute.bnd.service.progress.ProgressPlugin.Task) Task(aQute.bnd.service.progress.ProgressPlugin.Task) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) ArrayList(java.util.ArrayList)

Example 5 with ProgressPlugin

use of aQute.bnd.service.progress.ProgressPlugin in project bnd by bndtools.

the class OSGiRepositoryTest method testBndRepo.

public void testBndRepo() throws Exception {
    try (OSGiRepository r = new OSGiRepository()) {
        Map<String, String> map = new HashMap<>();
        map.put("locations", "https://bndtools.ci.cloudbees.com/job/bnd.master/lastSuccessfulBuild/artifact/dist/bundles/index.xml.gz");
        map.put("cache", cache.getPath());
        map.put("max.stale", "10000");
        r.setProperties(map);
        Processor p = new Processor();
        HttpClient httpClient = new HttpClient();
        httpClient.setCache(cache);
        httpClient.setRegistry(p);
        p.addBasicPlugin(httpClient);
        p.setBase(ws);
        p.addBasicPlugin(Workspace.createStandaloneWorkspace(p, ws.toURI()));
        r.setRegistry(p);
        final AtomicInteger tasks = new AtomicInteger();
        p.addBasicPlugin(new ProgressPlugin() {

            @Override
            public Task startTask(final String name, int size) {
                System.out.println("Starting " + name);
                tasks.incrementAndGet();
                return new Task() {

                    @Override
                    public void worked(int units) {
                        System.out.println("Worked " + name + " " + units);
                    }

                    @Override
                    public void done(String message, Throwable e) {
                        System.out.println("Done " + name + " " + message);
                    }

                    @Override
                    public boolean isCanceled() {
                        return false;
                    }
                };
            }
        });
        assertEquals(0, tasks.get());
        List<String> list = r.list(null);
        assertFalse(list.isEmpty());
        SortedSet<Version> versions = r.versions("aQute.libg");
        assertFalse(versions.isEmpty());
        File f1 = r.get("aQute.libg", versions.first(), null);
        assertNotNull(f1);
        // index + bundle
        assertEquals(2, tasks.get());
        File f2 = r.get("aQute.libg", versions.first(), null);
        // should use cache
        assertEquals(2, tasks.get());
        r.getIndex(true);
        File f3 = r.get("aQute.libg", versions.first(), null);
        // should fetch again
        assertEquals(4, tasks.get());
    }
}
Also used : Processor(aQute.bnd.osgi.Processor) HashMap(java.util.HashMap) ProgressPlugin(aQute.bnd.service.progress.ProgressPlugin) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(aQute.bnd.version.Version) HttpClient(aQute.bnd.http.HttpClient) File(java.io.File)

Aggregations

ProgressPlugin (aQute.bnd.service.progress.ProgressPlugin)7 HttpClient (aQute.bnd.http.HttpClient)6 Processor (aQute.bnd.osgi.Processor)6 HashMap (java.util.HashMap)3 TaggedData (aQute.bnd.service.url.TaggedData)2 Version (aQute.bnd.version.Version)2 File (java.io.File)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Task (aQute.bnd.service.progress.ProgressPlugin.Task)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Executor (java.util.concurrent.Executor)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1