View Javadoc
1   package org.codehaus.mojo.natives.javah;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
9    * associated documentation files (the "Software"), to deal in the Software without restriction,
10   * including without limitation the rights to use, copy, modify, merge, publish, distribute,
11   * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   *
14   * The above copyright notice and this permission notice shall be included in all copies or
15   * substantial portions of the Software.
16   *
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
18   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20   * DAMAGES OR OTHER 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 SOFTWARE.
22   */
23  
24  import org.codehaus.mojo.natives.NativeBuildException;
25  import org.codehaus.mojo.natives.util.CommandLineUtil;
26  import org.codehaus.plexus.util.cli.Commandline;
27  
28  import java.io.File;
29  
30  /**
31   * Sun's javah compatible implementation
32   */
33  
34  public class JavahExecutable
35      extends AbstractJavah
36  {
37      public JavahExecutable()
38      {
39      }
40  
41      public void compile( JavahConfiguration config )
42          throws NativeBuildException
43      {
44          Commandline cl = this.createJavahCommand( config );
45  
46          CommandLineUtil.execute( cl, this.getLogger() );
47      }
48  
49      protected Commandline createJavahCommand( JavahConfiguration config )
50          throws NativeBuildException
51      {
52          this.validateConfiguration( config );
53  
54          Commandline cl = new Commandline();
55  
56          if ( config.getWorkingDirectory() != null )
57          {
58              cl.setWorkingDirectory( config.getWorkingDirectory().getPath() );
59          }
60  
61          cl.setExecutable( this.getJavaHExecutable( config ) );
62  
63          if ( config.getFileName() != null && config.getFileName().length() > 0 )
64          {
65              File outputFile = new File( config.getOutputDirectory(), config.getFileName() );
66              cl.createArg().setValue( "-o" );
67              cl.createArg().setFile( outputFile );
68          }
69          else
70          {
71              if ( config.getOutputDirectory() != null )
72              {
73                  cl.createArg().setValue( "-d" );
74                  cl.createArg().setFile( config.getOutputDirectory() );
75              }
76          }
77  
78          String[] classPaths = config.getClassPaths();
79  
80          StringBuffer classPathBuffer = new StringBuffer();
81  
82          for ( int i = 0; i < classPaths.length; ++i )
83          {
84              classPathBuffer.append( classPaths[i] );
85              if ( i != classPaths.length - 1 )
86              {
87                  classPathBuffer.append( File.pathSeparatorChar );
88              }
89          }
90  
91          if ( config.getUseEnvClasspath() )
92          {
93              cl.addEnvironment( "CLASSPATH", classPathBuffer.toString() );
94          }
95          else
96          {
97              cl.createArg().setValue( "-classpath" );
98  
99              cl.createArg().setValue( classPathBuffer.toString() );
100         }
101 
102         if ( config.getVerbose() )
103         {
104             cl.createArg().setValue( "-verbose" );
105         }
106 
107         cl.addArguments( config.getClassNames() );
108 
109         return cl;
110     }
111 
112     private void validateConfiguration( JavahConfiguration config )
113         throws NativeBuildException
114     {
115         if ( config.getClassPaths() == null || config.getClassPaths().length == 0 )
116         {
117             throw new NativeBuildException( "javah classpaths can not be empty." );
118         }
119 
120         if ( config.getOutputDirectory() == null )
121         {
122             throw new NativeBuildException( "javah destDir can not be empty." );
123         }
124 
125         if ( !config.getOutputDirectory().exists() )
126         {
127             config.getOutputDirectory().mkdirs();
128         }
129 
130         if ( config.getClassNames() == null || config.getClassNames().length == 0 )
131         {
132             throw new NativeBuildException( "javah: java classes can not be empty." );
133         }
134 
135     }
136 
137     /**
138      * @return
139      */
140     protected String getJavaHExecutable( JavahConfiguration config )
141     {
142         String path = "javah";
143 
144         if ( config.getJavahPath() != null )
145         {
146             path = config.getJavahPath().getAbsolutePath();
147         }
148 
149         return path;
150     }
151 
152 }