1 package org.codehaus.mojo.natives.javah;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
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 }