| 1 | /** |
| 2 | * BSD-style license; for more info see http://xradar.sourceforge.net/license.html |
| 3 | */ |
| 4 | package org.sourceforge.xradar.resolver; |
| 5 | |
| 6 | import java.io.File; |
| 7 | import java.io.FileInputStream; |
| 8 | import java.io.IOException; |
| 9 | import java.io.InputStream; |
| 10 | import java.net.URL; |
| 11 | import java.net.URLConnection; |
| 12 | import java.util.logging.Level; |
| 13 | import java.util.logging.Logger; |
| 14 | import java.util.regex.Pattern; |
| 15 | |
| 16 | /** |
| 17 | * XRadarResourceLoader handles the so called 'xradar' protocol |
| 18 | * Basicly, it look for the file in the classpath. |
| 19 | * |
| 20 | * @author Romain PELISSE <belaran@gmail.com> |
| 21 | * |
| 22 | */ |
| 23 | public class XRadarResourceLoader extends URLConnection |
| 24 | { |
| 25 | private static final String[] windows = { "Windows 98", "Windows ME", "Windows 2000", "Windows XP", "Vista, Windows 2003" }; |
| 26 | private static final Logger logger = Logger.getLogger(XRadarResourceLoader.class.getSimpleName()); |
| 27 | private boolean isRunningOnWindows; |
| 28 | |
| 29 | protected XRadarResourceLoader(URL url) |
| 30 | { |
| 31 | super(url); |
| 32 | //FIXME: not really sure this OS checking code is still usefull |
| 33 | if ( isRunningOnWindowsOS() ) |
| 34 | { |
| 35 | isRunningOnWindows = true; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | private boolean isRunningOnWindowsOS() |
| 40 | { |
| 41 | boolean status = false; |
| 42 | int idWindows; |
| 43 | String os = System.getProperty("os.name"); |
| 44 | if (os != null ) |
| 45 | for ( idWindows = 0; idWindows < windows.length; idWindows++ ) |
| 46 | if ( os.startsWith(windows[idWindows]) ) |
| 47 | status = true; |
| 48 | return status; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void connect() throws IOException |
| 53 | { |
| 54 | // Not implemented as xradar:// is a "deconnected protocol" |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns input stream |
| 59 | * |
| 60 | * @return An inputstream to file or null if the file is not found |
| 61 | */ |
| 62 | @Override |
| 63 | public InputStream getInputStream() throws IOException |
| 64 | { |
| 65 | logger.log(Level.FINEST,"Resolving URL:" + this.getURL()); |
| 66 | String file = this.getURL().getFile(); |
| 67 | logger.log(Level.FINEST, "Loading file " + file + " from classpath."); |
| 68 | InputStream stream = this.getClass().getResourceAsStream(file); |
| 69 | if ( stream == null ) |
| 70 | { |
| 71 | logger.log(Level.FINEST, "File " + file + " not loaded. Trying file system..."); |
| 72 | // If this failed, we look on the file system... |
| 73 | stream = this.getInputStreamFromFileSystem(file); |
| 74 | } |
| 75 | return stream; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * If we can't file the file from the classpath, we use this method to retrieve it |
| 80 | * from the filesystem. |
| 81 | * |
| 82 | * @param file |
| 83 | * @return the inputstream or null. |
| 84 | * @throws IOException |
| 85 | */ |
| 86 | protected InputStream getInputStreamFromFileSystem(String file) throws IOException |
| 87 | { |
| 88 | InputStream stream = null; |
| 89 | // Hack for windows: testing if canonicalPath return a properly "rooted" path |
| 90 | if ( isRunningOnWindows ) |
| 91 | file = windowsHack(file); |
| 92 | File fileDescriptor = new File(file); |
| 93 | if ( ! fileDescriptor.exists() ) |
| 94 | logger.log(Level.FINER, "File " + file + " does not exist."); |
| 95 | else |
| 96 | { |
| 97 | if ( ! fileDescriptor.canRead() ) |
| 98 | logger.log(Level.FINEST, "File " + file + "can't be read."); |
| 99 | else |
| 100 | stream = new FileInputStream(fileDescriptor); |
| 101 | } |
| 102 | if ( stream != null ) |
| 103 | logger.log(Level.FINEST,"File " + file + "was found on file system."); |
| 104 | return stream; |
| 105 | } |
| 106 | |
| 107 | public String windowsHack(String file) throws IOException |
| 108 | { |
| 109 | logger.finest("windows adaptation need for filepath:" + file); |
| 110 | file = new File(file).getCanonicalPath(); |
| 111 | if ( file.contains("\\") && file.contains("/") ) |
| 112 | { |
| 113 | // We replace only the '\\' by '/' |
| 114 | file = file.replace('\\', '/'); |
| 115 | // If this is an absolute path, we remove the drive |
| 116 | Pattern pattern = Pattern.compile("^[A-Za-z]:.*$"); |
| 117 | if ( pattern.matcher(file).matches() ) |
| 118 | { |
| 119 | file = file.substring(2,file.length()); |
| 120 | } |
| 121 | } |
| 122 | logger.finest("Filepath modified to" + file); |
| 123 | return file; |
| 124 | } |
| 125 | } |