package vnsim.gui; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Vector; import java.util.jar.JarEntry; import java.util.jar.JarFile; import javax.swing.JFileChooser; import javax.swing.filechooser.FileSystemView; import sun.misc.URLClassPath; import com.sun.javaws.jnl.JARDesc; import com.sun.javaws.jnl.LaunchDesc; import com.sun.javaws.jnl.ResourcesDesc; import com.sun.jnlp.JNLPClassLoader; /** * An utility class with several methods that are used throughout the GUI classes * @author cipsm * */ public class Utils { private static final Utils _instance = new Utils(); public static Utils getInstance() { return _instance; } private Utils() { } public FileSystemView getSystemView(String root) { return new ClassSystemView(root); } /** A FileSystemView implementation that is used to display the classes loaded by the GUI used ClassLoader.. */ public static class ClassSystemView extends FileSystemView { private String root; private SystemViewHandler handler = null; public ClassSystemView(String root) { this.root = root; System.out.println(root); // init the context try { Method m = ClassLoader.class.getDeclaredMethod("getBootstrapClassPath", new Class[0]); m.setAccessible(true); URLClassPath p = (URLClassPath)m.invoke(getClass().getClassLoader(), new Object[0]); URL u[] = p.getURLs(); for (int i=0; i jarDescs = new ArrayList(); ResourcesDesc rd = ld.getResources(); if (rd != null) { JARDesc[] jars = rd.getEagerOrAllJarDescs(true); for (int i = 0; i < jars.length; i++) if (jars[i].isJavaFile() && !jars[i].isNativeLib()) jarDescs.add(jars[i]); } for (JARDesc jd : jarDescs) { URL location = jd.getLocation(); if (!location.getFile().endsWith("maps.jar")) continue; // download it ? File dirToDownload = new File(System.getProperty("user.home")+File.separatorChar+"maps"); if (!dirToDownload.exists()) { dirToDownload.mkdirs(); } String path = System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"maps.jar"; File tmp = new File(path); InputStream isDown; URLConnection connection = location.openConnection(); //write from web to local file FileOutputStream fOS = new FileOutputStream(tmp); isDown = connection.getInputStream(); int nBufferSize = 1024; byte buff[] = new byte[nBufferSize]; int nRead = -1; do { nRead = isDown.read(buff); if ( nRead > 0 ) { fOS.write(buff, 0, nRead); } } while ( nRead > 0 ); fOS.flush(); fOS.close(); isDown.close(); // System.out.println(path); JarHandler h = new JarHandler(path); final File ff[] = h.listResources(root); if (ff != null && ff.length != 0) { handler = h; break; } } } } catch (Throwable t1) { // can happen due to different versions of JavaWS } if (handler == null) { FileHandler h = new FileHandler("."); if (h.listResources(root) != null) handler = h; } } } if (handler == null) { FileHandler h = new FileHandler("."); if (h.listResources(root) != null) handler = h; } } catch (Throwable t) { t.printStackTrace(); } } public File[] getRoots() { if (handler == null) return null; return new File[] { new CustomFile(root, true) }; } public boolean isRoot(File f) { if (f == null) return false; return f.getName().equals(root); } public File[] getFiles(File dir, boolean useFileHiding) { // System.out.println(dir.getName()+" "+(dir instanceof CustomFile)); if (handler != null) { return handler.listResources(dir.getName()); } return null; } public String[] list() { if (handler != null) { File f[] = handler.listResources(root); if (f == null || f.length == 0) { return null; } String[] s = new String[f.length]; for (int i=0; i e = jf.entries(); JarEntry je; Vector v = new Vector(); while (e.hasMoreElements()){ je = e.nextElement(); String name = je.getName(); if (!name.startsWith("/")) name = "/"+name; // System.out.println(name + " " + resource); if (name.startsWith(resource) && name.length() > resource.length()) { String end = name.substring(resource.length()); if (end.length() > 0 && end.startsWith("/")) end = end.substring(1); if (end.contains("/") || end.length() < 1) continue; // uninteresting result v.add(new CustomFile(end, je.isDirectory())); } else continue; } if (v.size() == 0) return null; CustomFile ff[] = new CustomFile[v.size()]; ff = v.toArray(ff); return ff; } } catch (Throwable t) { t.printStackTrace(); return null; } return null; } } static class FileHandler implements SystemViewHandler { private String rootDir; public FileHandler(String dirName) { this.rootDir = dirName; } public File[] listResources(String resource) { if(File.separatorChar == '\\') { resource = resource.replace("/", "\\").trim(); } File dir = new File(rootDir+File.separatorChar+resource); if (dir.exists() && dir.isDirectory()) { File f[] = dir.listFiles(); if (f == null || f.length == 0) { return f; } CustomFile cf[] = new CustomFile[f.length]; for (int i=0; i