2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.yang2sources.plugin;
11 import java.io.FileInputStream;
12 import java.io.FileNotFoundException;
13 import java.io.InputStream;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
19 import org.apache.commons.io.FileUtils;
20 import org.apache.maven.artifact.Artifact;
21 import org.apache.maven.project.MavenProject;
23 import com.google.common.base.Function;
24 import com.google.common.collect.Collections2;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Maps;
29 static final String YANG_SUFFIX = "yang";
31 // Cache for listed directories and found yang files. Typically yang files
32 // are utilized twice. First: code is generated during generate-sources
33 // phase Second: yang files are copied as resources during
34 // generate-resources phase. This cache ensures that yang files are listed
36 private static Map<File, Collection<File>> cache = Maps
37 .newHashMapWithExpectedSize(10);
40 * List files recursively and return as array of String paths. Use cache of
43 static Collection<File> listFiles(File root) throws FileNotFoundException {
44 if (cache.get(root) != null)
45 return cache.get(root);
48 throw new FileNotFoundException(root.toString());
51 Collection<File> yangFiles = FileUtils.listFiles(root,
52 new String[] { YANG_SUFFIX }, true);
54 toCache(root, yangFiles);
58 static List<InputStream> listFilesAsStream(File rootDir)
59 throws FileNotFoundException {
60 List<InputStream> is = new ArrayList<InputStream>();
62 Collection<File> files = listFiles(rootDir);
63 for (File f : files) {
64 is.add(new NamedFileInputStream(f));
70 static class NamedFileInputStream extends FileInputStream {
71 private final File file;
73 NamedFileInputStream(File file) throws FileNotFoundException {
79 public String toString() {
80 return getClass().getSimpleName() + "{" + file + "}";
84 static String[] listFilesAsArrayOfPaths(File rootDir)
85 throws FileNotFoundException {
86 String[] filesArray = new String[] {};
87 Collection<File> yangFiles = listFiles(rootDir);
89 // If collection is empty, return empty array [] rather then [null]
90 // array, that is created by default
91 return yangFiles.isEmpty() ? filesArray : Collections2.transform(
92 yangFiles, new Function<File, String>() {
95 public String apply(File input) {
96 return input.getPath();
98 }).toArray(filesArray);
101 private static void toCache(final File rootDir,
102 final Collection<File> yangFiles) {
103 cache.put(rootDir, yangFiles);
107 * Instantiate object from fully qualified class name
109 static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
110 throws ClassNotFoundException, InstantiationException,
111 IllegalAccessException {
112 return baseType.cast(resolveClass(codeGeneratorClass, baseType)
116 private static Class<?> resolveClass(String codeGeneratorClass,
117 Class<?> baseType) throws ClassNotFoundException {
118 Class<?> clazz = Class.forName(codeGeneratorClass);
120 if (!isImplemented(baseType, clazz))
121 throw new IllegalArgumentException("Code generator " + clazz
122 + " has to implement " + baseType);
126 private static boolean isImplemented(Class<?> expectedIface,
128 for (Class<?> iface : byClazz.getInterfaces()) {
129 if (iface.equals(expectedIface))
135 static String message(String message, String logPrefix, Object... args) {
136 String innerMessage = String.format(message, args);
137 return String.format("%s %s", logPrefix, innerMessage);
140 public static List<File> getClassPath(MavenProject project) {
141 List<File> dependencies = Lists.newArrayList();
142 for (Artifact element : project.getArtifacts()) {
143 File asFile = element.getFile();
144 if (isJar(asFile) || asFile.isDirectory()) {
145 dependencies.add(asFile);
151 private static final String JAR_SUFFIX = ".jar";
153 private static boolean isJar(File element) {
154 return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
158 public static boolean acceptedFilter(String name, List<String> filter) {
159 for (String f : filter) {
160 if (name.endsWith(f)) {