lose embedded source jars from upstream branch
[debian/openrocket] / core / src / net / sf / openrocket / util / JarUtil.java
1 package net.sf.openrocket.util;
2
3 import java.io.File;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.net.URL;
7 import java.security.CodeSource;
8
9 import net.sf.openrocket.database.Database;
10
11 public class JarUtil {
12
13         /**
14          * Return the a File object pointing to the JAR file that this class belongs to,
15          * or <code>null</code> if it cannot be found.
16          * 
17          * @return      a File object of the current Java archive, or <code>null</code>
18          */
19         public static File getCurrentJarFile() {
20                 // Find the jar file this class is contained in
21                 URL jarUrl = null;
22                 CodeSource codeSource = Database.class.getProtectionDomain().getCodeSource();
23                 if (codeSource != null)
24                         jarUrl = codeSource.getLocation();
25                 
26                 if (jarUrl == null) {
27                         return null;
28                 }
29                 
30                 File file = urlToFile(jarUrl);
31                 if (file.isFile())
32                         return file;
33                 return null;
34         }
35         
36         
37
38         public static File urlToFile(URL url) {
39                 URI uri;
40                 try {
41                         uri = url.toURI();
42                 } catch (URISyntaxException e) {
43                         try {
44                                 uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), 
45                                                 url.getPort(), url.getPath(), url.getQuery(), url.getRef());
46                         } catch (URISyntaxException e1) {
47                                 throw new IllegalArgumentException("Broken URL: " + url);
48                         }
49                 }
50                 return new File(uri);
51         }
52         
53         
54 }