- private static final int CACHE_SIZE = 10;
- // Cache for listed directories and found yang files. Typically yang files
- // are utilized twice. First: code is generated during generate-sources
- // phase Second: yang files are copied as resources during
- // generate-resources phase. This cache ensures that yang files are listed
- // only once.
- private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE);
-
- /**
- * List files recursively and return as array of String paths. Use cache of
- * size 1.
- */
- static Collection<File> listFiles(final File root) throws FileNotFoundException {
- if (cache.get(root) != null) {
- return cache.get(root);
- }
-
- if (!root.exists()) {
- throw new FileNotFoundException(root.toString());
- }
-
- Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
-
- toCache(root, yangFiles);
- return yangFiles;
- }
-
- static Collection<File> listFiles(final File root, final File[] excludedFiles) throws FileNotFoundException {
- if (!root.exists()) {
- LOG.warn("{} YANG source directory {} not found. No code will be generated.", YangToSourcesProcessor
- .LOG_PREFIX, root.toString());
-
- return Collections.emptyList();
- }
- Collection<File> result = new ArrayList<>();
- Collection<File> yangFiles = FileUtils.listFiles(root, new String[] { YANG_SUFFIX }, true);
- for (File f : yangFiles) {
- boolean excluded = false;
- for (File ex : excludedFiles) {
- if (ex.equals(f)) {
- excluded = true;
- break;
- }
- }
- if (excluded) {
- LOG.info("{} {} file excluded {}", YangToSourcesProcessor.LOG_PREFIX, YANG_SUFFIX.toUpperCase(),
- f);
- } else {
- result.add(f);
- }
- }
-
- return result;
- }
-
- private static void toCache(final File rootDir, final Collection<File> yangFiles) {
- cache.put(rootDir, yangFiles);
- }