Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cfgdef; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import com.thoughtworks.qdox.JavaProjectBuilder; |
| 20 | import com.thoughtworks.qdox.model.JavaAnnotation; |
| 21 | import com.thoughtworks.qdox.model.JavaClass; |
| 22 | import com.thoughtworks.qdox.model.JavaField; |
| 23 | import com.thoughtworks.qdox.model.expression.Add; |
| 24 | import com.thoughtworks.qdox.model.expression.AnnotationValue; |
| 25 | import com.thoughtworks.qdox.model.expression.AnnotationValueList; |
| 26 | import com.thoughtworks.qdox.model.expression.FieldRef; |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 27 | import com.thoughtworks.qdox.parser.ParseException; |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 28 | |
| 29 | import java.io.File; |
| 30 | import java.io.FileOutputStream; |
| 31 | import java.io.IOException; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.List; |
| 35 | import java.util.Map; |
| 36 | import java.util.Optional; |
| 37 | import java.util.jar.JarEntry; |
| 38 | import java.util.jar.JarOutputStream; |
| 39 | |
| 40 | /** |
| 41 | * Produces ONOS component configuration catalogue resources. |
| 42 | */ |
| 43 | public class CfgDefGenerator { |
| 44 | |
| 45 | private static final String COMPONENT = "org.osgi.service.component.annotations.Component"; |
| 46 | private static final String PROPERTY = "property"; |
| 47 | private static final String SEP = "|"; |
| 48 | private static final String UTF_8 = "UTF-8"; |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 49 | private static final String JAVA = ".java"; |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 50 | private static final String EXT = ".cfgdef"; |
| 51 | private static final String STRING = "STRING"; |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 52 | private static final String NO_DESCRIPTION = "no description provided"; |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 53 | |
| 54 | private final File resourceJar; |
| 55 | private final JavaProjectBuilder builder; |
| 56 | |
| 57 | private final Map<String, String> constants = Maps.newHashMap(); |
| 58 | |
| 59 | public static void main(String[] args) throws IOException { |
| 60 | if (args.length < 2) { |
| 61 | System.err.println("usage: cfgdef outputJar javaSource javaSource ..."); |
| 62 | System.exit(1); |
| 63 | } |
| 64 | CfgDefGenerator gen = new CfgDefGenerator(args[0], Arrays.copyOfRange(args, 1, args.length)); |
| 65 | gen.analyze(); |
| 66 | gen.generate(); |
| 67 | } |
| 68 | |
| 69 | private CfgDefGenerator(String resourceJarPath, String[] sourceFilePaths) { |
| 70 | this.resourceJar = new File(resourceJarPath); |
| 71 | this.builder = new JavaProjectBuilder(); |
| 72 | Arrays.stream(sourceFilePaths).forEach(filename -> { |
| 73 | try { |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 74 | if (filename.endsWith(JAVA)) |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 75 | builder.addSource(new File(filename)); |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 76 | } catch (ParseException e) { |
| 77 | // When unable to parse, skip the source; leave it to javac to fail. |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 78 | } catch (IOException e) { |
| 79 | throw new IllegalArgumentException("Unable to open file", e); |
| 80 | } |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | public void analyze() { |
| 85 | builder.getClasses().forEach(this::collectConstants); |
| 86 | } |
| 87 | |
| 88 | private void collectConstants(JavaClass javaClass) { |
| 89 | javaClass.getFields().stream() |
| 90 | .filter(f -> f.isStatic() && f.isFinal() && !f.isPrivate()) |
| 91 | .forEach(f -> constants.put(f.getName(), f.getInitializationExpression())); |
| 92 | } |
| 93 | |
| 94 | public void generate() throws IOException { |
| 95 | JarOutputStream jar = new JarOutputStream(new FileOutputStream(resourceJar)); |
| 96 | for (JavaClass javaClass : builder.getClasses()) { |
| 97 | processClass(jar, javaClass); |
| 98 | } |
| 99 | jar.close(); |
| 100 | } |
| 101 | |
| 102 | private void processClass(JarOutputStream jar, JavaClass javaClass) throws IOException { |
| 103 | Optional<JavaAnnotation> annotation = javaClass.getAnnotations().stream() |
| 104 | .filter(ja -> ja.getType().getName().equals(COMPONENT)) |
| 105 | .findFirst(); |
| 106 | if (annotation.isPresent()) { |
| 107 | AnnotationValue property = annotation.get().getProperty(PROPERTY); |
| 108 | List<String> lines = new ArrayList<>(); |
| 109 | if (property instanceof AnnotationValueList) { |
| 110 | AnnotationValueList list = (AnnotationValueList) property; |
| 111 | list.getValueList().forEach(v -> processProperty(lines, javaClass, v)); |
| 112 | } else { |
| 113 | processProperty(lines, javaClass, property); |
| 114 | } |
| 115 | |
| 116 | if (!lines.isEmpty()) { |
| 117 | writeCatalog(jar, javaClass, lines); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | private void processProperty(List<String> lines, JavaClass javaClass, |
| 123 | AnnotationValue value) { |
| 124 | String s = elaborate(value); |
| 125 | String pex[] = s.split("=", 2); |
| 126 | |
| 127 | if (pex.length == 2) { |
| 128 | String rex[] = pex[0].split(":", 2); |
| 129 | String name = rex[0]; |
| 130 | String type = rex.length == 2 ? rex[1].toUpperCase() : STRING; |
| 131 | String def = pex[1]; |
| 132 | String desc = description(javaClass, name); |
| 133 | |
Thomas Vachuska | 9b6e45c | 2018-10-31 15:22:32 -0700 | [diff] [blame] | 134 | if (desc != null) { |
| 135 | String line = name + SEP + type + SEP + def + SEP + desc + "\n"; |
| 136 | lines.add(line); |
| 137 | } |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 141 | // Retrieve description from a comment preceding the field named the same |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 142 | // as the property or |
| 143 | // TODO: from an annotated comment. |
| 144 | private String description(JavaClass javaClass, String name) { |
Ray Milkey | 5504bd2 | 2019-03-22 16:24:38 -0700 | [diff] [blame] | 145 | if (name.startsWith("_")) { |
| 146 | // Static property - just leave it as is, not for inclusion in the cfg defs |
| 147 | return null; |
| 148 | } |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 149 | JavaField field = javaClass.getFieldByName(name); |
| 150 | if (field != null) { |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 151 | String comment = field.getComment(); |
| 152 | return comment != null ? comment : NO_DESCRIPTION; |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 153 | } |
Ray Milkey | bd508ed | 2019-03-19 14:22:02 -0700 | [diff] [blame] | 154 | throw new IllegalStateException("cfgdef could not find a variable named " + name + " in " + javaClass.getName()); |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | private String elaborate(AnnotationValue value) { |
| 158 | if (value instanceof Add) { |
| 159 | return elaborate(((Add) value).getLeft()) + elaborate(((Add) value).getRight()); |
| 160 | } else if (value instanceof FieldRef) { |
Thomas Vachuska | 0054d34 | 2018-10-29 10:19:47 -0700 | [diff] [blame] | 161 | return elaborate((FieldRef) value); |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 162 | } else if (value != null) { |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 163 | return stripped(value.toString()); |
Thomas Vachuska | bef430b | 2018-10-22 10:55:47 -0700 | [diff] [blame] | 164 | } else { |
| 165 | return ""; |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Thomas Vachuska | 0054d34 | 2018-10-29 10:19:47 -0700 | [diff] [blame] | 169 | private String elaborate(FieldRef field) { |
| 170 | String name = field.getName(); |
| 171 | String value = constants.get(name); |
| 172 | if (value != null) { |
| 173 | return stripped(value); |
| 174 | } |
| 175 | throw new IllegalStateException("Constant " + name + " cannot be elaborated;" + |
| 176 | " value not in the same compilation context"); |
| 177 | } |
| 178 | |
Thomas Vachuska | 4a34763 | 2018-09-11 11:53:08 -0700 | [diff] [blame] | 179 | private String stripped(String s) { |
| 180 | return s.trim().replaceFirst("^[^\"]*\"", "").replaceFirst("\"$", ""); |
| 181 | } |
| 182 | |
| 183 | private void writeCatalog(JarOutputStream jar, JavaClass javaClass, List<String> lines) |
| 184 | throws IOException { |
| 185 | String name = javaClass.getPackageName().replace('.', '/') + "/" + javaClass.getName() + EXT; |
| 186 | jar.putNextEntry(new JarEntry(name)); |
| 187 | jar.write("# This file is auto-generated\n".getBytes(UTF_8)); |
| 188 | for (String line : lines) { |
| 189 | jar.write(line.getBytes(UTF_8)); |
| 190 | } |
| 191 | jar.closeEntry(); |
| 192 | } |
| 193 | |
| 194 | } |