1 5 6 package org.joseki.server; 7 8 import java.io.* ; 9 import java.util.* ; 10 import org.apache.commons.logging.*; 11 import javax.servlet.* ; 12 13 import java.net.URL ; 14 import java.net.MalformedURLException ; 15 16 import com.hp.hpl.jena.rdf.model.* ; 17 import com.hp.hpl.jena.util.ModelLoader ; 18 19 20 30 31 public class FileManager 32 { 33 static Log logger = LogFactory.getLog(FileManager.class) ; 34 35 static FileManager instance = null ; 36 37 List handler = new ArrayList() ; 38 ServletContext servletContext = null ; 39 40 private FileManager() 41 { 42 handler.add(new FileLocator()) ; 43 handler.add(new ServletResourceLocator()) ; 44 45 handler.add(new ClassLoaderLocator(Thread.currentThread().getContextClassLoader())) ; 46 handler.add(new ClassLoaderLocator(ClassLoader.getSystemClassLoader())) ; 48 } 49 50 53 public static FileManager getInstance() 54 { 55 if ( instance == null ) 57 instance = new FileManager() ; 58 return instance ; 59 } 60 61 public void setServletContext(ServletContext _servletContext) 62 { 63 logger.trace("Setting servlet context") ; 64 servletContext = _servletContext ; 65 } 66 67 73 74 public Model loadModel(String filenameOrURI) 75 { return loadModel(filenameOrURI, null) ; } 76 77 85 86 public Model loadModel(String filenameOrURI, String baseURI) 87 { 88 Model m = ModelFactory.createDefaultModel() ; 89 return readModel(m, filenameOrURI, null) ; 90 } 91 92 98 99 public Model readModel(Model model, String filenameOrURI) 100 { return readModel(model, filenameOrURI, null); } 101 102 109 110 public Model readModel(Model model, String filenameOrURI, String baseURI) 111 112 { 113 if ( baseURI == null ) 114 baseURI = "" ; 115 116 String syntax = ModelLoader.guessLang(filenameOrURI) ; 118 if ( syntax == null || syntax.equals("") ) 119 syntax = "RDF/XML" ; 120 121 InputStream in = open(filenameOrURI) ; 122 if ( in == null ) 123 { 124 logger.debug("Failed to locate '"+filenameOrURI+"'") ; 125 return null ; 126 } 127 model.read(in,baseURI, syntax) ; 128 return model ; 129 } 130 131 132 public InputStream open(String filenameOrURI) 133 { 134 for ( Iterator iter = handler.iterator() ; iter.hasNext() ; ) 135 { 136 Locator loc = (Locator)iter.next() ; 137 InputStream in = loc.open(filenameOrURI) ; 138 if ( in != null ) 139 return in ; 140 } 141 return null; 142 } 143 144 145 private interface Locator 146 { 147 public InputStream open(String filenameOrURI) ; 148 } 149 150 private class FileLocator implements Locator 151 { 152 FileLocator() 153 { 154 try { 155 String wd = new File(".").getCanonicalPath() ; 157 logger.info("Base directory: "+wd) ; 158 } catch (IOException ex) 159 { 160 logger.error("Failed to discover the working directory", ex) ; 161 } 162 } 163 164 public InputStream open(String filenameOrURI) 165 { 166 String fn = toFilename(filenameOrURI) ; 167 if ( fn == null ) 168 return null ; 169 170 File f = new File(fn) ; 171 172 if ( !f.exists() ) 173 { 174 logger.trace("FileLocator: File not found: "+filenameOrURI) ; 175 return null ; 176 } 177 178 try { 179 InputStream in = new FileInputStream(fn) ; 180 if ( in == null ) 181 { 182 logger.trace("FileLocator: Failed to open: "+filenameOrURI) ; 184 return null ; 185 } 186 187 logger.debug("Read as file: "+filenameOrURI) ; 188 return in ; 192 } catch (IOException ioEx) 193 { 194 logger.warn("File unreadable (but exists): "+fn+" Exception: "+ioEx.getMessage()) ; 197 return null ; 198 } 199 } 200 } 201 202 private class URLLocator implements Locator 203 { 204 public InputStream open(String filenameOrURI) 205 { 206 if (isFile(filenameOrURI)) 207 return null; 208 try 209 { 210 URL url = new URL (filenameOrURI); 211 InputStream in = new BufferedInputStream(url.openStream()); 212 if ( in == null ) 213 { 214 logger.trace("URLLocator: not found: "+filenameOrURI) ; 215 return null ; 216 } 217 218 logger.debug("Read as URL: " + filenameOrURI); 219 return in; 220 } 221 catch (MalformedURLException ex) 222 { 223 logger.warn("Malformed URL: " + filenameOrURI); 224 return null; 225 } 226 catch (IOException ex) 227 { 228 logger.warn("IO Exception opening URL: " + filenameOrURI); 229 return null; 230 } 231 } 232 } 233 234 235 private class ServletResourceLocator implements Locator 236 { 237 public InputStream open(String filenameOrURI) 238 { 239 if ( servletContext == null) 240 return null ; 241 242 String fn = toFilename(filenameOrURI) ; 243 if ( fn == null ) 244 { 245 logger.trace("ServletResourceLocator: failed to open: "+filenameOrURI) ; 246 return null ; 247 } 248 249 InputStream in = servletContext.getResourceAsStream(fn); 250 if (in != null) 252 logger.debug("Reading as servlet resource: " + filenameOrURI); 253 return in ; 254 } 255 } 256 257 private class ClassLoaderLocator implements Locator 258 { 259 ClassLoader classLoader = null ; 260 ClassLoaderLocator(ClassLoader _classLoader) 261 { 262 classLoader =_classLoader ; 263 } 264 265 266 public InputStream open(String filenameOrURI) 267 { 268 if ( classLoader == null ) 269 return null ; 270 271 String fn = toFilename(filenameOrURI) ; 272 if ( fn == null ) 273 return null ; 274 275 InputStream in = classLoader.getResourceAsStream(fn) ; 276 if ( in == null ) 277 { 278 logger.trace("ClassLoaderLocator: failed to open: "+filenameOrURI) ; 279 } 280 281 logger.debug("Reading as resource: "+filenameOrURI) ; 283 return in ; 284 } 285 } 286 287 private String toFilename(String filenameOrURI) 288 { 289 if ( !isFile(filenameOrURI) ) 290 return null ; 291 292 String fn = filenameOrURI ; 293 if ( fn.startsWith("file:") ) 294 fn = fn.substring("file:".length()) ; 295 return fn ; 296 } 297 298 private boolean isFile(String name) 299 { 300 return name.startsWith("file:") || ! isURI(name) ; 301 } 302 303 304 private boolean isURI(String name) 305 { 306 if ( name.matches("[^/:]*:.*") ) 307 return true ; 308 return false ; 309 } 310 } 311 312 313 314 340 341 | Popular Tags |