View Javadoc
1   package org.codehaus.mojo.rmic;
2   
3   /*
4    * Copyright (c) 2004-2017, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   * @version $Id$
34   */
35  class BuiltInRmiCompiler extends AbstractRmiCompiler
36  {
37      private static final String EOL = System.getProperty( "line.separator" );
38  
39      /**
40       * The name of the class to use for rmi compilation.
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              // ignore
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 }