Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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.maven; |
| 17 | |
| 18 | import com.thoughtworks.qdox.JavaProjectBuilder; |
| 19 | import com.thoughtworks.qdox.model.JavaAnnotation; |
| 20 | import com.thoughtworks.qdox.model.JavaClass; |
| 21 | import com.thoughtworks.qdox.model.JavaField; |
| 22 | import org.apache.maven.plugin.AbstractMojo; |
| 23 | import org.apache.maven.plugin.MojoExecutionException; |
| 24 | import org.apache.maven.plugins.annotations.LifecyclePhase; |
| 25 | import org.apache.maven.plugins.annotations.Mojo; |
| 26 | import org.apache.maven.plugins.annotations.Parameter; |
| 27 | |
| 28 | import java.io.File; |
| 29 | import java.io.FileWriter; |
| 30 | import java.io.IOException; |
| 31 | import java.io.PrintWriter; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
| 35 | /** |
| 36 | * Produces ONOS component configuration catalogue resources. |
| 37 | */ |
| 38 | @Mojo(name = "cfg", defaultPhase = LifecyclePhase.GENERATE_RESOURCES) |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 39 | @java.lang.SuppressWarnings("squid:S1148") |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 40 | public class OnosCfgMojo extends AbstractMojo { |
| 41 | |
| 42 | private static final String COMPONENT = "org.apache.felix.scr.annotations.Component"; |
| 43 | private static final String PROPERTY = "org.apache.felix.scr.annotations.Property"; |
| 44 | private static final String SEP = "|"; |
| 45 | |
| 46 | /** |
| 47 | * The directory where the generated catalogue file will be put. |
| 48 | */ |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 49 | @Parameter(defaultValue = "${basedir}") |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 50 | protected File srcDirectory; |
| 51 | |
| 52 | /** |
| 53 | * The directory where the generated catalogue file will be put. |
| 54 | */ |
Thomas Vachuska | a98ae7f | 2015-04-14 14:00:36 -0700 | [diff] [blame] | 55 | @Parameter(defaultValue = "${project.build.outputDirectory}") |
Thomas Vachuska | 8c8b037 | 2015-03-10 11:11:24 -0700 | [diff] [blame] | 56 | protected File dstDirectory; |
| 57 | |
| 58 | @Override |
| 59 | public void execute() throws MojoExecutionException { |
| 60 | getLog().info("Generating ONOS component configuration catalogues..."); |
| 61 | try { |
| 62 | JavaProjectBuilder builder = new JavaProjectBuilder(); |
| 63 | builder.addSourceTree(new File(srcDirectory, "src/main/java")); |
| 64 | builder.getClasses().forEach(this::processClass); |
| 65 | } catch (Exception e) { |
| 66 | e.printStackTrace(); |
| 67 | throw e; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | private void processClass(JavaClass javaClass) { |
| 72 | boolean isComponent = javaClass.getAnnotations().stream() |
| 73 | .map(ja -> ja.getType().getName().equals(COMPONENT)) |
| 74 | .findFirst().isPresent(); |
| 75 | if (isComponent) { |
| 76 | List<String> lines = new ArrayList<>(); |
| 77 | javaClass.getFields().forEach(field -> processField(lines, javaClass, field)); |
| 78 | if (!lines.isEmpty()) { |
| 79 | writeCatalog(javaClass, lines); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private void writeCatalog(JavaClass javaClass, List<String> lines) { |
| 85 | File dir = new File(dstDirectory, javaClass.getPackageName().replace('.', '/')); |
| 86 | dir.mkdirs(); |
| 87 | |
| 88 | File cfgDef = new File(dir, javaClass.getName().replace('.', '/') + ".cfgdef"); |
| 89 | try (FileWriter fw = new FileWriter(cfgDef); |
| 90 | PrintWriter pw = new PrintWriter(fw)) { |
| 91 | pw.println("# This file is auto-generated by onos-maven-plugin"); |
| 92 | lines.forEach(pw::println); |
| 93 | } catch (IOException e) { |
| 94 | System.err.println("Unable to write catalog for " + javaClass.getName()); |
| 95 | e.printStackTrace(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private void processField(List<String> lines, JavaClass javaClass, JavaField field) { |
| 100 | field.getAnnotations().forEach(ja -> { |
| 101 | if (ja.getType().getName().equals(PROPERTY)) { |
| 102 | lines.add(expand(javaClass, ja.getNamedParameter("name").toString()) + |
| 103 | SEP + type(field) + |
| 104 | SEP + defaultValue(javaClass, field, ja) + |
| 105 | SEP + description(ja)); |
| 106 | } |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | // TODO: Stuff below is very much hack-ish and should be redone; it works for now though. |
| 111 | |
| 112 | private String description(JavaAnnotation annotation) { |
| 113 | String description = (String) annotation.getNamedParameter("label"); |
| 114 | return description.replaceAll("\" \\+ \"", "") |
| 115 | .replaceFirst("^[^\"]*\"", "").replaceFirst("\"$", ""); |
| 116 | } |
| 117 | |
| 118 | private String type(JavaField field) { |
| 119 | String ft = field.getType().getName().toUpperCase(); |
| 120 | return ft.equals("INT") ? "INTEGER" : ft; |
| 121 | } |
| 122 | |
| 123 | private String defaultValue(JavaClass javaClass, JavaField field, |
| 124 | JavaAnnotation annotation) { |
| 125 | String ft = field.getType().getName().toLowerCase(); |
| 126 | String defValueName = ft.equals("boolean") ? "boolValue" : |
| 127 | ft.equals("string") ? "value" : ft + "Value"; |
| 128 | Object dv = annotation.getNamedParameter(defValueName); |
| 129 | return dv == null ? "" : expand(javaClass, dv.toString()); |
| 130 | } |
| 131 | |
| 132 | private String stripQuotes(String string) { |
| 133 | return string.trim().replaceFirst("^[^\"]*\"", "").replaceFirst("\"$", ""); |
| 134 | } |
| 135 | |
| 136 | private String expand(JavaClass javaClass, String value) { |
| 137 | JavaField field = javaClass.getFieldByName(value); |
| 138 | return field == null ? stripQuotes(value) : |
| 139 | stripQuotes(field.getCodeBlock().replaceFirst(".*=", "").replaceFirst(";$", "")); |
| 140 | } |
| 141 | |
| 142 | } |