VisClient/org/hfbk/util/JARStarter.java

Go to the documentation of this file.
00001 package org.hfbk.util;
00002 
00003 import java.applet.Applet;
00004 import java.applet.AppletStub;
00005 import java.io.File;
00006 import java.io.IOException;
00007 import java.io.InputStream;
00008 import java.io.OutputStream;
00009 import java.lang.reflect.Field;
00010 import java.net.URISyntaxException;
00011 import java.util.ArrayList;
00012 import java.util.Arrays;
00013 import java.util.LinkedList;
00014 import java.util.List;
00015 import java.util.Map;
00016 
00017 import org.hfbk.vis.Prefs;
00018 
00019 public class JARStarter extends Applet implements AppletStub {
00020         
00021         static String[] jars=new String[]{".","lwjgl.jar","lwjgl_util.jar","jna.jar", "ffmpeg.jar", "jinput.jar", "js.jar"};
00022 
00023         static String mainclass="org.hfbk.vis.VisClient"; 
00024         
00025         public void init() {
00026                 try {
00027                         List<String> args=new ArrayList<String>();
00028                         for (Field f: Prefs.class.getFields()){
00029                                 String key=f.getName();
00030                                 String value=getParameter(key);
00031                                 if (value!=null){
00032                                         args.add(key);
00033                                         args.add(value);
00034                                 }                                       
00035                         }
00036                         
00037                         //get tmpdir for decrunched VisClient 
00038                         //File dir=new File(System.getProperty("java.io.tmpdir")+File.separator+"VisClient");
00039                         
00040                         //extract files 
00041                         //Installer.install(dir);
00042                                 
00043                         //add classpath
00044                         
00045                         /*Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] {URL.class});
00046                         method.setAccessible(true);
00047                         for (File f: dir.listFiles()){
00048                                 if (f.getName().matches(".*\\.jar")){
00049                                         URL u=f.toURL();
00050                                         method.invoke(getClass().getClassLoader(), new Object[] {u});                           
00051                                         System.out.println("Extending classpath: "+u);
00052                                 }
00053                         }
00054                         System.setProperty("org.lwjgl.librarypath", dir.getAbsolutePath());
00055                         
00056                         //new VisClientWindow();
00057                         //add applet
00058                         /*Applet a=new VisClientApplet();
00059                         a.setStub(this);
00060                         setLayout(new BorderLayout());
00061                         add(a);
00062                         a.init();
00063                         */
00064                         main(args.toArray(new String[0]));
00065                 } catch (Exception e) {
00066                         throw new RuntimeException(e);
00067                 }
00068         }
00069         
00070         public static void main(String[] args) throws IOException, URISyntaxException {
00071                 
00072                 String cp=join(File.pathSeparatorChar, jars);
00073 
00074                 List<String> cmd=new LinkedList<String>();
00075                 cmd.addAll(Arrays.asList(new String[]{"java", "-Xmx1000m", "-cp", cp, mainclass}));
00076                 cmd.addAll(Arrays.asList(args));
00077                 
00078                 Map<String,String> env=System.getenv();
00079                 String[] newenv=new String[env.size()+1];
00080                 int i=0;
00081                 for (Map.Entry e:  env.entrySet())
00082                         newenv[i++]=e.getKey()+"="+e.getValue();
00083                 newenv[i++]="LD_LIBRARY_PATH=.";
00084                 
00085                 
00086                 File tmpdir=new File(System.getProperty("java.io.tmpdir"));
00087                 
00088                 Installer.install(tmpdir);
00089                 String[] tmp=cmd.toArray(new String[0]);
00090                 Process p = Runtime.getRuntime().exec(tmp, newenv, new File(tmpdir.getAbsolutePath(), File.separatorChar+"VisClient"));
00091                 
00092                 new Pipe(p.getInputStream(), System.out);
00093                 new Pipe(p.getErrorStream(), System.err);
00094         }
00095         
00096         static String join(char c, String[] strings){
00097                 StringBuilder b=new StringBuilder();
00098                 for (String s: strings){
00099                         b.append(s);
00100                         b.append(c);
00101                 }
00102                 b.deleteCharAt(b.length()-1);
00103                 return b.toString();
00104         }
00105 
00107         static class Pipe extends Thread{
00108                 
00109                 InputStream in;
00110                 OutputStream out;
00111                 
00112                 public Pipe(InputStream in, OutputStream out) {
00113                         setName("Pipe");
00114                         this.in=in;
00115                         this.out=out;
00116                         start();
00117                 }
00118                 
00119                 public void run() {
00120                         int bytesRead; 
00121                         byte[] buffer=new byte[1024];
00122                         try {
00123                                 while ((bytesRead=in.read(buffer))>0)
00124                                         out.write(buffer,0,bytesRead);
00125                         } catch (IOException e) {
00126                                 throw new RuntimeException (e);
00127                         }
00128                 }
00129         }
00130 
00131         public void appletResize(int width, int height) {}
00132 }

Generated on Tue Apr 7 17:57:20 2009 for visclient by  doxygen 1.5.1