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.IOException;
14 import java.io.InputStream;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Enumeration;
18 import java.util.List;
20 import java.util.zip.ZipEntry;
21 import java.util.zip.ZipException;
22 import java.util.zip.ZipFile;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.maven.artifact.DependencyResolutionRequiredException;
26 import org.apache.maven.project.MavenProject;
28 import com.google.common.base.Function;
29 import com.google.common.collect.Collections2;
30 import com.google.common.collect.Lists;
31 import com.google.common.collect.Maps;
34 static final String YANG_SUFFIX = "yang";
36 // Cache for listed directories and found yang files. Typically yang files
37 // are utilized twice. First: code is generated during generate-sources
38 // phase Second: yang files are copied as resources during
39 // generate-resources phase. This cache ensures that yang files are listed
41 private static Map<String, Collection<File>> cache = Maps
42 .newHashMapWithExpectedSize(10);
45 * List files recursively and return as array of String paths. Use cache of
48 static Collection<File> listFiles(String rootDir) {
49 if (cache.get(rootDir) != null)
50 return cache.get(rootDir);
52 Collection<File> yangFiles = FileUtils.listFiles(new File(rootDir),
53 new String[] { YANG_SUFFIX }, true);
55 toCache(rootDir, yangFiles);
59 static Collection<InputStream> listFilesAsStream(String rootDir) throws FileNotFoundException {
60 Collection<InputStream> is = new ArrayList<InputStream>();
62 Collection<File> files = listFiles(rootDir);
64 is.add(new FileInputStream(f));
70 static String[] listFilesAsArrayOfPaths(String rootDir) {
71 String[] filesArray = new String[] {};
72 Collection<File> yangFiles = listFiles(rootDir);
74 // If collection is empty, return empty array [] rather then [null]
75 // array, that is created by default
76 return yangFiles.isEmpty() ? filesArray : Collections2.transform(
77 yangFiles, new Function<File, String>() {
80 public String apply(File input) {
81 return input.getPath();
83 }).toArray(filesArray);
86 private static void toCache(final String rootDir,
87 final Collection<File> yangFiles) {
88 cache.put(rootDir, yangFiles);
92 * Instantiate object from fully qualified class name
94 static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
95 throws ClassNotFoundException, InstantiationException,
96 IllegalAccessException {
97 return baseType.cast(resolveClass(codeGeneratorClass, baseType)
101 private static Class<?> resolveClass(String codeGeneratorClass,
102 Class<?> baseType) throws ClassNotFoundException {
103 Class<?> clazz = Class.forName(codeGeneratorClass);
105 if (!isImplemented(baseType, clazz))
106 throw new IllegalArgumentException("Code generator " + clazz
107 + " has to implement " + baseType);
111 private static boolean isImplemented(Class<?> expectedIface,
113 for (Class<?> iface : byClazz.getInterfaces()) {
114 if (iface.equals(expectedIface))
120 static String message(String message, String logPrefix, Object... args) {
121 String innerMessage = String.format(message, args);
122 return String.format("%s %s", logPrefix, innerMessage);
125 public static List<File> getClassPath(MavenProject project)
126 throws DependencyResolutionRequiredException {
127 List<File> dependencies = Lists.newArrayList();
129 for (Object element : project.getCompileClasspathElements()) {
130 File asFile = new File((String) element);
132 dependencies.add(asFile);
135 } catch (DependencyResolutionRequiredException e) {
141 private static final String JAR_SUFFIX = ".jar";
143 private static boolean isJar(File element) {
144 return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true
148 public static Collection<File> getFilesFromClasspath(
149 List<File> jarsOnClasspath, List<String> classPathFilter)
150 throws ZipException, IOException {
151 List<File> yangs = Lists.newArrayList();
153 for (File file : jarsOnClasspath) {
154 ZipFile zip = new ZipFile(file);
155 Enumeration<? extends ZipEntry> entries = zip.entries();
156 while (entries.hasMoreElements()) {
157 ZipEntry entry = entries.nextElement();
158 if (entry.getName().endsWith(YANG_SUFFIX)) {
159 InputStream stream = zip.getInputStream(entry);
167 public static boolean acceptedFilter(String name, List<String> filter) {
168 for(String f : filter) {
169 if(name.endsWith(f)) {