1   /*
2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3    *
4    * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5    *
6    * Oracle licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * @author lukas
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              // load cp from properties file
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          // save original classloader and java.class.path property
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              // set to values for tool invocation
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 }