public static void load()

in src/main/java/org/apache/tomee/website/Examples.java [80:154]


    public static void load() {
        if (!CACHE.isEmpty()) {
            return;
        }

        final File cache = new File(CACHE_FILE);
        if (cache.isFile()) {
            System.out.println("Reading examples from cache, delete " + CACHE_FILE + " if you want to reload them");
            try (final InputStream is = new FileInputStream(cache)) {
                final ExampleWrapper wrapper = new MapperBuilder().build().readObject(is, ExampleWrapper.class);
                CACHE.putAll(wrapper.getAll());
            } catch (IOException e) {
                throw new IllegalArgumentException(e);
            }
            return;
        }

        final Client client = ClientBuilder.newClient().register(new JohnzonProvider<>());
        try {
            final WebTarget github = client.target("https://api.github.com");
            final Invocation.Builder request = github.path("repos/apache/tomee/contents/examples").request(APPLICATION_JSON_TYPE);
            final String auth = System.getProperty("github.auth");
            if (auth != null) {
                request.header("Authorization", auth);
            }
            request
                    .get(new GenericType<Collection<GithubContentItem>>() {
                    }).stream().filter(i -> "dir".equals(i.getType()))
                    .parallel()
                    .sorted((i1, i2) -> i1.getName().compareTo(i2.getName()))
                    .map(i -> new Example(i.getName(), i.getHtmlUrl(), loadReadme(auth, github, i)))
                    .forEach(example -> {
                        final Collection<String> split = Stream.of(example.getName()
                                .replace("application-composer", "applicationcomposer")
                                .replace("configproperty", "config")
                                .replace("descriptors", "descriptor")
                                .replace("ejbs", "ejb")
                                .replace("env-entry", "enventry")
                                .replace("events", "event")
                                .replace("interceptors", "interceptor")
                                .split("-"))
                                .filter(s -> !EXCLUDED_KEYWORDS.contains(s))
                                .filter(s -> {
                                    try {
                                        Integer.parseInt(s);
                                        return false;
                                    } catch (final NumberFormatException nfe) {
                                        return true;
                                    }
                                })
                                .collect(toList());
                        if (split.isEmpty()) {
                            CACHE.computeIfAbsent("Unclassified", k -> new ArrayList<>()).add(example);
                        } else {
                            for (final String keyword : split) {
                                CACHE.computeIfAbsent(keyword, k -> new ArrayList<>()).add(example);
                            }
                        }
                    });

            // debug stats
            final int totalExamples = CACHE.size();
            final long exampleMissingReadme = CACHE.values().stream().flatMap(Collection::stream).filter(e -> DEFAULT_README.equals(e.getReadme())).count();
            System.out.println(exampleMissingReadme + "/" + totalExamples + " miss a README.md");
            CACHE.values().stream().flatMap(Collection::stream).filter(e -> DEFAULT_README.equals(e.getReadme())).forEach(e -> System.out.println("  - " + e.getName()));

            try (final OutputStream os = new FileOutputStream(CACHE_FILE)) {
                new MapperBuilder().setPretty(true).build().writeObject(loadAll(), os);
            } catch (final IOException e) {
                throw new IllegalArgumentException(e);
            }
        } finally {
            client.close();
        }
    }