Skip to content

Commit 9ca5467

Browse files
committed
check needed classpath for engines
1 parent 248b85f commit 9ca5467

File tree

8 files changed

+15
-19
lines changed

8 files changed

+15
-19
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/engine/AbstractJsGraphvizEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
public abstract class AbstractJsGraphvizEngine extends AbstractGraphvizEngine {
3535
private static final String VIZ_BASE = "META-INF/resources/webjars/viz.js-for-graphviz-java/2.1.2/";
36-
static boolean AVAILABLE = isOnClasspath(VIZ_BASE + "viz.js");
36+
static final boolean AVAILABLE = isOnClasspath(VIZ_BASE + "viz.js");
3737
private static final Pattern FONT_NAME_PATTERN = Pattern.compile("\"?fontname\"?\\s*=\\s*\"?(.*?)[\",;\\]]");
3838
private static final Map<Class<?>, ThreadLocal<JavascriptEngine>> ENGINES = new HashMap<>();
3939
private final Supplier<JavascriptEngine> engineSupplier;

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Graphviz.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ private static List<GraphvizEngine> availableEngines() {
8282
engines.add(new GraphvizJdkEngine());
8383
}
8484
if (engines.size() == 1) {
85-
LOG.warn("Only GraphvizServerEngine is available." +
86-
" If you want to use other engines, please put the needed dependencies on the classpath.");
85+
LOG.warn("Only GraphvizServerEngine is available."
86+
+ " If you want to use other engines, please put the needed dependencies on the classpath.");
8787
}
8888
return engines;
8989
}

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizCmdLineEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
public class GraphvizCmdLineEngine extends AbstractGraphvizEngine {
3939
private static final Logger LOG = LoggerFactory.getLogger(AbstractGraphvizEngine.class);
40-
static boolean AVAILABLE = isOnClasspath("org.apache.commons.exec.CommandLine");
40+
static final boolean AVAILABLE = isOnClasspath("org/apache/commons/exec/CommandLine.class");
4141

4242
private final String envPath;
4343
private final CommandRunner cmdRunner;

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizJdkEngine.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import static guru.nidi.graphviz.engine.IoUtils.isOnClasspath;
2121

2222
public class GraphvizJdkEngine extends AbstractJsGraphvizEngine {
23-
static boolean AVAILABLE = AbstractJsGraphvizEngine.AVAILABLE &&
24-
(tryGraal() != null || isOnClasspath("net.arnx.nashorn.lib.PromiseException"));
23+
static final boolean AVAILABLE = AbstractJsGraphvizEngine.AVAILABLE
24+
&& (tryGraal() != null || isOnClasspath("net/arnx/nashorn/lib/PromiseException.class"));
2525

2626
public GraphvizJdkEngine() {
2727
super(false, GraphvizJdkEngine::newEngine);
@@ -32,7 +32,7 @@ public GraphvizJdkEngine() {
3232

3333
private static JavascriptEngine newEngine() {
3434
final GraalJavascriptEngine graal = tryGraal();
35-
return graal != null ? graal : new NashornJavascriptEngine();
35+
return graal == null ? new NashornJavascriptEngine() : graal;
3636
}
3737

3838
@Nullable

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizV8Engine.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import static guru.nidi.graphviz.engine.IoUtils.isOnClasspath;
2121

2222
public class GraphvizV8Engine extends AbstractJsGraphvizEngine {
23-
static boolean AVAILABLE = AbstractJsGraphvizEngine.AVAILABLE && isOnClasspath("com.eclipsesource.v8.V8");
23+
static final boolean AVAILABLE = AbstractJsGraphvizEngine.AVAILABLE
24+
&& isOnClasspath("com/eclipsesource/v8/V8.class");
2425

2526
public GraphvizV8Engine() {
2627
this(null);

graphviz-java/src/main/java/guru/nidi/graphviz/engine/IoUtils.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,8 @@ static String readStream(InputStream in) throws IOException {
3232
return new String(buf, StandardCharsets.UTF_8);
3333
}
3434

35-
static boolean isOnClasspath(String clazz) {
36-
try {
37-
Class.forName(clazz);
38-
return true;
39-
} catch (ClassNotFoundException e) {
40-
return false;
41-
}
35+
static boolean isOnClasspath(String resource) {
36+
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource) != null;
4237
}
4338

4439
static void closeQuietly(AutoCloseable c) {

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Rasterizer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626

2727
public interface Rasterizer {
2828
@Nullable
29-
Rasterizer BATIK = isOnClasspath("org.apache.batik.transcoder.Transcoder") ? new BatikRasterizer() : null;
29+
Rasterizer BATIK = isOnClasspath("org/apache/batik/transcoder/Transcoder.class") ? new BatikRasterizer() : null;
3030
@Nullable
31-
Rasterizer SALAMANDER = isOnClasspath("com.kitfox.svg.SVGDiagram") ? new SalamanderRasterizer() : null;
31+
Rasterizer SALAMANDER = isOnClasspath("com/kitfox/svg/SVGDiagram.class") ? new SalamanderRasterizer() : null;
3232
@Nullable
3333
Rasterizer DEFAULT = getDefault();
3434

graphviz-java/src/test/java/guru/nidi/graphviz/CodeAnalysisTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected PmdResult analyzePmd() {
114114
.ignore("CyclomaticComplexity", "StdCyclomaticComplexity", "ModifiedCyclomaticComplexity", "NPathComplexity"),
115115
In.classes(GraphvizJdkEngine.class, GraphvizV8Engine.class, GraphvizServerEngine.class, AbstractGraphvizEngine.class)
116116
.ignore("PreserveStackTrace", "SignatureDeclareThrowsException", "AvoidCatchingGenericException"),
117-
In.locs("MutableGraph", "Serializer", "ParserImpl", "Label").ignore("GodClass"),
117+
In.locs("MutableGraph", "Serializer", "ParserImpl", "Label", "Graphviz").ignore("GodClass"),
118118
In.locs("ImmutableGraph", "MutableGraph").ignore("ExcessiveMethodLength", "ExcessiveParameterList", "LooseCoupling"),
119119
In.locs("Format", "ImmutableGraph$GraphAttributed").ignore("AccessorMethodGeneration"),
120120
In.locs("AttributeConfigs", "AttributeValidator", "FontTools").ignore("TooManyStaticImports"),
@@ -142,7 +142,7 @@ protected CpdResult analyzeCpd() {
142142
.apply(PmdConfigs.cpdIgnoreEqualsHashCodeToString())
143143
.because("It's java",
144144
In.loc("*Graph").ignore("Graph(strict, directed, cluster, name,", "if (strict != graph.strict) {"),
145-
In.loc("Format").ignore("String preProcess(String src) {"))
145+
In.loc("Format").ignore("EngineResult postProcess("))
146146
.just(In.locs("GraphvizGraalEngine", "GraphvizNashornEngine").ignore("void doInit()"));
147147
return new CpdAnalyzer(AnalyzerConfig.maven().main(), 36, collector).analyze();
148148
}

0 commit comments

Comments
 (0)