1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.codehaus.mojo.jaxws;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.net.MalformedURLException;
27 import java.net.URL;
28 import java.net.URLClassLoader;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Properties;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35
36
37
38
39 public final class Invoker
40 {
41
42 public static void main( String... args )
43 throws Exception
44 {
45 String toolClassname = args[0];
46
47 int idx = 1;
48 String cp = args[2];
49 if ( "-pathfile".equals( args[1] ) )
50 {
51
52 File pathFile = new File( args[2] );
53 pathFile.deleteOnExit();
54 Properties p = new Properties();
55 try ( InputStream is = new FileInputStream( pathFile ) ) {
56 p.load( is );
57 }
58 cp = p.getProperty( "cp" );
59 idx = 3;
60 }
61
62
63 ClassLoader orig = Thread.currentThread().getContextClassLoader();
64 String origJcp = System.getProperty( "java.class.path" );
65
66 try ( URLClassLoader cl = new URLClassLoader( toUrls( cp ) ) )
67 {
68
69 Thread.currentThread().setContextClassLoader( cl );
70 System.setProperty( "java.class.path", cp );
71
72 Class<?> toolClass = cl.loadClass( toolClassname );
73
74 Object tool = toolClass.getConstructor( OutputStream.class ).newInstance( System.out );
75 Method runMethod = toolClass.getMethod( "run", String[].class );
76
77 String[] wsargs = new String[args.length - idx];
78 System.arraycopy( args, idx, wsargs, 0, args.length - idx );
79
80 System.exit( (Boolean) runMethod.invoke( tool, new Object[] { wsargs } ) ? 0 : 1 );
81 }
82 catch ( NoSuchMethodException ex )
83 {
84 logSevere( ex );
85 }
86 catch ( SecurityException ex )
87 {
88 logSevere( ex );
89 }
90 catch ( ClassNotFoundException ex )
91 {
92 logSevere( ex );
93 }
94 catch ( InstantiationException ex )
95 {
96 logSevere( ex );
97 }
98 catch ( IllegalAccessException ex )
99 {
100 logSevere( ex );
101 }
102 catch ( IllegalArgumentException ex )
103 {
104 logSevere( ex );
105 }
106 catch ( InvocationTargetException ex )
107 {
108 Exception rex = new RuntimeException();
109 rex.initCause( ex );
110 throw ex;
111 }
112 finally
113 {
114 Thread.currentThread().setContextClassLoader( orig );
115 System.setProperty( "java.class.path", origJcp );
116 }
117 }
118
119 private static void logSevere( Exception ex )
120 {
121 Logger.getLogger( Invoker.class.getName() ).log( Level.SEVERE, null, ex );
122 }
123
124 private static URL[] toUrls( String c )
125 {
126 List<URL> urls = new ArrayList<>();
127 for ( String s : c.split( File.pathSeparator ) )
128 {
129 try
130 {
131 urls.add( new File( s ).toURI().toURL() );
132 }
133 catch ( MalformedURLException ex )
134 {
135 Logger.getLogger( Invoker.class.getName() ).log( Level.SEVERE, null, ex );
136 }
137 }
138 return urls.toArray( new URL[0] );
139 }
140 }