Urbi SDK Remote for Java  2.7.5
UMain.java
00001 /*
00002  * Copyright (C) 2010-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00011 package urbi;
00012 
00013 
00014 /// Import the liburbi classes in liburbi.main (located in in liburbi.jar).
00015 import urbi.UObject;
00016 import java.util.Map;
00017 import java.lang.UnsatisfiedLinkError;
00018 import java.io.File;
00019 import java.io.IOException;
00020 import java.io.FileInputStream;
00021 import java.io.FileNotFoundException;
00022 import java.lang.reflect.Method;
00023 import java.net.URL;
00024 import java.net.URLClassLoader;
00025 import java.util.jar.JarInputStream;
00026 import java.util.jar.JarEntry;
00027 
00028 public class UMain
00029 {
00030 
00031     static
00032     {
00033         try
00034         {
00035             /// Load the c++ native library.
00036             System.loadLibrary("urbijava");
00037         }
00038         catch (java.lang.UnsatisfiedLinkError e)
00039         {
00040             System.out.println ("Loading exception: " + e);
00041 
00042             // DEBUG INFORMATIONS
00043             System.out.println();
00044             System.out.println("----------- Java.library.path -------------");
00045             String lib_path = System.getProperty("java.library.path");
00046             System.out.println(lib_path);
00047 
00048             // get the system environment variables
00049             System.out.println();
00050             System.out.println("---------- ENVIRONMENT VARIABLES ----------");
00051             Map<String, String> envMap = System.getenv();
00052             // print the system environment variables
00053             for (String key : envMap.keySet())
00054                 System.out.println(key + " = " +  envMap.get(key));
00055             System.out.println();
00056 
00057             System.exit (1);
00058         }
00059     }
00060 
00061     // The methods addFile and associated final Class[] parameters
00062     // were gratefully copied from anthony_miguel @
00063     // http://forum.java.sun.com/thread.jsp?forum=32&thread=300557&tstart=0&trange=15
00064     private static final Class[] parameters = new Class[]{URL.class};
00065 
00066     public static void addFile(String s) throws IOException
00067     {
00068         addURL(new URL("file", null, s));
00069     }
00070 
00071     public static void addURL(URL u) throws IOException
00072     {
00073         URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
00074         Class sysclass = URLClassLoader.class;
00075         try {
00076             Method method = sysclass.getDeclaredMethod("addURL",parameters);
00077             method.setAccessible(true);
00078             method.invoke(sysloader,new Object[]{ u });
00079         } catch (Throwable t) {
00080             t.printStackTrace();
00081             throw new IOException("Error, could not add URL to system classloader");
00082         }//end try catch
00083     }//end method
00084 
00085     public static void usage()
00086     {
00087         System.out.println("usage:");
00088         System.out.println("<java command line> [OPTIONS] MODULES_NAMES ... [-- [urbi-launch options]]");
00089         System.out.println();
00090         System.out.println("Options:");
00091         System.out.println("  -h, --help  output this message and exit successfully");
00092         System.out.println();
00093         System.out.println("MODULE_NAMES is a list of JAR and ZIP archives containings UObjects.");
00094         System.exit(0);
00095     }
00096 
00097     public static void main(String argv[])
00098     {
00099         boolean done = false;
00100         int i = 0;
00101         String uob_path_s = System.getenv("URBI_UOBJECT_PATH");
00102         // If URBI_UOBJECT_PATH is not defined, look in .
00103         if (uob_path_s == null)
00104             uob_path_s = ".";
00105         String[] uob_path = uob_path_s.split(":");
00106         for (; i < argv.length && !done; ++i)
00107         {
00108             String s = argv[i];
00109             if (s.equals("-h") || s.equals("--help"))
00110                 usage();
00111             else if (s.equals("--"))
00112                 done = true;
00113             else
00114             {
00115                 try {
00116                     String jarname = argv[i];
00117                     boolean found = false;
00118                     // Test if path is absolute
00119                     if (new File(jarname).getAbsolutePath().equals(jarname)){
00120                         if ((new File(jarname)).exists())
00121                             found = true;
00122                     }
00123                     else
00124                         // Search for uobject in URBI_UOBJECT_PATH
00125                         for(String p: uob_path) {
00126                             String possible_path = String.format("%s/%s", p, jarname);
00127                             if ((new File(possible_path)).exists()) {
00128                                 jarname = possible_path;
00129                                 found = true;
00130                                 break;
00131                             }
00132                         }
00133                     if (!found) {
00134                         String msg = "JAR archive %s not found (is URBI_UOBJECT_PATH correctly set ?).";
00135                         msg = String.format(msg, jarname);
00136                         throw new FileNotFoundException(msg);
00137                     }
00138                     Log.info("Processing " + jarname);
00139                     addFile(jarname);
00140                     JarInputStream jis = new JarInputStream(new FileInputStream(jarname));
00141                     JarEntry entry = jis.getNextJarEntry();
00142                     while (entry != null) {
00143                         String name = entry.getName();
00144                         if (name.endsWith(".class")) {
00145                             name = name.substring(0, name.length() - 6);
00146                             name = name.replace('/', '.');
00147                             Class.forName(name);
00148                             Log.info("'" + name + "' loaded");
00149                         }
00150                         entry = jis.getNextJarEntry();
00151                     }
00152                 } catch (ClassNotFoundException cnfe) {
00153                     Log.error(cnfe.toString());
00154                     System.exit(1);
00155                 } catch (Exception e) {
00156                     Log.error(e.toString());
00157                     System.exit(1);
00158                 }
00159             }
00160         }
00161         String[] subargv = null;
00162         if (i < argv.length)
00163         {
00164             subargv = new String[argv.length - i];
00165             System.arraycopy(argv, i, subargv, 0, subargv.length);
00166         }
00167         else
00168             subargv = new String[0];
00169         try
00170         {
00171             UObject.main(subargv);
00172         }
00173         catch (Exception e)
00174         {
00175             Log.error(e.toString());
00176             System.exit(1);
00177         }
00178     }
00179 }