| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// ------------------------------------------------------------------------ |
| 5 |
// All rights reserved. This program and the accompanying materials |
| 6 |
// are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
// and Apache License v2.0 which accompanies this distribution. |
| 8 |
// |
| 9 |
// The Eclipse Public License is available at |
| 10 |
// http://www.eclipse.org/legal/epl-v10.html |
| 11 |
// |
| 12 |
// The Apache License v2.0 is available at |
| 13 |
// http://www.opensource.org/licenses/apache2.0.php |
| 14 |
// |
| 15 |
// You may elect to redistribute this code under either of these licenses. |
| 16 |
// ======================================================================== |
| 17 |
// |
| 18 |
|
| 19 |
package org.eclipse.jetty.start; |
| 20 |
|
| 21 |
import java.io.BufferedWriter; |
| 22 |
import java.io.IOException; |
| 23 |
import java.io.PrintWriter; |
| 24 |
import java.nio.charset.StandardCharsets; |
| 25 |
import java.nio.file.Files; |
| 26 |
import java.nio.file.Path; |
| 27 |
import java.nio.file.StandardOpenOption; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
/** |
| 31 |
* Generate a graphviz dot graph of the modules found |
| 32 |
*/ |
| 33 |
public class ModuleGraphWriter |
| 34 |
{ |
| 35 |
private String colorModuleBg; |
| 36 |
private String colorEnabledBg; |
| 37 |
private String colorTransitiveBg; |
| 38 |
private String colorCellBg; |
| 39 |
private String colorHeaderBg; |
| 40 |
private String colorModuleFont; |
| 41 |
|
| 42 |
public ModuleGraphWriter() |
| 43 |
{ |
| 44 |
colorModuleBg = "#B8FFB8"; |
| 45 |
colorEnabledBg = "#66FFCC"; |
| 46 |
colorTransitiveBg = "#66CC66"; |
| 47 |
colorCellBg = "#FFFFFF80"; |
| 48 |
colorHeaderBg = "#00000020"; |
| 49 |
colorModuleFont = "#888888"; |
| 50 |
} |
| 51 |
|
| 52 |
public void config(Props props) |
| 53 |
{ |
| 54 |
String prefix = "jetty.graph."; |
| 55 |
colorModuleBg = getProperty(props, prefix + "color.module.bg", colorModuleBg); |
| 56 |
colorEnabledBg = getProperty(props, prefix + "color.enabled.bg", colorEnabledBg); |
| 57 |
colorTransitiveBg = getProperty(props, prefix + "color.transitive.bg", colorTransitiveBg); |
| 58 |
colorCellBg = getProperty(props, prefix + "color.cell.bg", colorCellBg); |
| 59 |
colorHeaderBg = getProperty(props, prefix + "color.header.bg", colorHeaderBg); |
| 60 |
colorModuleFont = getProperty(props, prefix + "color.font", colorModuleFont); |
| 61 |
} |
| 62 |
|
| 63 |
private String getProperty(Props props, String key, String defVal) |
| 64 |
{ |
| 65 |
String val = props.getString(key, defVal); |
| 66 |
if (val == null) |
| 67 |
{ |
| 68 |
return defVal; |
| 69 |
} |
| 70 |
val = val.trim(); |
| 71 |
if (val.length() <= 0) |
| 72 |
{ |
| 73 |
return defVal; |
| 74 |
} |
| 75 |
return val; |
| 76 |
} |
| 77 |
|
| 78 |
public void write(Modules modules, Path outputFile) throws IOException |
| 79 |
{ |
| 80 |
try (BufferedWriter writer = Files.newBufferedWriter(outputFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); |
| 81 |
PrintWriter out = new PrintWriter(writer)) |
| 82 |
{ |
| 83 |
writeHeaderMessage(out, outputFile); |
| 84 |
|
| 85 |
out.println(); |
| 86 |
out.println("digraph modules {"); |
| 87 |
|
| 88 |
// Node Style |
| 89 |
out.println(" node [color=gray, style=filled, shape=rectangle];"); |
| 90 |
out.println(" node [fontname=\"Verdana\", size=\"20,20\"];"); |
| 91 |
// Graph Style |
| 92 |
out.println(" graph ["); |
| 93 |
out.println(" concentrate=false,"); |
| 94 |
out.println(" fontname=\"Verdana\","); |
| 95 |
out.println(" fontsize = 20,"); |
| 96 |
out.println(" rankdir = LR,"); |
| 97 |
out.println(" ranksep = 1.5,"); |
| 98 |
out.println(" nodesep = .5,"); |
| 99 |
out.println(" style = bold,"); |
| 100 |
out.println(" labeljust = l,"); |
| 101 |
out.println(" label = \"Jetty Modules\","); |
| 102 |
out.println(" ssize = \"20,40\""); |
| 103 |
out.println(" ];"); |
| 104 |
|
| 105 |
List<Module> enabled = modules.getEnabled(); |
| 106 |
|
| 107 |
// Module Nodes |
| 108 |
writeModules(out, modules, enabled); |
| 109 |
|
| 110 |
// Module Relationships |
| 111 |
writeRelationships(out, modules, enabled); |
| 112 |
|
| 113 |
out.println("}"); |
| 114 |
out.println(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private void writeHeaderMessage(PrintWriter out, Path outputFile) |
| 119 |
{ |
| 120 |
out.println("/*"); |
| 121 |
out.println(" * GraphViz Graph of Jetty Modules"); |
| 122 |
out.println(" * "); |
| 123 |
out.println(" * Jetty: https://eclipse.org/jetty/"); |
| 124 |
out.println(" * GraphViz: http://graphviz.org/"); |
| 125 |
out.println(" * "); |
| 126 |
out.println(" * To Generate Graph image using graphviz:"); |
| 127 |
String filename = outputFile.getFileName().toString(); |
| 128 |
String basename = filename.substring(0, filename.indexOf('.')); |
| 129 |
out.printf(" * $ dot -Tpng -Goverlap=false -o %s.png %s%n", basename, filename); |
| 130 |
out.println(" */"); |
| 131 |
} |
| 132 |
|
| 133 |
private void writeModuleDetailHeader(PrintWriter out, String header) |
| 134 |
{ |
| 135 |
writeModuleDetailHeader(out, header, 1); |
| 136 |
} |
| 137 |
|
| 138 |
private void writeModuleDetailHeader(PrintWriter out, String header, int count) |
| 139 |
{ |
| 140 |
out.printf(" <TR>"); |
| 141 |
out.printf("<TD BGCOLOR=\"%s\" ALIGN=\"LEFT\"><I>", colorHeaderBg); |
| 142 |
out.printf("%s%s</I></TD>", header, count > 1 ? "s" : ""); |
| 143 |
out.println("</TR>"); |
| 144 |
} |
| 145 |
|
| 146 |
private void writeModuleDetailLine(PrintWriter out, String line) |
| 147 |
{ |
| 148 |
out.printf(" <TR>"); |
| 149 |
StringBuilder escape = new StringBuilder(); |
| 150 |
for (char c : line.toCharArray()) |
| 151 |
{ |
| 152 |
switch (c) |
| 153 |
{ |
| 154 |
case '<': |
| 155 |
escape.append("<"); |
| 156 |
break; |
| 157 |
case '>': |
| 158 |
escape.append(">"); |
| 159 |
break; |
| 160 |
default: |
| 161 |
escape.append(c); |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
out.printf("<TD BGCOLOR=\"%s\" ALIGN=\"LEFT\">%s</TD></TR>%n", colorCellBg, escape.toString()); |
| 167 |
} |
| 168 |
|
| 169 |
private void writeModuleNode(PrintWriter out, Module module, boolean resolved) |
| 170 |
{ |
| 171 |
String color = colorModuleBg; |
| 172 |
if (module.isEnabled()) |
| 173 |
{ |
| 174 |
// specifically enabled by config |
| 175 |
color = colorEnabledBg; |
| 176 |
} |
| 177 |
else if (resolved) |
| 178 |
{ |
| 179 |
// enabled by transitive reasons |
| 180 |
color = colorTransitiveBg; |
| 181 |
} |
| 182 |
|
| 183 |
out.printf(" \"%s\" [ color=\"%s\" label=<", module.getName(), color); |
| 184 |
out.printf("<TABLE BORDER=\"0\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"2\">%n"); |
| 185 |
out.printf(" <TR><TD ALIGN=\"LEFT\"><B>%s</B></TD></TR>%n", module.getName()); |
| 186 |
|
| 187 |
if (module.isEnabled()) |
| 188 |
{ |
| 189 |
writeModuleDetailHeader(out, "ENABLED"); |
| 190 |
for (String selection : module.getEnableSources()) |
| 191 |
{ |
| 192 |
writeModuleDetailLine(out, "via: " + selection); |
| 193 |
} |
| 194 |
} |
| 195 |
else if (resolved) |
| 196 |
{ |
| 197 |
writeModuleDetailHeader(out, "TRANSITIVE"); |
| 198 |
} |
| 199 |
|
| 200 |
if (!module.getXmls().isEmpty()) |
| 201 |
{ |
| 202 |
List<String> xmls = module.getXmls(); |
| 203 |
writeModuleDetailHeader(out, "XML", xmls.size()); |
| 204 |
for (String xml : xmls) |
| 205 |
{ |
| 206 |
writeModuleDetailLine(out, xml); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (!module.getLibs().isEmpty()) |
| 211 |
{ |
| 212 |
List<String> libs = module.getLibs(); |
| 213 |
writeModuleDetailHeader(out, "LIB", libs.size()); |
| 214 |
for (String lib : libs) |
| 215 |
{ |
| 216 |
writeModuleDetailLine(out, lib); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if (!module.getIniTemplate().isEmpty()) |
| 221 |
{ |
| 222 |
List<String> inis = module.getIniTemplate(); |
| 223 |
writeModuleDetailHeader(out, "INI Template", inis.size()); |
| 224 |
} |
| 225 |
|
| 226 |
out.printf("</TABLE>>];%n"); |
| 227 |
} |
| 228 |
|
| 229 |
private void writeModules(PrintWriter out, Modules allmodules, List<Module> enabled) |
| 230 |
{ |
| 231 |
out.println(); |
| 232 |
out.println(" /* Modules */"); |
| 233 |
out.println(); |
| 234 |
|
| 235 |
out.println(" node [ labeljust = l ];"); |
| 236 |
|
| 237 |
for (Module module : allmodules) |
| 238 |
{ |
| 239 |
boolean resolved = enabled.contains(module); |
| 240 |
writeModuleNode(out, module, resolved); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
private void writeRelationships(PrintWriter out, Iterable<Module> modules, List<Module> enabled) |
| 245 |
{ |
| 246 |
for (Module module : modules) |
| 247 |
{ |
| 248 |
for (String depends : module.getDepends()) |
| 249 |
{ |
| 250 |
depends = Module.normalizeModuleName(depends); |
| 251 |
out.printf(" \"%s\" -> \"%s\";%n", module.getName(), depends); |
| 252 |
} |
| 253 |
for (String optional : module.getOptional()) |
| 254 |
{ |
| 255 |
out.printf(" \"%s\" => \"%s\";%n", module.getName(), optional); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} |