|
67 | 67 | import org.onosproject.yang.compiler.datamodel.utils.builtindatatype.YangDataTypes;
|
68 | 68 | import org.onosproject.yang.model.LeafObjectType;
|
69 | 69 | import org.onosproject.yang.model.SchemaId;
|
| 70 | +import org.slf4j.Logger; |
70 | 71 |
|
71 | 72 | import java.io.File;
|
72 | 73 | import java.io.FileOutputStream;
|
73 | 74 | import java.io.IOException;
|
74 | 75 | import java.io.InputStream;
|
| 76 | +import java.nio.file.Files; |
| 77 | +import java.nio.file.Path; |
| 78 | +import java.nio.file.Paths; |
75 | 79 | import java.text.SimpleDateFormat;
|
76 | 80 | import java.util.ArrayList;
|
77 | 81 | import java.util.Collection;
|
|
111 | 115 | import static org.onosproject.yang.model.LeafObjectType.LONG;
|
112 | 116 | import static org.onosproject.yang.model.LeafObjectType.SHORT;
|
113 | 117 | import static org.onosproject.yang.model.LeafObjectType.STRING;
|
| 118 | +import static org.slf4j.LoggerFactory.getLogger; |
114 | 119 |
|
115 | 120 | /**
|
116 | 121 | * Represents utilities for data model tree.
|
117 | 122 | */
|
118 | 123 | public final class DataModelUtils {
|
| 124 | + private static final Logger log = getLogger(DataModelUtils.class); |
| 125 | + |
119 | 126 | public static final String TRUE = "true";
|
120 | 127 | public static final String FALSE = "false";
|
121 | 128 | public static final String FMT_NOT_EXIST =
|
@@ -865,49 +872,56 @@ private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leav
|
865 | 872 | }
|
866 | 873 |
|
867 | 874 | /**
|
868 |
| - * Parses jar file and returns list of serialized file names. |
| 875 | + * Extracts contents from jar file and returns path to YangMetaData. |
869 | 876 | *
|
870 | 877 | * @param jarFile jar file to be parsed
|
871 |
| - * @param directory directory where to search |
872 |
| - * @return list of serialized files |
| 878 | + * @param directory directory where to output |
| 879 | + * @return path to serialized YangMetaData file copied in {@code directory} |
873 | 880 | * @throws IOException when fails to do IO operations
|
874 | 881 | */
|
875 | 882 | public static File parseDepSchemaPath(String jarFile, String directory)
|
876 | 883 | throws IOException {
|
877 |
| - JarFile jar = new JarFile(jarFile); |
878 |
| - Enumeration<?> enumEntries = jar.entries(); |
879 |
| - File serializedFile = null; |
880 |
| - while (enumEntries.hasMoreElements()) { |
881 |
| - JarEntry file = (JarEntry) enumEntries.nextElement(); |
882 |
| - if (file.getName().endsWith(".ser")) { |
883 |
| - |
884 |
| - if (file.getName().contains(SLASH)) { |
885 |
| - String[] strArray = file.getName().split(SLASH); |
886 |
| - String tempPath = ""; |
887 |
| - for (int i = 0; i < strArray.length - 1; i++) { |
888 |
| - tempPath = SLASH + tempPath + SLASH + strArray[i]; |
| 884 | + log.trace("From jarfile: {}", jarFile); |
| 885 | + try (JarFile jar = new JarFile(jarFile)) { |
| 886 | + Enumeration<?> enumEntries = jar.entries(); |
| 887 | + File serializedFile = null; |
| 888 | + while (enumEntries.hasMoreElements()) { |
| 889 | + JarEntry file = (JarEntry) enumEntries.nextElement(); |
| 890 | + if (file.getName().endsWith("YangMetaData.ser")) { |
| 891 | + |
| 892 | + Path jarRelPath = Paths.get(file.getName()); |
| 893 | + Path outBase = Paths.get(directory); |
| 894 | + String inFilename = Paths.get(jarFile).getFileName().toString(); |
| 895 | + String inBasename = inFilename.substring(0, inFilename.length() - ".jar".length()); |
| 896 | + // inject input jar basename right before the filename. |
| 897 | + Path serializedPath = outBase |
| 898 | + .resolve(jarRelPath.getParent()) |
| 899 | + .resolve(inBasename) |
| 900 | + .resolve(jarRelPath.getFileName()); |
| 901 | + |
| 902 | + if (Files.isDirectory(serializedPath)) { |
| 903 | + Files.createDirectories(serializedPath.getParent()); |
| 904 | + continue; |
| 905 | + } else { |
| 906 | + Files.createDirectories(serializedPath.getParent()); |
889 | 907 | }
|
890 |
| - File dir = new File(directory + tempPath); |
891 |
| - dir.mkdirs(); |
892 |
| - } |
893 |
| - serializedFile = new File(directory + SLASH + file.getName()); |
894 |
| - if (file.isDirectory()) { |
895 |
| - serializedFile.mkdirs(); |
896 |
| - continue; |
| 908 | + serializedFile = serializedPath.toFile(); |
| 909 | + log.trace(" writing {} to {}", file.getName(), serializedFile); |
| 910 | + InputStream inputStream = jar.getInputStream(file); |
| 911 | + |
| 912 | + FileOutputStream fileOutputStream = new FileOutputStream(serializedFile); |
| 913 | + IOUtils.copy(inputStream, fileOutputStream); |
| 914 | + fileOutputStream.close(); |
| 915 | + inputStream.close(); |
| 916 | + |
| 917 | + //As of now only one metadata files will be there so if we |
| 918 | + // found one then we should break the loop. |
| 919 | + return serializedFile; |
897 | 920 | }
|
898 |
| - InputStream inputStream = jar.getInputStream(file); |
899 |
| - |
900 |
| - FileOutputStream fileOutputStream = new FileOutputStream(serializedFile); |
901 |
| - IOUtils.copy(inputStream, fileOutputStream); |
902 |
| - fileOutputStream.close(); |
903 |
| - inputStream.close(); |
904 |
| - //As of now only one metadata files will be there so if we |
905 |
| - // found one then we should break the loop. |
906 |
| - break; |
907 | 921 | }
|
908 | 922 | }
|
909 |
| - jar.close(); |
910 |
| - return serializedFile; |
| 923 | + // Not found |
| 924 | + return null; |
911 | 925 | }
|
912 | 926 |
|
913 | 927 | /**
|
|
0 commit comments