1 package org.codehaus.mojo.rmic;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.codehaus.plexus.compiler.CompilerException;
26
27 import java.io.File;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30
31
32
33
34
35 class BuiltInRmiCompiler extends AbstractRmiCompiler
36 {
37 private static final String EOL = System.getProperty( "line.separator" );
38
39
40
41
42 private static final String RMIC_CLASSNAME = "sun.rmi.rmic.Main";
43
44 private static final String USE_GLASSFISH_RMIC =
45 " Built-in RMIC compiler not available in JDK9."
46 + " Add a dependency on org.glassfish.corba:rmic to the plugin.";
47
48 @Override
49 protected Class<?> createMainClass()
50 throws CompilerException
51 {
52 try
53 {
54 return getClassLoaderFacade().loadClass( BuiltInRmiCompiler.RMIC_CLASSNAME );
55 }
56 catch ( ClassNotFoundException ignored )
57 {
58
59 }
60
61 try
62 {
63 addToolsJarToPath();
64 return getClassLoaderFacade().loadClass( BuiltInRmiCompiler.RMIC_CLASSNAME );
65 }
66 catch ( Exception ex )
67 {
68 throw new CompilerException( getSecondTryMessage( ex ) );
69 }
70 }
71
72 private static String getSecondTryMessage( Exception e ) throws CompilerException
73 {
74 return builtInCompilerHidden( e ) ? USE_GLASSFISH_RMIC : getRmicCompilerNotAvailableMessage();
75 }
76
77 private static boolean builtInCompilerHidden( Exception e )
78 {
79 return compilerNotFound( e ) && isJigsawPresent();
80 }
81
82 private static boolean compilerNotFound( Exception e )
83 {
84 return e instanceof ClassNotFoundException;
85 }
86
87 private static boolean isJigsawPresent()
88 {
89 return !System.getProperty( "java.version" ).startsWith( "1." );
90 }
91
92 private static String getRmicCompilerNotAvailableMessage() throws CompilerException
93 {
94 return "Unable to locate the Rmi Compiler in:" + EOL + " " + getToolsJarUrl() + EOL
95 + "Please ensure you are using JDK 1.4 or above and" + EOL + "not a JRE (the " + RMIC_CLASSNAME
96 + " class is required)." + EOL + "In most cases you can change the location of your Java" + EOL
97 + "installation by setting the JAVA_HOME environment variable.";
98 }
99
100
101 private static void addToolsJarToPath() throws MalformedURLException, ClassNotFoundException, CompilerException
102 {
103 URL toolsJarUrl = getToolsJarUrl();
104 getClassLoaderFacade().prependUrls( toolsJarUrl );
105 }
106
107 private static URL getToolsJarUrl() throws CompilerException
108 {
109 File javaHome = new File( System.getProperty( "java.home" ) );
110 File toolsJar = new File( javaHome, "../lib/tools.jar" );
111 try
112 {
113 return toolsJar.toURI().toURL();
114 }
115 catch ( MalformedURLException e )
116 {
117 throw new CompilerException( "Could not convert the file reference to tools.jar to a URL path to the jar: '"
118 + toolsJar.getAbsolutePath() + "'.", e );
119 }
120 }
121 }