source: proiecte/ptvs/src/vnsim/gui/Utils.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 13.2 KB
Line 
1package vnsim.gui;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.lang.reflect.Method;
10import java.net.URL;
11import java.net.URLClassLoader;
12import java.net.URLConnection;
13import java.util.ArrayList;
14import java.util.Enumeration;
15import java.util.List;
16import java.util.Vector;
17import java.util.jar.JarEntry;
18import java.util.jar.JarFile;
19
20import javax.swing.JFileChooser;
21import javax.swing.filechooser.FileSystemView;
22
23import sun.misc.URLClassPath;
24
25import com.sun.javaws.jnl.JARDesc;
26import com.sun.javaws.jnl.LaunchDesc;
27import com.sun.javaws.jnl.ResourcesDesc;
28import com.sun.jnlp.JNLPClassLoader;
29
30
31/**
32 * An utility class with several methods that are used throughout the GUI classes
33 * @author cipsm
34 *
35 */
36public class Utils {
37
38        private static final Utils _instance = new Utils();
39
40        public static Utils getInstance() {
41                return _instance;
42        }
43
44        private Utils() { }
45
46        public FileSystemView getSystemView(String root) {
47                return new ClassSystemView(root);
48        }
49
50        /** A FileSystemView implementation that is used to display the classes loaded by the GUI used ClassLoader.. */
51        public static class ClassSystemView extends FileSystemView {
52
53                private String root;
54                private SystemViewHandler handler = null;
55
56                public ClassSystemView(String root) {
57                        this.root = root;
58                        System.out.println(root);
59                        // init the context
60                        try {
61                                Method m = ClassLoader.class.getDeclaredMethod("getBootstrapClassPath", new Class[0]);
62                                m.setAccessible(true);
63                                URLClassPath p = (URLClassPath)m.invoke(getClass().getClassLoader(), new Object[0]);
64                                URL u[] = p.getURLs();
65                                for (int i=0; i<u.length; i++) {
66                                        String path = u[i].toString();
67                                        if (path.startsWith("file://"))
68                                                path = path.substring(7);
69                                        else if (path.startsWith("file:/"))
70                                                path = path.substring(6);
71                                        if(File.separatorChar == '\\') {
72                                                path = path.replace("/", "\\");
73                                        }
74                                        path = path.replace("%20", " ").trim(); // for compatibility of space with the FS
75                                        if (path.endsWith(".jar")) {
76//                                              System.out.println(path);
77                                                JarHandler h = new JarHandler(path);
78                                                final File ff[] = h.listResources(root);
79                                                if (ff != null && ff.length != 0) {
80                                                        handler = h;
81                                                        break;
82                                                }
83                                        } else {
84                                                FileHandler h = new FileHandler(path);
85                                                final File ff[] = h.listResources(root);
86                                                if (ff != null && ff.length != 0) {
87                                                        handler = h;
88                                                        break;
89                                                }
90                                        }
91                                }
92                                if (handler == null) {
93                                        String classpath = System.getProperty("java.class.path");
94//                                      System.out.println("classpath="+classpath);
95                                       
96                                        if (classpath != null) {
97                                                String pp[] = null;
98                                                if (File.separatorChar == '\\')
99                                                        pp = classpath.split(";");
100                                                else
101                                                        pp = classpath.split(":");
102                                                if (pp != null && pp.length != 0) {
103                                                        for (int i=0; i<pp.length; i++) {
104                                                                String path = pp[i];
105                                                                if (path.startsWith("file://"))
106                                                                        path = path.substring(7);
107                                                                else if (path.startsWith("file:/"))
108                                                                        path = path.substring(6);
109                                                                path = path.replace("%20", " "); // for compatibility of space with the FS
110                                                                if (path.endsWith(".jar")) {
111//                                                                      System.out.println(path);
112                                                                        JarHandler h = new JarHandler(path);
113                                                                        final File ff[] = h.listResources(root);
114                                                                        if (ff != null && ff.length != 0) {
115                                                                                handler = h;
116                                                                                break;
117                                                                        }
118                                                                } else {
119                                                                        FileHandler h = new FileHandler(path);
120                                                                        final File ff[] = h.listResources(root);
121                                                                        if (ff != null && ff.length != 0) {
122                                                                                handler = h;
123                                                                                break;
124                                                                        }
125                                                                } 
126                                                        }
127                                                }
128                                        }
129                                }
130                                if (handler == null) { // still ?
131                                        ClassLoader l = getClass().getClassLoader();
132//                                      System.out.println("am intrat aici "+l);
133                                        if (l instanceof URLClassLoader) {
134                                                URLClassLoader lc = (URLClassLoader)l;
135                                                u = lc.getURLs();
136//                                              System.out.println(u.length);
137                                                for (int i=0; i<u.length; i++) {
138                                                        String path = u[i].toString();
139//                                                      System.out.println(path);
140                                                        if (path.startsWith("file://"))
141                                                                path = path.substring(7);
142                                                        else if (path.startsWith("file:/"))
143                                                                path = path.substring(6);
144                                                        if(File.separatorChar == '\\') {
145                                                                path = path.replace("/", "\\");
146                                                        }
147                                                        path = path.replace("%20", " ").trim(); // for compatibility of space with the FS
148                                                        if (path.endsWith(".jar")) {
149//                                                              System.out.println(path);
150                                                                JarHandler h = new JarHandler(path);
151                                                                final File ff[] = h.listResources(root);
152                                                                if (ff != null && ff.length != 0) {
153                                                                        handler = h;
154                                                                        break;
155                                                                }
156                                                        } else {
157                                                                FileHandler h = new FileHandler(path);
158                                                                final File ff[] = h.listResources(root);
159                                                                if (ff != null && ff.length != 0) {
160                                                                        handler = h;
161                                                                        break;
162                                                                }
163                                                        }
164                                                }
165                                        } else {
166                                                try {
167                                                        if (l instanceof JNLPClassLoader) {
168                                                                JNLPClassLoader jl = (JNLPClassLoader)l;
169                                                                LaunchDesc ld = jl.getLaunchDesc();
170                                                                final List<JARDesc> jarDescs = new ArrayList<JARDesc>();
171                                                                ResourcesDesc rd = ld.getResources();
172                                                                if (rd != null) {
173                                                                        JARDesc[] jars = rd.getEagerOrAllJarDescs(true);
174                                                                        for (int i = 0; i < jars.length; i++)
175                                                                                if (jars[i].isJavaFile() && !jars[i].isNativeLib())
176                                                                                        jarDescs.add(jars[i]);
177                                                                }
178                                                                for (JARDesc jd : jarDescs) {
179                                                                        URL location = jd.getLocation();
180                                                                        if (!location.getFile().endsWith("maps.jar")) continue;
181                                                                        // download it ?
182                                                                        File dirToDownload = new File(System.getProperty("user.home")+File.separatorChar+"maps");
183                                                                        if (!dirToDownload.exists()) {
184                                                                                dirToDownload.mkdirs();
185                                                                        }
186                                                                        String path = System.getProperty("user.home")+File.separatorChar+"maps"+File.separatorChar+"maps.jar";
187                                                                        File tmp = new File(path); 
188                                                                        InputStream isDown;
189                                                                        URLConnection connection = location.openConnection();
190                                                                        //write from web to local file
191                                                                        FileOutputStream fOS = new FileOutputStream(tmp);
192                                                                        isDown = connection.getInputStream();
193                                                                        int nBufferSize = 1024;
194                                                                        byte buff[] = new byte[nBufferSize];
195                                                                        int nRead = -1;
196                                                                        do {
197                                                                                nRead = isDown.read(buff);
198                                                                                if ( nRead > 0 ) {
199                                                                                        fOS.write(buff, 0, nRead);
200                                                                                }
201                                                                        } while ( nRead > 0 );
202                                                                        fOS.flush();
203                                                                        fOS.close();
204                                                                        isDown.close();
205//                                                                      System.out.println(path);
206                                                                        JarHandler h = new JarHandler(path);
207                                                                        final File ff[] = h.listResources(root);
208                                                                        if (ff != null && ff.length != 0) {
209                                                                                handler = h;
210                                                                                break;
211                                                                        }
212                                                                }
213                                                        }
214                                                } catch (Throwable t1) { 
215                                                        // can happen due to different versions of JavaWS
216                                                }
217                                                if (handler == null) {
218                                                        FileHandler h = new FileHandler(".");
219                                                        if (h.listResources(root) != null)
220                                                                handler = h;
221                                                }
222                                        }
223                                }
224                                if (handler == null) {
225                                        FileHandler h = new FileHandler(".");
226                                        if (h.listResources(root) != null)
227                                                handler = h;
228                                }
229                        } catch (Throwable t) {
230                                t.printStackTrace();
231                        }
232                }
233
234                public File[] getRoots() {
235                        if (handler == null)
236                                return null;
237                        return new File[] { new CustomFile(root, true) };
238                }
239
240                public boolean isRoot(File f) {
241                        if (f == null) return false;
242                        return f.getName().equals(root);
243                }
244
245                public File[] getFiles(File dir, boolean useFileHiding) {
246//                      System.out.println(dir.getName()+" "+(dir instanceof CustomFile));
247                        if (handler != null) {
248                                return handler.listResources(dir.getName());
249                        } 
250                        return null;
251                }
252
253                public String[] list() {
254                        if (handler != null) {
255                                File f[] = handler.listResources(root);
256                                if (f == null || f.length == 0) {
257                                        return null;
258                                }
259                                String[] s = new String[f.length];
260                                for (int i=0; i<s.length; i++) s[i] = f[i].getName();
261                                return s;
262                        }
263                        return null;
264                }
265
266                public File getHomeDirectory() {
267                        if (handler == null) return null;
268                        return new CustomFile(root, true);
269                }
270
271                public File getDefaultDirectory() {
272                        if (handler == null) return super.getDefaultDirectory();
273                        return new CustomFile(root, true);
274                }
275
276                /** Never used in this case */
277                public File createNewFolder(File containingDir) throws IOException {
278                        return null;
279                }
280
281        }
282
283        static class CustomFile extends File {
284                private String fileName;
285                private boolean isDir;
286                public CustomFile(String fileName, boolean isDir) {
287                        super(fileName);
288                        this.fileName = fileName;
289                        this.isDir = isDir;
290                }
291
292                public boolean isDirectory() {
293                        return isDir;
294                }
295
296                public String getName() {
297//                      try {
298//                      throw new Exception();
299//                      } catch (Exception e) {
300//                      e.printStackTrace();
301//                      }
302                        return fileName;
303                }
304
305                public String getParent() {
306//                      System.out.println(super.getParent());
307                        return "";
308                }
309        }
310
311        /** generic interface used to declare the common methods for handling a dir or jar file */
312        interface SystemViewHandler {
313                public File[] listResources(String resource);
314        }
315
316        /** Utility class used to handle the searching of particular resources inside a jar file */
317        static class JarHandler implements SystemViewHandler {
318                private String jarName;
319                public JarHandler(String jarName) {
320                        this.jarName = jarName;
321                }
322                public File[] listResources(String resource) {
323                        if (resource == null) return null;
324                        File f = new File(jarName);
325                        try {
326                                if (f.isFile() && f.getName().endsWith(".jar")){
327                                        final JarFile jf = new JarFile(f);
328//                                      System.out.println(f);
329                                        final Enumeration<JarEntry> e = jf.entries();
330                                        JarEntry je;
331                                        Vector<File> v = new Vector<File>();
332                                        while (e.hasMoreElements()){
333                                                je = e.nextElement();
334                                                String name = je.getName();
335                                                if (!name.startsWith("/")) name = "/"+name;
336//                                              System.out.println(name + " " + resource);
337                                                if (name.startsWith(resource) && name.length() > resource.length()) {
338                                                        String end = name.substring(resource.length());
339                                                        if (end.length() > 0 && end.startsWith("/")) end = end.substring(1);
340                                                        if (end.contains("/") || end.length() < 1) continue; // uninteresting result
341                                                        v.add(new CustomFile(end, je.isDirectory()));
342                                                } else continue;
343                                        }
344                                        if (v.size() == 0) return null;
345                                        CustomFile ff[] = new CustomFile[v.size()];
346                                        ff = v.toArray(ff);
347                                        return ff;
348                                }
349                        } catch (Throwable t) {
350                                t.printStackTrace();
351                                return null;
352                        }
353                        return null;
354                }
355        }
356
357        static class FileHandler implements SystemViewHandler { 
358                private String rootDir;
359                public FileHandler(String dirName) {
360                        this.rootDir = dirName;
361                }
362                public File[] listResources(String resource) {
363                        if(File.separatorChar == '\\') {
364                                resource = resource.replace("/", "\\").trim();
365                        }
366                        File dir = new File(rootDir+File.separatorChar+resource);
367                        if (dir.exists() && dir.isDirectory()) {
368                                File f[] = dir.listFiles();
369                                if (f == null || f.length == 0) {
370                                        return f;
371                                }
372                                CustomFile cf[] = new CustomFile[f.length];
373                                for (int i=0; i<f.length; i++)
374                                        cf[i] = new CustomFile(f[i].getName(), f[i].isDirectory());
375                                return cf;
376                        } 
377                        else {
378                        System.out.println("Dir "+rootDir+File.separatorChar+resource+" does not exist");
379                        }
380                        return new File[0];
381                }
382        }
383
384        // method used to get the lock on a inputstream of a an internal file representation...
385        public InputStream openStream(String name) throws Exception {
386//              System.out.println(name);
387                URL u = getClass().getClassLoader().getResource(name);
388
389                try {
390                        return u.openConnection().getInputStream();
391                } catch (Throwable t1) {
392                               
393                        try {
394                                        if(File.separatorChar == '\\') {
395                                                name = name.replace("/", "\\").trim();
396                                        }
397                                        return new FileInputStream(System.getProperty("user.dir") + name);
398                                }catch (Throwable t2) {
399                               
400                                        try {
401                                                if(File.separatorChar == '\\') {
402                                                        name = name.replace("/", "\\").trim();
403                                                }
404                                       
405                                                return new FileInputStream(System.getProperty("user.home") + name);
406                                        } catch(Throwable t3) {
407                                       
408                                                File f = new File(name);
409                                                if (!f.exists() || f.isDirectory()) return null;
410                                                return new FileInputStream(name);
411                                        }
412
413                                }
414                }
415
416        }
417
418        public OutputStream openWriteStream(String name) throws Exception {
419                if (name.startsWith("/")) name = name.substring(1);
420                URL u = getClass().getClassLoader().getResource(name);
421                try {
422                        return u.openConnection().getOutputStream();
423                } catch (Throwable t) {
424                        try {
425                                if(File.separatorChar == '\\') {
426                                        name = name.replace("/", "\\").trim();
427                                }
428                                return new FileOutputStream(System.getProperty("user.dir") + File.separatorChar + name);
429                        } catch (Throwable t2) {
430                                return new FileOutputStream(name);
431                        }
432                } 
433        }
434
435        public static void main(String args[]) {
436                FileSystemView v = Utils.getInstance().getSystemView("/maps/variations");
437                File f[] = v.getFiles(v.getRoots()[0], false);
438                JFileChooser fc=new JFileChooser("/maps/variations", v);
439                fc.setDialogTitle("Choose the file specifying the flow variations");
440                int retVal=fc.showOpenDialog(null);
441                System.out.println(fc.getSelectedFile().getAbsolutePath());
442        }
443
444} // end of class Utils
445
Note: See TracBrowser for help on using the repository browser.